Skip to content

Commit

Permalink
Added actions to show/hide and lock/unlock the selected layers
Browse files Browse the repository at this point in the history
As opposed to all the other layers, for which actions already existed.
  • Loading branch information
bjorn committed May 8, 2019
1 parent 13cd313 commit 359dcb9
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/tiled/layerdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ void LayerView::contextMenuEvent(QContextMenuEvent *event)
menu.addAction(handler->actionMoveLayersUp());
menu.addAction(handler->actionMoveLayersDown());
menu.addSeparator();
menu.addAction(handler->actionToggleSelectedLayers());
menu.addAction(handler->actionToggleLockSelectedLayers());
menu.addAction(handler->actionToggleOtherLayers());
menu.addAction(handler->actionToggleLockOtherLayers());
menu.addSeparator();
Expand Down
48 changes: 48 additions & 0 deletions src/tiled/layermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,54 @@ void LayerModel::renameLayer(Layer *layer, const QString &name)
emit layerChanged(layer);
}

/**
* Shows the layers when all are invisible, hides otherwise.
*/
void LayerModel::toggleLayers(const QList<Layer *> &layers)
{
if (layers.isEmpty())
return;

bool visible = std::all_of(layers.begin(), layers.end(),
[] (Layer *layer) { return !layer->isVisible(); });

QUndoStack *undoStack = mMapDocument->undoStack();
if (visible)
undoStack->beginMacro(tr("Show Layers"));
else
undoStack->beginMacro(tr("Hide Layers"));

for (Layer *l : layers)
if (visible != l->isVisible())
undoStack->push(new SetLayerVisible(mMapDocument, l, visible));

undoStack->endMacro();
}

/**
* Locks layers when any are unlocked, unlocks otherwise.
*/
void LayerModel::toggleLockLayers(const QList<Layer *> &layers)
{
if (layers.isEmpty())
return;

bool locked = std::any_of(layers.begin(), layers.end(),
[] (Layer *layer) { return !layer->isLocked(); });

QUndoStack *undoStack = mMapDocument->undoStack();
if (locked)
undoStack->beginMacro(tr("Lock Layers"));
else
undoStack->beginMacro(tr("Unlock Layers"));

for (Layer *l : layers)
if (locked != l->isLocked())
undoStack->push(new SetLayerLocked(mMapDocument, l, locked));

undoStack->endMacro();
}

/**
* Collects siblings of \a layers, including siblings of all parents. None of
* the layers provided as input are returned.
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/layermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class LayerModel : public QAbstractItemModel

void renameLayer(Layer *layer, const QString &name);

void toggleLayers(const QList<Layer *> &layers);
void toggleLockLayers(const QList<Layer *> &layers);
void toggleOtherLayers(const QList<Layer *> &layers);
void toggleLockOtherLayers(const QList<Layer *> &layers);

Expand Down
2 changes: 2 additions & 0 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
mLayerMenu->addAction(mActionHandler->actionMoveLayersUp());
mLayerMenu->addAction(mActionHandler->actionMoveLayersDown());
mLayerMenu->addSeparator();
mLayerMenu->addAction(mActionHandler->actionToggleSelectedLayers());
mLayerMenu->addAction(mActionHandler->actionToggleLockSelectedLayers());
mLayerMenu->addAction(mActionHandler->actionToggleOtherLayers());
mLayerMenu->addAction(mActionHandler->actionToggleLockOtherLayers());
mLayerMenu->addSeparator();
Expand Down
16 changes: 16 additions & 0 deletions src/tiled/mapdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,22 @@ void MapDocument::removeLayers(const QList<Layer *> &layers)
undoStack()->endMacro();
}

/**
* \see LayerModel::toggleLayers
*/
void MapDocument::toggleLayers(const QList<Layer *> &layers)
{
mLayerModel->toggleLayers(layers);
}

/**
* \see LayerModel::toggleLockLayers
*/
void MapDocument::toggleLockLayers(const QList<Layer *> &layers)
{
mLayerModel->toggleLockLayers(layers);
}

