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()
20 //qDebug()<<"Destr TreeModel this="<<this;
24 QVariant TreeModel::data(const QModelIndex &index, int role) const
29 if (role != Qt::DisplayRole)
32 TreeItem *item = getItem (index);
34 return item->data(index.column());
37 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
40 return Qt::NoItemFlags;
42 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
45 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
48 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
49 return rootItem->data(section);
54 QModelIndex TreeModel::index (TreeItem* ti)
59 return createIndex (ti->row(),0,ti);
62 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
65 // Make sure to return invalid index for invalid values (see modeltest)
66 if (row<0 || column<0) return QModelIndex();
67 if (column!=0) return QModelIndex();
71 if (!parent.isValid())
73 parentItem = rootItem;
75 cout << "TM::index() no parent?! xxx\n";
76 cout << " row="<<row<<" col="<<column<<endl;
77 cout << " parent.internal="<< parent.internalPointer()<<endl;
79 // Somehow index is requested where parentIndex is invalid.
80 // what's happening here...?
81 // Check if Qt examples also return index of rootIem then...
85 parentItem = getItem (parent);
87 TreeItem *childItem = parentItem->child(row);
88 //cout << "TM::index parentItem="<<parentItem<<" childItem="<<childItem<<" row="<<row<<" col="<<column<<endl;
90 return createIndex(row, column, childItem);
95 QModelIndex TreeModel::parent(const QModelIndex &index) const
100 //FIXME-3 cout << "TM::parent ri="<<rootItem<< " row="<<index.row()<<" col="<<index.column()<<endl;
101 TreeItem *ti= getItem (index);
102 //cout << " ti="<<ti<<endl;
103 //cout << " "<<ti->getHeadingStd()<<endl;
104 TreeItem *parentItem = ti->parent();
105 //cout << " pi="<<parentItem<<endl;
107 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
108 if (parentItem == rootItem)
109 return QModelIndex();
113 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
114 // see also my question on qt-interest in march
116 return createIndex(parentItem->childNumber(), 0, parentItem);
119 int TreeModel::rowCount(const QModelIndex &parent) const
121 TreeItem *parentItem;
123 if (!parent.isValid())
124 parentItem = rootItem;
126 parentItem = getItem (parent);
128 return parentItem->childCount();
131 int TreeModel::columnCount(const QModelIndex &parent) const
134 if (parent.isValid())
136 c= getItem (parent)->columnCount();
137 //cout << "TM::colCount c="<<c<<" parent="<<getItem (parent)<<endl;
141 c= rootItem->columnCount();
142 //cout << "TM::colCount c="<<c<<" parent=invalid"<<endl;
147 BranchItem* TreeModel::nextBranch (BranchItem* ¤t, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)
149 // Walk through map beginning at current with previous==0
150 // Start at root, if current==NULL
151 if (!current) current=(BranchItem*)rootItem;
153 // Are we just beginning to walk the map?
156 if (!start) start=current;
158 current=current->getFirstBranch();
163 // Going up or down (deeper)?
164 if (current->depth() > previous->depth() )
167 // Trying to go down deeper
168 if (current->branchCount() >0 )
171 current=current->getFirstBranch();
174 // turn around and go up again
175 BranchItem *bi=current;
181 // Trying to go down again to siblings
183 BranchItem *sibling=current->getBranchNum (previous->num()+1);
187 // Found sibling of previous, go there
193 // If we only needed to go through subtree, we are done now
194 if (start==current) return NULL;
196 // Go up and try to find siblings of current
198 current=(BranchItem*)current->parent();
200 // Check if we still can go somewhere
201 if (!current) return current;
203 while (current && current->depth() < previous->depth() )
204 current=nextBranch (current,previous,true,start);
211 cout << "TM::nextBranch shallow\n";
212 std::string ch="()"; if (current) ch=current->getHeadingStd();
213 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
214 cout << " cur="<<ch << " prev="<<ph<<endl;
217 // Try to find sibling with same depth
218 BranchItem *sibling=current->parent()->getBranchNum (current->num()+1);
221 // Found sibling of previous, go there
227 // Try to find next branch with same depth or greater
238 TreeItem *ti=model->getItem (ix);
239 cout << " level="<<level<<" ix=";
240 if (ti) cout << ti->getHeadingStd();
243 if (! treeEditor->isExpanded(ix))
246 cout <<" is expanded.";
248 ix=ix.sibling(row+1,col);
255 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
257 int last=row+count-1;
259 if (parent.isValid())
265 //cout << "TM::removeRows pi="<<pi<<" row="<<row<<" count="<<count<<endl;
266 for (int i=row; i<=last; i++)
268 ti=pi->getChildNum (row);
269 pi->removeChild (row); // does not delete object!
275 TreeItem *TreeModel::getItem(const QModelIndex &index) const
277 if (index.isValid()) {
278 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
279 if (item) return item;
284 BranchItem *TreeModel::getRootItem()