comparison demos/interview/model_d1.d @ 209:3f024154460c

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