1
/****************************************************************************
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
6
** This file is part of the demonstration applications of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
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 https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
18
** Alternatively, you may use this file under the terms of the BSD license
21
** "Redistribution and use in source and binary forms, with or without
22
** modification, are permitted provided that the following conditions are
24
** * Redistributions of source code must retain the above copyright
25
** notice, this list of conditions and the following disclaimer.
26
** * Redistributions in binary form must reproduce the above copyright
27
** notice, this list of conditions and the following disclaimer in
28
** the documentation and/or other materials provided with the
30
** * Neither the name of The Qt Company Ltd nor the names of its
31
** contributors may be used to endorse or promote products derived
32
** from this software without specific prior written permission.
35
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
49
****************************************************************************/
56
Model::Model(int rows, int columns, QObject *parent)
57
: QAbstractItemModel(parent),
58
services(QPixmap(":/images/services.png")),
59
rc(rows), cc(columns),
60
tree(new QVector<Node>(rows, Node(0)))
70
QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
72
if (row < rc && row >= 0 && column < cc && column >= 0) {
73
Node *parentNode = static_cast<Node*>(parent.internalPointer());
74
Node *childNode = node(row, parentNode);
76
return createIndex(row, column, childNode);
81
QModelIndex Model::parent(const QModelIndex &child) const
83
if (child.isValid()) {
84
Node *childNode = static_cast<Node*>(child.internalPointer());
85
Node *parentNode = parent(childNode);
87
return createIndex(row(parentNode), 0, parentNode);
92
int Model::rowCount(const QModelIndex &parent) const
94
return (parent.isValid() && parent.column() != 0) ? 0 : rc;
97
int Model::columnCount(const QModelIndex &parent) const
103
QVariant Model::data(const QModelIndex &index, int role) const
105
if (!index.isValid())
107
if (role == Qt::DisplayRole)
108
return QVariant("Item " + QString::number(index.row()) + ':' + QString::number(index.column()));
109
if (role == Qt::DecorationRole) {
110
if (index.column() == 0)
111
return iconProvider.icon(QFileIconProvider::Folder);
112
return iconProvider.icon(QFileIconProvider::File);
117
QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
119
if (role == Qt::DisplayRole)
120
return QString::number(section);
121
if (role == Qt::DecorationRole)
122
return QVariant::fromValue(services);
123
return QAbstractItemModel::headerData(section, orientation, role);
126
bool Model::hasChildren(const QModelIndex &parent) const
128
if (parent.isValid() && parent.column() != 0)
130
return rc > 0 && cc > 0;
133
Qt::ItemFlags Model::flags(const QModelIndex &index) const
135
if (!index.isValid())
137
return Qt::ItemIsDragEnabled|QAbstractItemModel::flags(index);
140
Model::Node *Model::node(int row, Node *parent) const
142
if (parent && !parent->children)
143
parent->children = new QVector<Node>(rc, Node(parent));
144
QVector<Node> *v = parent ? parent->children : tree;
145
return const_cast<Node*>(&(v->at(row)));
148
Model::Node *Model::parent(Node *child) const
150
return child ? child->parent : 0;
153
int Model::row(Node *node) const
155
const Node *first = node->parent ? &(node->parent->children->at(0)) : &(tree->at(0));