comparison examples/itemviews/editabletreemodel/treeitem.d @ 153:ea0861a37bf6

add editabletreemodel, still buggy
author mandel
date Tue, 16 Jun 2009 00:51:38 +0000
parents
children
comparison
equal deleted inserted replaced
152:4d1c5d1d1bbf 153:ea0861a37bf6
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial Usage
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Nokia.
14 **
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 module treeitem;
42
43
44 import qt.core.QVariant;
45
46 import tango.core.Array;
47
48
49 //substitute for QList methods
50 void insert(T)(ref T[] items, int pos, T item)
51 {
52 items = items[0..pos] ~ [item] ~ items[pos..$];
53 }
54
55 T takeAt(T)(ref T[] items, int pos)
56 {
57 T item = items[pos];
58 items = items[0..pos] ~ items[pos+1..$];
59 return item;
60 }
61
62 int indexOf(T)(T[] items, T item, int from = -1)
63 {
64 assert(from < cast(int) items.length);
65 auto beg = (from < 0) ? 0 : from;
66 auto pos = beg + find(items[beg..$], item);
67 return (pos == items.length) ? -1 : pos;
68 }
69
70 void remove(T)(ref T[] items, uint pos)
71 {
72 items = items[0..pos] ~ items[pos+1..$];
73 }
74
75
76 class TreeItem
77 {
78 public:
79
80 this(QVariant[] data, TreeItem parent = null)
81 {
82 parentItem = parent;
83 itemData = data;
84 }
85
86 ~this()
87 {
88 delete childItems;
89 }
90
91 TreeItem child(int number)
92 {
93 if(number < 0 || number >= childItems.length)
94 return null;
95 return childItems[number];
96 }
97
98 int childCount()
99 {
100 return childItems.length;
101 }
102
103 int columnCount()
104 {
105 return itemData.length;
106 }
107
108 QVariant data(int column)
109 {
110 if(column < 0 || column >= itemData.length)
111 return null;
112 return itemData[column];
113 }
114
115 bool insertChildren(int position, int count, int columns)
116 {
117 if (position < 0 || position > childItems.length)
118 return false;
119
120 for (int row = 0; row < count; ++row) {
121 auto data = new QVariant[](columns);
122 TreeItem item = new TreeItem(data, this);
123 insert(childItems, position, item);
124 }
125
126 return true;
127 }
128
129 bool insertColumns(int position, int columns)
130 {
131 if (position < 0 || position > itemData.length)
132 return false;
133
134 for (int column = 0; column < columns; ++column)
135 insert(itemData, position, new QVariant());
136
137 foreach (TreeItem child; childItems)
138 child.insertColumns(position, columns);
139
140 return true;
141 }
142
143 TreeItem parent()
144 {
145 return parentItem;
146 }
147
148 bool removeChildren(int position, int count)
149 {
150 if (position < 0 || position + count > childItems.length)
151 return false;
152
153 for (int row = 0; row < count; ++row)
154 {
155 auto tmp = takeAt(childItems, position);
156 delete tmp;
157 }
158 return true;
159 }
160
161 bool removeColumns(int position, int columns)
162 {
163 if (position < 0 || position + columns > itemData.length)
164 return false;
165
166 for (int column = 0; column < columns; ++column)
167 remove(itemData, position);
168
169 foreach (TreeItem child; childItems)
170 child.removeColumns(position, columns);
171
172 return true;
173 }
174
175 int childNumber()
176 {
177 if (parentItem)
178 return indexOf(parentItem.childItems, cast(TreeItem) this);
179
180 return 0;
181 }
182
183 bool setData(int column, QVariant value)
184 {
185 if (column < 0 || column >= itemData.length)
186 return false;
187
188 itemData[column] = value;
189 return true;
190 }
191
192 private:
193
194 TreeItem[] childItems;
195 QVariant[] itemData;
196 TreeItem parentItem;
197 }