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 TreeItem *item = getItem (index);
31 if (role != Qt::DisplayRole)
33 if (role == Qt::ForegroundRole )
34 return item->getHeadingColor();
39 return item->data(index.column());
42 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
45 return Qt::NoItemFlags;
47 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
50 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
53 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
54 return rootItem->data(section);
59 QModelIndex TreeModel::index (TreeItem* ti)
64 return createIndex (ti->row(),0,ti);
67 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
70 // Make sure to return invalid index for invalid values (see modeltest)
71 if (row<0 || column<0) return QModelIndex();
72 if (column!=0) return QModelIndex();
76 if (!parent.isValid())
78 parentItem = rootItem;
80 cout << "TM::index() no parent?! xxx\n";
81 cout << " row="<<row<<" col="<<column<<endl;
82 cout << " parent.internal="<< parent.internalPointer()<<endl;
84 // Somehow index is requested where parentIndex is invalid.
85 // what's happening here...?
86 // Check if Qt examples also return index of rootIem then...
90 parentItem = getItem (parent);
92 TreeItem *childItem = parentItem->child(row);
93 //cout << "TM::index parentItem="<<parentItem<<" childItem="<<childItem<<" row="<<row<<" col="<<column<<endl;
95 return createIndex(row, column, childItem);
100 QModelIndex TreeModel::parent(const QModelIndex &index) const
102 if (!index.isValid())
103 return QModelIndex();
105 //FIXME-3 cout << "TM::parent ri="<<rootItem<< " row="<<index.row()<<" col="<<index.column()<<endl;
106 TreeItem *ti= getItem (index);
107 //cout << " ti="<<ti<<endl;
108 //cout << " "<<ti->getHeadingStd()<<endl;
109 TreeItem *parentItem = ti->parent();
110 //cout << " pi="<<parentItem<<endl;
112 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
113 if (parentItem == rootItem)
114 return QModelIndex();
118 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
119 // see also my question on qt-interest in march
121 return createIndex(parentItem->childNumber(), 0, parentItem);
124 int TreeModel::rowCount(const QModelIndex &parent) const
126 TreeItem *parentItem;
128 if (!parent.isValid())
129 parentItem = rootItem;
131 parentItem = getItem (parent);
133 return parentItem->childCount();
136 int TreeModel::columnCount(const QModelIndex &parent) const
139 if (parent.isValid())
141 c= getItem (parent)->columnCount();
142 //cout << "TM::colCount c="<<c<<" parent="<<getItem (parent)<<endl;
146 c= rootItem->columnCount();
147 //cout << "TM::colCount c="<<c<<" parent=invalid"<<endl;
152 BranchItem* TreeModel::nextBranch (BranchItem* ¤t, BranchItem* &previous, bool deepLevelsFirst, BranchItem *start)
154 // Walk through map beginning at current with previous==0
155 // Start at root, if current==NULL
156 if (!current) current=(BranchItem*)rootItem;
158 // Are we just beginning to walk the map?
161 if (!start) start=current;
163 current=current->getFirstBranch();
168 // Going up or down (deeper)?
169 if (current->depth() > previous->depth() )
172 // Trying to go down deeper
173 if (current->branchCount() >0 )
176 current=current->getFirstBranch();
179 // turn around and go up again
180 BranchItem *bi=current;
186 // Trying to go down again to siblings
188 BranchItem *sibling=current->getBranchNum (previous->num()+1);
192 // Found sibling of previous, go there
198 // If we only needed to go through subtree, we are done now
199 if (start==current) return NULL;
201 // Go up and try to find siblings of current
203 current=(BranchItem*)current->parent();
205 // Check if we still can go somewhere
206 if (!current) return current;
208 while (current && current->depth() < previous->depth() )
209 current=nextBranch (current,previous,true,start);
216 cout << "TM::nextBranch shallow\n";
217 std::string ch="()"; if (current) ch=current->getHeadingStd();
218 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
219 cout << " cur="<<ch << " prev="<<ph<<endl;
222 // Try to find sibling with same depth
223 BranchItem *sibling=current->parent()->getBranchNum (current->num()+1);
226 // Found sibling of previous, go there
232 // Try to find next branch with same depth or greater
243 TreeItem *ti=model->getItem (ix);
244 cout << " level="<<level<<" ix=";
245 if (ti) cout << ti->getHeadingStd();
248 if (! treeEditor->isExpanded(ix))
251 cout <<" is expanded.";
253 ix=ix.sibling(row+1,col);
260 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
262 int last=row+count-1;
264 if (parent.isValid())
270 //cout << "TM::removeRows pi="<<pi<<" row="<<row<<" count="<<count<<endl;
271 for (int i=row; i<=last; i++)
273 ti=pi->getChildNum (row);
274 pi->removeChild (row); // does not delete object!
280 TreeItem *TreeModel::getItem(const QModelIndex &index) const
282 if (index.isValid()) {
283 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
284 if (item) return item;
289 BranchItem *TreeModel::getRootItem()