Skip to content

Commit

Permalink
Made adding "Copy" when duplicating optional
Browse files Browse the repository at this point in the history
The option is disabled by default. When enabled, the behavior is a
little bit nicer when creating many duplicates, since they now get
numbered rather than adding "Copy Copy Copy..."

Also, when editing layer names, the name now gets trimmed to avoid
accidentally leaving spaces at either end.
  • Loading branch information
bjorn committed Mar 22, 2024
1 parent 53b0e52 commit 64422cf
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 133 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
### Unreleased

* Added --project command-line parameter for use when exporting (#3797)
* Made adding "Copy" when duplicating optional and disabled by default (#3917)
* Layer names are now trimmed when edited in the UI, to avoid accidental whitespace
* Scripting: Added API for working with worlds (#3539)
* Scripting: Added Tile.image for accessing a tile's image data
* Scripting: Added Tileset.imageFileName and ImageLayer.imageFileName
Expand Down
29 changes: 29 additions & 0 deletions src/tiled/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,42 @@

#include "editor.h"

#include <QCoreApplication>
#include <QRegularExpression>

namespace Tiled {

Preference<bool> Editor::duplicateAddsCopy { "Editor/DuplicateAddsCopy" };

Editor::Editor(QObject *parent)
: QObject(parent)
{
}

QString Editor::nameOfDuplicate(const QString &name)
{
if (name.isEmpty() || !duplicateAddsCopy)
return name;

const QString copyText = tr("Copy");

// Look for an existing postfix, optionally capturing a number
const QRegularExpression regexp = QRegularExpression(QStringLiteral(
R"((.*)\s*%1\s*(\d+)?$)").arg(copyText));

const QRegularExpressionMatch match = regexp.match(name);
if (match.hasMatch()) {
const QString copyName = match.captured(1).trimmed();
const QString capturedNumber = match.captured(2);
const int copyNumber = capturedNumber.isNull() ? 2 : capturedNumber.toInt() + 1;

return QStringLiteral("%1 %2 %3").arg(copyName, copyText,
QString::number(copyNumber));
}

return QStringLiteral("%1 %2").arg(name, copyText);
}

} // namespace Tiled

#include "moc_editor.cpp"
6 changes: 6 additions & 0 deletions src/tiled/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#pragma once

#include "preferences.h"

#include <QObject>

class QToolBar;
Expand Down Expand Up @@ -69,6 +71,10 @@ class Editor : public QObject

virtual void resetLayout() = 0;

static Preference<bool> duplicateAddsCopy;

static QString nameOfDuplicate(const QString &name);

signals:
void enabledStandardActionsChanged();
};
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/layermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool LayerModel::setData(const QModelIndex &index, const QVariant &value,
return true;
}
} else if (role == Qt::EditRole) {
const QString newName = value.toString();
const QString newName = value.toString().trimmed();
if (layer->name() != newName) {
SetLayerName *rename = new SetLayerName(mMapDocument, { layer },
newName);
Expand Down
4 changes: 3 additions & 1 deletion src/tiled/mapdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "changeselectedarea.h"
#include "containerhelpers.h"
#include "editablemap.h"
#include "editor.h"
#include "flipmapobjects.h"
#include "geometry.h"
#include "grouplayer.h"
Expand Down Expand Up @@ -634,7 +635,7 @@ void MapDocument::duplicateLayers(const QList<Layer *> &layers)
}

objectRefs.reassignIds(dup.clone);
dup.clone->setName(tr("Copy of %1").arg(dup.clone->name()));
dup.clone->setName(Editor::nameOfDuplicate(dup.clone->name()));

duplications.append(dup);
}
Expand Down Expand Up @@ -1595,6 +1596,7 @@ void MapDocument::duplicateObjects(const QList<MapObject *> &objects)

for (MapObject *mapObject : objects) {
MapObject *clone = mapObject->clone();
clone->setName(Editor::nameOfDuplicate(clone->name()));
objectRefs.reassignId(clone);
objectsToAdd.append(AddMapObjects::Entry { clone, mapObject->objectGroup() });
objectsToAdd.last().index = mapObject->objectGroup()->objects().indexOf(mapObject) + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/mapobjectmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ bool MapObjectModel::setData(const QModelIndex &index, const QVariant &value,
return true;
}
case Qt::EditRole: {
const QString newName = value.toString();
const QString newName = value.toString().trimmed();
if (layer->name() != newName) {
SetLayerName *rename = new SetLayerName(mMapDocument,
{ layer },
Expand Down
12 changes: 9 additions & 3 deletions src/tiled/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ui_preferencesdialog.h"

#include "abstractobjecttool.h"
#include "editor.h"
#include "languagemanager.h"
#include "mapobjectitem.h"
#include "mapview.h"
Expand Down Expand Up @@ -71,8 +72,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent)
tr("Prefer Selected Layers"),
tr("Selected Layers Only") });

PluginListModel *pluginListModel = new PluginListModel(this);
QSortFilterProxyModel *pluginProxyModel = new QSortFilterProxyModel(this);
auto *pluginListModel = new PluginListModel(this);
auto *pluginProxyModel = new QSortFilterProxyModel(this);
pluginProxyModel->setSortLocaleAware(true);
pluginProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
pluginProxyModel->setSourceModel(pluginListModel);
Expand Down Expand Up @@ -111,8 +112,10 @@ PreferencesDialog::PreferencesDialog(QWidget *parent)
Sentry::instance()->setUserConsent(value ? Sentry::ConsentGiven : Sentry::ConsentRevoked);
});
mUi->crashReportingLabel->setOpenExternalLinks(true);
mUi->crashReportingAndUpdates->setTitle(tr("Updates and Crash Reporting"));
#else
mUi->crashReporting->setVisible(false);
mUi->sendCrashReports->setVisible(false);
mUi->crashReportingLabel->setVisible(false);
#endif

connect(mUi->languageCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
Expand All @@ -139,6 +142,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent)
this, [] (bool checked) { MapView::ourAutoScrollingEnabled = checked; });
connect(mUi->smoothScrolling, &QCheckBox::toggled,
this, [] (bool checked) { MapView::ourSmoothScrollingEnabled = checked; });
connect(mUi->duplicateAddsCopy, &QCheckBox::toggled,
this, [] (bool checked) { Editor::duplicateAddsCopy = checked; });

connect(mUi->styleCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &PreferencesDialog::styleComboChanged);
Expand Down Expand Up @@ -230,6 +235,7 @@ void PreferencesDialog::fromPreferences()
mUi->wheelZoomsByDefault->setChecked(prefs->wheelZoomsByDefault());
mUi->autoScrolling->setChecked(MapView::ourAutoScrollingEnabled);
mUi->smoothScrolling->setChecked(MapView::ourSmoothScrollingEnabled);
mUi->duplicateAddsCopy->setChecked(Editor::duplicateAddsCopy);

const QFont customFont = prefs->customFont();
mUi->fontGroupBox->setChecked(prefs->useCustomFont());
Expand Down
Loading

0 comments on commit 64422cf

Please sign in to comment.