6 TreeItem* TreeModel::itFirst=NULL;
7 TreeItem* TreeModel::itCur=NULL;
9 TreeModel::TreeModel(QObject *parent)
10 : QAbstractItemModel(parent)
12 QList<QVariant> rootData;
13 rootData << "Heading" << "Type" <<"Note";
14 rootItem = new TreeItem(rootData);
17 TreeModel::~TreeModel()
22 int TreeModel::columnCount(const QModelIndex &parent) const
25 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
27 return rootItem->columnCount();
30 QVariant TreeModel::data(const QModelIndex &index, int role) const
35 if (role != Qt::DisplayRole)
38 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
40 return item->data(index.column());
43 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
46 return Qt::ItemIsEnabled;
48 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
51 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
54 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
55 return rootItem->data(section);
60 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
65 if (!parent.isValid())
66 parentItem = rootItem;
68 parentItem = static_cast<TreeItem*>(parent.internalPointer());
70 TreeItem *childItem = parentItem->child(row);
72 return createIndex(row, column, childItem);
77 QModelIndex TreeModel::parent(const QModelIndex &index) const
82 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
83 TreeItem *parentItem = childItem->parent();
85 if (parentItem == rootItem)
88 return createIndex(parentItem->row(), 0, parentItem);
91 int TreeModel::rowCount(const QModelIndex &parent) const
95 if (!parent.isValid())
96 parentItem = rootItem;
98 parentItem = static_cast<TreeItem*>(parent.internalPointer());
100 return parentItem->childCount();
103 TreeItem* TreeModel::first()
110 TreeItem* TreeModel::next()
115 BranchObj *po=(BranchObj*)parObj;
117 if (branch.isEmpty())
124 // no itCur, we are just beginning
127 // we have children, return first one
133 // No children, so there is no next
141 { // We come from parent
144 // there are children, go there
149 { // no children, try to go up again
152 // go back to parent and try to find next there
161 // can't go up, I am mapCenter, no next
168 // We don't come from parent, but from brother or children
170 // Try to find last child, where we came from, in my own children
173 while (i<branch.size())
175 // Try to find itCur in my own children
176 if (itCur==branch.at(i))
178 // ok, we come from my own children
179 if (i<branch.size()-1)
189 { // found itCur in my children
192 // found a brother of lastLMO
200 if (this==itFirst) return NULL; // Stop at starting point
209 // can't go up, I am mapCenter
216 // couldn't find last child, it must be a nephew of mine
219 // proceed with my first child
221 return branch.first();
225 // or go back to my parents
236 // can't go up, I am mapCenter
245 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
247 int last=row+count-1;
248 beginRemoveRows (parent,row,last);
250 TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
252 for (int i=row; i<=last; i++)
254 std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
255 pi->removeChild (row);
262 QModelIndex TreeModel::index (TreeItem* ti)
264 return createIndex (ti->row(),ti->column(),ti);