[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[2] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "treeitem.h"
|
---|
| 44 | #include "treemodel.h"
|
---|
| 45 |
|
---|
| 46 | //! [0]
|
---|
| 47 | TreeModel::TreeModel(const QStringList &headers, const QString &data,
|
---|
| 48 | QObject *parent)
|
---|
| 49 | : QAbstractItemModel(parent)
|
---|
| 50 | {
|
---|
| 51 | QVector<QVariant> rootData;
|
---|
| 52 | foreach (QString header, headers)
|
---|
| 53 | rootData << header;
|
---|
| 54 |
|
---|
| 55 | rootItem = new TreeItem(rootData);
|
---|
| 56 | setupModelData(data.split(QString("\n")), rootItem);
|
---|
| 57 | }
|
---|
| 58 | //! [0]
|
---|
| 59 |
|
---|
| 60 | //! [1]
|
---|
| 61 | TreeModel::~TreeModel()
|
---|
| 62 | {
|
---|
| 63 | delete rootItem;
|
---|
| 64 | }
|
---|
| 65 | //! [1]
|
---|
| 66 |
|
---|
| 67 | //! [2]
|
---|
| 68 | int TreeModel::columnCount(const QModelIndex & /* parent */) const
|
---|
| 69 | {
|
---|
| 70 | return rootItem->columnCount();
|
---|
| 71 | }
|
---|
| 72 | //! [2]
|
---|
| 73 |
|
---|
| 74 | QVariant TreeModel::data(const QModelIndex &index, int role) const
|
---|
| 75 | {
|
---|
| 76 | if (!index.isValid())
|
---|
| 77 | return QVariant();
|
---|
| 78 |
|
---|
| 79 | if (role != Qt::DisplayRole && role != Qt::EditRole)
|
---|
| 80 | return QVariant();
|
---|
| 81 |
|
---|
| 82 | TreeItem *item = getItem(index);
|
---|
| 83 |
|
---|
| 84 | return item->data(index.column());
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | //! [3]
|
---|
| 88 | Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
|
---|
| 89 | {
|
---|
| 90 | if (!index.isValid())
|
---|
| 91 | return 0;
|
---|
| 92 |
|
---|
| 93 | return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
---|
| 94 | }
|
---|
| 95 | //! [3]
|
---|
| 96 |
|
---|
| 97 | //! [4]
|
---|
| 98 | TreeItem *TreeModel::getItem(const QModelIndex &index) const
|
---|
| 99 | {
|
---|
| 100 | if (index.isValid()) {
|
---|
| 101 | TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
|
---|
| 102 | if (item) return item;
|
---|
| 103 | }
|
---|
| 104 | return rootItem;
|
---|
| 105 | }
|
---|
| 106 | //! [4]
|
---|
| 107 |
|
---|
| 108 | QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
|
---|
| 109 | int role) const
|
---|
| 110 | {
|
---|
| 111 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
---|
| 112 | return rootItem->data(section);
|
---|
| 113 |
|
---|
| 114 | return QVariant();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | //! [5]
|
---|
| 118 | QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
|
---|
| 119 | {
|
---|
| 120 | if (parent.isValid() && parent.column() != 0)
|
---|
| 121 | return QModelIndex();
|
---|
| 122 | //! [5]
|
---|
| 123 |
|
---|
| 124 | //! [6]
|
---|
| 125 | TreeItem *parentItem = getItem(parent);
|
---|
| 126 |
|
---|
| 127 | TreeItem *childItem = parentItem->child(row);
|
---|
| 128 | if (childItem)
|
---|
| 129 | return createIndex(row, column, childItem);
|
---|
| 130 | else
|
---|
| 131 | return QModelIndex();
|
---|
| 132 | }
|
---|
| 133 | //! [6]
|
---|
| 134 |
|
---|
| 135 | bool TreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
|
---|
| 136 | {
|
---|
| 137 | bool success;
|
---|
| 138 |
|
---|
| 139 | beginInsertColumns(parent, position, position + columns - 1);
|
---|
| 140 | success = rootItem->insertColumns(position, columns);
|
---|
| 141 | endInsertColumns();
|
---|
| 142 |
|
---|
| 143 | return success;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
|
---|
| 147 | {
|
---|
| 148 | TreeItem *parentItem = getItem(parent);
|
---|
| 149 | bool success;
|
---|
| 150 |
|
---|
| 151 | beginInsertRows(parent, position, position + rows - 1);
|
---|
| 152 | success = parentItem->insertChildren(position, rows, rootItem->columnCount());
|
---|
| 153 | endInsertRows();
|
---|
| 154 |
|
---|
| 155 | return success;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | //! [7]
|
---|
| 159 | QModelIndex TreeModel::parent(const QModelIndex &index) const
|
---|
| 160 | {
|
---|
| 161 | if (!index.isValid())
|
---|
| 162 | return QModelIndex();
|
---|
| 163 |
|
---|
| 164 | TreeItem *childItem = getItem(index);
|
---|
| 165 | TreeItem *parentItem = childItem->parent();
|
---|
| 166 |
|
---|
| 167 | if (parentItem == rootItem)
|
---|
| 168 | return QModelIndex();
|
---|
| 169 |
|
---|
| 170 | return createIndex(parentItem->childNumber(), 0, parentItem);
|
---|
| 171 | }
|
---|
| 172 | //! [7]
|
---|
| 173 |
|
---|
| 174 | bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
|
---|
| 175 | {
|
---|
| 176 | bool success;
|
---|
| 177 |
|
---|
| 178 | beginRemoveColumns(parent, position, position + columns - 1);
|
---|
| 179 | success = rootItem->removeColumns(position, columns);
|
---|
| 180 | endRemoveColumns();
|
---|
| 181 |
|
---|
| 182 | if (rootItem->columnCount() == 0)
|
---|
| 183 | removeRows(0, rowCount());
|
---|
| 184 |
|
---|
| 185 | return success;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
|
---|
| 189 | {
|
---|
| 190 | TreeItem *parentItem = getItem(parent);
|
---|
| 191 | bool success = true;
|
---|
| 192 |
|
---|
| 193 | beginRemoveRows(parent, position, position + rows - 1);
|
---|
| 194 | success = parentItem->removeChildren(position, rows);
|
---|
| 195 | endRemoveRows();
|
---|
| 196 |
|
---|
| 197 | return success;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | //! [8]
|
---|
| 201 | int TreeModel::rowCount(const QModelIndex &parent) const
|
---|
| 202 | {
|
---|
| 203 | TreeItem *parentItem = getItem(parent);
|
---|
| 204 |
|
---|
| 205 | return parentItem->childCount();
|
---|
| 206 | }
|
---|
| 207 | //! [8]
|
---|
| 208 |
|
---|
| 209 | bool TreeModel::setData(const QModelIndex &index, const QVariant &value,
|
---|
| 210 | int role)
|
---|
| 211 | {
|
---|
| 212 | if (role != Qt::EditRole)
|
---|
| 213 | return false;
|
---|
| 214 |
|
---|
| 215 | TreeItem *item = getItem(index);
|
---|
| 216 | bool result = item->setData(index.column(), value);
|
---|
| 217 |
|
---|
| 218 | if (result)
|
---|
| 219 | emit dataChanged(index, index);
|
---|
| 220 |
|
---|
| 221 | return result;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
|
---|
| 225 | const QVariant &value, int role)
|
---|
| 226 | {
|
---|
| 227 | if (role != Qt::EditRole || orientation != Qt::Horizontal)
|
---|
| 228 | return false;
|
---|
| 229 |
|
---|
| 230 | bool result = rootItem->setData(section, value);
|
---|
| 231 |
|
---|
| 232 | if (result)
|
---|
| 233 | emit headerDataChanged(orientation, section, section);
|
---|
| 234 |
|
---|
| 235 | return result;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
|
---|
| 239 | {
|
---|
| 240 | QList<TreeItem*> parents;
|
---|
| 241 | QList<int> indentations;
|
---|
| 242 | parents << parent;
|
---|
| 243 | indentations << 0;
|
---|
| 244 |
|
---|
| 245 | int number = 0;
|
---|
| 246 |
|
---|
| 247 | while (number < lines.count()) {
|
---|
| 248 | int position = 0;
|
---|
| 249 | while (position < lines[number].length()) {
|
---|
| 250 | if (lines[number].mid(position, 1) != " ")
|
---|
| 251 | break;
|
---|
| 252 | position++;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | QString lineData = lines[number].mid(position).trimmed();
|
---|
| 256 |
|
---|
| 257 | if (!lineData.isEmpty()) {
|
---|
| 258 | // Read the column data from the rest of the line.
|
---|
| 259 | QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
|
---|
| 260 | QVector<QVariant> columnData;
|
---|
| 261 | for (int column = 0; column < columnStrings.count(); ++column)
|
---|
| 262 | columnData << columnStrings[column];
|
---|
| 263 |
|
---|
| 264 | if (position > indentations.last()) {
|
---|
| 265 | // The last child of the current parent is now the new parent
|
---|
| 266 | // unless the current parent has no children.
|
---|
| 267 |
|
---|
| 268 | if (parents.last()->childCount() > 0) {
|
---|
| 269 | parents << parents.last()->child(parents.last()->childCount()-1);
|
---|
| 270 | indentations << position;
|
---|
| 271 | }
|
---|
| 272 | } else {
|
---|
| 273 | while (position < indentations.last() && parents.count() > 0) {
|
---|
| 274 | parents.pop_back();
|
---|
| 275 | indentations.pop_back();
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | // Append a new item to the current parent's list of children.
|
---|
| 280 | TreeItem *parent = parents.last();
|
---|
| 281 | parent->insertChildren(parent->childCount(), 1, rootItem->columnCount());
|
---|
| 282 | for (int column = 0; column < columnData.size(); ++column)
|
---|
| 283 | parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | number++;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|