1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
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.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
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
29** distribution.
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.
33**
34**
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."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "model.h"
52
53#include <QPixmap>
54
55Model::Model(int rows, int columns, QObject *parent)
56 : QAbstractItemModel(parent),
57 services(QPixmap(":/images/services.png")),
58 rc(rows),
59 cc(columns),
60 tree(new QList<Node>(rows, Node()))
61{
62
63}
64
65Model::~Model()
66{
67 delete tree;
68}
69
70QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
71{
72 if (row < rc && row >= 0 && column < cc && column >= 0) {
73 Node *parentNode = static_cast<Node*>(parent.internalPointer());
74 Node *childNode = node(row, parentNode);
75 if (childNode)
76 return createIndex(row, column, childNode);
77 }
78 return QModelIndex();
79}
80
81QModelIndex Model::parent(const QModelIndex &child) const
82{
83 if (child.isValid()) {
84 Node *childNode = static_cast<Node*>(child.internalPointer());
85 Node *parentNode = parent(childNode);
86 if (parentNode)
87 return createIndex(row(parentNode), 0, parentNode);
88 }
89 return QModelIndex();
90}
91
92int Model::rowCount(const QModelIndex &parent) const
93{
94 return (parent.isValid() && parent.column() != 0) ? 0 : rc;
95}
96
97int Model::columnCount(const QModelIndex &parent) const
98{
99 Q_UNUSED(parent);
100 return cc;
101}
102
103QVariant Model::data(const QModelIndex &index, int role) const
104{
105 if (!index.isValid())
106 return QVariant();
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);
113 }
114 return QVariant();
115}
116
117QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
118{
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);
124}
125
126bool Model::hasChildren(const QModelIndex &parent) const
127{
128 if (parent.isValid() && parent.column() != 0)
129 return false;
130 return rc > 0 && cc > 0;
131}
132
133Qt::ItemFlags Model::flags(const QModelIndex &index) const
134{
135 if (!index.isValid())
136 return {};
137 return Qt::ItemIsDragEnabled|QAbstractItemModel::flags(index);
138}
139
140Model::Node *Model::node(int row, Node *parent) const
141{
142 if (parent && !parent->children)
143 parent->children = new QList<Node>(rc, Node(parent));
144 QList<Node> *v = parent ? parent->children : tree;
145 return const_cast<Node*>(&(v->at(row)));
146}
147
148Model::Node *Model::parent(Node *child) const
149{
150 return child ? child->parent : nullptr;
151}
152
153int Model::row(Node *node) const
154{
155 const Node *first = node->parent ? &(node->parent->children->at(0)) : &(tree->at(0));
156 return node - first;
157}
158