1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/findresultmodel.cpp Tue Mar 02 13:59:19 2010 +0000
1.3 @@ -0,0 +1,229 @@
1.4 +#include <QtGui>
1.5 +
1.6 +#include "findresultitem.h"
1.7 +#include "findresultmodel.h"
1.8 +
1.9 +FindResultModel::FindResultModel( QObject *parent)
1.10 + : QAbstractItemModel(parent)
1.11 +{
1.12 + QVector<QVariant> rootData;
1.13 + rootData << "Foo"<<"bar";
1.14 +
1.15 + rootItem = new FindResultItem(rootData);
1.16 + //setupModelData(data.split(QString("\n")), rootItem);
1.17 +}
1.18 +
1.19 +
1.20 +FindResultModel::~FindResultModel()
1.21 +{
1.22 + delete rootItem;
1.23 +}
1.24 +
1.25 +int FindResultModel::columnCount(const QModelIndex & /* parent */) const
1.26 +{
1.27 + return rootItem->columnCount();
1.28 +}
1.29 +
1.30 +QVariant FindResultModel::data(const QModelIndex &index, int role) const
1.31 +{
1.32 + if (!index.isValid())
1.33 + return QVariant();
1.34 +
1.35 + if (role != Qt::DisplayRole && role != Qt::EditRole)
1.36 + return QVariant();
1.37 +
1.38 + FindResultItem *item = getItem(index);
1.39 +
1.40 + return item->data(index.column());
1.41 +}
1.42 +
1.43 +Qt::ItemFlags FindResultModel::flags(const QModelIndex &index) const
1.44 +{
1.45 + if (!index.isValid())
1.46 + return 0;
1.47 +
1.48 + return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
1.49 +}
1.50 +
1.51 +FindResultItem *FindResultModel::getItem(const QModelIndex &index) const
1.52 +{
1.53 + if (index.isValid()) {
1.54 + FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
1.55 + if (item) return item;
1.56 + }
1.57 + return rootItem;
1.58 +}
1.59 +
1.60 +QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
1.61 + int role) const
1.62 +{
1.63 + if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
1.64 + return rootItem->data(section);
1.65 +
1.66 + return QVariant();
1.67 +}
1.68 +
1.69 +QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
1.70 +{
1.71 + if (parent.isValid() && parent.column() != 0)
1.72 + return QModelIndex();
1.73 +
1.74 + FindResultItem *parentItem = getItem(parent);
1.75 +
1.76 + FindResultItem *childItem = parentItem->child(row);
1.77 + if (childItem)
1.78 + return createIndex(row, column, childItem);
1.79 + else
1.80 + return QModelIndex();
1.81 +}
1.82 +
1.83 +bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
1.84 +{
1.85 + bool success;
1.86 +
1.87 + beginInsertColumns(parent, position, position + columns - 1);
1.88 + success = rootItem->insertColumns(position, columns);
1.89 + endInsertColumns();
1.90 +
1.91 + return success;
1.92 +}
1.93 +
1.94 +bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
1.95 +{
1.96 + FindResultItem *parentItem = getItem(parent);
1.97 + bool success;
1.98 +
1.99 + beginInsertRows(parent, position, position + rows - 1);
1.100 + success = parentItem->insertChildren(position, rows, rootItem->columnCount());
1.101 + endInsertRows();
1.102 +
1.103 + return success;
1.104 +}
1.105 +
1.106 +QModelIndex FindResultModel::parent(const QModelIndex &index) const
1.107 +{
1.108 + if (!index.isValid())
1.109 + return QModelIndex();
1.110 +
1.111 + FindResultItem *childItem = getItem(index);
1.112 + FindResultItem *parentItem = childItem->parent();
1.113 +
1.114 + if (parentItem == rootItem)
1.115 + return QModelIndex();
1.116 +
1.117 + return createIndex(parentItem->childNumber(), 0, parentItem);
1.118 +}
1.119 +
1.120 +bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
1.121 +{
1.122 + bool success;
1.123 +
1.124 + beginRemoveColumns(parent, position, position + columns - 1);
1.125 + success = rootItem->removeColumns(position, columns);
1.126 + endRemoveColumns();
1.127 +
1.128 + if (rootItem->columnCount() == 0)
1.129 + removeRows(0, rowCount());
1.130 +
1.131 + return success;
1.132 +}
1.133 +
1.134 +bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
1.135 +{
1.136 + FindResultItem *parentItem = getItem(parent);
1.137 + bool success = true;
1.138 +
1.139 + beginRemoveRows(parent, position, position + rows - 1);
1.140 + success = parentItem->removeChildren(position, rows);
1.141 + endRemoveRows();
1.142 +
1.143 + return success;
1.144 +}
1.145 +
1.146 +int FindResultModel::rowCount(const QModelIndex &parent) const
1.147 +{
1.148 + FindResultItem *parentItem = getItem(parent);
1.149 +
1.150 + return parentItem->childCount();
1.151 +}
1.152 +
1.153 +bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
1.154 + int role)
1.155 +{
1.156 + if (role != Qt::EditRole)
1.157 + return false;
1.158 +
1.159 + FindResultItem *item = getItem(index);
1.160 + bool result = item->setData(index.column(), value);
1.161 +
1.162 + if (result)
1.163 + emit dataChanged(index, index);
1.164 +
1.165 + return result;
1.166 +}
1.167 +
1.168 +bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
1.169 + const QVariant &value, int role)
1.170 +{
1.171 + if (role != Qt::EditRole || orientation != Qt::Horizontal)
1.172 + return false;
1.173 +
1.174 + bool result = rootItem->setData(section, value);
1.175 +
1.176 + if (result)
1.177 + emit headerDataChanged(orientation, section, section);
1.178 +
1.179 + return result;
1.180 +}
1.181 +
1.182 +void FindResultModel::setupModelData(const QStringList &lines, FindResultItem *parent)
1.183 +{
1.184 + QList<FindResultItem*> parents;
1.185 + QList<int> indentations;
1.186 + parents << parent;
1.187 + indentations << 0;
1.188 +
1.189 + int number = 0;
1.190 +
1.191 + while (number < lines.count()) {
1.192 + int position = 0;
1.193 + while (position < lines[number].length()) {
1.194 + if (lines[number].mid(position, 1) != " ")
1.195 + break;
1.196 + position++;
1.197 + }
1.198 +
1.199 + QString lineData = lines[number].mid(position).trimmed();
1.200 +
1.201 + if (!lineData.isEmpty()) {
1.202 + // Read the column data from the rest of the line.
1.203 + QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
1.204 + QVector<QVariant> columnData;
1.205 + for (int column = 0; column < columnStrings.count(); ++column)
1.206 + columnData << columnStrings[column];
1.207 +
1.208 + if (position > indentations.last()) {
1.209 + // The last child of the current parent is now the new parent
1.210 + // unless the current parent has no children.
1.211 +
1.212 + if (parents.last()->childCount() > 0) {
1.213 + parents << parents.last()->child(parents.last()->childCount()-1);
1.214 + indentations << position;
1.215 + }
1.216 + } else {
1.217 + while (position < indentations.last() && parents.count() > 0) {
1.218 + parents.pop_back();
1.219 + indentations.pop_back();
1.220 + }
1.221 + }
1.222 +
1.223 + // Append a new item to the current parent's list of children.
1.224 + FindResultItem *parent = parents.last();
1.225 + parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
1.226 + for (int column = 0; column < columnData.size(); ++column)
1.227 + parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
1.228 + }
1.229 +
1.230 + number++;
1.231 + }
1.232 +}