Skip to content

Commit 5e74ff4

Browse files
committed
Addressed various issues with mapping indexes from source model
Introduced ProjectView::filePath since we need to get the path from a proxy index a lot. Renamed ProjectView::model to ProjectView::projectModel to reduce chance of accidentally using the source model with proxy indices.
1 parent c55102c commit 5e74ff4

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

src/tiled/projectdock.cpp

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class ProjectView final : public QTreeView
6262
*/
6363
QSize sizeHint() const override;
6464

65-
ProjectModel *model() const { return mProjectModel; }
65+
ProjectModel *projectModel() const { return mProjectModel; }
6666

6767
// TODO: Add 'select by file name'
6868

@@ -72,6 +72,8 @@ class ProjectView final : public QTreeView
7272

7373
void selectPath(const QString &path);
7474

75+
QString filePath(const QModelIndex &index) const;
76+
7577
protected:
7678
void contextMenuEvent(QContextMenuEvent *event) override;
7779

@@ -111,8 +113,10 @@ ProjectDock::ProjectDock(QWidget *parent)
111113
connect(mProjectView->selectionModel(), &QItemSelectionModel::currentRowChanged,
112114
this, &ProjectDock::onCurrentRowChanged);
113115

114-
connect(mProjectView->model(), &ProjectModel::folderAdded, this, &ProjectDock::folderAdded);
115-
connect(mProjectView->model(), &ProjectModel::folderRemoved, this, &ProjectDock::folderRemoved);
116+
// Forwarding signals
117+
auto projectModel = mProjectView->projectModel();
118+
connect(projectModel, &ProjectModel::folderAdded, this, &ProjectDock::folderAdded);
119+
connect(projectModel, &ProjectModel::folderRemoved, this, &ProjectDock::folderRemoved);
116120
}
117121

118122
void ProjectDock::addFolderToProject()
@@ -134,15 +138,15 @@ void ProjectDock::addFolderToProject()
134138
if (folder.isEmpty())
135139
return;
136140

137-
mProjectView->model()->addFolder(folder);
141+
mProjectView->projectModel()->addFolder(folder);
138142
mProjectView->addExpandedPath(folder);
139143

140144
project.save();
141145
}
142146

143147
void ProjectDock::refreshProjectFolders()
144148
{
145-
mProjectView->model()->refreshFolders();
149+
mProjectView->projectModel()->refreshFolders();
146150
}
147151

148152
void ProjectDock::setExpandedPaths(const QStringList &expandedPaths)
@@ -167,7 +171,7 @@ void ProjectDock::onCurrentRowChanged(const QModelIndex &current)
167171
if (!current.isValid())
168172
return;
169173

170-
const auto filePath = mProjectView->model()->filePath(current);
174+
const auto filePath = mProjectView->filePath(current);
171175
if (QFileInfo { filePath }.isFile())
172176
emit fileSelected(filePath);
173177
}
@@ -206,17 +210,17 @@ ProjectView::ProjectView(QWidget *parent)
206210

207211
connect(this, &QTreeView::expanded,
208212
this, [=] (const QModelIndex &index) {
209-
mExpandedPaths.insert(mProjectModel->filePath(mProxyModel->mapToSource(index)));
213+
mExpandedPaths.insert(filePath(index));
210214
});
211215
connect(this, &QTreeView::collapsed,
212216
this, [=] (const QModelIndex &index) {
213-
mExpandedPaths.remove(mProjectModel->filePath(mProxyModel->mapToSource(index)));
217+
mExpandedPaths.remove(filePath(index));
214218
});
215219

