3 #include "attributeitem.h"
4 #include "branchitem.h"
10 TreeModel::TreeModel(QObject *parent)
11 : QAbstractItemModel(parent)
13 //qDebug()<<"Constr TreeModel this="<<this;
14 QList<QVariant> rootData;
15 rootData << "Heading" << "Type";
16 rootItem = new BranchItem(rootData);
19 TreeModel::~TreeModel()
21 //qDebug()<<"Destr TreeModel this="<<this;
25 QVariant TreeModel::data(const QModelIndex &index, int role) const
30 TreeItem *item = getItem (index);
32 if (role != Qt::DisplayRole)
34 if (role == Qt::ForegroundRole )
35 return item->getHeadingColor();
36 //if (role == Qt::BackgroundRole ) // does not look nice
37 // return item->getBackgroundColor();
42 return item->data(index.column());
45 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
48 return Qt::NoItemFlags;
50 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
53 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
56 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
57 return rootItem->data(section);
62 QModelIndex TreeModel::index (TreeItem* ti)
67 return createIndex (ti->row(),0,ti);
70 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
73 // Make sure to return invalid index for invalid values (see modeltest)
74 if (row<0 || column<0) return QModelIndex();
75 if (column!=0) return QModelIndex();
79 if (!parent.isValid())
80 parentItem = rootItem;
82 parentItem = getItem (parent);
84 TreeItem *childItem = parentItem->child(row);
86 return createIndex(row, column, childItem);
91 QModelIndex TreeModel::parent(const QModelIndex &index) const
96 TreeItem *ti= getItem (index);
97 TreeItem *parentItem = ti->parent();
98 if (parentItem == rootItem) return QModelIndex();
99 return createIndex(parentItem->childNumber(), 0, parentItem);
102 int TreeModel::rowCount(const QModelIndex &parent) const
104 TreeItem *parentItem;
106 if (!parent.isValid())
107 parentItem = rootItem;
109 parentItem = getItem (parent);
111 return parentItem->childCount();
114 int TreeModel::columnCount(const QModelIndex &parent) const
117 if (parent.isValid())
118 c= getItem (parent)->columnCount();
120 c= rootItem->columnCount();
124 void TreeModel::nextBranch (BranchItem* ¤t, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)
128 // Walk through map beginning at current with previous==0
129 // Start at root, if current==NULL
135 previous=current->parentBranch();
138 previous=(BranchItem*)rootItem;
139 current = previous->getFirstBranch();
144 // Walk the tree by always turning "left"
145 // and returning an element when going up
146 if (current == previous)
148 // Had leaf before, go up again.
149 if (start && start == current)
154 current = current->parentBranch();
155 if (!current) return;
156 return nextBranch(current, previous, deepLevelsFirst, start);
159 if (current->depth() > previous->depth() )
161 // Coming from above, try to go deeper
162 if (current->branchCount() >0 )
164 // Turn "left" and go deeper
166 current=current->getFirstBranch();
167 return nextBranch (current, previous, deepLevelsFirst);
170 // turn around and go up again
176 // Coming from below, try to go down again to siblings
178 BranchItem *sibling=current->getBranchNum (previous->num()+1);
181 // Found sibling of previous, go there
184 return nextBranch (current, previous, deepLevelsFirst, start);
188 if (current == rootItem) current = NULL;
195 // Walk through map beginning at current with previous==0
196 // Start at root, if current==NULL
202 previous=(BranchItem*)(start->parent() );
206 previous=(BranchItem*)rootItem;
207 current = previous->getFirstBranch();
213 if (current->depth() > previous->depth() )
216 if (current->branchCount() >0 )
218 // Turn "left" and go deeper
220 current=current->getFirstBranch();
224 // turn around and go up again
226 nextBranch (current, previous, deepLevelsFirst, start);
231 if (start && previous == start)
237 BranchItem *sibling=current->getBranchNum (previous->num()+1);
240 // Found sibling of previous, go there
246 // no sibling, go further up left
248 current = current->parentBranch();
255 nextBranch (current, previous, deepLevelsFirst, start);
263 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
265 int last=row+count-1;
267 if (parent.isValid())
273 for (int i=row; i<=last; i++)
275 ti=pi->getChildNum (row);
276 pi->removeChild (row); // does not delete object!
282 TreeItem *TreeModel::getItem(const QModelIndex &index) const
284 if (index.isValid()) {
285 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
286 if (item) return item;
291 BranchItem *TreeModel::getRootItem()
296 int TreeModel::xlinkCount()
298 return xlinks.count();
301 Link* TreeModel::getXLinkNum (const int &n)
303 if (n>=0 && n<xlinks.count())