193
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** Commercial Usage
|
|
9 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
10 ** accordance with the Qt Commercial License Agreement provided with the
|
|
11 ** Software or, alternatively, in accordance with the terms contained in
|
|
12 ** a written agreement between you and Nokia.
|
|
13 **
|
|
14 **
|
|
15 ** GNU General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU
|
|
17 ** General Public License versions 2.0 or 3.0 as published by the Free
|
|
18 ** Software Foundation and appearing in the file LICENSE.GPL included in
|
|
19 ** the packaging of this file. Please review the following information
|
|
20 ** to ensure GNU General Public Licensing requirements will be met:
|
|
21 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
22 ** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
|
23 ** exception, Nokia gives you certain additional rights. These rights
|
|
24 ** are described in the Nokia Qt GPL Exception version 1.3, included in
|
|
25 ** the file GPL_EXCEPTION.txt in this package.
|
|
26 **
|
|
27 ** Qt for Windows(R) Licensees
|
|
28 ** As a special exception, Nokia, as the sole copyright holder for Qt
|
|
29 ** Designer, grants users of the Qt/Eclipse Integration plug-in the
|
|
30 ** right for the Qt/Eclipse Integration to link to functionality
|
|
31 ** provided by Qt Designer and its related libraries.
|
|
32 **
|
|
33 ** If you are unsure which license is appropriate for your use, please
|
|
34 ** contact the sales department at qt-sales@nokia.com.
|
|
35 **
|
|
36 ****************************************************************************/
|
|
37
|
|
38 module model_d2;
|
|
39
|
|
40 import qt.gui.QIcon;
|
|
41 import qt.gui.QPixmap;
|
|
42 import qt.gui.QFileIconProvider;
|
|
43
|
|
44 import qt.core.QAbstractItemModel;
|
|
45 import qt.core.QVariant;
|
|
46 import qt.core.QModelIndex;
|
|
47
|
197
|
48 import std.conv; //, std.algorithm;
|
199
|
49 import std.stdio;
|
193
|
50
|
|
51 class Node
|
|
52 {
|
|
53 this(Node parent_ = null)
|
|
54 {
|
|
55 parent = parent_;
|
|
56 }
|
|
57
|
|
58 Node parent;
|
|
59 Node[] children;
|
|
60 }
|
199
|
61
|
197
|
62 size_t find(Node[] arr, Node elem)
|
|
63 {
|
|
64 size_t res = arr.length;
|
|
65 for(size_t i = 0; i < arr.length; i++)
|
|
66 if (arr[i] is elem)
|
|
67 res = i;
|
|
68 return res;
|
|
69 }
|
199
|
70
|
193
|
71 class Model : QAbstractItemModel
|
|
72 {
|
|
73
|
|
74 this(int rows, int columns, QObject parent = null)
|
|
75 {
|
|
76 super(parent);
|
|
77 rc = rows;
|
|
78 cc = columns;
|
|
79 tree = new Node[rows];
|
|
80 foreach(ref node; tree) {
|
|
81 node = new Node;
|
|
82 }
|
|
83
|
|
84 iconProvider = new QFileIconProvider;
|
|
85
|
|
86 folder = iconProvider.icon(QFileIconProvider.Folder);
|
|
87 file = iconProvider.icon(QFileIconProvider.File);
|
|
88 services = new QIcon(":/images/services.png");
|
|
89 }
|
|
90
|
|
91
|
202
|
92 override QModelIndex index(int row, int column, const QModelIndex parent)
|
193
|
93 {
|
|
94 if (row < rc && row >= 0 && column < cc && column >= 0) {
|
|
95 Node p = cast(Node) parent.internalPointer();
|
|
96 Node n = getNode(row, p);
|
202
|
97 if (n !is null)
|
|
98 return createIndex(row, column, cast(void*)n);
|
193
|
99 }
|
|
100 return QModelIndex();
|
|
101 }
|
|
102
|
202
|
103 override QModelIndex parent(const QModelIndex child)
|
193
|
104 {
|
|
105 if (child.isValid()) {
|
|
106 Node n = cast(Node) child.internalPointer();
|
|
107 Node p = parent(n);
|
|
108 if (p !is null)
|
|
109 return createIndex(row(p), 0, cast(void*)p);
|
|
110 }
|
|
111 return QModelIndex();
|
|
112 }
|
|
113
|
202
|
114 override int rowCount(const QModelIndex parent)
|
193
|
115 {
|
|
116 return (parent.isValid() && parent.column() != 0) ? 0 : rc;
|
|
117 }
|
|
118
|
202
|
119 override int columnCount(const QModelIndex parent)
|
193
|
120 {
|
|
121 return cc;
|
|
122 }
|
|
123
|
202
|
124 override QVariant data(const QModelIndex index, int role)
|
193
|
125 {
|
|
126 if (!index.isValid)
|
|
127 return new QVariant;
|
|
128 if (role == Qt.DisplayRole)
|
|
129 return new QVariant("Item " ~ to!string(index.row) ~ ":" ~ to!string(index.column));
|
|
130 if (role == Qt.DecorationRole) {
|
|
131 if (index.column == 0)
|
|
132 //return iconProvider.icon(QFileIconProvider::Folder);
|
|
133 return folder.toVariant;
|
|
134 return file.toVariant;
|
|
135 }
|
|
136 return new QVariant;
|
|
137 }
|
|
138
|
202
|
139 override QVariant headerData(int section, Qt.Orientation orientation, int role)
|
193
|
140 {
|
|
141 if (role == Qt.DisplayRole)
|
|
142 return new QVariant(to!string(section));
|
|
143 if (role == Qt.DecorationRole)
|
|
144 return services.toVariant;
|
|
145 return QAbstractItemModel.headerData(section, orientation, role);
|
|
146 }
|
|
147
|
202
|
148 override bool hasChildren(const QModelIndex parent)
|
193
|
149 {
|
|
150 if (parent.isValid && parent.column != 0)
|
|
151 return false;
|
|
152 return rc > 0 && cc > 0;
|
|
153 }
|
|
154
|
202
|
155 override int flags(const QModelIndex index)
|
193
|
156 {
|
|
157 if (!index.isValid)
|
|
158 return 0;
|
|
159 return (Qt.ItemIsDragEnabled | Qt.ItemIsSelectable | Qt.ItemIsEnabled);
|
|
160 }
|
|
161
|
|
162 Node getNode(int row, Node parent)
|
|
163 {
|
|
164 if(parent !is null && parent.children.length == 0) {
|
|
165 for(int i = 0; i < rc; i++)
|
|
166 parent.children ~= new Node(parent);
|
|
167 }
|
|
168
|
|
169 Node[] v = parent !is null ? parent.children : tree;
|
|
170 return v[row];
|
|
171 }
|
|
172
|
201
|
173 Node parent(Node child)
|
193
|
174 {
|
|
175 return child !is null ? child.parent : null;
|
|
176 }
|
|
177
|
|
178 int row(Node node)
|
|
179 {
|
|
180 Node[] v = node.parent !is null ? node.parent.children : tree;
|
199
|
181 return find(v, node);
|
193
|
182 }
|
|
183
|
|
184
|
|
185 int rc, cc;
|
|
186 Node[] tree;
|
|
187 QFileIconProvider iconProvider;
|
|
188
|
|
189 QIcon folder, services, file;
|
|
190 }
|