1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
46 Model::Model(int rows, int columns, QObject *parent)
47 : QAbstractItemModel(parent),
48 services(QPixmap(":/images/services.png")),
49 rc(rows), cc(columns),
50 tree(new QVector<Node>(rows, Node(0)))
60 QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
62 if (row < rc && row >= 0 && column < cc && column >= 0) {
63 Node *p = static_cast<Node*>(parent.internalPointer());
64 Node *n = node(row, p);
66 return createIndex(row, column, n);
71 QModelIndex Model::parent(const QModelIndex &child) const
73 if (child.isValid()) {
74 Node *n = static_cast<Node*>(child.internalPointer());
77 return createIndex(row(p), 0, p);
82 int Model::rowCount(const QModelIndex &parent) const
84 return (parent.isValid() && parent.column() != 0) ? 0 : rc;
87 int Model::columnCount(const QModelIndex &parent) const
93 QVariant Model::data(const QModelIndex &index, int role) const
97 if (role == Qt::DisplayRole)
98 return "Item " + QString::number(index.row()) + ":" + QString::number(index.column());
99 if (role == Qt::DecorationRole) {
100 if (index.column() == 0)
101 return iconProvider.icon(QFileIconProvider::Folder);
102 return iconProvider.icon(QFileIconProvider::File);
107 QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
109 if (role == Qt::DisplayRole)
110 return QString::number(section);
111 if (role == Qt::DecorationRole)
112 return QVariant::fromValue(services);
113 return QAbstractItemModel::headerData(section, orientation, role);
116 bool Model::hasChildren(const QModelIndex &parent) const
118 if (parent.isValid() && parent.column() != 0)
120 return rc > 0 && cc > 0;
123 Qt::ItemFlags Model::flags(const QModelIndex &index) const
125 if (!index.isValid())
127 return (Qt::ItemIsDragEnabled|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
130 Model::Node *Model::node(int row, Node *parent) const
132 if (parent && !parent->children)
133 parent->children = new QVector<Node>(rc, Node(parent));
134 QVector<Node> *v = parent ? parent->children : tree;
135 return const_cast<Node*>(&(v->at(row)));
138 Model::Node *Model::parent(Node *child) const
140 return child ? child->parent : 0;
143 int Model::row(Node *node) const
145 const Node *first = node->parent ? &(node->parent->children->at(0)) : &(tree->at(0));
146 return (node - first);