1 /*
2  * tilestampmodel.h
3  * Copyright 2015, 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 "tilestamp.h"
24 
25 #include <QAbstractItemModel>
26 
27 namespace Tiled {
28 
29 class Map;
30 
31 
32 struct TileStampVariation;
33 
34 class TileStampModel : public QAbstractItemModel
35 {
36     Q_OBJECT
37 
38 public:
39     TileStampModel(QObject *parent = nullptr);
40 
41     QModelIndex index(int row, int column,
42                       const QModelIndex &parent = QModelIndex()) const override;
43     QModelIndex index(const TileStamp &stamp) const;
44 
45     QModelIndex parent(const QModelIndex &index) const override;
46 
47     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
48     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
49 
50     QVariant headerData(int section, Qt::Orientation orientation,
51                         int role = Qt::DisplayRole) const override;
52 
53     bool setData(const QModelIndex &index, const QVariant &value,
54                  int role = Qt::EditRole) override;
55 
56     QVariant data(const QModelIndex &index,
57                   int role = Qt::DisplayRole) const override;
58 
59     Qt::ItemFlags flags(const QModelIndex &index) const override;
60 
61     bool removeRows(int row, int count, const QModelIndex &parent) override;
62 
63     /**
64      * Returns the stamp at the given \a index.
65      */
66     const TileStamp &stampAt(const QModelIndex &index) const;
67     bool isStamp(const QModelIndex &index) const;
68 
69     const TileStampVariation *variationAt(const QModelIndex &index) const;
70 
71     const QList<TileStamp> &stamps() const;
72 
73     void addStamp(const TileStamp &stamp);
74     void removeStamp(const TileStamp &stamp);
75 
76     void addVariation(const TileStamp &stamp,
77                       const TileStampVariation &variation);
78 
79     void clear();
80 
81 signals:
82     void stampAdded(const TileStamp &stamp);
83     void stampRenamed(const TileStamp &stamp);
84     void stampChanged(const TileStamp &stamp);
85     void stampRemoved(const TileStamp &stamp);
86 
87 private:
88     QList<TileStamp> mStamps;
89 
90     mutable QHash<Map *, QPixmap> mThumbnailCache;
91 };
92 
93 
index(const TileStamp & stamp)94 inline QModelIndex TileStampModel::index(const TileStamp &stamp) const
95 {
96     const int i = mStamps.indexOf(stamp);
97     return i == -1 ? QModelIndex() : TileStampModel::index(i, 0);
98 }
99 
stamps()100 inline const QList<TileStamp> &TileStampModel::stamps() const
101 {
102     return mStamps;
103 }
104 
105 } // namespace Tiled
106