3 #include "attributeitem.h"
4 #include "branchitem.h"
11 TreeModel::TreeModel(QObject *parent) : QAbstractItemModel(parent)
13 // qDebug()<<"Constr TreeModel this=" << this;
14 QList<QVariant> rootData;
15 rootData << "Heading";
16 // rootData << "Type";
17 rootItem = new BranchItem();
20 TreeModel::~TreeModel()
22 // qDebug()<<"Destr TreeModel this="<<this;
26 QVariant TreeModel::data(const QModelIndex &index, int role) const
31 TreeItem *item = getItem(index);
32 BranchItem *bi = nullptr;
33 if (item->isBranchLikeType())
34 bi = (BranchItem*)item;
36 if (role == Qt::EditRole || role == Qt::DisplayRole)
37 return item->data(index.column());
39 if (role == Qt::ForegroundRole)
40 return item->getHeadingColor();
42 if (role == Qt::BackgroundRole) {
44 BranchItem *frameBI = bi->getFramedParentBranch(bi);
45 if (frameBI && index.column() != 5) {
46 BranchObj *bo = frameBI->getBranchObj();
48 return bo->getFrameBrushColor();
51 return backgroundColor;
58 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
61 return Qt::NoItemFlags;
63 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
66 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
69 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
70 return rootItem->data(section);
75 QModelIndex TreeModel::index(TreeItem *ti)
80 return createIndex(ti->row(), 0, ti);
83 QModelIndex TreeModel::index(int row, int column,
84 const QModelIndex &parent) const
86 // Make sure to return invalid index for invalid values (see modeltest)
87 if (row < 0 || column < 0)
94 if (!parent.isValid())
95 parentItem = rootItem;
97 parentItem = getItem(parent);
99 TreeItem *childItem = parentItem->child(row);
101 return createIndex(row, column, childItem);
103 return QModelIndex();
106 QModelIndex TreeModel::parent(const QModelIndex &index) const
108 if (!index.isValid())
109 return QModelIndex();
111 TreeItem *ti = getItem(index);
112 TreeItem *parentItem = ti->parent();
113 if (parentItem == rootItem)
114 return QModelIndex();
115 return createIndex(parentItem->childNumber(), 0, parentItem);
118 int TreeModel::rowCount(const QModelIndex &parent) const
120 TreeItem *parentItem;
122 if (!parent.isValid())
123 parentItem = rootItem;
125 parentItem = getItem(parent);
127 return parentItem->childCount();
130 int TreeModel::columnCount(const QModelIndex &parent) const
133 if (parent.isValid())
134 c = getItem(parent)->columnCount();
136 c = rootItem->columnCount();
140 void TreeModel::nextBranch(BranchItem *¤t, BranchItem *&previous,
141 bool deepLevelsFirst, BranchItem *start)
143 if (deepLevelsFirst) {
144 // Walk through map beginning at current with previous==0
145 // Start at root, if current==NULL
149 previous = current->parentBranch();
152 previous = (BranchItem *)rootItem;
153 current = previous->getFirstBranch();
158 // Walk the tree by always turning "left"
159 // and returning an element when going up
160 if (current == previous) {
161 // Had leaf before, go up again.
162 if (start && start == current) {
166 current = current->parentBranch();
169 return nextBranch(current, previous, deepLevelsFirst, start);
172 if (current->depth() > previous->depth()) {
173 // Coming from above, try to go deeper
174 if (current->branchCount() > 0) {
175 // Turn "left" and go deeper
177 current = current->getFirstBranch();
178 return nextBranch(current, previous, deepLevelsFirst);
181 // turn around and go up again
187 // Coming from below, try to go down again to siblings
189 BranchItem *sibling = current->getBranchNum(previous->num() + 1);
191 // Found sibling of previous, go there
194 return nextBranch(current, previous, deepLevelsFirst, start);
198 if (current == rootItem)
206 // Walk through map beginning at current with previous==0
207 // Start at root, if current==NULL
211 previous = (BranchItem *)(start->parent());
215 previous = (BranchItem *)rootItem;
216 current = previous->getFirstBranch();
221 if (current->depth() > previous->depth()) {
223 if (current->branchCount() > 0) {
224 // Turn "left" and go deeper
226 current = current->getFirstBranch();
230 // turn around and go up again
232 nextBranch(current, previous, deepLevelsFirst, start);
237 if (start && previous == start) {
242 BranchItem *sibling = current->getBranchNum(previous->num() + 1);
244 // Found sibling of previous, go there
250 // no sibling, go further up left
252 current = current->parentBranch();
258 nextBranch(current, previous, deepLevelsFirst, start);
266 bool TreeModel::removeRows(int row, int count, const QModelIndex &parent)
268 int last = row + count - 1;
270 if (parent.isValid())
271 pi = getItem(parent);
276 for (int i = row; i <= last; i++) {
277 ti = pi->getChildNum(row);
278 pi->removeChild(row); // does not delete object!
284 TreeItem *TreeModel::getItem(const QModelIndex &index) const
286 if (index.isValid()) {
287 TreeItem *item = static_cast<TreeItem *>(index.internalPointer());
294 BranchItem *TreeModel::getRootItem() { return rootItem; }
296 int TreeModel::xlinkCount() { return xlinks.count(); }
298 Link *TreeModel::getXLinkNum(const int &n)
300 if (n >= 0 && n < xlinks.count())