216220
// Reselect a previously selected path and restore scrollbar after refresh
217221
connect(mProjectModel, &ProjectModel::aboutToRefresh,
218222
this, [=] {
219-
mSelectedPath = mProjectModel->filePath(mProxyModel->mapToSource(currentIndex()));
223+
mSelectedPath = filePath(currentIndex());
220224
mScrollBarValue = verticalScrollBar()->value();
221225
});
222226
connect(mProjectModel, &ProjectModel::refreshed,
@@ -243,40 +247,44 @@ void ProjectView::addExpandedPath(const QString &path)
243247

244248
void ProjectView::selectPath(const QString &path)
245249
{
246-
auto sourceIndex = mProjectModel->index(path);
247-
auto proxyIndex = mProxyModel->mapFromSource(sourceIndex);
248-
if (proxyIndex.isValid()) {
250+
const auto sourceIndex = mProjectModel->index(path);
251+
const auto proxyIndex = mProxyModel->mapFromSource(sourceIndex);
252+
if (proxyIndex.isValid())
249253
setCurrentIndex(proxyIndex);
250-
}
254+
}
255+
256+
QString ProjectView::filePath(const QModelIndex &index) const
257+
{
258+
return mProjectModel->filePath(mProxyModel->mapToSource(index));
251259
}
252260

253261
void ProjectView::contextMenuEvent(QContextMenuEvent *event)
254262
{
255-
const QModelIndex index = indexAt(event->pos());
263+
const auto index = indexAt(event->pos());
256264

257265
QMenu menu;
258266

259267
if (index.isValid()) {
260-
const auto filePath = model()->filePath(index);
268+
const auto path = filePath(index);
261269

262-
Utils::addFileManagerActions(menu, filePath);
270+
Utils::addFileManagerActions(menu, path);
263271

264-
if (QFileInfo { filePath }.isFile()) {
265-
Utils::addOpenWithSystemEditorAction(menu, filePath);
272+
if (QFileInfo { path }.isFile()) {
273+
Utils::addOpenWithSystemEditorAction(menu, path);
266274

267275
auto mapDocumentActionHandler = MapDocumentActionHandler::instance();
268276
auto mapDocument = mapDocumentActionHandler->mapDocument();
269277

270278
// Add template-specific actions
271-
auto objectTemplate = TemplateManager::instance()->loadObjectTemplate(filePath);
279+
auto objectTemplate = TemplateManager::instance()->loadObjectTemplate(path);
272280
if (objectTemplate->object()) {
273281
menu.addSeparator();
274282
menu.addAction(tr("Select Template Instances"), [=] {
275283
mapDocumentActionHandler->selectAllInstances(objectTemplate);
276284
})->setEnabled(mapDocument != nullptr);
277285
}
278286
// Add tileset-specific actions
279-
else if (auto tileset = TilesetManager::instance()->loadTileset(filePath)) {
287+
else if (auto tileset = TilesetManager::instance()->loadTileset(path)) {
280288
if (mapDocument) {
281289
auto documentManager = DocumentManager::instance();
282290
auto mapEditor = static_cast<MapEditor*>(documentManager->editor(Document::MapDocumentType));
@@ -302,8 +310,8 @@ void ProjectView::contextMenuEvent(QContextMenuEvent *event)
302310
if (!index.parent().isValid()) {
303311
menu.addSeparator();
304312
auto removeFolder = menu.addAction(tr("&Remove Folder from Project"), [=] {
305-
model()->removeFolder(index.row());
306-
model()->project().save();
313+
projectModel()->removeFolder(index.row());
314+
projectModel()->project().save();
307315
});
308316
Utils::setThemeIcon(removeFolder, "list-remove");
309317
}
@@ -320,29 +328,26 @@ void ProjectView::contextMenuEvent(QContextMenuEvent *event)
320328

321329
void ProjectView::onActivated(const QModelIndex &index)
322330
{
323-
const QString path = model()->filePath(index);
331+
const QString path = filePath(index);
324332
if (QFileInfo(path).isFile())
325333
DocumentManager::instance()->openFile(path);
326334
}
327335

328336
void ProjectView::onRowsInserted(const QModelIndex &parent)
329337
{
330-
if (parent.isValid()) {
331-
auto sourceParent = mProxyModel->mapToSource(parent);
332-
restoreExpanded(sourceParent);
333-
}
338+
if (parent.isValid())
339+
restoreExpanded(parent);
334340
}
335341

336342
void ProjectView::restoreExpanded(const QModelIndex &parent)
337343
{
338-
const QString path = mProjectModel->filePath(parent);
344+
const QString path = filePath(parent);
339345

340346
if (mExpandedPaths.contains(path)) {
341-
auto proxyParent = mProxyModel->mapFromSource(parent);
342-
setExpanded(proxyParent, true);
347+
setExpanded(parent, true);
343348

344-
for (int row = 0, count = mProjectModel->rowCount(parent); row < count; ++row)
345-
restoreExpanded(mProjectModel->index(row, 0, parent));
349+
for (int row = 0, count = model()->rowCount(parent); row < count; ++row)
350+
restoreExpanded(model()->index(row, 0, parent));
346351
}
347352
}
348353

src/tiled/projectdock.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
#pragma once
2222

23-
#include "project.h"
24-
2523
#include <QDockWidget>
2624

2725
namespace Tiled {

0 commit comments

Comments
 (0)