-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWAbstractItemModel.C
More file actions
412 lines (332 loc) · 8.93 KB
/
WAbstractItemModel.C
File metadata and controls
412 lines (332 loc) · 8.93 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
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Wt/WEvent"
#include "Wt/WModelIndex"
#include "Wt/WAbstractItemModel"
#include "Wt/WItemSelectionModel"
#include "WebUtils.h"
#ifdef WIN32
#define snprintf _snprintf
#endif
namespace {
const char *DRAG_DROP_MIME_TYPE = "application/x-wabstractitemmodelselection";
}
namespace Wt {
WAbstractItemModel::WAbstractItemModel(WObject *parent)
: WObject(parent),
columnsAboutToBeInserted_(this),
columnsAboutToBeRemoved_(this),
columnsInserted_(this),
columnsRemoved_(this),
rowsAboutToBeInserted_(this),
rowsAboutToBeRemoved_(this),
rowsInserted_(this),
rowsRemoved_(this),
dataChanged_(this),
headerDataChanged_(this),
layoutAboutToBeChanged_(this),
layoutChanged_(this),
modelReset_(this)
{ }
WAbstractItemModel::~WAbstractItemModel()
{ }
bool WAbstractItemModel::canFetchMore(const WModelIndex& parent) const
{
return false;
}
void WAbstractItemModel::fetchMore(const WModelIndex& parent)
{ }
WFlags<ItemFlag> WAbstractItemModel::flags(const WModelIndex& index) const
{
return ItemIsSelectable;
}
WFlags<HeaderFlag> WAbstractItemModel::headerFlags(int section,
Orientation orientation)
const
{
return 0;
}
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()) {
for (int i = 0; i <= BarBrushColorRole; ++i)
result[i] = data(index, i);
result[UserRole] = data(index, UserRole);
}
return result;
}
boost::any WAbstractItemModel::data(int row, int column, int role,
const WModelIndex& parent) const
{
return data(index(row, column, parent), role);
}
boost::any WAbstractItemModel::headerData(int section,
Orientation orientation,
int role) const
{
if (role == LevelRole)
return 0;
else
return boost::any();
}
void WAbstractItemModel::sort(int column, SortOrder order)
{ }
void WAbstractItemModel::expandColumn(int column)
{ }
void WAbstractItemModel::collapseColumn(int column)
{ }
bool WAbstractItemModel::insertColumns(int column, int count,
const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::insertRows(int row, int count,
const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::removeColumns(int column, int count,
const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::removeRows(int row, int count,
const WModelIndex& parent)
{
return false;
}
bool WAbstractItemModel::setData(const WModelIndex& index,
const boost::any& value, int role)
{
return false;
}
bool WAbstractItemModel::setHeaderData(int section, Orientation orientation,
const boost::any& value, int role)
{
return false;
}
bool WAbstractItemModel::setHeaderData(int section, const boost::any& value)
{
return setHeaderData(section, Horizontal, value);
}
bool WAbstractItemModel::setItemData(const WModelIndex& index,
const DataMap& values)
{
bool result = true;
bool wasBlocked = dataChanged().isBlocked();
dataChanged().setBlocked(true);
for (DataMap::const_iterator i = values.begin(); i != values.end(); ++i)
// if (i->first != EditRole)
if (!setData(index, i->second, i->first))
result = false;
dataChanged().setBlocked(wasBlocked);
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 boost::any& value,
int 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);
}
#ifndef WT_TARGET_JAVA
WModelIndex WAbstractItemModel::createIndex(int row, int column, ::uint64_t id)
const
{
return WModelIndex(row, column, this, id);
}
#endif // WT_TARGET_JAVA
void *WAbstractItemModel::toRawIndex(const WModelIndex& index) const
{
return 0;
}
WModelIndex WAbstractItemModel::fromRawIndex(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 WAbstractItemModel *source,
const WModelIndex& sIndex,
WAbstractItemModel *destination,
const WModelIndex& dIndex)
{
DataMap values = destination->itemData(dIndex);
for (DataMap::const_iterator i = values.begin(); i != values.end(); ++i)
destination->setData(dIndex, boost::any(), i->first);
destination->setItemData(dIndex, source->itemData(sIndex));
}
void WAbstractItemModel::dropEvent(const WDropEvent& e, DropAction action,
int row, int column,
const WModelIndex& parent)
{
// TODO: For now, we assumes selectionBehavior() == RowSelection !
WItemSelectionModel *selectionModel
= dynamic_cast<WItemSelectionModel *>(e.source());
if (selectionModel) {
WAbstractItemModel *sourceModel = selectionModel->model();
/*
* (1) Insert new rows (or later: cells ?)
*/
if (action == MoveAction || row == -1) {
if (row == -1)
row = rowCount(parent);
insertRows(row, selectionModel->selectedIndexes().size(), parent);
}
/*
* (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() == SelectRows) {
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(sourceModel, s, this, d);
}
++r;
} else {
}
}
/*
* (3) Remove original data
*/
if (action == MoveAction) {
while (!selectionModel->selectedIndexes().empty()) {
WModelIndex i = Utils::last(selectionModel->selectedIndexes());
sourceModel->removeRow(i.row(), i.parent());
}
}
}
}
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,
int role,
const boost::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 & MatchWrap))
break;
else
row -= rc;
}
WModelIndex idx = index(row, start.column(), start.parent());
boost::any v = data(idx, role);
if (Impl::matchValue(v, value, flags))
result.push_back(idx);
}
return result;
}
}