Menu

[c54480]: / treemodel.cpp  Maximize  Restore  History

Download this file

310 lines (271 with data), 7.7 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
#include <QtGui>
#include "attributeitem.h"
#include "branchitem.h"
#include "imageitem.h"
#include "treeitem.h"
#include "treemodel.h"
#include "xlinkitem.h"
TreeModel::TreeModel(QObject *parent)
: QAbstractItemModel(parent)
{
//qDebug()<<"Constr TreeModel this="<<this;
QList<QVariant> rootData;
rootData << "Heading" << "Type";
rootItem = new BranchItem(rootData);
}
TreeModel::~TreeModel()
{
//qDebug()<<"Destr TreeModel this="<<this;
delete rootItem;
}
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
TreeItem *item = getItem (index);
if (role != Qt::DisplayRole)
{
if (role == Qt::ForegroundRole )
return item->getHeadingColor();
//if (role == Qt::BackgroundRole ) // does not look nice
// return item->getBackgroundColor();
return QVariant();
}
return item->data(index.column());
}
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
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 (TreeItem* ti)
{
if (!ti->parent())
return QModelIndex();
else
return createIndex (ti->row(),0,ti);
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
const
{
// Make sure to return invalid index for invalid values (see modeltest)
if (row<0 || column<0) return QModelIndex();
if (column!=0) return QModelIndex();
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = getItem (parent);
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 *ti= getItem (index);
TreeItem *parentItem = ti->parent();
if (parentItem == rootItem) return QModelIndex();
return createIndex(parentItem->childNumber(), 0, parentItem);
}
int TreeModel::rowCount(const QModelIndex &parent) const
{
TreeItem *parentItem;
if (!parent.isValid())
parentItem = rootItem;
else
parentItem = getItem (parent);
return parentItem->childCount();
}
int TreeModel::columnCount(const QModelIndex &parent) const
{
int c;
if (parent.isValid())
c= getItem (parent)->columnCount();
else
c= rootItem->columnCount();
return c;
}
void TreeModel::nextBranch (BranchItem* &current, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)
{
if (deepLevelsFirst)
{
// Walk through map beginning at current with previous==0
// Start at root, if current==NULL
if (!current)
{
if (start)
{
current=start;
previous=current->parentBranch();
} else
{
previous=(BranchItem*)rootItem;
current = previous->getFirstBranch();
return;
}
}
// Walk the tree by always turning "left"
// and returning an element when going up
if (current == previous)
{
// Had leaf before, go up again.
if (start && start == current)
{
current=NULL;
return;
}
current = current->parentBranch();
if (!current) return;
return nextBranch(current, previous, deepLevelsFirst, start);
}
if (current->depth() > previous->depth() )
{
// Coming from above, try to go deeper
if (current->branchCount() >0 )
{
// Turn "left" and go deeper
previous=current;
current=current->getFirstBranch();
return nextBranch (current, previous, deepLevelsFirst);
} else
{
// turn around and go up again
previous=current;
return;
}
} else
{
// Coming from below, try to go down again to siblings
BranchItem *sibling=current->getBranchNum (previous->num()+1);
if (sibling)
{
// Found sibling of previous, go there
previous=current;
current=sibling;
return nextBranch (current, previous, deepLevelsFirst, start);
} else
{
// and go further up
if (current == rootItem) current = NULL;
previous = current;
return;
}
}
} else
{
// Walk through map beginning at current with previous==0
// Start at root, if current==NULL
if (!current)
{
if (start)
{
current=start;
previous=(BranchItem*)(start->parent() );
return;
} else
{
previous=(BranchItem*)rootItem;
current = previous->getFirstBranch();
return;
}
}
if (current->depth() > previous->depth() )
{
// Going deeper
if (current->branchCount() >0 )
{
// Turn "left" and go deeper
previous=current;
current=current->getFirstBranch();
return;
} else
{
// turn around and go up again
previous=current;
nextBranch (current, previous, deepLevelsFirst, start);
return;
}
} else
{
if (start && previous == start)
{
current=NULL;
return;
}
BranchItem *sibling=current->getBranchNum (previous->num()+1);
if (sibling)
{
// Found sibling of previous, go there
previous=current;
current=sibling;
return;
} else
{
// no sibling, go further up left
previous=current;
current = current->parentBranch();
if (!current)
{
current = NULL;
return;
} else
{
nextBranch (current, previous, deepLevelsFirst, start);
}
}
return;
}
}
}
bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
{
int last=row+count-1;
TreeItem *pi;
if (parent.isValid())
pi=getItem (parent);
else
pi=rootItem;
TreeItem *ti;
for (int i=row; i<=last; i++)
{
ti=pi->getChildNum (row);
pi->removeChild (row); // does not delete object!
delete ti;
}
return true;
}
TreeItem *TreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
if (item) return item;
}
return NULL;
}
BranchItem *TreeModel::getRootItem()
{
return rootItem;
}
int TreeModel::xlinkCount()
{
return xlinks.count();
}
Link* TreeModel::getXLinkNum (const int &n)
{
if (n>=0 && n<xlinks.count())
return xlinks.at(n);
else
return NULL;
}
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.