-
Notifications
You must be signed in to change notification settings - Fork 10
/
LayersWidget.h
206 lines (175 loc) · 5.33 KB
/
LayersWidget.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef __LAYERS_WIDGET_H__
#define __LAYERS_WIDGET_H__
#include <QWidget>
#include <QListView>
#include <QMouseEvent>
#include <iostream>
class ListView;
class LayerListView;
class LayerListModel;
////////////////////////////////////////////////////////////////////////////////
// TODO: Replace this & all instances of this with a real layer object
class LayerObject
{
public:
LayerObject() : visible(true), name("") { }
LayerObject(const std::string& n, bool v) : visible(v), name(n) { }
public:
bool visible;
std::string name;
public:
LayerObject& operator=(const LayerObject& other)
{
if (this != &other)
{
visible = other.visible;
name = other.name;
}
return *this;
};
};
////////////////////////////////////////////////////////////////////////////////
class LayersWidget : public QWidget
{
Q_OBJECT
public:
LayersWidget(QWidget* parent = NULL);
~LayersWidget() {}
QSize minimumSizeHint() const;
QSize sizeHint() const;
public slots:
void newLayer();
void deleteSelected();
void duplicateSelected();
//void moveSelectedUp();
//void moveSelectedDown();
private:
LayerListView* m_listView;
LayerListModel* m_listModel;
};
////////////////////////////////////////////////////////////////////////////////
class LayerListModel : public QAbstractListModel
{
public:
LayerListModel(QObject* parent = NULL) :
QAbstractListModel(parent),
listdata(),
eyePic(":/icons/layerEye.png"),
blankPic(":/icons/layerEye.png")
{
// Init data in - this is obviously silly and needs to be elsewhere
listdata.push_back(LayerObject(std::string("Layer 1"), true));
listdata.push_back(LayerObject(std::string("Layer 2"), false));
listdata.push_back(LayerObject(std::string("Layer 3"), true));
listdata.push_back(LayerObject(std::string("Layer 4"), true));
// Clear out the eye image so as to create an 'invisible' image.
blankPic.fill(QColor(255, 255, 255, 0));
}
~LayerListModel() {}
public:
// TODO: Make private
std::vector<LayerObject> listdata;
QPixmap eyePic;
QPixmap blankPic;
public:
int rowCount(const QModelIndex&) const
{
return listdata.size();
}
bool insertRows(int row, int, const QModelIndex& parent = QModelIndex())
{
beginInsertRows(parent, row, row);
listdata.insert(listdata.begin() + row, LayerObject());
endInsertRows();
return true;
}
bool removeRows(int row, int, const QModelIndex& parent = QModelIndex())
{
beginRemoveRows(parent, row, row);
listdata.erase(listdata.begin() + row);
endRemoveRows();
return true;
}
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
return QVariant(QString(listdata[index.row()].name.c_str()));
}
else if (role == Qt::DecorationRole)
{
if (listdata[index.row()].visible == true)
return QVariant(eyePic);
else
return QVariant(blankPic);
}
return QVariant();
}
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)
{
int row = index.row();
if (role == Qt::EditRole)
{
listdata[row].name = value.toString().toStdString();
emit dataChanged(index, index);
}
else if (role == Qt::DecorationRole)
{
// SLOW COMPARE!
if (value.value<QPixmap>().toImage() == blankPic.toImage())
listdata[row].visible = false;
else
listdata[row].visible = true;
}
return true;
}
Qt::DropActions supportedDropActions() const
{
return Qt::MoveAction;
}
Qt::ItemFlags flags(const QModelIndex& index) const
{
Qt::ItemFlags defaultFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (index.isValid())
{
return (Qt::ItemIsDragEnabled | Qt::ItemIsEditable | defaultFlags);
}
else
{
return (Qt::ItemIsDropEnabled | Qt::ItemIsEditable | defaultFlags);
}
}
};
////////////////////////////////////////////////////////////////////////////////
class LayerListView : public QListView
{
public:
LayerListView(QWidget* parent = NULL) : QListView(parent)
{
setDragDropMode(QListView::InternalMove);
setDragDropOverwriteMode(false);
setViewMode(QListView::ListMode);
setMovement(QListView::Snap);
setUniformItemSizes(true);
}
~LayerListView() {}
protected:
void mousePressEvent(QMouseEvent* event)
{
QModelIndex index = indexAt(event->pos());
if (event->pos().x() < 53)
{
LayerListModel* mdl = dynamic_cast<LayerListModel*>(model());
if (mdl->listdata[index.row()].visible)
mdl->listdata[index.row()].visible = false;
else
mdl->listdata[index.row()].visible = true;
emit dataChanged(index, index);
}
// Proceed with the parent's control
QListView::mousePressEvent(event);
}
};
#endif