findresultmodel.cpp
author insilmaril
Tue, 02 Mar 2010 13:59:19 +0000
changeset 825 1ad892c1a709
child 829 832e96c9abb6
permissions -rw-r--r--
Added file for later dockwidget showing all search results at once
     1 #include <QtGui>
     2 
     3 #include "findresultitem.h"
     4 #include "findresultmodel.h"
     5 
     6 FindResultModel::FindResultModel( QObject *parent)
     7     : QAbstractItemModel(parent)
     8 {
     9     QVector<QVariant> rootData;
    10 	rootData << "Foo"<<"bar";
    11 
    12     rootItem = new FindResultItem(rootData);
    13     //setupModelData(data.split(QString("\n")), rootItem);
    14 }
    15 
    16 
    17 FindResultModel::~FindResultModel()
    18 {
    19     delete rootItem;
    20 }
    21 
    22 int FindResultModel::columnCount(const QModelIndex & /* parent */) const
    23 {
    24     return rootItem->columnCount();
    25 }
    26 
    27 QVariant FindResultModel::data(const QModelIndex &index, int role) const
    28 {
    29     if (!index.isValid())
    30         return QVariant();
    31 
    32     if (role != Qt::DisplayRole && role != Qt::EditRole)
    33         return QVariant();
    34 
    35     FindResultItem *item = getItem(index);
    36 
    37     return item->data(index.column());
    38 }
    39 
    40 Qt::ItemFlags FindResultModel::flags(const QModelIndex &index) const
    41 {
    42     if (!index.isValid())
    43         return 0;
    44 
    45     return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    46 }
    47 
    48 FindResultItem *FindResultModel::getItem(const QModelIndex &index) const
    49 {
    50     if (index.isValid()) {
    51         FindResultItem *item = static_cast<FindResultItem*>(index.internalPointer());
    52         if (item) return item;
    53     }
    54     return rootItem;
    55 }
    56 
    57 QVariant FindResultModel::headerData(int section, Qt::Orientation orientation,
    58                                int role) const
    59 {
    60     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    61         return rootItem->data(section);
    62 
    63     return QVariant();
    64 }
    65 
    66 QModelIndex FindResultModel::index(int row, int column, const QModelIndex &parent) const
    67 {
    68     if (parent.isValid() && parent.column() != 0)
    69         return QModelIndex();
    70 
    71     FindResultItem *parentItem = getItem(parent);
    72 
    73     FindResultItem *childItem = parentItem->child(row);
    74     if (childItem)
    75         return createIndex(row, column, childItem);
    76     else
    77         return QModelIndex();
    78 }
    79 
    80 bool FindResultModel::insertColumns(int position, int columns, const QModelIndex &parent)
    81 {
    82     bool success;
    83 
    84     beginInsertColumns(parent, position, position + columns - 1);
    85     success = rootItem->insertColumns(position, columns);
    86     endInsertColumns();
    87 
    88     return success;
    89 }
    90 
    91 bool FindResultModel::insertRows(int position, int rows, const QModelIndex &parent)
    92 {
    93     FindResultItem *parentItem = getItem(parent);
    94     bool success;
    95 
    96     beginInsertRows(parent, position, position + rows - 1);
    97     success = parentItem->insertChildren(position, rows, rootItem->columnCount());
    98     endInsertRows();
    99 
   100     return success;
   101 }
   102 
   103 QModelIndex FindResultModel::parent(const QModelIndex &index) const
   104 {
   105     if (!index.isValid())
   106         return QModelIndex();
   107 
   108     FindResultItem *childItem = getItem(index);
   109     FindResultItem *parentItem = childItem->parent();
   110 
   111     if (parentItem == rootItem)
   112         return QModelIndex();
   113 
   114     return createIndex(parentItem->childNumber(), 0, parentItem);
   115 }
   116 
   117 bool FindResultModel::removeColumns(int position, int columns, const QModelIndex &parent)
   118 {
   119     bool success;
   120 
   121     beginRemoveColumns(parent, position, position + columns - 1);
   122     success = rootItem->removeColumns(position, columns);
   123     endRemoveColumns();
   124 
   125     if (rootItem->columnCount() == 0)
   126         removeRows(0, rowCount());
   127 
   128     return success;
   129 }
   130 
   131 bool FindResultModel::removeRows(int position, int rows, const QModelIndex &parent)
   132 {
   133     FindResultItem *parentItem = getItem(parent);
   134     bool success = true;
   135 
   136     beginRemoveRows(parent, position, position + rows - 1);
   137     success = parentItem->removeChildren(position, rows);
   138     endRemoveRows();
   139 
   140     return success;
   141 }
   142 
   143 int FindResultModel::rowCount(const QModelIndex &parent) const
   144 {
   145     FindResultItem *parentItem = getItem(parent);
   146 
   147     return parentItem->childCount();
   148 }
   149 
   150 bool FindResultModel::setData(const QModelIndex &index, const QVariant &value,
   151                         int role)
   152 {
   153     if (role != Qt::EditRole)
   154         return false;
   155 
   156     FindResultItem *item = getItem(index);
   157     bool result = item->setData(index.column(), value);
   158 
   159     if (result)
   160         emit dataChanged(index, index);
   161 
   162     return result;
   163 }
   164 
   165 bool FindResultModel::setHeaderData(int section, Qt::Orientation orientation,
   166                               const QVariant &value, int role)
   167 {
   168     if (role != Qt::EditRole || orientation != Qt::Horizontal)
   169         return false;
   170 
   171     bool result = rootItem->setData(section, value);
   172 
   173     if (result)
   174         emit headerDataChanged(orientation, section, section);
   175 
   176     return result;
   177 }
   178 
   179 void FindResultModel::setupModelData(const QStringList &lines, FindResultItem *parent)
   180 {
   181     QList<FindResultItem*> parents;
   182     QList<int> indentations;
   183     parents << parent;
   184     indentations << 0;
   185 
   186     int number = 0;
   187 
   188     while (number < lines.count()) {
   189         int position = 0;
   190         while (position < lines[number].length()) {
   191             if (lines[number].mid(position, 1) != " ")
   192                 break;
   193             position++;
   194         }
   195 
   196         QString lineData = lines[number].mid(position).trimmed();
   197 
   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];
   204 
   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.
   208 
   209                 if (parents.last()->childCount() > 0) {
   210                     parents << parents.last()->child(parents.last()->childCount()-1);
   211                     indentations << position;
   212                 }
   213             } else {
   214                 while (position < indentations.last() && parents.count() > 0) {
   215                     parents.pop_back();
   216                     indentations.pop_back();
   217                 }
   218             }
   219 
   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]);
   225         }
   226 
   227         number++;
   228     }
   229 }