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 do this to avoid segfault, but why?
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 TreeItem* TreeModel::next(TreeItem* ¤t, TreeItem* &previous, int &d0)
112 // Walk through map beginning at current with previous==0
113 // Start at root, if current==NULL
114 if (!current) current=rootItem;
116 // Are we just beginning to walk the map?
121 current=current->getFirstBranch();
125 //std::cout << " cur="<<current->getHeading().toStdString();
126 //std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
128 // Going up or down (deeper)?
129 if (current->depth() > previous->depth() )
132 // Trying to go down deeper
133 if (current->branchCount() >0 )
136 current=current->getFirstBranch();
139 // turn around and go up again
142 // Coming from below,
143 // Trying to go down again to siblings
145 TreeItem *sibling=current->getBranchNum (previous->num()+1);
149 // Found sibling of previous, go there
155 // Go up and try to find siblings of current
157 current=current->parent();
159 // Check if we still can go somewhere
160 if (!current) return current;
162 while (current && current->depth() < previous->depth() )
163 next (current,previous,d0);
168 bool TreeModel::insertRows ( int row, int count, const QModelIndex & parent)
170 std::cout << "TreeModel insertRows()\n";
171 int last=row+count-1;
172 beginInsertRows (parent,row,last);
174 for (int i=row; i<=last; i++)
176 std::cout << "TreeModel::insertRows inserting i="<<i<<std::endl;
182 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
184 int last=row+count-1;
185 cout << "TreeModel::removeRows row="<<row<<" count="<<count<<" last="<<last<<endl;
186 TreeItem *pi= getItem (parent);
187 cout << " pi="<<pi<<" "<<pi->getHeading().toStdString()<<endl;
189 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);