6 #include "branchitem.h"
11 TreeModel::TreeModel(QObject *parent)
12 : QAbstractItemModel(parent)
14 QList<QVariant> rootData;
15 rootData << "Heading" << "Type" <<"Note";
16 rootItem = new TreeItem(rootData);
19 TreeModel::~TreeModel()
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::ItemIsEnabled;
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(int row, int column, const QModelIndex &parent)
59 if (!parent.isValid())
60 parentItem = rootItem;
62 parentItem = getItem (parent);
64 TreeItem *childItem = parentItem->child(row);
66 return createIndex(row, column, childItem);
71 QModelIndex TreeModel::parent(const QModelIndex &index) const
76 TreeItem *ti= getItem (index);
77 TreeItem *parentItem = ti->parent();
79 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
80 if (parentItem == rootItem)
84 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
85 // see also my question on qt-interest in march
87 return createIndex(parentItem->childNumber(), 0, parentItem);
90 int TreeModel::rowCount(const QModelIndex &parent) const
94 if (!parent.isValid())
95 parentItem = rootItem;
97 parentItem = getItem (parent);
99 return parentItem->childCount();
102 int TreeModel::columnCount(const QModelIndex &parent) const
104 if (parent.isValid())
105 return getItem (parent)->columnCount();
107 return rootItem->columnCount();
110 BranchItem* TreeModel::next(BranchItem* ¤t, BranchItem* &previous, BranchItem* start)
112 /*FIXME-3 cout << "TM::next \n";
113 std::string ch="()"; if (current) ch=current->getHeadingStd();
114 std::string ph="()"; if (previous) ph=previous->getHeadingStd();
115 cout << " cur="<<ch << " prev="<<ph<<endl;
118 // Walk through map beginning at current with previous==0
119 // Start at root, if current==NULL
120 if (!current) current=(BranchItem*)rootItem;
122 // Are we just beginning to walk the map?
125 if (!start) start=current;
127 current=current->getFirstBranch();
131 // Going up or down (deeper)?
132 if (current->depth() > previous->depth() )
135 // Trying to go down deeper
136 // cout << " trying to go deeper\n";
137 if (current->branchCount() >0 )
139 // cout << " yes, going deeper\n";
141 current=current->getFirstBranch();
144 // turn around and go up again
145 // cout << " sorry, turn around\n";
146 BranchItem *bi=current;
152 cout << " coming from below\n";
153 ch="()"; if (current) ch=current->getHeadingStd();
154 ph="()"; if (previous) ph=previous->getHeadingStd();
155 cout << " cur="<<ch << " prev="<<ph<<endl;
158 // Trying to go down again to siblings
160 BranchItem *sibling=current->getBranchNum (previous->num()+1);
161 // cout <<" prev->num()="<<previous->num()<<endl;
165 // Found sibling of previous, go there
166 // cout << " sib=cur="<<sibling->getHeadingStd()<<endl;
172 // If we only needed to go through subtree, we are done now
173 if (start==current) return NULL;
175 // Go up and try to find siblings of current
176 // cout <<" going up again...\n";
178 current=(BranchItem*)current->parent();
180 // Check if we still can go somewhere
181 if (!current) return current;
183 while (current && current->depth() < previous->depth() )
184 current=next (current,previous,start);
189 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
191 int last=row+count-1;
192 TreeItem *pi= getItem (parent);
195 for (int i=row; i<=last; i++)
197 ti=pi->getChildNum (row);
198 pi->removeChild (row); // does not delete object!
199 switch (ti->getType())
201 case TreeItem::MapCenter:
202 delete (BranchItem*)ti;
204 case TreeItem::Branch:
205 delete (BranchItem*)ti;
207 case TreeItem::Image:
208 delete (ImageItem*)ti;
218 TreeItem *TreeModel::getItem(const QModelIndex &index) const
220 if (index.isValid()) {
221 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
223 if (item) return item;
228 TreeItem *TreeModel::getRootItem()
233 QModelIndex TreeModel::index (TreeItem* ti)
235 return createIndex (ti->row(),ti->column(),ti);