1.1 --- a/findresultmodel.cpp Tue Mar 02 13:59:19 2010 +0000
1.2 +++ b/findresultmodel.cpp Mon Mar 08 12:24:26 2010 +0000
1.3 @@ -2,23 +2,32 @@
1.4
1.5 #include "findresultitem.h"
1.6 #include "findresultmodel.h"
1.7 +#include "treeitem.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 + rootData << "Heading";
1.15
1.16 rootItem = new FindResultItem(rootData);
1.17 //setupModelData(data.split(QString("\n")), rootItem);
1.18 }
1.19
1.20 -
1.21 FindResultModel::~FindResultModel()
1.22 {
1.23 delete rootItem;
1.24 }
1.25
1.26 +void FindResultModel::clear()
1.27 +{
1.28 + if (rootItem->childCount()>0)
1.29 + {
1.30 + //QModelIndex ix=createIndex(0,0,rootItem);
1.31 + removeRows (0,rowCount (QModelIndex ()));
1.32 + }
1.33 +}
1.34 +
1.35 int FindResultModel::columnCount(const QModelIndex & /* parent */) const
1.36 {
1.37 return rootItem->columnCount();
1.38 @@ -63,6 +72,15 @@
1.39 return QVariant();
1.40 }
1.41
1.42 +QModelIndex FindResultModel::index (FindResultItem *fri)
1.43 +{
1.44 + if (!fri->parent())
1.45 + return QModelIndex();
1.46 + else
1.47 + return createIndex (fri->row(),0,fri);
1.48 +}
1.49 +
1.50 +
1.51 QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
1.52 {
1.53 if (parent.isValid() && parent.column() != 0)
1.54 @@ -176,54 +194,38 @@
1.55 return result;
1.56 }
1.57
1.58 -void FindResultModel::setupModelData(const QStringList &lines, FindResultItem *parent)
1.59 +void FindResultModel::addItem (TreeItem *ti)
1.60 {
1.61 - QList<FindResultItem*> parents;
1.62 - QList<int> indentations;
1.63 - parents << parent;
1.64 - indentations << 0;
1.65 + if (ti)
1.66 + {
1.67 + QModelIndex ix (index (rootItem));
1.68 + //QAbstractItemModel *resultsModel = view->model();
1.69 +
1.70 + if (!insertRow(ix.row()+1, ix.parent()))
1.71 + return;
1.72
1.73 - int number = 0;
1.74 + for (int column = 0; column < columnCount(ix.parent()); ++column) {
1.75 + QModelIndex child = index(ix.row()+1, column, ix.parent());
1.76 + setData(child, QVariant(ti->getHeading()), Qt::EditRole);
1.77 + getItem(child)->setOriginal (ti);
1.78 + }
1.79 + }
1.80 +}
1.81
1.82 - while (number < lines.count()) {
1.83 - int position = 0;
1.84 - while (position < lines[number].length()) {
1.85 - if (lines[number].mid(position, 1) != " ")
1.86 - break;
1.87 - position++;
1.88 - }
1.89 +void FindResultModel::addItem (const QString &s)
1.90 +{
1.91 + if (!s.isEmpty())
1.92 + {
1.93 + QModelIndex ix ( index (rootItem));
1.94 +
1.95 + if (!insertRow(ix.row()+1, ix.parent()))
1.96 + return;
1.97
1.98 - QString lineData = lines[number].mid(position).trimmed();
1.99 + for (int column = 0; column < columnCount(ix.parent()); ++column) {
1.100 + QModelIndex child = index(ix.row()+1, column, ix.parent());
1.101 + setData(child, QVariant(s), Qt::EditRole);
1.102 + }
1.103 + }
1.104 +}
1.105
1.106 - if (!lineData.isEmpty()) {
1.107 - // Read the column data from the rest of the line.
1.108 - QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
1.109 - QVector<QVariant> columnData;
1.110 - for (int column = 0; column < columnStrings.count(); ++column)
1.111 - columnData << columnStrings[column];
1.112
1.113 - if (position > indentations.last()) {
1.114 - // The last child of the current parent is now the new parent
1.115 - // unless the current parent has no children.
1.116 -
1.117 - if (parents.last()->childCount() > 0) {
1.118 - parents << parents.last()->child(parents.last()->childCount()-1);
1.119 - indentations << position;
1.120 - }
1.121 - } else {
1.122 - while (position < indentations.last() && parents.count() > 0) {
1.123 - parents.pop_back();
1.124 - indentations.pop_back();
1.125 - }
1.126 - }
1.127 -
1.128 - // Append a new item to the current parent's list of children.
1.129 - FindResultItem *parent = parents.last();
1.130 - parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
1.131 - for (int column = 0; column < columnData.size(); ++column)
1.132 - parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
1.133 - }
1.134 -
1.135 - number++;
1.136 - }
1.137 -}