From 65eb861b9a64cdc2b375c2ba603efe2b433ba225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Wed, 27 Mar 2024 19:02:19 +0100 Subject: [PATCH] Show parent layer names in "Move Object to Layer" menu (#3811) Also reversed the order of the layers, to match the order in the Layers and Objects views. Move-to-layer menu implementation is now shared between object tools and objects dock. Added object group icon to the menu items. Closes #3454 --- NEWS.md | 1 + flake.lock | 30 ++++++++++++++++---- src/tiled/abstractobjecttool.cpp | 14 ++++------ src/tiled/mapdocumentactionhandler.cpp | 38 ++++++++++++++++++++++++++ src/tiled/mapdocumentactionhandler.h | 2 ++ src/tiled/mapobjectmodel.h | 7 +++++ src/tiled/objectsdock.cpp | 20 ++++++++------ 7 files changed, 89 insertions(+), 23 deletions(-) diff --git a/NEWS.md b/NEWS.md index 9fb9e91ed9..83dddd0e88 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ ### Unreleased * Added --project command-line parameter for use when exporting (#3797) +* Added group layer names in "Move Object to Layer" menu (#3454) * 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) diff --git a/flake.lock b/flake.lock index 5e6bfb9690..5e1e7a2f50 100644 --- a/flake.lock +++ b/flake.lock @@ -1,12 +1,15 @@ { "nodes": { "flake-utils": { + "inputs": { + "systems": "systems" + }, "locked": { - "lastModified": 1667395993, - "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", "type": "github" }, "original": { @@ -17,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1675183161, - "narHash": "sha256-Zq8sNgAxDckpn7tJo7V1afRSk2eoVbu3OjI1QklGLNg=", + "lastModified": 1711333969, + "narHash": "sha256-5PiWGn10DQjMZee5NXzeA6ccsv60iLu+Xtw+mfvkUAs=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e1e1b192c1a5aab2960bf0a0bd53a2e8124fa18e", + "rev": "57e6b3a9e4ebec5aa121188301f04a6b8c354c9b", "type": "github" }, "original": { @@ -36,6 +39,21 @@ "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/src/tiled/abstractobjecttool.cpp b/src/tiled/abstractobjecttool.cpp index 21180fe342..a65b004e1d 100644 --- a/src/tiled/abstractobjecttool.cpp +++ b/src/tiled/abstractobjecttool.cpp @@ -27,6 +27,7 @@ #include "changetileobjectgroup.h" #include "documentmanager.h" #include "mapdocument.h" +#include "mapdocumentactionhandler.h" #include "map.h" #include "mapobject.h" #include "mapobjectitem.h" @@ -708,18 +709,13 @@ void AbstractObjectTool::showContextMenu(MapObject *clickedObject, menu.addAction(tr("Lower Object to Bottom"), this, &AbstractObjectTool::lowerToBottom, Qt::Key_End); } - auto objectGroups = mapDocument()->map()->objectGroups(); - auto objectGroupsIterator = objectGroups.begin(); - if (objectGroupsIterator != objectGroups.end() && objectGroupsIterator.next()) { + if (LayerIterator(mapDocument()->map(), Layer::ObjectGroupType).next()) { menu.addSeparator(); QMenu *moveToLayerMenu = menu.addMenu(tr("Move %n Object(s) to Layer", "", selectedObjects.size())); - for (Layer *layer : objectGroups) { - ObjectGroup *objectGroup = static_cast(layer); - QAction *action = moveToLayerMenu->addAction(objectGroup->name()); - action->setData(QVariant::fromValue(objectGroup)); - action->setEnabled(objectGroup != sameObjectGroup); - } + + auto actionHandler = MapDocumentActionHandler::instance(); + actionHandler->populateMoveToLayerMenu(moveToLayerMenu, sameObjectGroup); } menu.addSeparator(); diff --git a/src/tiled/mapdocumentactionhandler.cpp b/src/tiled/mapdocumentactionhandler.cpp index 4ee7c849a5..acebd2cca8 100644 --- a/src/tiled/mapdocumentactionhandler.cpp +++ b/src/tiled/mapdocumentactionhandler.cpp @@ -32,6 +32,7 @@ #include "map.h" #include "mapdocument.h" #include "mapobject.h" +#include "mapobjectmodel.h" #include "maprenderer.h" #include "mapview.h" #include "movelayer.h" @@ -332,6 +333,43 @@ QMenu *MapDocumentActionHandler::createGroupLayerMenu(QWidget *parent) const return groupLayerMenu; } +void MapDocumentActionHandler::populateMoveToLayerMenu(QMenu *menu, const ObjectGroup *current) +{ + if (!mMapDocument) + return; + + const GroupLayer *parentLayer = nullptr; + + LayerIterator objectGroupsIterator(mMapDocument->map(), Layer::ObjectGroupType); + objectGroupsIterator.toBack(); + + const auto objectGroupIcon = mMapDocument->mapObjectModel()->objectGroupIcon(); + + while (auto objectGroup = static_cast(objectGroupsIterator.previous())) { + // Create a separator to indicate the parent layer(s), using "(Parent1/Parent2)". + if (parentLayer != objectGroup->parentLayer()) { + auto separator = menu->addSeparator(); + separator->setEnabled(false); + + parentLayer = objectGroup->parentLayer(); + if (parentLayer) { + QStringList parentLayerNames; + auto currentParentLayer = parentLayer; + while (currentParentLayer) { + parentLayerNames.prepend(currentParentLayer->name()); + currentParentLayer = currentParentLayer->parentLayer(); + } + + separator->setText(parentLayerNames.join(QLatin1String("/"))); + } + } + + QAction *action = menu->addAction(objectGroupIcon, objectGroup->name()); + action->setData(QVariant::fromValue(objectGroup)); + action->setEnabled(objectGroup != current); + } +} + /** * Used to check whether we can cut or delete the current tile selection. */ diff --git a/src/tiled/mapdocumentactionhandler.h b/src/tiled/mapdocumentactionhandler.h index ff7448437d..9a81e6187f 100644 --- a/src/tiled/mapdocumentactionhandler.h +++ b/src/tiled/mapdocumentactionhandler.h @@ -93,6 +93,8 @@ class MapDocumentActionHandler : public QObject QMenu *createNewLayerMenu(QWidget *parent) const; QMenu *createGroupLayerMenu(QWidget *parent) const; + void populateMoveToLayerMenu(QMenu *menu, const ObjectGroup *current); + public slots: void cut(); bool copy(); diff --git a/src/tiled/mapobjectmodel.h b/src/tiled/mapobjectmodel.h index 9b5f271e3c..5d046d6040 100644 --- a/src/tiled/mapobjectmodel.h +++ b/src/tiled/mapobjectmodel.h @@ -108,6 +108,8 @@ class MapObjectModel : public QAbstractItemModel void moveObjects(ObjectGroup *og, int from, int to, int count); + QIcon objectGroupIcon() const; + private: void layerAdded(Layer *layer); void layerAboutToBeRemoved(GroupLayer *groupLayer, int index); @@ -128,4 +130,9 @@ class MapObjectModel : public QAbstractItemModel QIcon mObjectGroupIcon; }; +inline QIcon MapObjectModel::objectGroupIcon() const +{ + return mObjectGroupIcon; +} + } // namespace Tiled diff --git a/src/tiled/objectsdock.cpp b/src/tiled/objectsdock.cpp index 96fc85b712..b751a0db92 100644 --- a/src/tiled/objectsdock.cpp +++ b/src/tiled/objectsdock.cpp @@ -23,13 +23,13 @@ #include "actionmanager.h" #include "documentmanager.h" #include "filteredit.h" -#include "grouplayer.h" #include "map.h" #include "mapdocument.h" #include "mapdocumentactionhandler.h" #include "mapobject.h" #include "objectgroup.h" #include "objectsview.h" +#include "raiselowerhelper.h" #include "utils.h" #include @@ -74,8 +74,11 @@ ObjectsDock::ObjectsDock(QWidget *parent) connect(mActionNewLayer, &QAction::triggered, handler->actionAddObjectGroup(), &QAction::trigger); + QIcon objectsGroupIcon(QLatin1String("://images/16/layer-object.png")); + objectsGroupIcon.addFile(QLatin1String("://images/32/layer-object.png")); + mActionMoveToGroup = new QAction(this); - mActionMoveToGroup->setIcon(QIcon(QLatin1String(":/images/16/layer-object.png"))); + mActionMoveToGroup->setIcon(objectsGroupIcon); mActionMoveUp = new QAction(this); mActionMoveUp->setIcon(QIcon(QLatin1String(":/images/16/go-up.png"))); @@ -86,7 +89,7 @@ ObjectsDock::ObjectsDock(QWidget *parent) Utils::setThemeIcon(mActionMoveUp, "go-up"); Utils::setThemeIcon(mActionMoveDown, "go-down"); - QToolBar *toolBar = new QToolBar; + auto *toolBar = new QToolBar; toolBar->setFloatable(false); toolBar->setMovable(false); toolBar->setIconSize(Utils::smallIconSize()); @@ -195,17 +198,18 @@ void ObjectsDock::aboutToShowMoveToMenu() { mMoveToMenu->clear(); - for (Layer *layer : mMapDocument->map()->objectGroups()) { - QAction *action = mMoveToMenu->addAction(layer->name()); - action->setData(QVariant::fromValue(static_cast(layer))); - } + auto &selectedObjects = mMapDocument->selectedObjects(); + auto sameObjectGroup = RaiseLowerHelper::sameObjectGroup(selectedObjects); + + auto handler = MapDocumentActionHandler::instance(); + handler->populateMoveToLayerMenu(mMoveToMenu, sameObjectGroup); } void ObjectsDock::triggeredMoveToMenu(QAction *action) { MapDocumentActionHandler *handler = MapDocumentActionHandler::instance(); - ObjectGroup *objectGroup = action->data().value(); + auto *objectGroup = action->data().value(); handler->moveObjectsToGroup(objectGroup); }