Menu

[r16]: / TreeModel.cpp  Maximize  Restore  History

Download this file

484 lines (415 with data), 15.2 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include <QtGui>
#include <QByteArray>
#include <QLinearGradient>
#include "TreeItem.h"
#include "TreeModel.h"
#include "TreeView.h"
TreeModel::TreeModel(const QList<Group> &data, const QList<Leaf> &values, QObject *parent, TreeView* view)
: QAbstractItemModel(parent)
{
//QList<QVariant> rootData;
//QString rootData = "Name";// << "Comments";
rootItem = NULL;
view_ = view;
groups_ = data;
leafs_ = values;
setupModelData();// .split(QString("\n")), rootItem);
}
TreeModel::~TreeModel()
{
delete rootItem;
}
void TreeModel::addNewGroup(QString value)
{
groups_.push_back(Group(value,groups_.size(),-1));
setupModelData();
reset();
}
void TreeModel::removeGroup(int groupId)
{
QList<Group> result;
for(QList<Group>::iterator it = groups_.begin(), itend = groups_.end();
it != itend; ++it)
if( it->groupId() != groupId)
result.push_back(*it);
groups_ = result;
setupModelData();
//emit dataChanged();
reset();
}
int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
else
return rootItem->columnCount();
}
bool TreeModel::setData ( const QModelIndex & index, const QVariant & value, int role )
{
if( role == Qt::EditRole)
{
TreeItem* item = getItem(index);// static_cast<TreeItem*>(index.internalPointer());
if( !item->isLeaf() )
{
GroupTreeItem* gti = (GroupTreeItem*)item;
int pos = findGroupPositionById( gti->getGroupInfo()->groupId() );
if( pos >=0 )
{
groups_[pos].setName(value.toString());
item->setData(value.toString());
}
}
return true;
}
else
{
return false;
}
}
int TreeModel::findLeafPositionByName(QString name)
{
int pos = 0;
for(QList<Leaf>::iterator it = leafs_.begin(), itend = leafs_.end();
it != itend; ++it)
{
Leaf s = (*it);
if( s.name() == name )
return pos;
pos++;
}
return -1;
}
int TreeModel::findGroupPositionById(int groupId)
{
int pos = 0;
for(QList<Group>::iterator it = groups_.begin(), itend = groups_.end();
it != itend; ++it)
{
Group g = (*it);
if( g.groupId() == groupId )
return pos;//&(*it);
pos++;
}
return -1;
}
TreeItem* TreeModel::getItem(const QModelIndex &index)const
{
TreeItem* ti = static_cast<TreeItem*>(index.internalPointer());
return ti;
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
switch( role )
{
case Qt::DecorationRole:
{
TreeItem *item = getItem(index);
if( item->isLeaf() )
return QIcon(":/Resources/putty-icon.png");
else
return QIcon(":/Resources/folder.png");
break;
}
case Qt::EditRole:
{
TreeItem *item = getItem(index);//static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
break;
}
break;
case Qt::DisplayRole:
{
TreeItem *item = getItem(index);//static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
break;
}
// case Qt::BackgroundRole:
// {
// /*QLinearGradient gradient(0, 0, 50, 5);
// gradient.setColorAt(0, QColor::fromRgbF(0, 1, 0, 1));
// gradient.setColorAt(1, QColor::fromRgbF(0, 0, 0, 0));
// */
// /* int lastVal=0;
// QBrush brush(QColor(0, 0, 0, 64));
// //QLinearGradient gradient(rect().topLeft(), rect().bottomRight());
// QLinearGradient gradient(0,0, 100,5);
// const QColor col = brush.color();
// gradient.setColorAt(0, col);
// gradient.setColorAt(1, col.dark(int(200 + lastVal * 50)));*/
// /* QLinearGradient linearGradient(0, 0, 100, 50);
// linearGradient.setColorAt(0.0, Qt::white);
// linearGradient.setColorAt(0.2, Qt::green);
// linearGradient.setColorAt(1.0, Qt::black);
//*/
// QGradientStops stops;
// //QLinearGradient gradient(frame.topLeft(), frame.topLeft() + QPointF(200,200));
// QLinearGradient gradient(0,0,100,50);
// stops << QGradientStop(0.0, QColor(60, 60, 60));
// stops << QGradientStop(0.5, QColor(102, 176, 54));
// //stops << QGradientStop(((frame.height() + h)/2 )/frame.height(), QColor(157, 195, 55));
// stops << QGradientStop(1.0, QColor(215, 215, 215));
// gradient.setStops(stops);
// //gradient.setStart(innerFrame.topLeft());
// //gradient.setFinalStop(innerFrame.bottomRight());
// stops.clear();
// stops << QGradientStop(0.0, QColor(215, 255, 200));
// stops << QGradientStop(0.5, QColor(102, 176, 54));
// stops << QGradientStop(1.0, QColor(0, 0, 0));
// gradient.setStops(stops);
// //painter->setBrush(QBrush(gradient));
// //QGradient gardient;
// //gardient.setCoordinateMode(QLinearGradient);
// //QColor c( 209,121,59 );
// //int factor = 200;
// //return QVariant( QBrush( c.darker(factor) ) );
// return QVariant( QBrush( gradient ) );
// }
// break;
default:
return QVariant();
}
}
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
// if (!index.isValid())
// return 0;
// return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
if(!index.isValid())
return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
Qt::ItemFlags flags;
TreeItem* item = getItem(index);
//if( item->isLeaf() )
// flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled| Qt::ItemIsDropEnabled;
//else
flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled| Qt::ItemIsDropEnabled;
//if(mColumns.at(index.column()).isEditable)
if( !item->isLeaf() )
flags |= Qt::ItemIsEditable;
return flags;
}
Qt::DropActions TreeModel::supportedDropActions() const
{
//return Qt::CopyAction | Qt::MoveAction;
return Qt::MoveAction;
}
bool TreeModel::dropMimeData ( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent )
{
if (action == Qt::IgnoreAction)
return true;
qDebug() << "dropMimeData: " << "row=" << row << ",column=" << column << ",parent=" << parent;
if(!data->hasFormat("application/x-qabstractitemmodeldatalist") )
return true;
QByteArray encoded = data->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&encoded, QIODevice::ReadOnly);
while (!stream.atEnd())
{
int fromrow, fromcol;
QMap<int, QVariant> roleDataMap;
stream >> fromrow >> fromcol >> roleDataMap;
qDebug() << "fromrow=" << fromrow << ",fromcol=" << fromcol << ",roleDataMap=" << roleDataMap;
QVariant v = roleDataMap[0];
QModelIndexList selectList = view_->selectedIndexes();
if( selectList.size() == 0 )
break;
//GroupTreeItem* item2Move = (GroupTreeItem*)rootItem->findChildByText(v.toString());
//QList<QTreeWidgetItem *> selectList = this->in selectedItems();
//TreeItem* item2Move = selectList.at(0);
const QModelIndex selectedindex = selectList.at(0);
TreeItem* item2Move = getItem(selectedindex);
//TreeItem* item2Move = rootItem->findChildByText(v.toString());
if( item2Move )
{
GroupTreeItem* item;
if( parent.isValid())
item = (GroupTreeItem*)getItem(parent);
else
item = (GroupTreeItem*)rootItem;
if(item2Move->isLeaf())
{
int pos = findLeafPositionByName(((LeafTreeItem*)item2Move)->getLeafInfo()->name());
if( row >0 )
{
TreeItem* treeit = item->child(row-1);
if( treeit->isLeaf() )
{
QMessageBox::warning(NULL,tr("Operation not possible"),tr("You can move PuTTY sessions only to groups"));
}
else
{
qDebug() << leafs_[pos].name() << " now have parent: " << ((GroupTreeItem*)treeit)->getGroupInfo()->groupId();
leafs_[pos].setGroupId(((GroupTreeItem*)treeit)->getGroupInfo()->groupId());
}
}else{
qDebug() << leafs_[pos].name() << " now have root parent ";
leafs_[pos].setGroupId(((GroupTreeItem*)rootItem)->getGroupInfo()->groupId());
}
}
else
{
int pos =/*Group* g =*/ findGroupPositionById(((GroupTreeItem*)item2Move)->getGroupInfo()->groupId());
if( row >0 )
{
item = (GroupTreeItem*)item->child(row-1);
//qDebug() << groups_[pos].groupId() << " now have parent: " << item->getGroupInfo()->groupId();
groups_[pos].setParentGroupId(item->getGroupInfo()->groupId());
}else{
//qDebug() << groups_[pos].groupId() << " now have root parent ";
groups_[pos].setParentGroupId(((GroupTreeItem*)rootItem)->getGroupInfo()->groupId());
}
}
setupModelData();
reset();
view_->expandAll();
}
}
//QList<QUrl> dd = data->urls();
//qDebug() << dd;
//qDebug() << data->data();
return true;
}
QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
//if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
// return rootItem->data(section);
return QVariant();
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex TreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
void TreeModel::recreateLeafs()
{
foreach(Leaf s, leafs_)
{
//if( s.groupId() == -1 )
//{
TreeItem* myparent = dynamic_cast<GroupTreeItem*>(rootItem)->findChildByGroupId(s.groupId());
if( myparent )
myparent->appendChild(new LeafTreeItem(s, myparent ));
//}
}
}
void TreeModel::recreateNodes()
{
if(rootItem)
delete rootItem;
rootItem = new GroupTreeItem(Group("Name",-1,-1));
// foreach(Group g, groups_)
// {
// qDebug() << g.name() << "of id:" << g.groupId() << ", parent:" << g.parentGroupId();
// }
foreach(Group g, groups_)
{
if( g.parentGroupId() == -1 )
{
rootItem->appendChild(new GroupTreeItem(g, rootItem ));
}
}
for(int i=0;i<groups_.size();++i)
//foreach(Group g, groups_)
{
if( i < 0 )
continue;
Group g = groups_[i];
if( g.parentGroupId() == -1 )
continue;
TreeItem* test = dynamic_cast<GroupTreeItem*>(rootItem)->findChildByGroupId(g.groupId());
if( test ) //already in tree
continue;
if( GroupTreeItem* gti = dynamic_cast<GroupTreeItem*>(rootItem) )
{
TreeItem* it = gti->findChildByGroupId(g.parentGroupId());
if( it )
{
it->appendChild(new GroupTreeItem(g, it ));
i=-1;
continue;
}
}
}
}
void TreeModel::setupModelData()
{
recreateNodes();
recreateLeafs();
}
void TreeModel::refreshMe()
{
layoutChanged();
}
const TreeItem* TreeModel::getRootItem()const
{
return rootItem;
}
/*
bool TreeModel::canFetchMore(const QModelIndex& parent) const
{
if (!parent.isValid()) return false;
TreeItem *node = static_cast<TreeItem*>(parent.internalPointer());
return parentNode->hasChildren && !parentNode->children.count();
}
void TreeModel::fetchMore (const QModelIndex& parent)
{
if (!canFetchMore(parent)) return;
TreeItem *node = static_cast<TreeItem*>(parent.internalPointer());
beginInsertRows(node, 0, mm_IDs.count()-1);
setLists(node, mm_IDs, mm_Names);
endInsertRows();
}*/
void TreeModel::addItem(QString text, QString comments, TreeItem* parent)
{
TreeItem* p = NULL;
if(parent==NULL)
p=rootItem;
else
p = parent;
QModelIndex parentIndex=createIndex(p->row(),0,p);
//parentIndex = parentIndex.child(0,0);
beginInsertRows(parentIndex,p->childCount(),p->childCount());
//TreeItem *newItem=new TreeItem( (QList<QVariant>() << text << comments) ,p);
//p->appendChild(newItem);
endInsertRows();
reset();
/*
int rootRowsCount = rowCount( QModelIndex() );
qDebug() << "rootRowsCount=" << rootRowsCount;
beginInsertRows( index(rootRowsCount,0), rootRowsCount,rootRowsCount);
p->appendChild( new TreeItem( (QList<QVariant>() << text << comments) , parent) );
endInsertRows();*/
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.