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 QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
55 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
56 return rootItem->data(section);
61 QModelIndex FindResultModel::index (FindResultItem *fri)
66 return createIndex (fri->row(),0,fri);
70 QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
72 if (parent.isValid() && parent.column() != 0)
75 FindResultItem *parentItem = getItem(parent);
77 FindResultItem *childItem = parentItem->child(row);
79 return createIndex(row, column, childItem);
84 bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
88 beginInsertColumns(parent, position, position + columns - 1);
89 success = rootItem->insertColumns(position, columns);
95 bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
97 FindResultItem *parentItem = getItem(parent);
100 beginInsertRows(parent, position, position + rows - 1);
101 success = parentItem->insertChildren(position, rows, rootItem->columnCount());
107 QModelIndex FindResultModel::parent(const QModelIndex &index) const
109 if (!index.isValid())
110 return QModelIndex();
112 FindResultItem *childItem = getItem(index);
113 FindResultItem *parentItem = childItem->parent();
115 if (parentItem == rootItem)
116 return QModelIndex();
118 return createIndex(parentItem->childNumber(), 0, parentItem);
121 bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
125 beginRemoveColumns(parent, position, position + columns - 1);
126 success = rootItem->removeColumns(position, columns);
129 if (rootItem->columnCount() == 0)
130 removeRows(0, rowCount());
135 bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
137 FindResultItem *parentItem = getItem(parent);
140 beginRemoveRows(parent, position, position + rows - 1);
141 success = parentItem->removeChildren(position, rows);
147 int FindResultModel::rowCount(const QModelIndex &parent) const
149 FindResultItem *parentItem = getItem(parent);
151 return parentItem->childCount();
154 bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
157 if (role != Qt::EditRole)
160 FindResultItem *item = getItem(index);
161 bool result = item->setData(index.column(), value);
164 emit dataChanged(index, index);
169 bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
170 const QVariant &value, int role)
172 if (role != Qt::EditRole || orientation != Qt::Horizontal)
175 bool result = rootItem->setData(section, value);
178 emit headerDataChanged(orientation, section, section);
183 FindResultItem* FindResultModel::getItem(const QModelIndex &index) const
185 if (index.isValid()) {
186 FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
187 if (item) return item;
192 FindResultItem *FindResultModel::findTreeItem(TreeItem *)
194 return NULL; // FIXME-3 not used so far
197 FindResultItem* FindResultModel::addItem (TreeItem *ti)
199 FindResultItem *ni=NULL;
202 QModelIndex parix (index (rootItem));
204 emit (layoutAboutToBeChanged() );
206 int n=rowCount (parix);
207 beginInsertRows (parix,n,n);
208 if (rootItem->insertChildren (n,1,0) )
210 QModelIndex ix=index(n,0,QModelIndex());
211 setData (ix,QVariant(ti->getHeading()),Qt::EditRole);
213 ni->setOriginal (ti);
217 emit (layoutChanged() );
222 FindResultItem* FindResultModel::addSubItem (FindResultItem *parent,const QString &s, TreeItem *pi, int i)
224 FindResultItem *ni=NULL;
227 QModelIndex parix ( index (parent));
229 emit (layoutAboutToBeChanged() );
231 int n=rowCount (parix);
232 beginInsertRows (parix,n,n);
235 if (parent->insertChildren (n,1,0))
238 setData (ix,QVariant(s),Qt::EditRole);
240 ni->setOriginal (pi);
241 ni->setOriginalIndex (i);
244 emit (layoutChanged() );
249 void FindResultModel::setSearchString( const QString &s)
254 QString FindResultModel::getSearchString()
259 void FindResultModel::setSearchFlags( QTextDocument::FindFlags f)
264 QTextDocument::FindFlags FindResultModel::getSearchFlags()