Mercurial > projects > qtd
comparison demos/interview/model.d @ 1:e78566595089
initial import
author | mandel |
---|---|
date | Mon, 11 May 2009 16:01:50 +0000 |
parents | |
children | 7be693ea7070 |
comparison
equal
deleted
inserted
replaced
0:36fb74dc547d | 1:e78566595089 |
---|---|
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; | |
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 | |
48 import tango.core.Array; | |
49 import Integer = tango.text.convert.Integer; | |
50 | |
51 import tango.io.Stdout; | |
52 | |
53 class Node | |
54 { | |
55 this(Node parent_ = null) | |
56 { | |
57 parent = parent_; | |
58 } | |
59 | |
60 Node parent; | |
61 Node[] children; | |
62 } | |
63 | |
64 class Model : QAbstractItemModel | |
65 { | |
66 | |
67 this(int rows, int columns, QObject parent = null) | |
68 { | |
69 super(parent); | |
70 rc = rows; | |
71 cc = columns; | |
72 tree = new Node[rows]; | |
73 foreach(ref node; tree) { | |
74 node = new Node; | |
75 } | |
76 | |
77 iconProvider = new QFileIconProvider; | |
78 | |
79 folder = iconProvider.icon(QFileIconProvider.Folder); | |
80 file = iconProvider.icon(QFileIconProvider.File); | |
81 services = new QIcon("images/services.png"); | |
82 } | |
83 | |
84 | |
85 QModelIndex index(int row, int column, QModelIndex parent) | |
86 { | |
87 if (row < rc && row >= 0 && column < cc && column >= 0) { | |
88 Node p = cast(Node) parent.internalPointer(); | |
89 Node n = getNode(row, p); | |
90 if (n !is null) | |
91 return createIndex(row, column, cast(void*)n); | |
92 } | |
93 return QModelIndex(); | |
94 } | |
95 | |
96 QModelIndex parent(QModelIndex child) | |
97 { | |
98 if (child.isValid()) { | |
99 Node n = cast(Node) child.internalPointer(); | |
100 Node p = parent(n); | |
101 if (p !is null) | |
102 return createIndex(row(p), 0, cast(void*)p); | |
103 } | |
104 return QModelIndex(); | |
105 } | |
106 | |
107 int rowCount(QModelIndex parent) | |
108 { | |
109 return (parent.isValid() && parent.column() != 0) ? 0 : rc; | |
110 } | |
111 | |
112 int columnCount(QModelIndex parent) | |
113 { | |
114 return cc; | |
115 } | |
116 | |
117 QVariant data(QModelIndex index, int role) | |
118 { | |
119 if (!index.isValid) | |
120 return new QVariant; | |
121 if (role == Qt.DisplayRole) | |
122 return new QVariant("Item " ~ Integer.toString(index.row) ~ ":" ~ Integer.toString(index.column)); | |
123 if (role == Qt.DecorationRole) { | |
124 if (index.column == 0) | |
125 //return iconProvider.icon(QFileIconProvider::Folder); | |
126 return folder.toVariant; | |
127 return file.toVariant; | |
128 } | |
129 return new QVariant; | |
130 } | |
131 | |
132 QVariant headerData(int section, Qt.Orientation orientation, int role) | |
133 { | |
134 if (role == Qt.DisplayRole) | |
135 return new QVariant(Integer.toString(section)); | |
136 if (role == Qt.DecorationRole) | |
137 return services.toVariant; | |
138 return QAbstractItemModel.headerData(section, orientation, role); | |
139 } | |
140 | |
141 bool hasChildren(QModelIndex parent) | |
142 { | |
143 if (parent.isValid && parent.column != 0) | |
144 return false; | |
145 return rc > 0 && cc > 0; | |
146 } | |
147 | |
148 int flags(QModelIndex index) | |
149 { | |
150 if (!index.isValid) | |
151 return 0; | |
152 return (Qt.ItemIsDragEnabled | Qt.ItemIsSelectable | Qt.ItemIsEnabled); | |
153 } | |
154 | |
155 Node getNode(int row, Node parent) | |
156 { | |
157 if(parent !is null && parent.children.length == 0) { | |
158 for(int i = 0; i < rc; i++) | |
159 parent.children ~= new Node(parent); | |
160 } | |
161 | |
162 Node[] v = parent !is null ? parent.children : tree; | |
163 return v[row]; | |
164 } | |
165 | |
166 Node parent(Node child) | |
167 { | |
168 return child !is null ? child.parent : null; | |
169 } | |
170 | |
171 int row(Node node) | |
172 { | |
173 Node[] v = node.parent !is null ? node.parent.children : tree; | |
174 return find(v, node); | |
175 } | |
176 | |
177 | |
178 int rc, cc; | |
179 Node[] tree; | |
180 QFileIconProvider iconProvider; | |
181 | |
182 QIcon folder, services, file; | |
183 } |