3 #include "findresultitem.h"
4 #include "findresultmodel.h"
6 FindResultModel::FindResultModel( QObject *parent)
7 : QAbstractItemModel(parent)
9 QVector<QVariant> rootData;
10 rootData << "Foo"<<"bar";
12 rootItem = new FindResultItem(rootData);
13 //setupModelData(data.split(QString("\n")), rootItem);
17 FindResultModel::~FindResultModel()
22 int FindResultModel::columnCount(const QModelIndex & /* parent */) const
24 return rootItem->columnCount();
27 QVariant FindResultModel::data(const QModelIndex &index, int role) const
32 if (role != Qt::DisplayRole && role != Qt::EditRole)
35 FindResultItem *item = getItem(index);
37 return item->data(index.column());
40 Qt::ItemFlags FindResultModel::flags(const QModelIndex &index) const
45 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
48 FindResultItem *FindResultModel::getItem(const QModelIndex &index) const
50 if (index.isValid()) {
51 FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
52 if (item) return item;
57 QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
60 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
61 return rootItem->data(section);
66 QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
68 if (parent.isValid() && parent.column() != 0)
71 FindResultItem *parentItem = getItem(parent);
73 FindResultItem *childItem = parentItem->child(row);
75 return createIndex(row, column, childItem);
80 bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
84 beginInsertColumns(parent, position, position + columns - 1);
85 success = rootItem->insertColumns(position, columns);
91 bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
93 FindResultItem *parentItem = getItem(parent);
96 beginInsertRows(parent, position, position + rows - 1);
97 success = parentItem->insertChildren(position, rows, rootItem->columnCount());
103 QModelIndex FindResultModel::parent(const QModelIndex &index) const
105 if (!index.isValid())
106 return QModelIndex();
108 FindResultItem *childItem = getItem(index);
109 FindResultItem *parentItem = childItem->parent();
111 if (parentItem == rootItem)
112 return QModelIndex();
114 return createIndex(parentItem->childNumber(), 0, parentItem);
117 bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
121 beginRemoveColumns(parent, position, position + columns - 1);
122 success = rootItem->removeColumns(position, columns);
125 if (rootItem->columnCount() == 0)
126 removeRows(0, rowCount());
131 bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
133 FindResultItem *parentItem = getItem(parent);
136 beginRemoveRows(parent, position, position + rows - 1);
137 success = parentItem->removeChildren(position, rows);
143 int FindResultModel::rowCount(const QModelIndex &parent) const
145 FindResultItem *parentItem = getItem(parent);
147 return parentItem->childCount();
150 bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
153 if (role != Qt::EditRole)
156 FindResultItem *item = getItem(index);
157 bool result = item->setData(index.column(), value);
160 emit dataChanged(index, index);
165 bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
166 const QVariant &value, int role)
168 if (role != Qt::EditRole || orientation != Qt::Horizontal)
171 bool result = rootItem->setData(section, value);
174 emit headerDataChanged(orientation, section, section);
179 void FindResultModel::setupModelData(const QStringList &lines, FindResultItem *parent)
181 QList<FindResultItem*> parents;
182 QList<int> indentations;
188 while (number < lines.count()) {
190 while (position < lines[number].length()) {
191 if (lines[number].mid(position, 1) != " ")
196 QString lineData = lines[number].mid(position).trimmed();
198 if (!lineData.isEmpty()) {
199 // Read the column data from the rest of the line.
200 QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
201 QVector<QVariant> columnData;
202 for (int column = 0; column < columnStrings.count(); ++column)
203 columnData << columnStrings[column];
205 if (position > indentations.last()) {
206 // The last child of the current parent is now the new parent
207 // unless the current parent has no children.
209 if (parents.last()->childCount() > 0) {
210 parents << parents.last()->child(parents.last()->childCount()-1);
211 indentations << position;
214 while (position < indentations.last() && parents.count() > 0) {
216 indentations.pop_back();
220 // Append a new item to the current parent's list of children.
221 FindResultItem *parent = parents.last();
222 parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
223 for (int column = 0; column < columnData.size(); ++column)
224 parent->child(parent->childCount() - 1)->setData(column, columnData[column]);