6 #include "branchitem.h"
10 TreeModel::TreeModel(QObject *parent)
11 : QAbstractItemModel(parent)
13 QList<QVariant> rootData;
14 rootData << "Heading" << "Type" <<"Note";
15 rootItem = new TreeItem(rootData);
18 TreeModel::~TreeModel()
23 QVariant TreeModel::data(const QModelIndex &index, int role) const
28 if (role != Qt::DisplayRole)
31 TreeItem *item = getItem (index);
33 return item->data(index.column());
36 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
39 return Qt::ItemIsEnabled;
41 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
44 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
47 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
48 return rootItem->data(section);
53 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
58 if (!parent.isValid())
59 parentItem = rootItem;
61 parentItem = getItem (parent);
63 TreeItem *childItem = parentItem->child(row);
65 return createIndex(row, column, childItem);
70 QModelIndex TreeModel::parent(const QModelIndex &index) const
75 TreeItem *ti= getItem (index);
76 TreeItem *parentItem = ti->parent();
78 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
79 if (parentItem == rootItem)
84 cout <<"TreeModel::parent ti=="<<ti<<" "<<ti->getHeading().toStdString()<<endl;
85 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
86 // see also my question on qt-interest in march
89 return createIndex(parentItem->childNumber(), 0, parentItem);
92 int TreeModel::rowCount(const QModelIndex &parent) const
96 if (!parent.isValid())
97 parentItem = rootItem;
99 parentItem = getItem (parent);
101 return parentItem->childCount();
104 int TreeModel::columnCount(const QModelIndex &parent) const
106 if (parent.isValid())
107 return getItem (parent)->columnCount();
109 return rootItem->columnCount();
112 BranchItem* TreeModel::next(BranchItem* ¤t, BranchItem* &previous, int &d0)
114 // Walk through map beginning at current with previous==0
115 // Start at root, if current==NULL
116 if (!current) current=(BranchItem*)rootItem;
118 // Are we just beginning to walk the map?
123 current=current->getFirstBranch();
127 //std::cout << " cur="<<current->getHeading().toStdString();
128 //std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
130 // Going up or down (deeper)?
131 if (current->depth() > previous->depth() )
134 // Trying to go down deeper
135 if (current->branchCount() >0 )
138 current=current->getFirstBranch();
141 // turn around and go up again
144 // Coming from below,
145 // Trying to go down again to siblings
147 BranchItem *sibling=current->getBranchNum (previous->num()+1);
151 // Found sibling of previous, go there
157 // Go up and try to find siblings of current
159 current=(BranchItem*)current->parent();
161 // Check if we still can go somewhere
162 if (!current) return current;
164 while (current && current->depth() < previous->depth() )
165 next (current,previous,d0);
170 bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
172 std::cout << "TreeModel insertRows()\n";
173 int last=row+count-1;
174 beginInsertRows (parent,row,last);
176 for (int i=row; i<=last; i++)
178 std::cout << "TreeModel::insertRows inserting i="<<i<<std::endl;
184 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
186 int last=row+count-1;
187 cout << "TreeModel::removeRows row="<<row<<" count="<<count<<" last="<<last<<endl;
188 TreeItem *pi= getItem (parent);
189 cout << " pi="<<pi<<" "<<pi->getHeading().toStdString()<<endl;
190 beginRemoveRows (parent,row,last);
192 for (int i=row; i<=last; i++)
194 cout << "TreeModel::removeRows removing i="<<i<<std::endl;
195 pi->removeChild (row);
201 TreeItem *TreeModel::getItem(const QModelIndex &index) const
203 if (index.isValid()) {
204 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
206 if (item) return item;
211 QModelIndex TreeModel::index (TreeItem* ti)
213 return createIndex (ti->row(),ti->column(),ti);