-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWAbstractItemModel.C
More file actions
461 lines (372 loc) · 11.8 KB
/
WAbstractItemModel.C
File metadata and controls
461 lines (372 loc) · 11.8 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Wt/WAbstractItemModel.h"
#include "Wt/WEvent.h"
#include "Wt/WException.h"
#include "Wt/WItemSelectionModel.h"
#include "Wt/WLogger.h"
#include "Wt/WModelIndex.h"
#include "WebUtils.h"
#ifdef WT_WIN32
#define snprintf _snprintf
#endif
namespace {
const char *DRAG_DROP_MIME_TYPE = "application/x-wabstractitemmodelselection";
}
namespace Wt {
LOGGER("WAbstractItemModel");
WAbstractItemModel::WAbstractItemModel()
{ }
WAbstractItemModel::~WAbstractItemModel()
{ }
bool WAbstractItemModel::canFetchMore(WT_MAYBE_UNUSED const WModelIndex& parent) const
{
return false;
}
void WAbstractItemModel::fetchMore(WT_MAYBE_UNUSED const WModelIndex& parent)
{ }
WFlags<ItemFlag> WAbstractItemModel::flags(WT_MAYBE_UNUSED const WModelIndex& index) const
{
return ItemFlag::Selectable;
}
WFlags<HeaderFlag> WAbstractItemModel::headerFlags(WT_MAYBE_UNUSED int section, WT_MAYBE_UNUSED Orientation orientation)
const
{
return None;
}
bool WAbstractItemModel::hasChildren(const WModelIndex& index) const
{
return rowCount(index) > 0 && columnCount(index) > 0;
}
bool WAbstractItemModel::hasIndex(int row, int column,
const WModelIndex& parent) const
{
return (row >= 0
&& column >= 0
&& row < rowCount(parent)
&& column < columnCount(parent));
}
WAbstractItemModel::DataMap
WAbstractItemModel::itemData(const WModelIndex& index) const
{
DataMap result;
if (index.isValid()) {
#ifndef WT_TARGET_JAVA
for (int i = 0; i <= ItemDataRole::BarBrushColor; ++i)
#else
for (int i = 0; i <= ItemDataRole::BarBrushColor.value(); ++i)
#endif
result[ItemDataRole(i)] = data(index, ItemDataRole(i));
result[ItemDataRole::User] = data(index, ItemDataRole::User);
}
return result;
}
cpp17::any WAbstractItemModel::data(int row, int column, ItemDataRole role,
const WModelIndex& parent) const
{
return data(index(row, column, parent), role);
}
cpp17::any WAbstractItemModel::headerData(WT_MAYBE_UNUSED int section, WT_MAYBE_UNUSED Orientation orientation, ItemDataRole role) const
{
if (role == ItemDataRole::Level)
return cpp17::any((int)0);
else
return cpp17::any();
}
void WAbstractItemModel::sort(WT_MAYBE_UNUSED int column, WT_MAYBE_UNUSED SortOrder order)
{ }
void WAbstractItemModel::expandColumn(WT_MAYBE_UNUSED int column)
{ }
void WAbstractItemModel::collapseColumn(WT_MAYBE_UNUSED int column)
{ }
bool WAbstractItemModel::insertColumns(WT_MAYBE_UNUSED int column, WT_MAYBE_UNUSED int count, WT_MAYBE_UNUSED const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::insertRows(WT_MAYBE_UNUSED int column, WT_MAYBE_UNUSED int count, WT_MAYBE_UNUSED const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::removeColumns(WT_MAYBE_UNUSED int column, WT_MAYBE_UNUSED int count, WT_MAYBE_UNUSED const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::removeRows(WT_MAYBE_UNUSED int column, WT_MAYBE_UNUSED int count, WT_MAYBE_UNUSED const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::setData(WT_MAYBE_UNUSED const WModelIndex& index, WT_MAYBE_UNUSED const cpp17::any& value, WT_MAYBE_UNUSED ItemDataRole role)
{
return false;
}
bool WAbstractItemModel::setHeaderData(WT_MAYBE_UNUSED int section, WT_MAYBE_UNUSED Orientation orientation, WT_MAYBE_UNUSED const cpp17::any& value, WT_MAYBE_UNUSED ItemDataRole role)
{
return false;
}
bool WAbstractItemModel::setHeaderData(int section, const cpp17::any& value)
{
return setHeaderData(section, Orientation::Horizontal, value);
}
bool WAbstractItemModel::setItemData(const WModelIndex& index,
const DataMap& values)
{
bool result = true;
for (DataMap::const_iterator i = values.begin(); i != values.end(); ++i)
// if (i->first != ItemDataRole::Edit)
if (!setData(index, i->second, i->first))
result = false;
dataChanged().emit(index, index);
return result;
}
bool WAbstractItemModel::insertColumn(int column, const WModelIndex& parent)
{
return insertColumns(column, 1, parent);
}
bool WAbstractItemModel::insertRow(int row, const WModelIndex& parent)
{
return insertRows(row, 1, parent);
}
bool WAbstractItemModel::removeColumn(int column, const WModelIndex& parent)
{
return removeColumns(column, 1, parent);
}
bool WAbstractItemModel::removeRow(int row, const WModelIndex& parent)
{
return removeRows(row, 1, parent);
}
bool WAbstractItemModel::setData(int row, int column, const cpp17::any& value,
ItemDataRole role, const WModelIndex& parent)
{
WModelIndex i = index(row, column, parent);
if (i.isValid())
return setData(i, value, role);
else
return false;
}
void WAbstractItemModel::reset()
{
modelReset_.emit();
}
WModelIndex WAbstractItemModel::createIndex(int row, int column, void *ptr)
const
{
return WModelIndex(row, column, this, ptr);
}
WModelIndex WAbstractItemModel::createIndex(int row, int column, ::uint64_t id)
const
{
return WModelIndex(row, column, this, id);
}
void *WAbstractItemModel::toRawIndex(WT_MAYBE_UNUSED const WModelIndex& index) const
{
return nullptr;
}
WModelIndex WAbstractItemModel::fromRawIndex(WT_MAYBE_UNUSED void* rawIndex) const
{
return WModelIndex();
}
std::string WAbstractItemModel::mimeType() const
{
return DRAG_DROP_MIME_TYPE;
}
std::vector<std::string> WAbstractItemModel::acceptDropMimeTypes() const
{
std::vector<std::string> result;
result.push_back(DRAG_DROP_MIME_TYPE);
return result;
}
void WAbstractItemModel::copyData(const WModelIndex& sIndex,
const WModelIndex& dIndex)
{
if (dIndex.model() != this)
throw WException("WAbstractItemModel::copyData(): dIndex must be an index of this model");
DataMap values = itemData(dIndex);
for (DataMap::const_iterator i = values.begin(); i != values.end(); ++i)
setData(dIndex, cpp17::any(), i->first);
auto source = sIndex.model();
setItemData(dIndex, source->itemData(sIndex));
}
void WAbstractItemModel::dropEvent(const WDropEvent& e, DropAction action,
int row, WT_MAYBE_UNUSED int column,
const WModelIndex& parent)
{
// TODO: For now, we assumes selectionBehavior() == RowSelection !
WItemSelectionModel *selectionModel
= dynamic_cast<WItemSelectionModel *>(e.source());
if (selectionModel) {
auto sourceModel = selectionModel->model();
/*
* (1) Insert new rows (or later: cells ?)
*/
if (action == DropAction::Move || row == -1) {
if (row == -1)
row = rowCount(parent);
if (!insertRows(row, selectionModel->selectedIndexes().size(), parent)) {
LOG_ERROR("dropEvent(): could not insertRows()");
return;
}
}
/*
* (2) Copy data
*/
WModelIndexSet selection = selectionModel->selectedIndexes();
int r = row;
for (WModelIndexSet::const_iterator i = selection.begin();
i != selection.end(); ++i) {
WModelIndex sourceIndex = *i;
if (selectionModel->selectionBehavior() ==
SelectionBehavior::Rows) {
WModelIndex sourceParent = sourceIndex.parent();
for (int col = 0; col < sourceModel->columnCount(sourceParent); ++col) {
WModelIndex s = sourceModel->index(sourceIndex.row(), col,
sourceParent);
WModelIndex d = index(r, col, parent);
copyData(s, d);
}
++r;
}
}
/*
* (3) Remove original data
*/
if (action == DropAction::Move) {
while (!selectionModel->selectedIndexes().empty()) {
WModelIndex i = Utils::last(selectionModel->selectedIndexes());
if (!sourceModel->removeRow(i.row(), i.parent())) {
LOG_ERROR("dropEvent(): could not removeRows()");
return;
}
}
}
}
}
void WAbstractItemModel::dropEvent(const WDropEvent& e, DropAction action,
const WModelIndex& pindex, Wt::Side side)
{
WItemSelectionModel *selectionModel
= dynamic_cast<WItemSelectionModel *>(e.source());
if (selectionModel) {
auto sourceModel = selectionModel->model();
const WModelIndex& parent = pindex.parent();
int row = !pindex.isValid() ? rowCount() :
side == Side::Bottom ? pindex.row()+1 : pindex.row();
/*
* (1) Insert new rows (or later: cells ?)
*/
if (!insertRows(row, selectionModel->selectedIndexes().size(), parent)) {
LOG_ERROR("dropEvent(): could not insertRows()");
return;
}
/*
* (2) Copy data
*/
WModelIndexSet selection = selectionModel->selectedIndexes();
int r = row;
for (WModelIndexSet::const_iterator i = selection.begin();
i != selection.end(); ++i) {
WModelIndex sourceIndex = *i;
if (selectionModel->selectionBehavior() ==
SelectionBehavior::Rows) {
WModelIndex sourceParent = sourceIndex.parent();
for (int col = 0; col < sourceModel->columnCount(sourceParent); ++col) {
WModelIndex s = sourceModel->index(sourceIndex.row(), col,
sourceParent);
WModelIndex d = index(r, col, parent);
copyData(s, d);
}
++r;
}
}
/*
* (3) Remove original data
*/
if (action == DropAction::Move) {
while (!selectionModel->selectedIndexes().empty()) {
WModelIndex i = Utils::last(selectionModel->selectedIndexes());
if (!sourceModel->removeRow(i.row(), i.parent())) {
LOG_ERROR("dropEvent(): could not removeRows()");
return;
}
}
}
}
}
void WAbstractItemModel::beginInsertColumns(const WModelIndex& parent,
int first, int last)
{
first_ = first;
last_ = last;
parent_ = parent;
columnsAboutToBeInserted().emit(parent_, first, last);
}
void WAbstractItemModel::endInsertColumns()
{
columnsInserted().emit(parent_, first_, last_);
}
void WAbstractItemModel::beginInsertRows(const WModelIndex& parent,
int first, int last)
{
first_ = first;
last_ = last;
parent_ = parent;
rowsAboutToBeInserted().emit(parent, first, last);
}
void WAbstractItemModel::endInsertRows()
{
rowsInserted().emit(parent_, first_, last_);
}
void WAbstractItemModel::beginRemoveColumns(const WModelIndex& parent,
int first, int last)
{
first_ = first;
last_ = last;
parent_ = parent;
columnsAboutToBeRemoved().emit(parent, first, last);
}
void WAbstractItemModel::endRemoveColumns()
{
columnsRemoved().emit(parent_, first_, last_);
}
void WAbstractItemModel::beginRemoveRows(const WModelIndex& parent,
int first, int last)
{
first_ = first;
last_ = last;
parent_ = parent;
rowsAboutToBeRemoved().emit(parent, first, last);
}
void WAbstractItemModel::endRemoveRows()
{
rowsRemoved().emit(parent_, first_, last_);
}
WModelIndexList WAbstractItemModel::match(const WModelIndex& start,
ItemDataRole role,
const cpp17::any& value,
int hits,
WFlags<MatchFlag> flags)
const
{
WModelIndexList result;
const int rc = rowCount(start.parent());
for (int i = 0; i < rc; ++i) {
int row = start.row() + i;
if (row >= rc) {
if (!(flags & MatchFlag::Wrap))
break;
else
row -= rc;
}
WModelIndex idx = index(row, start.column(), start.parent());
cpp17::any v = data(idx, role);
if (Impl::matchValue(v, value, flags)) {
result.push_back(idx);
if (hits != -1 && (int)result.size() == hits)
break;
}
}
return result;
}
}