3 #include "findresultitem.h"
4 #include "findresultmodel.h"
7 FindResultModel::FindResultModel( QObject *parent)
8 : QAbstractItemModel(parent)
10 QVector<QVariant> rootData;
11 rootData << "Heading";
12 rootItem = new FindResultItem(rootData);
15 FindResultModel::~FindResultModel()
20 void FindResultModel::clear()
22 if (rootItem->childCount()>0)
23 removeRows (0,rowCount (QModelIndex ()));
26 int FindResultModel::columnCount(const QModelIndex & /* parent */) const
28 return rootItem->columnCount();
31 QVariant FindResultModel::data(const QModelIndex &index, int role) const
36 if (role != Qt::DisplayRole && role != Qt::EditRole)
39 FindResultItem *item = getItem(index);
41 return item->data(index.column());
44 Qt::ItemFlags FindResultModel::flags(const QModelIndex &index) const
49 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
52 FindResultItem *FindResultModel::getItem(const QModelIndex &index) const
54 if (index.isValid()) {
55 FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
56 if (item) return item;
61 QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
64 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
65 return rootItem->data(section);
70 QModelIndex FindResultModel::index (FindResultItem *fri)
75 return createIndex (fri->row(),0,fri);
79 QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
81 if (parent.isValid() && parent.column() != 0)
84 FindResultItem *parentItem = getItem(parent);
86 FindResultItem *childItem = parentItem->child(row);
88 return createIndex(row, column, childItem);
93 bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
97 beginInsertColumns(parent, position, position + columns - 1);
98 success = rootItem->insertColumns(position, columns);
104 bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
106 FindResultItem *parentItem = getItem(parent);
109 beginInsertRows(parent, position, position + rows - 1);
110 success = parentItem->insertChildren(position, rows, rootItem->columnCount());
116 QModelIndex FindResultModel::parent(const QModelIndex &index) const
118 if (!index.isValid())
119 return QModelIndex();
121 FindResultItem *childItem = getItem(index);
122 FindResultItem *parentItem = childItem->parent();
124 if (parentItem == rootItem)
125 return QModelIndex();
127 return createIndex(parentItem->childNumber(), 0, parentItem);
130 bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
134 beginRemoveColumns(parent, position, position + columns - 1);
135 success = rootItem->removeColumns(position, columns);
138 if (rootItem->columnCount() == 0)
139 removeRows(0, rowCount());
144 bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
146 FindResultItem *parentItem = getItem(parent);
149 beginRemoveRows(parent, position, position + rows - 1);
150 success = parentItem->removeChildren(position, rows);
156 int FindResultModel::rowCount(const QModelIndex &parent) const
158 FindResultItem *parentItem = getItem(parent);
160 return parentItem->childCount();
163 bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
166 if (role != Qt::EditRole)
169 FindResultItem *item = getItem(index);
170 bool result = item->setData(index.column(), value);
173 emit dataChanged(index, index);
178 bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
179 const QVariant &value, int role)
181 if (role != Qt::EditRole || orientation != Qt::Horizontal)
184 bool result = rootItem->setData(section, value);
187 emit headerDataChanged(orientation, section, section);
192 void FindResultModel::addItem (TreeItem *ti)
196 QModelIndex ix (index (rootItem));
197 //QAbstractItemModel *resultsModel = view->model();
199 if (!insertRow(ix.row()+1, ix.parent()))
202 for (int column = 0; column < columnCount(ix.parent()); ++column) {
203 QModelIndex child = index(ix.row()+1, column, ix.parent());
204 setData(child, QVariant(ti->getHeading()), Qt::EditRole);
205 getItem(child)->setOriginal (ti);
210 void FindResultModel::addItem (const QString &s)
214 QModelIndex ix ( index (rootItem));
216 if (!insertRow(ix.row()+1, ix.parent()))
219 for (int column = 0; column < columnCount(ix.parent()); ++column) {
220 QModelIndex child = index(ix.row()+1, column, ix.parent());
221 setData(child, QVariant(s), Qt::EditRole);
226 void FindResultModel::addSubItem (TreeItem *parent,const QString &s, TreeItem *ti, int index)