1 /*
2  * tilesetview.h
3  * Copyright 2008-2010, Thorbjørn Lindeijer <thorbjorn@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 "tilesetmodel.h"
24 #include "wangset.h"
25 
26 #include <QTableView>
27 
28 namespace Tiled {
29 
30 class ChangeEvent;
31 class TilesetDocument;
32 class Zoomable;
33 
34 /**
35  * The tileset view. May only be used with the TilesetModel.
36  */
37 class TilesetView : public QTableView
38 {
39     Q_OBJECT
40 
41 public:
42     TilesetView(QWidget *parent = nullptr);
43 
44     /**
45      * Sets the tileset document associated with the tileset to be displayed,
46      * which is needed for the undo support.
47      */
48     void setTilesetDocument(TilesetDocument *tilesetDocument);
49     TilesetDocument *tilesetDocument() const;
50 
51     QSize sizeHint() const override;
52 
53     int sizeHintForColumn(int column) const override;
54     int sizeHintForRow(int row) const override;
55 
zoomable()56     Zoomable *zoomable() const { return mZoomable; }
57 
58     /**
59      * Returns the scale at which the tileset is displayed.
60      */
61     qreal scale() const;
62 
drawGrid()63     bool drawGrid() const { return mDrawGrid; }
64 
65     void setDynamicWrapping(bool enabled);
66     bool dynamicWrapping() const;
67 
68     void setModel(QAbstractItemModel *model) override;
69 
70     /**
71      * Convenience method that returns the model as a TilesetModel.
72      */
tilesetModel()73     TilesetModel *tilesetModel() const
74     { return static_cast<TilesetModel *>(model()); }
75 
76     /**
77      * Sets whether animated tiles should be marked graphically. Enabled by
78      * default.
79      */
80     void setMarkAnimatedTiles(bool enabled);
81     bool markAnimatedTiles() const;
82 
83     void setRelocateTiles(bool enabled);
isRelocateTiles()84     bool isRelocateTiles() const { return mRelocateTiles; }
85 
86     void setEditWangSet(bool enabled);
isEditWangSet()87     bool isEditWangSet() const { return mEditWangSet; }
88 
wangSet()89     WangSet *wangSet() const { return mWangSet; }
90     void setWangSet(WangSet *wangSet);
91 
wangId()92     WangId wangId() const { return mWangId; }
93     void setWangId(WangId  wangId);
94 
95     void setWangColor(int color);
96 
hoveredIndex()97     QModelIndex hoveredIndex() const { return mHoveredIndex; }
98 
99     QIcon imageMissingIcon() const;
100 
101     void updateBackgroundColor();
102 
103 signals:
104     void wangSetImageSelected(Tile *tile);
105     void wangColorImageSelected(Tile *tile, int index);
106     void wangIdUsedChanged(WangId wangId);
107     void currentWangIdChanged(WangId wangId);
108     void swapTilesRequested(Tile *tileA, Tile *tileB);
109 
110 protected:
111     bool event(QEvent *event) override;
112     void keyPressEvent(QKeyEvent *event) override;
113     void mousePressEvent(QMouseEvent *event) override;
114     void mouseMoveEvent(QMouseEvent *event) override;
115     void mouseReleaseEvent(QMouseEvent *event) override;
116     void leaveEvent(QEvent *) override;
117     void wheelEvent(QWheelEvent *event) override;
118     void contextMenuEvent(QContextMenuEvent *event) override;
119     void resizeEvent(QResizeEvent *event) override;
120 
121 private:
122     void onChange(const ChangeEvent &change);
123 
124     void selectWangSetImage();
125     void selectWangColorImage();
126     void editTileProperties();
127     void swapTiles();
128     void setDrawGrid(bool drawGrid);
129 
130     void adjustScale();
131     void refreshColumnCount();
132 
133     void applyWangId();
134     void finishWangIdChange();
135     Tile *currentTile() const;
136     void setHandScrolling(bool handScrolling);
137 
138     enum WangBehavior {
139         AssignWholeId,      // Assigning templates
140         AssignHoveredIndex, // Assigning color to hovered index
141     };
142 
143     enum WrapBehavior {
144         WrapDefault,
145         WrapDynamic,
146         WrapFixed,
147     };
148 
149     Zoomable *mZoomable;
150     TilesetDocument *mTilesetDocument = nullptr;
151     bool mDrawGrid;
152     bool mMarkAnimatedTiles = true;
153     bool mRelocateTiles = false;
154     bool mEditWangSet = false;
155     WrapBehavior mWrapBehavior = WrapDefault;
156     WangBehavior mWangBehavior = AssignWholeId;
157     WangSet *mWangSet = nullptr;
158     WangId mWangId;
159     int mWangColorIndex = 0;
160     QModelIndex mHoveredIndex;
161     bool mWangIdChanged = false;
162 
163     bool mHandScrolling = false;
164     QPoint mLastMousePos;
165 
166     const QIcon mImageMissingIcon;
167 };
168 
tilesetDocument()169 inline TilesetDocument *TilesetView::tilesetDocument() const
170 {
171     return mTilesetDocument;
172 }
173 
markAnimatedTiles()174 inline bool TilesetView::markAnimatedTiles() const
175 {
176     return mMarkAnimatedTiles;
177 }
178 
179 } // namespace Tiled
180 
181 Q_DECLARE_METATYPE(Tiled::TilesetView *)
182