From 4c2ab41e68b55b81c4defa52aebb3e940a11048c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 29 Jun 2023 12:47:11 +0200 Subject: [PATCH] Share the selection between Tilesets view and Tileset Editor Now TilesetDocument owns a single TilesetModel and associated QItemSelectionModel, which are used by the TilesetView instances in the TilesetDock as well as in the TilesetEditor. --- src/tiled/tilesetdock.cpp | 10 ++++++---- src/tiled/tilesetdocument.cpp | 18 ++++++++++++++++++ src/tiled/tilesetdocument.h | 7 +++++++ src/tiled/tileseteditor.cpp | 13 ++++++------- src/tiled/tilesetmodel.cpp | 3 ++- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/tiled/tilesetdock.cpp b/src/tiled/tilesetdock.cpp index ff4eb196d37..a66be350e68 100644 --- a/src/tiled/tilesetdock.cpp +++ b/src/tiled/tilesetdock.cpp @@ -1065,12 +1065,14 @@ TilesetView *TilesetDock::tilesetViewAt(int index) const void TilesetDock::setupTilesetModel(TilesetView *view, TilesetDocument *tilesetDocument) { - view->setModel(new TilesetModel(tilesetDocument, view)); + QItemSelectionModel *selectionModel = tilesetDocument->tilesetSelectionModel(); - QItemSelectionModel *s = view->selectionModel(); - connect(s, &QItemSelectionModel::selectionChanged, + view->setModel(tilesetDocument->tilesetModel()); + view->setSelectionModel(selectionModel); + + connect(selectionModel, &QItemSelectionModel::selectionChanged, this, &TilesetDock::selectionChanged); - connect(s, &QItemSelectionModel::currentChanged, + connect(selectionModel, &QItemSelectionModel::currentChanged, this, &TilesetDock::currentChanged); connect(view, &TilesetView::pressed, this, &TilesetDock::indexPressed); diff --git a/src/tiled/tilesetdocument.cpp b/src/tiled/tilesetdocument.cpp index 89e4ec4250a..c57384b777f 100644 --- a/src/tiled/tilesetdocument.cpp +++ b/src/tiled/tilesetdocument.cpp @@ -28,12 +28,14 @@ #include "mapdocument.h" #include "tile.h" #include "tilesetformat.h" +#include "tilesetmodel.h" #include "tilesetwangsetmodel.h" #include "wangcolormodel.h" #include "wangset.h" #include #include +#include #include #include @@ -384,6 +386,22 @@ QList TilesetDocument::currentObjects() const return Document::currentObjects(); } +TilesetModel *TilesetDocument::tilesetModel() +{ + if (!mTilesetModel) + mTilesetModel = new TilesetModel(this, this); + + return mTilesetModel; +} + +QItemSelectionModel *TilesetDocument::tilesetSelectionModel() +{ + if (!mTilesetSelectionModel) + mTilesetSelectionModel = new QItemSelectionModel(tilesetModel(), this); + + return mTilesetSelectionModel; +} + /** * Returns the WangColorModel instance for the given \a wangSet. * The model instances are created on-demand and owned by the document. diff --git a/src/tiled/tilesetdocument.h b/src/tiled/tilesetdocument.h index 05f54db7265..d87a220b171 100644 --- a/src/tiled/tilesetdocument.h +++ b/src/tiled/tilesetdocument.h @@ -31,6 +31,8 @@ #include #include +class QItemSelectionModel; + namespace Tiled { class ObjectGroup; @@ -38,6 +40,7 @@ class WangColor; class MapDocument; class TilesetDocument; +class TilesetModel; class TilesetWangSetModel; class WangColorModel; @@ -109,6 +112,8 @@ class TilesetDocument : public Document TilesetWangSetModel *wangSetModel() const { return mWangSetModel; } + TilesetModel *tilesetModel(); + QItemSelectionModel *tilesetSelectionModel(); WangColorModel *wangColorModel(WangSet *wangSet); void setTileImage(Tile *tile, const QPixmap &image, const QUrl &source); @@ -178,6 +183,8 @@ class TilesetDocument : public Document SharedTileset mTileset; QList mMapDocuments; + TilesetModel *mTilesetModel = nullptr; + QItemSelectionModel *mTilesetSelectionModel = nullptr; TilesetWangSetModel *mWangSetModel; std::unordered_map> mWangColorModels; diff --git a/src/tiled/tileseteditor.cpp b/src/tiled/tileseteditor.cpp index 794dd1ea103..f9c5b443536 100644 --- a/src/tiled/tileseteditor.cpp +++ b/src/tiled/tileseteditor.cpp @@ -280,11 +280,11 @@ void TilesetEditor::addDocument(Document *document) view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); - TilesetModel *tilesetModel = new TilesetModel(tilesetDocument, view); - view->setModel(tilesetModel); + TilesetModel *tilesetModel = tilesetDocument->tilesetModel(); + QItemSelectionModel *selectionModel = tilesetDocument->tilesetSelectionModel(); - connect(tilesetDocument, &TilesetDocument::tileWangSetChanged, - tilesetModel, &TilesetModel::tilesChanged); + view->setModel(tilesetModel); + view->setSelectionModel(selectionModel); connect(tilesetDocument, &TilesetDocument::tilesetChanged, this, &TilesetEditor::tilesetChanged); @@ -296,9 +296,8 @@ void TilesetEditor::addDocument(Document *document) connect(view, &TilesetView::wangIdUsedChanged, mWangDock, &WangDock::onWangIdUsedChanged); connect(view, &TilesetView::currentWangIdChanged, mWangDock, &WangDock::onCurrentWangIdChanged); - QItemSelectionModel *s = view->selectionModel(); - connect(s, &QItemSelectionModel::selectionChanged, this, &TilesetEditor::selectionChanged); - connect(s, &QItemSelectionModel::currentChanged, this, &TilesetEditor::currentChanged); + connect(selectionModel, &QItemSelectionModel::selectionChanged, this, &TilesetEditor::selectionChanged); + connect(selectionModel, &QItemSelectionModel::currentChanged, this, &TilesetEditor::currentChanged); connect(view, &TilesetView::pressed, this, &TilesetEditor::indexPressed); mViewForTileset.insert(tilesetDocument, view); diff --git a/src/tiled/tilesetmodel.cpp b/src/tiled/tilesetmodel.cpp index e214ef18d7e..a10fe1b3754 100644 --- a/src/tiled/tilesetmodel.cpp +++ b/src/tiled/tilesetmodel.cpp @@ -21,7 +21,6 @@ #include "tilesetmodel.h" -#include "map.h" #include "relocatetiles.h" #include "tile.h" #include "tiled.h" @@ -40,6 +39,8 @@ TilesetModel::TilesetModel(TilesetDocument *tilesetDocument, QObject *parent) connect(tilesetDocument, &TilesetDocument::tileImageSourceChanged, this, &TilesetModel::tileChanged); + connect(tilesetDocument, &TilesetDocument::tileWangSetChanged, + this, &TilesetModel::tilesChanged); connect(tilesetDocument, &TilesetDocument::tileAnimationChanged, this, &TilesetModel::tileChanged); }