-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitem.cpp
More file actions
347 lines (282 loc) · 6.49 KB
/
item.cpp
File metadata and controls
347 lines (282 loc) · 6.49 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "item.h"
#include "normalstate.h"
#include "superstate.h"
#include <QLabel>
#include <QStyleOption>
#include <QMouseEvent>
Item::Item(StateType type, QWidget *parent)
: ControlContainer<Item>(parent), _stateType(type)
{
init();
}
Item::~Item()
{
if (_state) delete _state;
if (_labTitle) _labTitle->deleteLater();
if (_wgtLeft) _wgtLeft->deleteLater();
if (_wgtRight) _wgtRight->deleteLater();
}
bool Item::add(Item *item)
{
if (_stateType != Super) setStateType(Super);
return ControlContainer::add(item);
}
bool Item::insert(int idx, Item *item)
{
if (_stateType != Super) setStateType(Super);
return ControlContainer::insert(idx, item);
}
bool Item::remove(int idx)
{
bool res = ControlContainer::remove(idx);
if (isEmpty() && _stateType != Normal) setStateType(Normal);
return res;
}
bool Item::remove(Item *item)
{
bool res = ControlContainer::remove(item);
if (isEmpty() && _stateType != Normal) setStateType(Normal);
return res;
}
void Item::clear()
{
if (_stateType != Normal) setStateType(Normal);
}
void Item::setStateType(StateType type)
{
if (type == _stateType && _state) return;
_stateType = type;
// Destroy old state
if (_state)
{
delete _state;
_state = nullptr;
}
// Set new state
if (_stateType == Normal) _state = new NormalState(this);
else if (_stateType == Super) _state = new SuperState(this);
update();
}
void Item::setFold(bool flag)
{
if (flag == _fold) return;
_fold = flag;
showSubItems(!flag);
refresh();
}
void Item::setSelected(bool flag)
{
if (flag == _selected) return;
// Cancel last select first, then select this item
_selected = flag;
// Update style
style()->unpolish(_labTitle);
style()->polish(_labTitle);
}
void Item::setItemHeight(int h)
{
if (h == _itemHeight) return;
_itemHeight = h;
_labTitle->setFixedHeight(h);
refresh();
}
void Item::setMargin(int m)
{
if (m == _margin) return;
_margin = m;
refresh();
}
void Item::setTitle(const QString &title)
{
_labTitle->setText(title);
}
Item::StateType Item::getStateType() const
{
return _stateType;
}
bool Item::isFold() const
{
return _fold;
}
bool Item::isSelected() const
{
return _selected;
}
int Item::getItemHeight() const
{
return _itemHeight;
}
int Item::getMargin() const
{
return _margin;
}
QString Item::getTitle() const
{
return _labTitle->text();
}
int Item::totalHeight() const
{
int h = _itemHeight;
if (_wgtRight->isVisible())
{
for (auto item : _listItems) h += item->totalHeight();
}
return (isVisible() ? h : 0);
}
Item *Item::parentItem() const
{
Item *parItem = nullptr;
if (!parent() || !parent()->parent()) return parItem;
parItem = qobject_cast<Item *>(parent()->parent());
return parItem;
}
Item *Item::root() const
{
Item *root = const_cast<Item *>(this);
while (root->parentItem()) root = root->parentItem();
return root;
}
Item *Item::getSelectedItem() const
{
if (isSelected()) return const_cast<Item *>(this);
for (auto item : _listItems)
{
auto res = item->getSelectedItem();
if (res) return res;
}
return nullptr;
}
void Item::destroyMe()
{
auto par = parentItem();
if (!par) return;
par->remove(this);
delete this;
}
void Item::addItem()
{
auto item = new Item();
item->setTitle("New item");
add(item);
}
void Item::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event)
const int wLeft = 30;
_labTitle->setFixedHeight(_itemHeight);
_wgtLeft->setFixedWidth(wLeft);
_wgtRight->setFixedWidth(width()-wLeft);
_labTitle->move(_itemHeight, 0);
_wgtLeft->move(0, _itemHeight);
_wgtRight->move(wLeft, _itemHeight);
}
void Item::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
// Enable style sheet
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
// Draw user interface
if (_state) _state->draw(p);
}
void Item::mousePressEvent(QMouseEvent *event)
{
QPoint pos = event->pos();
QRect rect(0, 0, _itemHeight, _itemHeight);
if (_stateType == Super && rect.contains(pos))
{
setFold(!_fold);
}
rect = _labTitle->geometry();
if (rect.contains(pos))
{
// Here must use `root()` to start the traverse
cancelLastSelect();
setSelected(true);
}
}
void Item::refresh()
{
// Clear old layout
for (auto kid : _wgtRight->children())
{
auto item = qobject_cast<Item *>(kid);
if (!item) continue;
if (_listItems.contains(item))
{
item->setParent(nullptr);
item->hide();
}
}
// Add elements to new layout
int h = 0;
for (auto item : _listItems)
{
// item->move(0, h);
item->setParent(_wgtRight);
item->show();
item->setGeometry(0, h, _wgtRight->width(), item->totalHeight());
h += item->totalHeight() + _margin;
}
// Resize this item widget
_wgtLeft->setFixedHeight(h);
_wgtRight->setFixedHeight(h);
if (!_fold) setFixedHeight(h+_itemHeight+_margin*2);
else setFixedHeight(_itemHeight+_margin);
// If has parent item, then refresh it too
if (parentItem()) parentItem()->refresh();
}
void Item::init()
{
initMember();
initUI();
initSignalSlot();
}
void Item::initMember()
{
// Assign member variables with different value, so that we can let the `set` functions make effects
_state = nullptr;
_fold = false;
_selected = true;
_itemHeight = -1;
_margin = -1;
_labTitle = new QLabel(this);
_wgtLeft = new QWidget(this);
_wgtRight = new QWidget(this);
// Set object name
_labTitle->setObjectName("_labTitle");
}
void Item::initUI()
{
// Set style sheet
QFile file(":/qss/item");
file.open(QIODevice::ReadOnly);
QString style = file.readAll();
setStyleSheet(style);
file.close();
// Set other attributes
setMinimumWidth(400); // To modify
setSelected(false);
setItemHeight(30);
setMargin(0);
setStateType(_stateType);
setFold(true);
// Set member attributes
_labTitle->setScaledContents(true);
}
void Item::initSignalSlot()
{
// To do
}
void Item::showSubItems(bool flag)
{
_wgtLeft->setVisible(flag);
_wgtRight->setVisible(flag);
}
void Item::cancelLastSelect()
{
auto item = root()->getSelectedItem();
if (item) item->setSelected(false);
}