6 #include "attributeitem.h"
7 #include "branchitem.h"
10 #include "treemodel.h"
12 TreeModel::TreeModel(QObject *parent)
13 : QAbstractItemModel(parent)
15 QList<QVariant> rootData;
16 rootData << "Heading" << "Type" <<"Note";
17 rootItem = new TreeItem(rootData);
20 TreeModel::~TreeModel()
25 QVariant TreeModel::data(const QModelIndex &index, int role) const
30 if (role != Qt::DisplayRole)
33 TreeItem *item = getItem (index);
35 return item->data(index.column());
38 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
41 return Qt::ItemIsEnabled;
43 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
46 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
49 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
50 return rootItem->data(section);
55 QModelIndex TreeModel::index (TreeItem* ti)
60 return createIndex (ti->row(),ti->column(),ti);
63 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
68 if (!parent.isValid())
69 { //FIXME-3 left here for testing only, seems to work now...
70 parentItem = rootItem;
72 cout << "TM::index() no parent?! xxx\n";
73 cout << " row="<<row<<" col="<<column<<endl;
74 cout << " parent.internal="<< parent.internalPointer()<<endl;
76 //return QModelIndex(); //FIXME-3 this line is new (testing)
77 // Somehow index is requested where parentIndex is invalid.
78 // what's happening here...?
79 // Check if Qt examples also return index of rootIem then...
83 parentItem = getItem (parent);
85 TreeItem *childItem = parentItem->child(row);
87 return createIndex(row, column, childItem);
92 QModelIndex TreeModel::parent(const QModelIndex &index) const
97 //FIXME-3 cout << "TM::parent ri="<<rootItem<< " row="<<index.row()<<" col="<<index.column()<<endl;
98 TreeItem *ti= getItem (index);
99 //cout << " ti="<<ti<<endl;
100 //cout << " "<<ti->getHeadingStd()<<endl;
101 TreeItem *parentItem = ti->parent();
102 //cout << " pi="<<parentItem<<endl;
104 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
105 if (parentItem == rootItem)
106 return QModelIndex();
109 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
110 // see also my question on qt-interest in march
112 return createIndex(parentItem->childNumber(), 0, parentItem);
115 int TreeModel::rowCount(const QModelIndex &parent) const
117 TreeItem *parentItem;
119 if (!parent.isValid())
120 parentItem = rootItem;
122 parentItem = getItem (parent);
124 return parentItem->childCount();
127 int TreeModel::columnCount(const QModelIndex &parent) const
129 if (parent.isValid())
130 return getItem (parent)->columnCount();
132 return rootItem->columnCount();
135 BranchItem* TreeModel::next(BranchItem* ¤t, BranchItem* &previous, BranchItem* start)
137 /*FIXME-3 cout << "TM::next \n";
138 std::string ch="()"; if (current) ch=current->getHeadingStd();
139 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
140 cout << " cur="<<ch << " prev="<<ph<<endl;
143 // Walk through map beginning at current with previous==0
144 // Start at root, if current==NULL
145 if (!current) current=(BranchItem*)rootItem;
147 // Are we just beginning to walk the map?
150 if (!start) start=current;
152 current=current->getFirstBranch();
156 // Going up or down (deeper)?
157 if (current->depth() > previous->depth() )
160 // Trying to go down deeper
161 // cout << " trying to go deeper\n";
162 if (current->branchCount() >0 )
164 // cout << " yes, going deeper\n";
166 current=current->getFirstBranch();
169 // turn around and go up again
170 // cout << " sorry, turn around\n";
171 BranchItem *bi=current;
177 cout << " coming from below\n";
178 ch="()"; if (current) ch=current->getHeadingStd();
179 ph="()"; if (previous) ph=previous->getHeadingStd();
180 cout << " cur="<<ch << " prev="<<ph<<endl;
183 // Trying to go down again to siblings
185 BranchItem *sibling=current->getBranchNum (previous->num()+1);
186 // cout <<" prev->num()="<<previous->num()<<endl;
190 // Found sibling of previous, go there
191 // cout << " sib=cur="<<sibling->getHeadingStd()<<endl;
197 // If we only needed to go through subtree, we are done now
198 if (start==current) return NULL;
200 // Go up and try to find siblings of current
201 // cout <<" going up again...\n";
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=next (current,previous,start);
214 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
216 int last=row+count-1;
217 TreeItem *pi= getItem (parent);
221 cout << "TM::removeRows row="<<row<<" count="<<count<<endl;
222 cout << " pi="<<pi<<" ti="<<ti<<endl;
224 for (int i=row; i<=last; i++)
226 ti=pi->getChildNum (row);
227 pi->removeChild (row); // does not delete object!
228 switch (ti->getType())
230 case TreeItem::MapCenter:
231 delete (BranchItem*)ti;
233 case TreeItem::Branch:
234 delete (BranchItem*)ti;
236 case TreeItem::Image:
237 delete (ImageItem*)ti;
239 case TreeItem::Attribute:
240 delete (AttributeItem*)ti;
250 TreeItem *TreeModel::getItem(const QModelIndex &index) const
252 //FIXME-3 cout << "TM::getItem "<<index.internalPointer()<<endl;
253 if (index.isValid()) {
254 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
256 // cout << " item="<<item<<endl;
257 if (item) return item;
262 TreeItem *TreeModel::getRootItem()