Skip to content

Commit

Permalink
Made expanded group layers persistent
Browse files Browse the repository at this point in the history
Both when switching maps within a session, as well as between restarts,
the expanded group layers are now persistent.

This change also affects expanded group layers and object layers in the
Objects view.

Part of issue #3282
  • Loading branch information
bjorn committed Apr 4, 2022
1 parent 2c60806 commit fa73f87
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 47 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,3 +1,7 @@
### Unreleased 1.8 patch

* Made expanded group layers persistent

### Tiled 1.8.4 (31 March 2022)

* Fixed crash when trying to create an object
Expand Down
33 changes: 32 additions & 1 deletion src/tiled/layerdock.cpp
Expand Up @@ -285,6 +285,9 @@ LayerView::LayerView(QWidget *parent)

connect(selectionModel(), &QItemSelectionModel::currentRowChanged, this, &LayerView::currentRowChanged);

connect(this, &QTreeView::expanded, this, &LayerView::onExpanded);
connect(this, &QTreeView::collapsed, this, &LayerView::onCollapsed);

connect(this, &QAbstractItemView::pressed, this, &LayerView::indexPressed);
}

Expand All @@ -307,7 +310,8 @@ void LayerView::setMapDocument(MapDocument *mapDocument)
mMapDocument = mapDocument;

if (mMapDocument) {
mProxyModel->setSourceModel(mMapDocument->layerModel());
auto layerModel = mMapDocument->layerModel();
mProxyModel->setSourceModel(layerModel);

connect(mMapDocument, &MapDocument::currentLayerChanged,
this, &LayerView::currentLayerChanged);
Expand All @@ -316,6 +320,15 @@ void LayerView::setMapDocument(MapDocument *mapDocument)
connect(mMapDocument, &MapDocument::layerRemoved,
this, &LayerView::layerRemoved);

// Restore expanded layers
for (const int layerId : qAsConst(mMapDocument->expandedGroupLayers)) {
if (Layer *layer = mMapDocument->map()->findLayerById(layerId)) {
const QModelIndex sourceIndex = layerModel->index(layer);
const QModelIndex index = mProxyModel->mapFromSource(sourceIndex);
setExpanded(index, true);
}
}

currentLayerChanged(mMapDocument->currentLayer());
selectedLayersChanged();
} else {
Expand All @@ -328,6 +341,24 @@ void LayerView::editLayerModelIndex(const QModelIndex &layerModelIndex)
edit(mProxyModel->mapFromSource(layerModelIndex));
}

void LayerView::onExpanded(const QModelIndex &proxyIndex)
{
const LayerModel *layerModel = mMapDocument->layerModel();
const QModelIndex index = mProxyModel->mapToSource(proxyIndex);
if (Layer *layer = layerModel->toLayer(index))
if (mMapDocument)
mMapDocument->expandedGroupLayers.insert(layer->id());
}

void LayerView::onCollapsed(const QModelIndex &proxyIndex)
{
const LayerModel *layerModel = mMapDocument->layerModel();
const QModelIndex index = mProxyModel->mapToSource(proxyIndex);
if (Layer *layer = layerModel->toLayer(index))
if (mMapDocument)
mMapDocument->expandedGroupLayers.remove(layer->id());
}

void LayerView::currentRowChanged(const QModelIndex &proxyIndex)
{
if (!mMapDocument)
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/layerdock.h
Expand Up @@ -101,6 +101,9 @@ class LayerView : public QTreeView
const QEvent *event = nullptr) const override;

private:
void onExpanded(const QModelIndex &index);
void onCollapsed(const QModelIndex &index);

void currentRowChanged(const QModelIndex &proxyIndex);
void indexPressed(const QModelIndex &proxyIndex);
void currentLayerChanged(Layer *layer);
Expand Down
4 changes: 4 additions & 0 deletions src/tiled/mapdocument.h
Expand Up @@ -31,6 +31,7 @@

