6 #include "branchitem.h"
10 TreeModel::TreeModel(QObject *parent)
11 : QAbstractItemModel(parent)
13 QList<QVariant> rootData;
14 rootData << "Heading" << "Type" <<"Note";
15 rootItem = new TreeItem(rootData);
18 TreeModel::~TreeModel()
23 QVariant TreeModel::data(const QModelIndex &index, int role) const
28 if (role != Qt::DisplayRole)
31 TreeItem *item = getItem (index);
33 return item->data(index.column());
36 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
39 return Qt::ItemIsEnabled;
41 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
44 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
47 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
48 return rootItem->data(section);
53 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
58 if (!parent.isValid())
59 parentItem = rootItem;
61 parentItem = getItem (parent);
63 TreeItem *childItem = parentItem->child(row);
65 return createIndex(row, column, childItem);
70 QModelIndex TreeModel::parent(const QModelIndex &index) const
75 TreeItem *ti= getItem (index);
76 TreeItem *parentItem = ti->parent();
78 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
79 if (parentItem == rootItem)
83 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
84 // see also my question on qt-interest in march
86 return createIndex(parentItem->childNumber(), 0, parentItem);
89 int TreeModel::rowCount(const QModelIndex &parent) const
93 if (!parent.isValid())
94 parentItem = rootItem;
96 parentItem = getItem (parent);
98 return parentItem->childCount();
101 int TreeModel::columnCount(const QModelIndex &parent) const
103 if (parent.isValid())
104 return getItem (parent)->columnCount();
106 return rootItem->columnCount();
109 BranchItem* TreeModel::next(BranchItem* ¤t, BranchItem* &previous, int &d0)
111 // Walk through map beginning at current with previous==0
112 // Start at root, if current==NULL
113 if (!current) current=(BranchItem*)rootItem;
115 // Are we just beginning to walk the map?
120 current=current->getFirstBranch();
124 //std::cout << " cur="<<current->getHeading().toStdString();
125 //std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
127 // Going up or down (deeper)?
128 if (current->depth() > previous->depth() )
131 // Trying to go down deeper
132 if (current->branchCount() >0 )
135 current=current->getFirstBranch();
138 // turn around and go up again
141 // Coming from below,
142 // Trying to go down again to siblings
144 BranchItem *sibling=current->getBranchNum (previous->num()+1);
148 // Found sibling of previous, go there
154 // Go up and try to find siblings of current
156 current=(BranchItem*)current->parent();
158 // Check if we still can go somewhere
159 if (!current) return current;
161 while (current && current->depth() < previous->depth() )
162 next (current,previous,d0);
167 bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
169 std::cout << "TreeModel insertRows()\n";
170 int last=row+count-1;
171 beginInsertRows (parent,row,last);
173 for (int i=row; i<=last; i++)
175 std::cout << "TreeModel::insertRows inserting i="<<i<<std::endl;
181 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
183 int last=row+count-1;
184 TreeItem *pi= getItem (parent);
186 beginRemoveRows (parent,row,last);
188 for (int i=row; i<=last; i++)
190 ti=pi->getChildNum (row);
191 pi->removeChild (row); // does not delete object!
192 switch (ti->getType())
194 case TreeItem::MapCenter:
195 delete (BranchItem*)ti;
197 case TreeItem::Branch:
198 delete (BranchItem*)ti;
209 TreeItem *TreeModel::getItem(const QModelIndex &index) const
211 if (index.isValid()) {
212 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
214 if (item) return item;
219 TreeItem *TreeModel::getRootItem()
224 QModelIndex TreeModel::index (TreeItem* ti)
226 return createIndex (ti->row(),ti->column(),ti);