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

file rename for interview demo
author eldar
date Tue, 14 Jul 2009 09:52:28 +0000
parents demos/interview/model_d2.d@57c3714ed282
children 7ea67ec3cf29
comparison
equal deleted inserted replaced
208:be08f1b4ba39 209:3f024154460c
33 ** If you are unsure which license is appropriate for your use, please 33 ** If you are unsure which license is appropriate for your use, please
34 ** contact the sales department at qt-sales@nokia.com. 34 ** contact the sales department at qt-sales@nokia.com.
35 ** 35 **
36 ****************************************************************************/ 36 ****************************************************************************/
37 37
38 module model; 38 module model_d2;
39 39
40 import qt.gui.QIcon; 40 import qt.gui.QIcon;
41 import qt.gui.QPixmap; 41 import qt.gui.QPixmap;
42 import qt.gui.QFileIconProvider; 42 import qt.gui.QFileIconProvider;
43 43
44 import qt.core.QAbstractItemModel; 44 import qt.core.QAbstractItemModel;
45 import qt.core.QVariant; 45 import qt.core.QVariant;
46 import qt.core.QModelIndex; 46 import qt.core.QModelIndex;
47 47
48 import tango.core.Array; 48 import std.conv; //, std.algorithm;
49 import Integer = tango.text.convert.Integer; 49 import std.stdio;
50 50
51
52 class Node 51 class Node
53 { 52 {
54 this(Node parent_ = null) 53 this(Node parent_ = null)
55 { 54 {
56 parent = parent_; 55 parent = parent_;
57 } 56 }
58 57
59 Node parent; 58 Node parent;
60 Node[] children; 59 Node[] children;
61 } 60 }
62 61
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 }
70
63 class Model : QAbstractItemModel 71 class Model : QAbstractItemModel
64 { 72 {
65 73
66 this(int rows, int columns, QObject parent = null) 74 this(int rows, int columns, QObject parent = null)
67 { 75 {
79 file = iconProvider.icon(QFileIconProvider.File); 87 file = iconProvider.icon(QFileIconProvider.File);
80 services = new QIcon(":/images/services.png"); 88 services = new QIcon(":/images/services.png");
81 } 89 }
82 90
83 91
84 override QModelIndex index(int row, int column, QModelIndex parent) 92 override QModelIndex index(int row, int column, const QModelIndex parent)
85 { 93 {
86 if (row < rc && row >= 0 && column < cc && column >= 0) { 94 if (row < rc && row >= 0 && column < cc && column >= 0) {
87 Node p = cast(Node) parent.internalPointer(); 95 Node p = cast(Node) parent.internalPointer();
88 Node n = getNode(row, p); 96 Node n = getNode(row, p);
89 if (n !is null) 97 if (n !is null)
90 return createIndex(row, column, cast(void*)n); 98 return createIndex(row, column, cast(void*)n);
91 } 99 }
92 return QModelIndex(); 100 return QModelIndex();
93 } 101 }
94 102
95 override QModelIndex parent(QModelIndex child) 103 override QModelIndex parent(const QModelIndex child)
96 { 104 {
97 if (child.isValid()) { 105 if (child.isValid()) {
98 Node n = cast(Node) child.internalPointer(); 106 Node n = cast(Node) child.internalPointer();
99 Node p = parent(n); 107 Node p = parent(n);
100 if (p !is null) 108 if (p !is null)
101 return createIndex(row(p), 0, cast(void*)p); 109 return createIndex(row(p), 0, cast(void*)p);
102 } 110 }
103 return QModelIndex(); 111 return QModelIndex();
104 } 112 }
105 113
106 override int rowCount(QModelIndex parent) 114 override int rowCount(const QModelIndex parent)
107 { 115 {
108 return (parent.isValid() && parent.column() != 0) ? 0 : rc; 116 return (parent.isValid() && parent.column() != 0) ? 0 : rc;
109 } 117 }
110 118
111 override int columnCount(QModelIndex parent) 119 override int columnCount(const QModelIndex parent)
112 { 120 {
113 return cc; 121 return cc;
114 } 122 }
115 123
116 override QVariant data(QModelIndex index, int role) 124 override QVariant data(const QModelIndex index, int role)
117 { 125 {
118 if (!index.isValid) 126 if (!index.isValid)
119 return new QVariant; 127 return new QVariant;
120 if (role == Qt.DisplayRole) 128 if (role == Qt.DisplayRole)
121 return new QVariant("Item " ~ Integer.toString(index.row) ~ ":" ~ Integer.toString(index.column)); 129 return new QVariant("Item " ~ to!string(index.row) ~ ":" ~ to!string(index.column));
122 if (role == Qt.DecorationRole) { 130 if (role == Qt.DecorationRole) {
123 if (index.column == 0) 131 if (index.column == 0)
124 //return iconProvider.icon(QFileIconProvider::Folder); 132 //return iconProvider.icon(QFileIconProvider::Folder);
125 return folder.toVariant; 133 return folder.toVariant;
126 return file.toVariant; 134 return file.toVariant;
129 } 137 }
130 138
131 override QVariant headerData(int section, Qt.Orientation orientation, int role) 139 override QVariant headerData(int section, Qt.Orientation orientation, int role)
132 { 140 {
133 if (role == Qt.DisplayRole) 141 if (role == Qt.DisplayRole)
134 return new QVariant(Integer.toString(section)); 142 return new QVariant(to!string(section));
135 if (role == Qt.DecorationRole) 143 if (role == Qt.DecorationRole)
136 return services.toVariant; 144 return services.toVariant;
137 return QAbstractItemModel.headerData(section, orientation, role); 145 return QAbstractItemModel.headerData(section, orientation, role);
138 } 146 }
139 147
140 override bool hasChildren(QModelIndex parent) 148 override bool hasChildren(const QModelIndex parent)
141 { 149 {
142 if (parent.isValid && parent.column != 0) 150 if (parent.isValid && parent.column != 0)
143 return false; 151 return false;
144 return rc > 0 && cc > 0; 152 return rc > 0 && cc > 0;
145 } 153 }
146 154
147 override int flags(QModelIndex index) 155 override int flags(const QModelIndex index)
148 { 156 {
149 if (!index.isValid) 157 if (!index.isValid)
150 return 0; 158 return 0;
151 return (Qt.ItemIsDragEnabled | Qt.ItemIsSelectable | Qt.ItemIsEnabled); 159 return (Qt.ItemIsDragEnabled | Qt.ItemIsSelectable | Qt.ItemIsEnabled);
152 } 160 }