Skip to content

Commit

Permalink
Share the selection between Tilesets view and Tileset Editor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjorn committed Jun 17, 2024
1 parent bd891d6 commit ec6b9e2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/tiled/tilesetdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,12 +1062,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);
Expand Down
18 changes: 18 additions & 0 deletions src/tiled/tilesetdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
#include "mapdocument.h"
#include "tile.h"
#include "tilesetformat.h"
#include "tilesetmodel.h"
#include "tilesetwangsetmodel.h"
#include "wangcolormodel.h"
#include "wangset.h"

#include <QCoreApplication>
#include <QFileInfo>
#include <QItemSelectionModel>
#include <QQmlEngine>
#include <QUndoStack>

Expand Down Expand Up @@ -386,6 +388,22 @@ QList<Object *> 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.
Expand Down
7 changes: 7 additions & 0 deletions src/tiled/tilesetdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
#include <memory>
#include <unordered_map>

class QItemSelectionModel;

namespace Tiled {

class ObjectGroup;
class WangColor;

class MapDocument;
class TilesetDocument;
class TilesetModel;
class TilesetWangSetModel;
class WangColorModel;

Expand Down Expand Up @@ -109,6 +112,8 @@ class TilesetDocument final : 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);
Expand Down Expand Up @@ -178,6 +183,8 @@ class TilesetDocument final : public Document
SharedTileset mTileset;
QList<MapDocument*> mMapDocuments;

TilesetModel *mTilesetModel = nullptr;
QItemSelectionModel *mTilesetSelectionModel = nullptr;
TilesetWangSetModel *mWangSetModel;
std::unordered_map<WangSet*, std::unique_ptr<WangColorModel>> mWangColorModels;

Expand Down
11 changes: 5 additions & 6 deletions src/tiled/tileseteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ void TilesetEditor::addDocument(Document *document)
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

auto *tilesetModel = new TilesetModel(tilesetDocument, view);
view->setModel(tilesetModel);
auto *selectionModel = tilesetDocument->tilesetSelectionModel();

connect(tilesetDocument, &TilesetDocument::tileWangSetChanged,
tilesetModel, &TilesetModel::tilesChanged);
view->setModel(tilesetModel);
view->setSelectionModel(selectionModel);

connect(tilesetDocument, &TilesetDocument::tilesetChanged,
this, &TilesetEditor::tilesetChanged);
Expand All @@ -297,9 +297,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);
Expand Down
3 changes: 2 additions & 1 deletion src/tiled/tilesetmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "tilesetmodel.h"

#include "map.h"
#include "relocatetiles.h"
#include "tile.h"
#include "tiled.h"
Expand All @@ -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);
}
Expand Down

0 comments on commit ec6b9e2

Please sign in to comment.