diff -r fac2248e2afa -r 832e96c9abb6 findresultmodel.cpp --- a/findresultmodel.cpp Mon Mar 08 12:22:15 2010 +0000 +++ b/findresultmodel.cpp Mon Mar 08 12:24:26 2010 +0000 @@ -2,23 +2,32 @@ #include "findresultitem.h" #include "findresultmodel.h" +#include "treeitem.h" FindResultModel::FindResultModel( QObject *parent) : QAbstractItemModel(parent) { QVector rootData; - rootData << "Foo"<<"bar"; + rootData << "Heading"; rootItem = new FindResultItem(rootData); //setupModelData(data.split(QString("\n")), rootItem); } - FindResultModel::~FindResultModel() { delete rootItem; } +void FindResultModel::clear() +{ + if (rootItem->childCount()>0) + { + //QModelIndex ix=createIndex(0,0,rootItem); + removeRows (0,rowCount (QModelIndex ())); + } +} + int FindResultModel::columnCount(const QModelIndex & /* parent */) const { return rootItem->columnCount(); @@ -63,6 +72,15 @@ return QVariant(); } +QModelIndex FindResultModel::index (FindResultItem *fri) +{ + if (!fri->parent()) + return QModelIndex(); + else + return createIndex (fri->row(),0,fri); +} + + QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) @@ -176,54 +194,38 @@ return result; } -void FindResultModel::setupModelData(const QStringList &lines, FindResultItem *parent) +void FindResultModel::addItem (TreeItem *ti) { - QList parents; - QList indentations; - parents << parent; - indentations << 0; + if (ti) + { + QModelIndex ix (index (rootItem)); + //QAbstractItemModel *resultsModel = view->model(); + + if (!insertRow(ix.row()+1, ix.parent())) + return; - int number = 0; + for (int column = 0; column < columnCount(ix.parent()); ++column) { + QModelIndex child = index(ix.row()+1, column, ix.parent()); + setData(child, QVariant(ti->getHeading()), Qt::EditRole); + getItem(child)->setOriginal (ti); + } + } +} - while (number < lines.count()) { - int position = 0; - while (position < lines[number].length()) { - if (lines[number].mid(position, 1) != " ") - break; - position++; - } +void FindResultModel::addItem (const QString &s) +{ + if (!s.isEmpty()) + { + QModelIndex ix ( index (rootItem)); + + if (!insertRow(ix.row()+1, ix.parent())) + return; - QString lineData = lines[number].mid(position).trimmed(); + for (int column = 0; column < columnCount(ix.parent()); ++column) { + QModelIndex child = index(ix.row()+1, column, ix.parent()); + setData(child, QVariant(s), Qt::EditRole); + } + } +} - if (!lineData.isEmpty()) { - // Read the column data from the rest of the line. - QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); - QVector columnData; - for (int column = 0; column < columnStrings.count(); ++column) - columnData << columnStrings[column]; - if (position > indentations.last()) { - // The last child of the current parent is now the new parent - // unless the current parent has no children. - - if (parents.last()->childCount() > 0) { - parents << parents.last()->child(parents.last()->childCount()-1); - indentations << position; - } - } else { - while (position < indentations.last() && parents.count() > 0) { - parents.pop_back(); - indentations.pop_back(); - } - } - - // Append a new item to the current parent's list of children. - FindResultItem *parent = parents.last(); - parent->insertChildren(parent->childCount(), 1, rootItem->columnCount()); - for (int column = 0; column < columnData.size(); ++column) - parent->child(parent->childCount() - 1)->setData(column, columnData[column]); - } - - number++; - } -}