/**
* \see LayerModel::toggleOtherLayers
*/
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/mapdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class MapDocument : public Document
void moveLayersUp(const QList<Layer *> &layers);
void moveLayersDown(const QList<Layer *> &layers);
void removeLayers(const QList<Layer *> &layers);
void toggleLayers(const QList<Layer *> &layers);
void toggleLockLayers(const QList<Layer *> &layers);
void toggleOtherLayers(const QList<Layer *> &layers);
void toggleLockOtherLayers(const QList<Layer *> &layers);

Expand Down
45 changes: 41 additions & 4 deletions src/tiled/mapdocumentactionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,32 @@ MapDocumentActionHandler::MapDocumentActionHandler(QObject *parent)
mActionMoveLayersDown->setIcon(
QIcon(QLatin1String(":/images/16x16/go-down.png")));

QIcon toggleVisibilityIcon;
toggleVisibilityIcon.addFile(QLatin1String(":/images/14x14/hidden.png"));
toggleVisibilityIcon.addFile(QLatin1String(":/images/16x16/hidden.png"));
toggleVisibilityIcon.addFile(QLatin1String(":/images/24x24/hidden.png"));

mActionToggleSelectedLayers = new QAction(this);
mActionToggleSelectedLayers->setShortcut(tr("Ctrl+H"));
mActionToggleSelectedLayers->setIcon(toggleVisibilityIcon);

QIcon lockedIcon;
lockedIcon.addFile(QLatin1String(":/images/14x14/locked.png"));
lockedIcon.addFile(QLatin1String(":/images/16x16/locked.png"));
lockedIcon.addFile(QLatin1String(":/images/24x24/locked.png"));

mActionToggleLockSelectedLayers = new QAction(this);
mActionToggleLockSelectedLayers->setShortcut(tr("Ctrl+L"));
mActionToggleLockSelectedLayers->setIcon(lockedIcon);

mActionToggleOtherLayers = new QAction(this);
mActionToggleOtherLayers->setShortcut(tr("Ctrl+Shift+H"));
mActionToggleOtherLayers->setIcon(
QIcon(QLatin1String(":/images/16x16/show_hide_others.png")));

mActionToggleLockOtherLayers = new QAction(this);
mActionToggleLockOtherLayers->setShortcut(tr("Ctrl+Shift+L"));
mActionToggleLockOtherLayers->setIcon(
QIcon(QLatin1String(":/images/16x16/locked.png")));
mActionToggleLockOtherLayers->setIcon(lockedIcon);

mActionLayerProperties = new QAction(this);
mActionLayerProperties->setIcon(
Expand Down Expand Up @@ -171,6 +188,8 @@ MapDocumentActionHandler::MapDocumentActionHandler(QObject *parent)
connect(mActionRemoveLayers, &QAction::triggered, this, &MapDocumentActionHandler::removeLayers);
connect(mActionMoveLayersUp, &QAction::triggered, this, &MapDocumentActionHandler::moveLayersUp);
connect(mActionMoveLayersDown, &QAction::triggered, this, &MapDocumentActionHandler::moveLayersDown);
connect(mActionToggleSelectedLayers, &QAction::triggered, this, &MapDocumentActionHandler::toggleSelectedLayers);
connect(mActionToggleLockSelectedLayers, &QAction::triggered, this, &MapDocumentActionHandler::toggleLockSelectedLayers);
connect(mActionToggleOtherLayers, &QAction::triggered, this, &MapDocumentActionHandler::toggleOtherLayers);
connect(mActionToggleLockOtherLayers, &QAction::triggered, this, &MapDocumentActionHandler::toggleLockOtherLayers);
connect(mActionLayerProperties, &QAction::triggered, this, &MapDocumentActionHandler::layerProperties);
Expand Down Expand Up @@ -199,6 +218,8 @@ MapDocumentActionHandler::MapDocumentActionHandler(QObject *parent)
ActionManager::registerAction(mActionRemoveLayers, "RemoveLayers");
ActionManager::registerAction(mActionMoveLayersUp, "MoveLayersUp");
ActionManager::registerAction(mActionMoveLayersDown, "MoveLayersDown");
ActionManager::registerAction(mActionToggleSelectedLayers, "ToggleSelectedLayers");
ActionManager::registerAction(mActionToggleLockSelectedLayers, "ToggleLockSelectedLayers");
ActionManager::registerAction(mActionToggleOtherLayers, "ToggleOtherLayers");
ActionManager::registerAction(mActionToggleLockOtherLayers, "ToggleLockOtherLayers");
ActionManager::registerAction(mActionLayerProperties, "LayerProperties");
Expand Down Expand Up @@ -239,8 +260,10 @@ void MapDocumentActionHandler::retranslateUi()
mActionSelectNextLayer->setText(tr("Select &Next Layer"));
mActionMoveLayersUp->setText(tr("R&aise Layers"));
mActionMoveLayersDown->setText(tr("&Lower Layers"));
mActionToggleOtherLayers->setText(tr("Show/&Hide all Other Layers"));
mActionToggleLockOtherLayers->setText(tr("Lock/&Unlock all Other Layers"));
mActionToggleSelectedLayers->setText(tr("Show/&Hide Layers"));
mActionToggleLockSelectedLayers->setText(tr("Lock/&Unlock Layers"));
mActionToggleOtherLayers->setText(tr("Show/&Hide Other Layers"));
mActionToggleLockOtherLayers->setText(tr("Lock/&Unlock Other Layers"));
mActionLayerProperties->setText(tr("Layer &Properties..."));
}