#include <QList>
#include <QRegion>
#include <QSet>

#include <memory>

Expand Down Expand Up @@ -257,6 +258,9 @@ class MapDocument : public Document

void checkIssues() override;

QSet<int> expandedGroupLayers;
QSet<int> expandedObjectLayers;

signals:
/**
* Emitted when the selected tile region changes. Sends the currently
Expand Down
30 changes: 20 additions & 10 deletions src/tiled/mapeditor.cpp
Expand Up @@ -172,10 +172,10 @@ MapEditor::MapEditor(QObject *parent)
mMainWindow->setCentralWidget(mWidgetStack);

mToolsToolBar = new QToolBar(mMainWindow);
mToolsToolBar->setObjectName(QLatin1String("toolsToolBar"));
mToolsToolBar->setObjectName(QStringLiteral("toolsToolBar"));

mToolSpecificToolBar = new QToolBar(mMainWindow);
mToolSpecificToolBar->setObjectName(QLatin1String("toolSpecificToolBar"));
mToolSpecificToolBar->setObjectName(QStringLiteral("toolSpecificToolBar"));

mStampBrush = new StampBrush(this);
mWangBrush = new WangBrush(this);
Expand Down Expand Up @@ -338,6 +338,13 @@ void MapEditor::addDocument(Document *document)
MapDocument *mapDocument = qobject_cast<MapDocument*>(document);
Q_ASSERT(mapDocument);

// Some file state settings need to be restored before the map becomes current
const QVariantMap fileState = Session::current().fileState(mapDocument->fileName());
if (!fileState.isEmpty()) {
mapDocument->expandedGroupLayers = fromSettingsValue<QSet<int>>(fileState.value(QStringLiteral("expandedGroupLayers")));
mapDocument->expandedObjectLayers = fromSettingsValue<QSet<int>>(fileState.value(QStringLiteral("expandedObjectLayers")));
}

MapView *view = new MapView(mWidgetStack);
MapScene *scene = new MapScene(view); // scene is owned by the view

Expand Down Expand Up @@ -605,14 +612,17 @@ void MapEditor::saveDocumentState(MapDocument *mapDocument) const
if (mapDocument->fileName().isEmpty())
return;

QVariantMap fileState;
fileState.insert(QLatin1String("scale"), mapView->zoomable()->scale());

const QRect viewportRect = mapView->viewport()->rect();
const QPointF viewCenter = mapView->mapToScene(viewportRect).boundingRect().center();

fileState.insert(QLatin1String("viewCenter"), toSettingsValue(viewCenter));
fileState.insert(QLatin1String("selectedLayer"), globalIndex(mapDocument->currentLayer()));
QVariantMap fileState;
fileState.insert(QLatin1String("scale"), mapView->zoomable()->scale());
fileState.insert(QStringLiteral("viewCenter"), toSettingsValue(viewCenter));
fileState.insert(QStringLiteral("selectedLayer"), globalIndex(mapDocument->currentLayer()));
if (!mapDocument->expandedGroupLayers.isEmpty())
fileState.insert(QStringLiteral("expandedGroupLayers"), toSettingsValue(mapDocument->expandedGroupLayers));
if (!mapDocument->expandedObjectLayers.isEmpty())
fileState.insert(QStringLiteral("expandedObjectLayers"), toSettingsValue(mapDocument->expandedObjectLayers));

Session::current().setFileState(mapDocument->fileName(), fileState);
}
Expand All @@ -627,14 +637,14 @@ void MapEditor::restoreDocumentState(MapDocument *mapDocument) const
if (fileState.isEmpty())
return;

const qreal scale = fileState.value(QLatin1String("scale")).toReal();
const qreal scale = fileState.value(QStringLiteral("scale")).toReal();
if (scale > 0)
mapView->zoomable()->setScale(scale);

const QPointF viewCenter = fromSettingsValue<QPointF>(fileState.value(QLatin1String("viewCenter")));
const QPointF viewCenter = fromSettingsValue<QPointF>(fileState.value(QStringLiteral("viewCenter")));
mapView->forceCenterOn(viewCenter);

const int layerIndex = fileState.value(QLatin1String("selectedLayer")).toInt();
const int layerIndex = fileState.value(QStringLiteral("selectedLayer")).toInt();
if (Layer *layer = layerAtGlobalIndex(mapDocument->map(), layerIndex))
mapDocument->switchCurrentLayer(layer);
}
Expand Down
14 changes: 1 addition & 13 deletions src/tiled/objectsdock.cpp
Expand Up @@ -115,9 +115,6 @@ ObjectsDock::ObjectsDock(QWidget *parent)
ActionManager::registerAction(mActionMoveUp, "MoveObjectsUp");
ActionManager::registerAction(mActionMoveDown, "MoveObjectsDown");

connect(DocumentManager::instance(), &DocumentManager::documentAboutToClose,
this, &ObjectsDock::documentAboutToClose);

connect(mActionMoveUp, &QAction::triggered, this, &ObjectsDock::moveObjectsUp);
connect(mActionMoveDown, &QAction::triggered, this, &ObjectsDock::moveObjectsDown);
}
Expand All @@ -136,17 +133,14 @@ void ObjectsDock::moveObjectsDown()

void ObjectsDock::setMapDocument(MapDocument *mapDoc)
{
if (mMapDocument) {
mObjectsView->saveExpandedLayers();
if (mMapDocument)
mMapDocument->disconnect(this);
}

mMapDocument = mapDoc;

mObjectsView->setMapDocument(mapDoc);

if (mMapDocument) {
mObjectsView->restoreExpandedLayers();
connect(mMapDocument, &MapDocument::selectedObjectsChanged,
this, &ObjectsDock::updateActions);
}
Expand Down Expand Up @@ -222,10 +216,4 @@ void ObjectsDock::objectProperties()
emit mMapDocument->editCurrentObject();
}

void ObjectsDock::documentAboutToClose(Document *document)
{
if (MapDocument *mapDocument = qobject_cast<MapDocument*>(document))
mObjectsView->clearExpandedLayers(mapDocument);
}

#include "moc_objectsdock.cpp"
1 change: 0 additions & 1 deletion src/tiled/objectsdock.h
Expand Up @@ -49,7 +49,6 @@ class ObjectsDock : public QDockWidget
void aboutToShowMoveToMenu();
void triggeredMoveToMenu(QAction *action);
void objectProperties();
void documentAboutToClose(Document *document);
void moveObjectsUp();
void moveObjectsDown();

Expand Down
27 changes: 11 additions & 16 deletions src/tiled/objectsview.cpp
Expand Up @@ -35,6 +35,7 @@
#include <QKeyEvent>
#include <QMenu>
#include <QPainter>
#include <QScopedValueRollback>

namespace Tiled {

Expand Down Expand Up @@ -79,8 +80,10 @@ void ObjectsView::setMapDocument(MapDocument *mapDoc)
if (mapDoc == mMapDocument)
return;

if (mMapDocument)
if (mMapDocument) {
saveExpandedLayers();
mMapDocument->disconnect(this);
}

mMapDocument = mapDoc;

Expand All @@ -100,6 +103,8 @@ void ObjectsView::setMapDocument(MapDocument *mapDoc)

if (mActiveFilter)
expandAll();
else
restoreExpandedLayers();
} else {
mProxyModel->setSourceModel(nullptr);
}
Expand Down Expand Up @@ -150,7 +155,7 @@ void ObjectsView::saveExpandedLayers()
if (mActiveFilter)
return;

mExpandedLayers[mMapDocument].clear();
mMapDocument->expandedObjectLayers.clear();

for (Layer *layer : mMapDocument->map()->allLayers()) {
if (!layer->isObjectGroup() && !layer->isGroupLayer())
Expand All @@ -159,16 +164,13 @@ void ObjectsView::saveExpandedLayers()
const QModelIndex sourceIndex = mMapDocument->mapObjectModel()->index(layer);
const QModelIndex index = mProxyModel->mapFromSource(sourceIndex);
if (isExpanded(index))
mExpandedLayers[mMapDocument].append(layer->id());
mMapDocument->expandedObjectLayers.insert(layer->id());
}
}

void ObjectsView::restoreExpandedLayers()
{
if (mActiveFilter)
return;

const auto layers = mExpandedLayers.take(mMapDocument);
const auto &layers = mMapDocument->expandedObjectLayers;
for (const int layerId : layers) {
if (Layer *layer = mMapDocument->map()->findLayerById(layerId)) {
const QModelIndex sourceIndex = mMapDocument->mapObjectModel()->index(layer);
Expand All @@ -178,11 +180,6 @@ void ObjectsView::restoreExpandedLayers()
}
}

void ObjectsView::clearExpandedLayers(MapDocument *mapDocument)
{
mExpandedLayers.remove(mapDocument);
}

bool ObjectsView::event(QEvent *event)
{
if (event->type() == QEvent::ShortcutOverride) {
Expand Down Expand Up @@ -275,9 +272,8 @@ void ObjectsView::selectionChanged(const QItemSelection &selected,
}

if (selectedObjects != mMapDocument->selectedObjects()) {
mSynching = true;
QScopedValueRollback<bool> synching(mSynching, true);
mMapDocument->setSelectedObjects(selectedObjects);
mSynching = false;
}
}

Expand Down Expand Up @@ -374,12 +370,11 @@ void ObjectsView::synchronizeSelectedItems()
itemSelection.select(index, index);
}

mSynching = true;
QScopedValueRollback<bool> synching(mSynching, true);
selectionModel()->select(itemSelection,
QItemSelectionModel::Select |
QItemSelectionModel::Rows |
QItemSelectionModel::Clear);
mSynching = false;
}

void ObjectsView::expandToSelectedObjects()
Expand Down
9 changes: 3 additions & 6 deletions src/tiled/objectsview.h
Expand Up @@ -51,11 +51,6 @@ class ObjectsView : public QTreeView

void setFilter(const QString &filter);

public slots:
void saveExpandedLayers();
void restoreExpandedLayers();
void clearExpandedLayers(MapDocument *mapDocument);

protected:
bool event(QEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
Expand All @@ -69,6 +64,9 @@ public slots:
const QModelIndex &index) const override;

private:
void saveExpandedLayers();
void restoreExpandedLayers();

void onActivated(const QModelIndex &proxyIndex);
void onSectionResized(int logicalIndex);
void selectedObjectsChanged();
Expand All @@ -85,7 +83,6 @@ public slots:

MapDocument *mMapDocument = nullptr;
ReversingRecursiveFilterModel *mProxyModel;
QMap<MapDocument*, QList<int> > mExpandedLayers;
bool mSynching = false;
bool mActiveFilter = false;
};
Expand Down
20 changes: 20 additions & 0 deletions src/tiled/session.h
Expand Up @@ -113,6 +113,26 @@ inline QVariant toSettingsValue<QPointF>(const QPointF &point)
};
}

template<>
inline QSet<int> fromSettingsValue<QSet<int>>(const QVariant &value)
{
const auto variantList = value.toList();
QSet<int> set;
for (const auto &variantValue : variantList)
set.insert(variantValue.value<int>());
return set;
}

template<>
inline QVariant toSettingsValue<QSet<int>>(const QSet<int> &set)
{
QVariantList variantList;
variantList.reserve(set.size());
for (const int value : set)
variantList.append(value);
return variantList;
}


class Session : protected FileHelper
{
Expand Down

0 comments on commit fa73f87

Please sign in to comment.