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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QHash>
#include <algorithm>
#include "mappingtreemodel.h"
#include "smtreeitem.h"
MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mForbidden("/"), mType(-1) {
//init database
mDb = QSqlDatabase::database("treedb");
mTypesQ = new QSqlQuery(mDb);
mTypesQ->prepare("SELECT imappings_type_id, tmappings_type_name FROM mappings_type");
getMappingTypes();
//prepare Queries
mTypeParentsQ = new QSqlQuery(mDb);
mTypeParentsQ->prepare("SELECT imapping_id, tmapping_name, tscreated FROM mappings WHERE imapping_id NOT IN (SELECT imapping_id FROM mappings_parents) AND imapping_type = :type ORDER BY tmapping_name");
mUpdateTypeQ = new QSqlQuery(mDb);
mUpdateTypeQ->prepare("UPDATE mappings_type SET tmappings_type_name = :value WHERE imappings_type_id = :id");
mUpdateChildQ = new QSqlQuery(mDb);
mUpdateChildQ->prepare("UPDATE mappings SET tmapping_name = :value WHERE imapping_id = :id");
mAddMappingTypeQ = new QSqlQuery(mDb);
mAddMappingTypeQ->prepare("INSERT INTO mappings_type (tmappings_type_name) VALUES(:value)");
mDeleteMappingTypeQ = new QSqlQuery(mDb);
mDeleteMappingTypeQ->prepare("DELETE FROM mappings_type WHERE imappings_type_id = :id");
mAddChildQ = new QSqlQuery(mDb);
mAddChildQ->prepare("INSERT INTO mappings (tmapping_name, imapping_type) VALUES(:name, :type)");
mSelectChildQ = new QSqlQuery(mDb);
mSelectChildQ->prepare("SELECT imapping_id, tmapping_name, tscreated FROM mappings WHERE tmapping_name = :name AND imapping_type = :type");
mAddParentQ = new QSqlQuery(mDb);
mAddParentQ->prepare("INSERT INTO mappings_parents (imapping_id, iparent_id) VALUES(:id, :parentid)");
mDeleteChildQ = new QSqlQuery(mDb);
mDeleteChildQ->prepare("DELETE FROM mappings WHERE imapping_id = :id");
mUpdateParentQ = new QSqlQuery(mDb);
mUpdateParentQ->prepare("UPDATE mappings_parents SET iparent_id = :pid WHERE imapping_id = :id");
mDeleteMappingParentQ = new QSqlQuery(mDb);
mDeleteMappingParentQ->prepare("DELETE FROM mappings_parents WHERE imapping_id = :id");
mMappingsForFileIdQ = new QSqlQuery(mDb);
mMappingsForFileIdQ->prepare("SELECT DISTINCT(imapping_id) FROM pics_mappings WHERE ipics_id = :pid");
}
MappingTreeModel::~MappingTreeModel(){
delete mTypesQ;
delete mTypeParentsQ;
delete mUpdateTypeQ;
delete mUpdateChildQ;
delete mAddMappingTypeQ;
delete mDeleteMappingTypeQ;
delete mAddChildQ;
delete mSelectChildQ;
delete mAddParentQ;
delete mDeleteChildQ;
delete mUpdateParentQ;
delete mMappingsForFileIdQ;
mDb = QSqlDatabase();
}
QStringList MappingTreeModel::mappingTypeNames() const {
QStringList retval;
foreach(mappingType t, mMappingTypes){
retval << t.name.toString();
}
qSort(retval);
return retval;
}
int MappingTreeModel::mappingTypeIdFromName(const QVariant &name) const{
foreach(const mappingType t, mMappingTypes){
if(t.name == name){
return t.id.toInt();
}
}
return -1;
}
QString MappingTreeModel::mappingTypeNameFromId(int id) const{
foreach(const mappingType t, mMappingTypes){
if(t.id == id){
return t.name.toString();
}
}
return QString();
}
//caller has ownership of this item!
SmTreeItem *MappingTreeModel::treeFromPaths(const QStringList &paths){
if(paths.isEmpty()){
return 0;
}
QHash<QString, SmTreeItem*> partsHash;
SmTreeItem *root = new SmTreeItem(1);
partsHash.insert(QString(), root);
SmTreeItem *pItem = root;
// create tree
for(int i = 0; i < paths.count(); ++i){
//split the paths
QStringList parts = paths.at(i).split(forbidden());
//process path items
for(int j = 0; j < parts.count(); ++j){
if(partsHash.contains(parts.at(j))){
pItem = partsHash.value(parts.at(j)); //we've already seen this item, set it as new parent
}else{
//create a new item, save old root
SmTreeItem *oldRoot = pItem;
pItem = new SmTreeItem(QList<QVariant>() << parts.at(j), oldRoot);
oldRoot->appendChild(pItem);
partsHash.insert(parts.at(j), pItem);
}
}
}
return root;
}
QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{
if(!index.isValid()){
return QVariant();
}
SmTreeItem *item = itemAt(index);
if(role == NameRole){
return item->data(Name);
}
if(role == IdRole){
return item->data(Id);
}
if(role == AddedRole){
return item->data(Added);
}
if(role == Qt::ForegroundRole){
if(mSelectedMappings.contains(item)){
return QColor(Qt::blue);
}
}
return SmTreeModel::data(index, role);
}
QList<QVariant> MappingTreeModel::childList(const QVariant &value, int column) const{
QModelIndex itemIdx = findRecursive(value, column, createIndex(0, 0, root()));
SmTreeItem *item = static_cast<SmTreeItem*>(itemIdx.internalPointer());
if(item->childCount()){
return getChildListRecursive(item, column);
}
return QList<QVariant>() << value;
}
QList<QVariant> MappingTreeModel::mappingsForFile(const QVariant &fileId) const{
QList<QVariant> retval;
mMappingsForFileIdQ->addBindValue(fileId);
if(mMappingsForFileIdQ->exec()){
while(mMappingsForFileIdQ->next()){
retval << mMappingsForFileIdQ->value(0);
}
}
return retval;
}
QModelIndex MappingTreeModel::indexFromPath(const QString &path, int column) const{
QStringList items = path.split("/");
if(items.isEmpty() || column >= NumFields){
return QModelIndex();
}
QModelIndex retval;
for(int i = 0; i < items.count(); ++i){
retval = find(items.at(i), column, retval);
}
return retval;
}
bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, int role){
if(!index.isValid()){
return false;
}
SmTreeItem *item = itemAt(index);
if(role == Qt::EditRole){
if(index.column() == Name){
if(value.toString().contains(mForbidden)){
return false;
}
}
mDb.transaction();
QSqlQuery *q = 0;
if(item == root()){
q = mUpdateTypeQ;
}else{
q = mUpdateChildQ;
}
q->bindValue(":value", value);
q->bindValue(":id", item->data(Id));
if(q->exec()){
item->setData(Name, value);
emit dataChanged(index, index);
mDb.commit();
return true;
}
mDb.rollback();
}
if(role == NameRole){
if(value.toString().contains(mForbidden)){
return false;
}
item->setData(Name, value);
}
if(role == IdRole){
item->setData(Id, value);
}
if(role == AddedRole){
item->setData(Added, value);
}
return true;
}
void MappingTreeModel::move(const QModelIndex &source, const QModelIndex &dest){
SmTreeItem *sItem = itemAt(source);
SmTreeItem *dItem = itemAt(dest);
int sourceId = sItem->data(Id).toInt();
if(dItem->parent() == root()){
mDeleteMappingParentQ->bindValue(":id", sourceId);
mDeleteMappingParentQ->exec();
reparent(source, dest, true);
return;
}
int newParentId = dItem->data(Id).toInt();
mAddParentQ->bindValue(":id", sourceId);
mAddParentQ->bindValue(":parentid", newParentId);
if(!mAddParentQ->exec()){
mUpdateParentQ->bindValue(":pid", newParentId);
mUpdateParentQ->bindValue(":id", sourceId);
mUpdateParentQ->exec();
}
reparent(source, dest, true);
}
bool MappingTreeModel::addMappingType(const QString &type){
mAddMappingTypeQ->bindValue(":value", type);
mDb.transaction();
if(mAddMappingTypeQ->exec()){
mDb.commit();
getMappingTypes();
return true;
}
mDb.rollback();
return false;
}
bool MappingTreeModel::deleteMappingType(int typeId){
mDeleteMappingTypeQ->bindValue(":id", typeId);
mDb.transaction();
if(mDeleteMappingTypeQ->exec()){
mDb.commit();
getMappingTypes();
return true;
}
mDb.rollback();
return false;
}
bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent){
if(!parent.isValid()){
return false;
}
if(name.toString().contains(mForbidden)){
return false;
}
SmTreeItem *pItem = itemAt(parent);
mAddChildQ->bindValue(":name", name);
mAddChildQ->bindValue(":type", mType);
if(mAddChildQ->exec()){
mSelectChildQ->bindValue(":name", name);
mSelectChildQ->bindValue(":type", mType);
mSelectChildQ->exec();
while(mSelectChildQ->next()){
int where = lowerBound(pItem, mSelectChildQ->value(1), Name);
if(insertRows(where, 1, parent)){
QModelIndex newIdx = index(where, 0, parent);
setData(newIdx, mSelectChildQ->value(1), NameRole);
setData(newIdx, mSelectChildQ->value(0), IdRole);
setData(newIdx, mSelectChildQ->value(2), AddedRole);
}
if(pItem->parent() != root()){
mAddParentQ->bindValue(":id", mSelectChildQ->value(0)); //Id
mAddParentQ->bindValue(":parentid", pItem->data(Id));
mAddParentQ->exec();
}
mValidMappings.clear();
mValidMappings = mappingData(root());
return true;
}
}
return false;
}
bool MappingTreeModel::deleteChild(const QModelIndex &idx){
if(!idx.isValid()){
return false;
}
SmTreeItem *item = itemAt(idx);
if(item->childCount() > 0){
return false;
}
mDeleteChildQ->bindValue(":id", item->data(Id));
if(mDeleteChildQ->exec()){
removeRows(idx.row(), 1, idx.parent());
mValidMappings.clear();
mValidMappings = mappingData(root());
return true;
}
return false;
}
int MappingTreeModel::childCount(const QModelIndex &idx) const{
if(!idx.isValid()){
return 0;
}
SmTreeItem *item = itemAt(idx);
return item->childCount();
}
MappingData MappingTreeModel::mappingDataFromId(int mappingId) const{
MappingData retval = { -1, QString(), QStringList() };
foreach(MappingData d, mValidMappings){
if(d.id == mappingId){
retval = d;
break;
}
}
return retval;
}
QStringList MappingTreeModel::paths() const{
return getPathsRecursive(root());
}
void MappingTreeModel::populate(){
if(mType == -1){
return;
}
// get nodes with root as parent
mTypeParentsQ->bindValue(":type", mType);
if(mTypeParentsQ->exec()){
SmTreeItem *rootItem = new SmTreeItem(NumFields);
SmTreeItem *firstChild = new SmTreeItem(NumFields, rootItem);
firstChild->setData(Name, mappingTypeNameFromId(mType));
//no real id needed... conflicts with mapping ids!
firstChild->setData(Id, 0);
rootItem->appendChild(firstChild);
//collect children recursive
while(mTypeParentsQ->next()){
QList<QVariant> childData;
childData << mTypeParentsQ->value(1) << mTypeParentsQ->value(0) << mTypeParentsQ->value(2);
SmTreeItem *childItem = new SmTreeItem(childData, firstChild);
firstChild->appendChild(childItem);
getChildrenRecursive(childItem);
}
setRoot(rootItem);
mValidMappings.clear();
mValidMappings = mappingData(root());
emit needExpansion();
}
}
void MappingTreeModel::getMappingTypes(){
bool qRes = mTypesQ->exec();
if(qRes){
mMappingTypes.clear();
while(mTypesQ->next()){
mappingType t;
t.id = mTypesQ->value(0);
t.name = mTypesQ->value(1);
mMappingTypes << t;
}
emit mappingTypesChanged();
}
}
void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){
QSqlQuery cq(mDb);
cq.prepare("SELECT mappings.imapping_id, mappings.tmapping_name, mappings.tscreated FROM mappings, mappings_parents WHERE mappings_parents.iparent_id = :id AND mappings.imapping_id = mappings_parents.imapping_id ORDER BY mappings.tmapping_name");
cq.bindValue(":id", item->data(Id));
if(cq.exec()){
while(cq.next()){
QList<QVariant> childData;
childData << cq.value(1) << cq.value(0) << cq.value(2);
SmTreeItem *child = new SmTreeItem(childData, item);
item->appendChild(child);
getChildrenRecursive(child);
}
}
}
QStringList MappingTreeModel::getPathsRecursive(SmTreeItem *parent) const{
QStringList retval;
if(!basePath(parent).isEmpty()){
retval << basePath(parent);
}
for(int i = 0; i < parent->childCount(); ++i){
if(parent->child(i)->childCount()){
retval << getPathsRecursive(parent->child(i));
}else{
retval << QString("%1/%2").arg(basePath(parent)).arg(parent->child(i)->data(Name).toString());
}
}
return retval;
}
QList<QVariant> MappingTreeModel::getChildListRecursive(SmTreeItem *item, int column) const{
QList<QVariant> retval;
if(!item){
return retval;
}
for(int i = 0; i < item->childCount(); ++i){
if(item->child(i)->childCount()){
retval << getChildListRecursive(item->child(i), column);
}else{
retval << item->child(i)->data(column);
}
}
return retval;
}
QString MappingTreeModel::basePath(SmTreeItem *item) const{
QStringList pItems;
SmTreeItem *cur = item;
while(cur != root()){
pItems << cur->data(Name).toString();
cur = cur->parent();
}
if(pItems.isEmpty()){
return QString();
}
std::reverse(pItems.begin(), pItems.end());
return pItems.join("/");
}
int MappingTreeModel::lowerBound(SmTreeItem *item, const QVariant &value, int column) const {
for(int i = 0; i < item->childCount(); ++i){
SmTreeItem *child = item->child(i);
if(child->data(column).toString() > value.toString()){
return i;
}
}
return item->childCount();
}
QList<MappingData> MappingTreeModel::mappingData(SmTreeItem *item){
QList<MappingData> retval;
if(item->childCount() > 0){
for(int i = 0; i < item->childCount(); ++i){
retval << mappingData(item->child(i));
}
}
MappingData mapItem = { item->data(Id).toInt(), item->data(Name).toString(), QStringList() };
QStringList path;
SmTreeItem *p = item;
while(p->parent()){
path << p->data(Name).toString();
p = p->parent();
}
std::reverse(path.begin(), path.end());
mapItem.path = path;
retval << mapItem;
return retval;
}
MappingTreeResultModel::MappingTreeResultModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent) { }
Qt::ItemFlags MappingTreeResultModel::flags(const QModelIndex &index) const {
Q_UNUSED(index);
return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}
bool MappingTreeResultModel::setData(const QModelIndex &index, const QVariant &value, int role){
SmTreeItem *item = itemAt(index);
if(role == NameRole){
item->setData(Name, value);
return true;
}
if(role == IdRole){
item->setData(Id, value);
return true;
}
return SmTreeModel::setData(index, value, role);
}
void MappingTreeResultModel::addItem(const MappingData &data){
SmTreeItem *curItem = root();
for(int i = 0; i < data.path.count(); ++i){
int childPos = hasChild(curItem, data.path.at(i));
if(childPos != -1){
//child already exists
curItem = curItem->child(childPos);
continue;
}else{
//insert child
int id = -1;
if(i == data.path.count() - 1){
id = data.id;
}
QModelIndex curIdx = insertChild(data.path.at(i), id, curItem);
curItem = itemAt(curIdx);
}
}
}
QModelIndex MappingTreeResultModel::insertChild(const QVariant &data, int id, SmTreeItem *parent){
QModelIndex parentIdx;
int row = parent->childCount();
if(parent != root()){
for(int i = 0; i < parent->childCount(); ++i){
if(parent->child(i)->data(0).toString() > data.toString()){
row = i;
}
}
parentIdx = createIndex(0, 0, parent);
}
insertRows(row, 1, parentIdx);
QModelIndex newIdx = index(row, 0, parentIdx);
setData(newIdx, data, NameRole);
setData(newIdx, id, IdRole);
return newIdx;
}
QList<int> MappingTreeResultModel::mappingsIds() const {
return mappingIdsRecursive(root());
}
void MappingTreeResultModel::clearData(){
setRoot(new SmTreeItem(NumFields));
}
int MappingTreeResultModel::hasChild(SmTreeItem *item, const QVariant &name, int column) const{
for(int i = 0; i < item->childCount(); ++i){
if(item->child(i)->data(column) == name){
return i;
}
}
return -1;
}
QList<int> MappingTreeResultModel::mappingIdsRecursive(SmTreeItem *parent) const {
QList<int> retval;
if(!parent->childCount()){
return retval;
}
for(int i = 0; i < parent->childCount(); ++i){
SmTreeItem *child = parent->child(i);
int id = child->data(Id).toInt();
if(id != -1){
retval << id;
}
retval << mappingIdsRecursive(child);
}
return retval;
}
|