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 = static_cast<TreeItem*>(index.internalPointer());
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 = static_cast<TreeItem*>(parent.internalPointer());
62 TreeItem *childItem = parentItem->child(row);
64 return createIndex(row, column, childItem);
69 QModelIndex TreeModel::parent(const QModelIndex &index) const
74 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer());
75 TreeItem *parentItem = childItem->parent();
77 if (parentItem == rootItem)
80 return createIndex(parentItem->row(), 0, parentItem);
83 int TreeModel::rowCount(const QModelIndex &parent) const
87 if (!parent.isValid())
88 parentItem = rootItem;
90 parentItem = static_cast<TreeItem*>(parent.internalPointer());
92 return parentItem->childCount();
95 int TreeModel::columnCount(const QModelIndex &parent) const
98 return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
100 return rootItem->columnCount();
103 TreeItem* TreeModel::next(TreeItem* ¤t, TreeItem* &previous, int &d0)
105 // Walk through map beginning at current with previous==0
106 // Start at root, if current==NULL
107 if (!current) current=rootItem;
109 // Are we just beginning to walk the map?
114 current=current->getFirstBranch();
118 //std::cout << " cur="<<current->getHeading().toStdString();
119 //std::cout << " prev="<<previous->getHeading().toStdString()<<std::endl;
121 // Going up or down (deeper)?
122 if (current->depth() > previous->depth() )
125 // Trying to go down deeper
126 if (current->branchCount() >0 )
129 current=current->getFirstBranch();
132 // turn around and go up again
135 // Coming from below,
136 // Trying to go down again to siblings
138 TreeItem *sibling=current->getBranchNum (previous->num()+1);
142 // Found sibling of previous, go there
148 // Go up and try to find siblings of current
150 current=current->parent();
152 // Check if we still can go somewhere
153 if (!current) return current;
155 while (current && current->depth() < previous->depth() )
156 next (current,previous,d0);
161 bool TreeModel::removeRows ( int row, int count, const QModelIndex & parent)
163 int last=row+count-1;
164 beginRemoveRows (parent,row,last);
166 TreeItem *pi= static_cast<TreeItem*>(parent.internalPointer());
167 for (int i=row; i<=last; i++)
169 std::cout << "TreeModel::removeRows removing i="<<i<<std::endl;
170 pi->removeChild (row);
177 QModelIndex TreeModel::index (TreeItem* ti)
179 return createIndex (ti->row(),ti->column(),ti);