3 #include "attributeitem.h"
4 #include "branchitem.h"
10 TreeModel::TreeModel(QObject *parent)
11 : QAbstractItemModel(parent)
13 QList<QVariant> rootData;
14 rootData << "Heading" << "Type";
15 rootItem = new BranchItem(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::NoItemFlags;
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 (TreeItem* ti)
58 return createIndex (ti->row(),0,ti);
61 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
64 // Make sure to return invalid index for invalid values (see modeltest)
65 if (row<0 || column<0) return QModelIndex();
66 if (column!=0) return QModelIndex();
70 if (!parent.isValid())
72 parentItem = rootItem;
74 cout << "TM::index() no parent?! xxx\n";
75 cout << " row="<<row<<" col="<<column<<endl;
76 cout << " parent.internal="<< parent.internalPointer()<<endl;
78 // Somehow index is requested where parentIndex is invalid.
79 // what's happening here...?
80 // Check if Qt examples also return index of rootIem then...
84 parentItem = getItem (parent);
86 TreeItem *childItem = parentItem->child(row);
87 //cout << "TM::index parentItem="<<parentItem<<" childItem="<<childItem<<" row="<<row<<" col="<<column<<endl;
89 return createIndex(row, column, childItem);
94 QModelIndex TreeModel::parent(const QModelIndex &index) const
99 //FIXME-3 cout << "TM::parent ri="<<rootItem<< " row="<<index.row()<<" col="<<index.column()<<endl;
100 TreeItem *ti= getItem (index);
101 //cout << " ti="<<ti<<endl;
102 //cout << " "<<ti->getHeadingStd()<<endl;
103 TreeItem *parentItem = ti->parent();
104 //cout << " pi="<<parentItem<<endl;
106 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
107 if (parentItem == rootItem)
108 return QModelIndex();
112 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
113 // see also my question on qt-interest in march
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())
135 c= getItem (parent)->columnCount();
136 //cout << "TM::colCount c="<<c<<" parent="<<getItem (parent)<<endl;
140 c= rootItem->columnCount();
141 //cout << "TM::colCount c="<<c<<" parent=invalid"<<endl;
146 BranchItem* TreeModel::nextBranch (BranchItem* ¤t, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)
148 // Walk through map beginning at current with previous==0
149 // Start at root, if current==NULL
150 if (!current) current=(BranchItem*)rootItem;
152 // Are we just beginning to walk the map?
155 if (!start) start=current;
157 current=current->getFirstBranch();
162 // Going up or down (deeper)?
163 if (current->depth() > previous->depth() )
166 // Trying to go down deeper
167 if (current->branchCount() >0 )
170 current=current->getFirstBranch();
173 // turn around and go up again
174 BranchItem *bi=current;
180 // Trying to go down again to siblings
182 BranchItem *sibling=current->getBranchNum (previous->num()+1);
186 // Found sibling of previous, go there
192 // If we only needed to go through subtree, we are done now
193 if (start==current) return NULL;
195 // Go up and try to find siblings of current
197 current=(BranchItem*)current->parent();
199 // Check if we still can go somewhere
200 if (!current) return current;
202 while (current && current->depth() < previous->depth() )
203 current=nextBranch (current,previous,true,start);
210 cout << "TM::nextBranch shallow\n";
211 std::string ch="()"; if (current) ch=current->getHeadingStd();
212 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
213 cout << " cur="<<ch << " prev="<<ph<<endl;
216 // Try to find sibling with same depth
217 BranchItem *sibling=current->parent()->getBranchNum (current->num()+1);
220 // Found sibling of previous, go there
226 // Try to find next branch with same depth or greater
237 TreeItem *ti=model->getItem (ix);
238 cout << " level="<<level<<" ix=";
239 if (ti) cout << ti->getHeadingStd();
242 if (! treeEditor->isExpanded(ix))
245 cout <<" is expanded.";
247 ix=ix.sibling(row+1,col);
254 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
256 int last=row+count-1;
258 if (parent.isValid())
264 //cout << "TM::removeRows pi="<<pi<<" row="<<row<<" count="<<count<<endl;
265 for (int i=row; i<=last; i++)
267 ti=pi->getChildNum (row);
268 pi->removeChild (row); // does not delete object!
274 TreeItem *TreeModel::getItem(const QModelIndex &index) const
276 if (index.isValid()) {
277 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
278 if (item) return item;
283 BranchItem *TreeModel::getRootItem()