1 /*
2  * tilesetdocumentsmodel.h
3  * Copyright 2017, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include "tilesetdocument.h"
24 
25 #include <QAbstractListModel>
26 #include <QList>
27 #include <QSortFilterProxyModel>
28 
29 namespace Tiled {
30 
31 class Tileset;
32 
33 class MapDocument;
34 
35 /**
36  * This model exposes the list of tileset documents. This includes opened
37  * tileset files, as well as both internal and external tilesets loaded as part
38  * of a map.
39  *
40  * It also owns the TilesetDocument instances.
41  */
42 class TilesetDocumentsModel : public QAbstractListModel
43 {
44 public:
45     enum {
46         TilesetDocumentRole = Qt::UserRole,
47         TilesetRole,
48     };
49 
50     TilesetDocumentsModel(QObject *parent = nullptr);
51 
52     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
53     QVariant data(const QModelIndex &index, int role) const override;
54 
55     const QVector<TilesetDocumentPtr> &tilesetDocuments() const;
56 
57     bool contains(TilesetDocument *tilesetDocument) const;
58     void append(TilesetDocument *tilesetDocument);
59     void insert(int index, TilesetDocument *tilesetDocument);
60     void remove(TilesetDocument *tilesetDocument);
61     void remove(int index);
62 
63 private:
64     void tilesetNameChanged(Tileset *tileset);
65     void tilesetFileNameChanged();
66 
67     QVector<TilesetDocumentPtr> mTilesetDocuments;
68 };
69 
70 
tilesetDocuments()71 inline const QVector<TilesetDocumentPtr> &TilesetDocumentsModel::tilesetDocuments() const
72 {
73     return mTilesetDocuments;
74 }
75 
contains(TilesetDocument * tilesetDocument)76 inline bool TilesetDocumentsModel::contains(TilesetDocument *tilesetDocument) const
77 {
78     return std::find(mTilesetDocuments.begin(),
79                      mTilesetDocuments.end(),
80                      tilesetDocument) != mTilesetDocuments.end();
81 }
82 
append(TilesetDocument * tilesetDocument)83 inline void TilesetDocumentsModel::append(TilesetDocument *tilesetDocument)
84 {
85     insert(mTilesetDocuments.size(), tilesetDocument);
86 }
87 
remove(TilesetDocument * tilesetDocument)88 inline void TilesetDocumentsModel::remove(TilesetDocument *tilesetDocument)
89 {
90     remove(mTilesetDocuments.indexOf(tilesetDocument->sharedFromThis()));
91 }
92 
93 
94 /**
95  * Sorts the tilesets in alphabetical order and filters out embedded tilesets
96  * that are not part of the current map document.
97  */
98 class TilesetDocumentsFilterModel : public QSortFilterProxyModel
99 {
100 public:
101     TilesetDocumentsFilterModel(QObject *parent = nullptr);
102 
103     void setMapDocument(MapDocument *mapDocument);
104 
105 protected:
106     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
107 
108 private:
109     MapDocument *mMapDocument;
110 };
111 
112 } // namespace Tiled
113