Expand Down Expand Up @@ -700,6 +723,18 @@ void MapDocumentActionHandler::removeLayers()
mMapDocument->removeLayers(mMapDocument->selectedLayers());
}

void MapDocumentActionHandler::toggleSelectedLayers()
{
if (mMapDocument)
mMapDocument->toggleLayers(mMapDocument->selectedLayers());
}

void MapDocumentActionHandler::toggleLockSelectedLayers()
{
if (mMapDocument)
mMapDocument->toggleLockLayers(mMapDocument->selectedLayers());
}

void MapDocumentActionHandler::toggleOtherLayers()
{
if (mMapDocument)
Expand Down Expand Up @@ -795,6 +830,8 @@ void MapDocumentActionHandler::updateActions()
mActionSelectNextLayer->setEnabled(hasNextLayer);
mActionMoveLayersUp->setEnabled(canMoveLayersUp);
mActionMoveLayersDown->setEnabled(canMoveLayersDown);
mActionToggleSelectedLayers->setEnabled(!selectedLayers.isEmpty());
mActionToggleLockSelectedLayers->setEnabled(!selectedLayers.isEmpty());
mActionToggleOtherLayers->setEnabled(currentLayer && (hasNextLayer || hasPreviousLayer));
mActionToggleLockOtherLayers->setEnabled(currentLayer && (hasNextLayer || hasPreviousLayer));
mActionRemoveLayers->setEnabled(!selectedLayers.isEmpty());
Expand Down
6 changes: 6 additions & 0 deletions src/tiled/mapdocumentactionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class MapDocumentActionHandler : public QObject
QAction *actionSelectNextLayer() const { return mActionSelectNextLayer; }
QAction *actionMoveLayersUp() const { return mActionMoveLayersUp; }
QAction *actionMoveLayersDown() const { return mActionMoveLayersDown; }
QAction *actionToggleSelectedLayers() const { return mActionToggleSelectedLayers; }
QAction *actionToggleLockSelectedLayers() const { return mActionToggleLockSelectedLayers; }
QAction *actionToggleOtherLayers() const { return mActionToggleOtherLayers; }
QAction *actionToggleLockOtherLayers() const { return mActionToggleLockOtherLayers; }
QAction *actionLayerProperties() const { return mActionLayerProperties; }
Expand Down Expand Up @@ -121,6 +123,8 @@ public slots:
void moveLayersUp();
void moveLayersDown();
void removeLayers();
void toggleSelectedLayers();
void toggleLockSelectedLayers();
void toggleOtherLayers();
void toggleLockOtherLayers();
void layerProperties();
Expand Down Expand Up @@ -159,6 +163,8 @@ private slots:
QAction *mActionSelectNextLayer;
QAction *mActionMoveLayersUp;
QAction *mActionMoveLayersDown;
QAction *mActionToggleSelectedLayers;
QAction *mActionToggleLockSelectedLayers;
QAction *mActionToggleOtherLayers;
QAction *mActionToggleLockOtherLayers;
QAction *mActionLayerProperties;
Expand Down

0 comments on commit 359dcb9

Please sign in to comment.