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 TreeItem(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::ItemIsEnabled;
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(),ti->column(),ti);
64 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
69 if (!parent.isValid())
70 { //FIXME-3 left here for testing only, seems to work now...
71 parentItem = rootItem;
73 cout << "TM::index() no parent?! xxx\n";
74 cout << " row="<<row<<" col="<<column<<endl;
75 cout << " parent.internal="<< parent.internalPointer()<<endl;
77 //return QModelIndex(); //FIXME-3 this line is new (testing)
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);
88 return createIndex(row, column, childItem);
93 QModelIndex TreeModel::parent(const QModelIndex &index) const
98 //FIXME-3 cout << "TM::parent ri="<<rootItem<< " row="<<index.row()<<" col="<<index.column()<<endl;
99 TreeItem *ti= getItem (index);
100 //cout << " ti="<<ti<<endl;
101 //cout << " "<<ti->getHeadingStd()<<endl;
102 TreeItem *parentItem = ti->parent();
103 //cout << " pi="<<parentItem<<endl;
105 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
106 if (parentItem == rootItem)
107 return QModelIndex();
110 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
111 // see also my question on qt-interest in march
113 return createIndex(parentItem->childNumber(), 0, parentItem);
116 int TreeModel::rowCount(const QModelIndex &parent) const
118 TreeItem *parentItem;
120 if (!parent.isValid())
121 parentItem = rootItem;
123 parentItem = getItem (parent);
125 return parentItem->childCount();
128 int TreeModel::columnCount(const QModelIndex &parent) const
130 if (parent.isValid())
131 return getItem (parent)->columnCount();
133 return rootItem->columnCount();
136 BranchItem* TreeModel::next(BranchItem* ¤t, BranchItem* &previous, BranchItem* start)
138 /*FIXME-3 cout << "TM::next \n";
139 std::string ch="()"; if (current) ch=current->getHeadingStd();
140 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
141 cout << " cur="<<ch << " prev="<<ph<<endl;
144 // Walk through map beginning at current with previous==0
145 // Start at root, if current==NULL
146 if (!current) current=(BranchItem*)rootItem;
148 // Are we just beginning to walk the map?
151 if (!start) start=current;
153 current=current->getFirstBranch();
157 // Going up or down (deeper)?
158 if (current->depth() > previous->depth() )
161 // Trying to go down deeper
162 // cout << " trying to go deeper\n";
163 if (current->branchCount() >0 )
165 // cout << " yes, going deeper\n";
167 current=current->getFirstBranch();
170 // turn around and go up again
171 // cout << " sorry, turn around\n";
172 BranchItem *bi=current;
178 cout << " coming from below\n";
179 ch="()"; if (current) ch=current->getHeadingStd();
180 ph="()"; if (previous) ph=previous->getHeadingStd();
181 cout << " cur="<<ch << " prev="<<ph<<endl;
184 // Trying to go down again to siblings
186 BranchItem *sibling=current->getBranchNum (previous->num()+1);
187 // cout <<" prev->num()="<<previous->num()<<endl;
191 // Found sibling of previous, go there
192 // cout << " sib=cur="<<sibling->getHeadingStd()<<endl;
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
202 // cout <<" going up again...\n";
204 current=(BranchItem*)current->parent();
206 // Check if we still can go somewhere
207 if (!current) return current;
209 while (current && current->depth() < previous->depth() )
210 current=next (current,previous,start);
215 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
217 int last=row+count-1;
218 TreeItem *pi= getItem (parent);
221 cout << "TM::removeRows row="<<row<<" count="<<count<<endl;
222 for (int i=row; i<=last; i++)
224 ti=pi->getChildNum (row);
225 cout << " pi="<<pi<<" ti="<<ti<<endl;
226 pi->removeChild (row); // does not delete object!
227 switch (ti->getType())
229 case TreeItem::MapCenter:
230 delete (BranchItem*)ti;
232 case TreeItem::Branch:
233 delete (BranchItem*)ti;
235 case TreeItem::Image:
236 delete (ImageItem*)ti;
238 case TreeItem::Attribute:
239 delete (AttributeItem*)ti;
241 case TreeItem::XLink:
242 delete (XLinkItem*)ti;
252 TreeItem *TreeModel::getItem(const QModelIndex &index) const
254 //FIXME-3 cout << "TM::getItem "<<index.internalPointer()<<endl;
255 if (index.isValid()) {
256 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
258 // cout << " item="<<item<<endl;
259 if (item) return item;
264 TreeItem *TreeModel::getRootItem()