1 /*
2  * tilesetwangsetmodel.h
3  * Copyright 2017, Benjamin Trotter <bdtrotte@ucsc.edu>
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 "wangset.h"
24 
25 #include <QAbstractItemModel>
26 
27 #include <memory>
28 
29 namespace Tiled {
30 
31 class Tileset;
32 
33 class TilesetDocument;
34 
35 /**
36  * This model displays the Wang sets of a single tileset.
37  *
38  * It also provides some functions for modifying those Wang sets.
39  */
40 class TilesetWangSetModel : public QAbstractListModel
41 {
42     Q_OBJECT
43 
44 public:
45     // Synchronized with WangSetModel
46     enum UserRoles {
47         WangSetRole = Qt::UserRole,
48         TilesetDocumentRole,
49     };
50 
51     explicit TilesetWangSetModel(TilesetDocument *tilesetDocument,
52                                  QObject *parent = nullptr);
53     ~TilesetWangSetModel() override;
54 
55     using QAbstractListModel::index;
56     QModelIndex index(WangSet *wangSet);
57 
58     /**
59      * Returns the number of rows. For the root, this is the number of wangSets
60      * in the tileset.
61      */
62     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
63 
64     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
65 
66     QVariant data(const QModelIndex &index,
67                   int role = Qt::DisplayRole) const override;
68 
69     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
70 
71     Qt::ItemFlags flags(const QModelIndex &index) const override;
72 
73     WangSet *wangSetAt(const QModelIndex &index) const;
74 
75     void insertWangSet(int index, std::unique_ptr<WangSet> wangSet);
76     std::unique_ptr<WangSet> takeWangSetAt(int index);
77     void setWangSetName(WangSet *wangSet, const QString &name);
78     void setWangSetType(WangSet *wangSet, WangSet::Type type);
79     void setWangSetColorCount(WangSet *wangSet, int value);
80     void setWangSetImage(WangSet *wangSet, int tileId);
81     void insertWangColor(WangSet *wangSet, const QSharedPointer<WangColor> &wangColor);
82     QSharedPointer<WangColor> takeWangColorAt(WangSet *wangSet, int color);
83 
84 signals:
85     void wangSetAdded(Tileset *tileset, int index);
86     void wangSetRemoved(WangSet *wangSet);
87 
88     void wangColorRemoved(WangColor *wangColor);
89 
90     /**
91      * Emitted when either the name, image, colorCount or type of a wangSet
92      * changed.
93      */
94     void wangSetChanged(Tileset *tileset, int index);
95 
96 private:
97     void emitWangSetChange(WangSet *wangSet);
98 
99     TilesetDocument *mTilesetDocument;
100 };
101 
102 } // namespace Tiled
103