6 #include "attributeitem.h"
7 #include "branchitem.h"
10 #include "treemodel.h"
11 #include "xlinkitem.h"
13 TreeModel::TreeModel(QObject *parent)
14 : QAbstractItemModel(parent)
16 QList<QVariant> rootData;
17 rootData << "Heading" << "Type";
18 rootItem = new BranchItem(rootData);
21 TreeModel::~TreeModel()
26 QVariant TreeModel::data(const QModelIndex &index, int role) const
31 if (role != Qt::DisplayRole)
34 TreeItem *item = getItem (index);
36 return item->data(index.column());
39 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
42 return Qt::NoItemFlags;
44 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
47 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
50 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
51 return rootItem->data(section);
56 QModelIndex TreeModel::index (TreeItem* ti)
61 return createIndex (ti->row(),0,ti);
64 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
67 // Make sure to return invalid index for invalid values (see modeltest)
68 if (row<0 || column<0) return QModelIndex();
69 if (column!=0) return QModelIndex();
73 if (!parent.isValid())
75 parentItem = rootItem;
77 cout << "TM::index() no parent?! xxx\n";
78 cout << " row="<<row<<" col="<<column<<endl;
79 cout << " parent.internal="<< parent.internalPointer()<<endl;
81 // Somehow index is requested where parentIndex is invalid.
82 // what's happening here...?
83 // Check if Qt examples also return index of rootIem then...
87 parentItem = getItem (parent);
89 TreeItem *childItem = parentItem->child(row);
90 //cout << "TM::index parentItem="<<parentItem<<" childItem="<<childItem<<" row="<<row<<" col="<<column<<endl;
92 return createIndex(row, column, childItem);
97 QModelIndex TreeModel::parent(const QModelIndex &index) const
100 return QModelIndex();
102 //FIXME-3 cout << "TM::parent ri="<<rootItem<< " row="<<index.row()<<" col="<<index.column()<<endl;
103 TreeItem *ti= getItem (index);
104 //cout << " ti="<<ti<<endl;
105 //cout << " "<<ti->getHeadingStd()<<endl;
106 TreeItem *parentItem = ti->parent();
107 //cout << " pi="<<parentItem<<endl;
109 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
110 if (parentItem == rootItem)
111 return QModelIndex();
115 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
116 // see also my question on qt-interest in march
118 return createIndex(parentItem->childNumber(), 0, parentItem);
121 int TreeModel::rowCount(const QModelIndex &parent) const
123 TreeItem *parentItem;
125 if (!parent.isValid())
126 parentItem = rootItem;
128 parentItem = getItem (parent);
130 return parentItem->childCount();
133 int TreeModel::columnCount(const QModelIndex &parent) const
136 if (parent.isValid())
138 c= getItem (parent)->columnCount();
139 //cout << "TM::colCount c="<<c<<" parent="<<getItem (parent)<<endl;
143 c= rootItem->columnCount();
144 //cout << "TM::colCount c="<<c<<" parent=invalid"<<endl;
149 BranchItem* TreeModel::nextBranch (BranchItem* ¤t, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)
151 // Walk through map beginning at current with previous==0
152 // Start at root, if current==NULL
153 if (!current) current=(BranchItem*)rootItem;
155 // Are we just beginning to walk the map?
158 if (!start) start=current;
160 current=current->getFirstBranch();
165 // Going up or down (deeper)?
166 if (current->depth() > previous->depth() )
169 // Trying to go down deeper
170 if (current->branchCount() >0 )
173 current=current->getFirstBranch();
176 // turn around and go up again
177 BranchItem *bi=current;
183 // Trying to go down again to siblings
185 BranchItem *sibling=current->getBranchNum (previous->num()+1);
189 // Found sibling of previous, go there
195 // If we only needed to go through subtree, we are done now
196 if (start==current) return NULL;
198 // Go up and try to find siblings of current
200 current=(BranchItem*)current->parent();
202 // Check if we still can go somewhere
203 if (!current) return current;
205 while (current && current->depth() < previous->depth() )
206 current=nextBranch (current,previous,true,start);
213 cout << "TM::nextBranch shallow\n";
214 std::string ch="()"; if (current) ch=current->getHeadingStd();
215 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
216 cout << " cur="<<ch << " prev="<<ph<<endl;
219 // Try to find sibling with same depth
220 BranchItem *sibling=current->parent()->getBranchNum (current->num()+1);
223 // Found sibling of previous, go there
229 // Try to find next branch with same depth or greater
240 TreeItem *ti=model->getItem (ix);
241 cout << " level="<<level<<" ix=";
242 if (ti) cout << ti->getHeadingStd();
245 if (! treeEditor->isExpanded(ix))
248 cout <<" is expanded.";
250 ix=ix.sibling(row+1,col);
257 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
259 int last=row+count-1;
261 if (parent.isValid())
267 //cout << "TM::removeRows pi="<<pi<<" row="<<row<<" count="<<count<<endl;
268 for (int i=row; i<=last; i++)
270 ti=pi->getChildNum (row);
271 pi->removeChild (row); // does not delete object!
277 TreeItem *TreeModel::getItem(const QModelIndex &index) const
279 if (index.isValid()) {
280 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
281 if (item) return item;
286 BranchItem *TreeModel::getRootItem()