9 TreeModel::TreeModel(QObject *parent)
10 : QAbstractItemModel(parent)
12 QList<QVariant> rootData;
13 rootData << "Heading" << "Type" <<"Note";
14 rootItem = new TreeItem(rootData);
17 TreeModel::~TreeModel()
22 QVariant TreeModel::data(const QModelIndex &index, int role) const
27 if (role != Qt::DisplayRole)
30 TreeItem *item = getItem (index);
32 return item->data(index.column());
35 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
38 return Qt::ItemIsEnabled;
40 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
43 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
46 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
47 return rootItem->data(section);
52 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
57 if (!parent.isValid())
58 parentItem = rootItem;
60 parentItem = getItem (parent);
62 TreeItem *childItem = parentItem->child(row);
64 return createIndex(row, column, childItem);
69 QModelIndex TreeModel::parent(const QModelIndex &index) const
74 TreeItem *ti= getItem (index);
75 TreeItem *parentItem = ti->parent();
77 //cout << "TreeModel::parent ti="<<ti<<" "<<ti->getHeading().toStdString()<<" pi="<<parentItem<<" "<<endl;
78 if (parentItem == rootItem)
83 cout <<"TreeModel::parent ti=="<<ti<<" "<<ti->getHeading().toStdString()<<endl;
84 return QModelIndex(); // FIXME-3 do this to avoid segfault, but why?
85 // see also my question on qt-interest in march
88 return createIndex(parentItem->childNumber(), 0, parentItem);
91 int TreeModel::rowCount(const QModelIndex &parent) const
95 if (!parent.isValid())
96 parentItem = rootItem;
98 parentItem = getItem (parent);
100 return parentItem->childCount();
103 int TreeModel::columnCount(const QModelIndex &parent) const
105 if (parent.isValid())
106 return getItem (parent)->columnCount();
108 return rootItem->columnCount();
111 TreeItem* TreeModel::next(TreeItem* ¤t, TreeItem* &previous, int &d0)
113 // Walk through map beginning at current with previous==0
114 // Start at root, if current==NULL
115 if (!current) current=rootItem;
117 // Are we just beginning to walk the map?
122 current=current->getFirstBranch();
126 //std::cout << " cur="<<current->getHeading().toStdString();
127 //std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
129 // Going up or down (deeper)?
130 if (current->depth() > previous->depth() )
133 // Trying to go down deeper
134 if (current->branchCount() >0 )
137 current=current->getFirstBranch();
140 // turn around and go up again
143 // Coming from below,
144 // Trying to go down again to siblings
146 TreeItem *sibling=current->getBranchNum (previous->num()+1);
150 // Found sibling of previous, go there
156 // Go up and try to find siblings of current
158 current=current->parent();
160 // Check if we still can go somewhere
161 if (!current) return current;
163 while (current && current->depth() < previous->depth() )
164 next (current,previous,d0);
169 bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
171 std::cout << "TreeModel insertRows()\n";
172 int last=row+count-1;
173 beginInsertRows (parent,row,last);
175 for (int i=row; i<=last; i++)
177 std::cout << "TreeModel::insertRows inserting i="<<i<<std::endl;
183 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
185 int last=row+count-1;
186 cout << "TreeModel::removeRows row="<<row<<" count="<<count<<" last="<<last<<endl;
187 TreeItem *pi= getItem (parent);
188 cout << " pi="<<pi<<" "<<pi->getHeading().toStdString()<<endl;
189 beginRemoveRows (parent,row,last);
191 for (int i=row; i<=last; i++)
193 cout << "TreeModel::removeRows removing i="<<i<<std::endl;
194 pi->removeChild (row);
200 TreeItem *TreeModel::getItem(const QModelIndex &index) const
202 if (index.isValid()) {
203 TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
205 if (item) return item;
210 QModelIndex TreeModel::index (TreeItem* ti)
212 return createIndex (ti->row(),ti->column(),ti);