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::next(BranchItem* ¤t, BranchItem* &previous, BranchItem* start) // FIXME-3 change this to nextBranch and use "next" for all TIs
151 /*FIXME-3 cout << "TM::next \n";
152 std::string ch="()"; if (current) ch=current->getHeadingStd();
153 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
154 cout << " cur="<<ch << " prev="<<ph<<endl;
157 // Walk through map beginning at current with previous==0
158 // Start at root, if current==NULL
159 if (!current) current=(BranchItem*)rootItem;
161 // Are we just beginning to walk the map?
164 if (!start) start=current;
166 current=current->getFirstBranch();
170 // Going up or down (deeper)?
171 if (current->depth() > previous->depth() )
174 // Trying to go down deeper
175 // cout << " trying to go deeper\n";
176 if (current->branchCount() >0 )
178 // cout << " yes, going deeper\n";
180 current=current->getFirstBranch();
183 // turn around and go up again
184 // cout << " sorry, turn around\n";
185 BranchItem *bi=current;
191 cout << " coming from below\n";
192 ch="()"; if (current) ch=current->getHeadingStd();
193 ph="()"; if (previous) ph=previous->getHeadingStd();
194 cout << " cur="<<ch << " prev="<<ph<<endl;
197 // Trying to go down again to siblings
199 BranchItem *sibling=current->getBranchNum (previous->num()+1);
200 // cout <<" prev->num()="<<previous->num()<<endl;
204 // Found sibling of previous, go there
205 // cout << " sib=cur="<<sibling->getHeadingStd()<<endl;
211 // If we only needed to go through subtree, we are done now
212 if (start==current) return NULL;
214 // Go up and try to find siblings of current
215 // cout <<" going up again...\n";
217 current=(BranchItem*)current->parent();
219 // Check if we still can go somewhere
220 if (!current) return current;
222 while (current && current->depth() < previous->depth() )
223 current=next (current,previous,start);
228 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
230 int last=row+count-1;
231 TreeItem *pi= getItem (parent);
234 cout << "TM::removeRows row="<<row<<" count="<<count<<endl;
235 for (int i=row; i<=last; i++)
237 ti=pi->getChildNum (row);
238 cout << " pi="<<pi<<" ti="<<ti<<endl;
239 pi->removeChild (row); // does not delete object!
242 switch (ti->getType())
244 case TreeItem::MapCenter:
245 delete (BranchItem*)ti;
247 case TreeItem::Branch:
248 delete (BranchItem*)ti;
250 case TreeItem::Image:
251 delete (ImageItem*)ti;
253 case TreeItem::Attribute:
254 delete (AttributeItem*)ti;
256 case TreeItem::XLink:
257 delete (XLinkItem*)ti;
268 TreeItem *TreeModel::getItem(const QModelIndex &index) const
270 //FIXME-3 cout << "TM::getItem "<<index.internalPointer()<<endl;
271 if (index.isValid()) {
272 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
274 // cout << " item="<<item<<endl;
275 if (item) return item;
280 TreeItem *TreeModel::getRootItem()