1 /*
2  * tilesetmodel.h
3  * Copyright 2008-2009, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4  * Copyright 2009, Edward Hutchins <eah1@yahoo.com>
5  *
6  * This file is part of Tiled.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #pragma once
23 
24 #include "tile.h"
25 
26 #include <QAbstractListModel>
27 
28 namespace Tiled {
29 
30 class Tileset;
31 class TilesetDocument;
32 
33 /**
34  * A model wrapping a tileset of a map. Used to display the tiles.
35  */
36 class TilesetModel : public QAbstractListModel
37 {
38     Q_OBJECT
39 
40 public:
41     /**
42      * Constructor.
43      *
44      * @param tilesetDocument the initial tileset to display
45      */
46     TilesetModel(TilesetDocument *tilesetDocument, QObject *parent = nullptr);
47 
48     /**
49      * Returns the number of rows.
50      */
51     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
52 
53     /**
54      * Returns the number of columns.
55      */
56     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
57 
58     /**
59      * Returns the data stored under the given <i>role</i> for the item
60      * referred to by the <i>index</i>.
61      */
62     QVariant data(const QModelIndex &index,
63                   int role = Qt::DisplayRole) const override;
64 
65 
66 
67     /**
68      * Returns a small size hint, to prevent the headers from affecting the
69      * minimum width and height of the sections.
70      */
71     QVariant headerData(int section, Qt::Orientation orientation,
72                         int role = Qt::DisplayRole) const override;
73 
74     Qt::ItemFlags flags(const QModelIndex &index) const override;
75     Qt::DropActions supportedDropActions() const override;
76 
77     QStringList mimeTypes() const override;
78     QMimeData *mimeData(const QModelIndexList &indexes) const override;
79     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
80                       int row, int column,
81                       const QModelIndex &parent) override;
82 
83     /**
84      * Returns the tile at the given index.
85      */
86     Tile *tileAt(const QModelIndex &index) const;
87 
88     /**
89      * Returns the index of the given \a tile. The tile is required to be from
90      * the tileset used by this model.
91      */
92     QModelIndex tileIndex(const Tile *tile) const;
93 
94     /**
95      * Returns the tileset associated with this model.
96      */
97     Tileset *tileset() const;
98 
99     /**
100      * Refreshes the list of tile IDs. Should be called after tiles are added
101      * or removed from the tileset.
102      */
103     void tilesetChanged();
104 
105     void setColumnCountOverride(int columnCount);
106 
107 public slots:
108     /**
109      * Should be called when anything changes about the given \a tiles that
110      * affects their display in any views on this model.
111      *
112      * Tiles that are not from the tileset displayed by this model are simply
113      * ignored. All tiles in the list are assumed to be from the same tileset.
114      *
115      * \sa TilesetDocument::tileWangSetChanged
116      */
117     void tilesChanged(const QList<Tile*> &tiles);
118 
119 private:
120     /**
121      * Should be called when anything changes about the given \a tile that
122      * affects its display in any views on this model.
123      *
124      * \sa TilesetDocument::tileAnimationChanged
125      * \sa TilesetDocument::tileImageSourceChanged
126      */
127     void tileChanged(Tile *tile);
128 
129     void refreshTileIds();
130 
131     TilesetDocument *mTilesetDocument;
132     QList<int> mTileIds;
133     int mColumnCountOverride = 0;
134 };
135 
136 } // namespace Tiled
137