Skip to content

Commit

Permalink
Show parent layer names in "Move Object to Layer" menu (mapeditor#3811)
Browse files Browse the repository at this point in the history
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 mapeditor#3454
  • Loading branch information
bjorn authored Mar 27, 2024
1 parent 64422cf commit 65eb861
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 23 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
30 changes: 24 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions src/tiled/abstractobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<ObjectGroup*>(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();
Expand Down
38 changes: 38 additions & 0 deletions src/tiled/mapdocumentactionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<ObjectGroup*>(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.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/mapdocumentactionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/mapobjectmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -128,4 +130,9 @@ class MapObjectModel : public QAbstractItemModel
QIcon mObjectGroupIcon;
};

inline QIcon MapObjectModel::objectGroupIcon() const
{
return mObjectGroupIcon;
}

} // namespace Tiled
20 changes: 12 additions & 8 deletions src/tiled/objectsdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QBoxLayout>
Expand Down Expand Up @@ -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")));
Expand All @@ -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());
Expand Down Expand Up @@ -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<ObjectGroup*>(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<ObjectGroup*>();
auto *objectGroup = action->data().value<ObjectGroup*>();
handler->moveObjectsToGroup(objectGroup);
}

Expand Down

0 comments on commit 65eb861

Please sign in to comment.