comparison demos/interview/model.d @ 300:c2c70e501860 signals

updated interview demo
author eldar
date Sun, 06 Dec 2009 22:21:22 +0000
parents 7ea67ec3cf29
children e67ce7c21758
comparison
equal deleted inserted replaced
299:87db643519a4 300:c2c70e501860
57 57
58 Node parent; 58 Node parent;
59 Node[] children; 59 Node[] children;
60 } 60 }
61 61
62 size_t find(Node[] arr, Node elem) 62 size_t find(const Node[] arr, const Node elem)
63 { 63 {
64 size_t res = arr.length; 64 size_t res = arr.length;
65 for(size_t i = 0; i < arr.length; i++) 65 for(size_t i = 0; i < arr.length; i++)
66 if (arr[i] is elem) 66 if (arr[i] is elem)
67 res = i; 67 res = i;
87 file = iconProvider.icon(QFileIconProvider.File); 87 file = iconProvider.icon(QFileIconProvider.File);
88 services = new QIcon(":/images/services.png"); 88 services = new QIcon(":/images/services.png");
89 } 89 }
90 90
91 91
92 override QModelIndex index(int row, int column, const QModelIndex parent) 92 override QModelIndex index(int row, int column, const QModelIndex parent) const
93 { 93 {
94 if (row < rc && row >= 0 && column < cc && column >= 0) { 94 if (row < rc && row >= 0 && column < cc && column >= 0) {
95 Node p = cast(Node) parent.internalPointer(); 95 auto p = cast(Node) parent.internalPointer();
96 Node n = getNode(row, p); 96 auto n = getNode(row, p);
97 if (n !is null) 97 if (n !is null)
98 return createIndex(row, column, cast(void*)n); 98 return createIndex(row, column, cast(void*)n);
99 } 99 }
100 return QModelIndex(); 100 return QModelIndex();
101 } 101 }
102 102
103 override QModelIndex parent(const QModelIndex child) 103 override QModelIndex parent(const QModelIndex child) const
104 { 104 {
105 if (child.isValid()) { 105 if (child.isValid()) {
106 Node n = cast(Node) child.internalPointer(); 106 Node n = cast(Node) child.internalPointer();
107 Node p = parent(n); 107 Node p = parent(n);
108 if (p !is null) 108 if (p !is null)
109 return createIndex(row(p), 0, cast(void*)p); 109 return createIndex(row(p), 0, cast(void*)p);
110 } 110 }
111 return QModelIndex(); 111 return QModelIndex();
112 } 112 }
113 113
114 override int rowCount(const QModelIndex parent) 114 override int rowCount(const QModelIndex parent) const
115 { 115 {
116 return (parent.isValid() && parent.column() != 0) ? 0 : rc; 116 return (parent.isValid() && parent.column() != 0) ? 0 : rc;
117 } 117 }
118 118
119 override int columnCount(const QModelIndex parent) 119 override int columnCount(const QModelIndex parent) const
120 { 120 {
121 return cc; 121 return cc;
122 } 122 }
123 123
124 override QVariant data(const QModelIndex index, int role) 124 override QVariant data(const QModelIndex index, int role) const
125 { 125 {
126 if (!index.isValid) 126 if (!index.isValid)
127 return new QVariant; 127 return new QVariant;
128 if (role == Qt.DisplayRole) 128 if (role == Qt.DisplayRole)
129 return new QVariant("Item " ~ to!string(index.row) ~ ":" ~ to!string(index.column)); 129 return new QVariant("Item " ~ to!string(index.row) ~ ":" ~ to!string(index.column));
134 return file.toVariant; 134 return file.toVariant;
135 } 135 }
136 return new QVariant; 136 return new QVariant;
137 } 137 }
138 138
139 override QVariant headerData(int section, Qt.Orientation orientation, int role) 139 override QVariant headerData(int section, Qt.Orientation orientation, int role) const
140 { 140 {
141 if (role == Qt.DisplayRole) 141 if (role == Qt.DisplayRole)
142 return new QVariant(to!string(section)); 142 return new QVariant(to!string(section));
143 if (role == Qt.DecorationRole) 143 if (role == Qt.DecorationRole)
144 return services.toVariant; 144 return services.toVariant;
145 return QAbstractItemModel.headerData(section, orientation, role); 145 return QAbstractItemModel.headerData(section, orientation, role);
146 } 146 }
147 147
148 override bool hasChildren(const QModelIndex parent) 148 override bool hasChildren(const QModelIndex parent) const
149 { 149 {
150 if (parent.isValid && parent.column != 0) 150 if (parent.isValid && parent.column != 0)
151 return false; 151 return false;
152 return rc > 0 && cc > 0; 152 return rc > 0 && cc > 0;
153 } 153 }
154 154
155 override int flags(const QModelIndex index) 155 override int flags(const QModelIndex index) const
156 { 156 {
157 if (!index.isValid) 157 if (!index.isValid)
158 return 0; 158 return 0;
159 return (Qt.ItemIsDragEnabled | Qt.ItemIsSelectable | Qt.ItemIsEnabled); 159 return (Qt.ItemIsDragEnabled | Qt.ItemIsSelectable | Qt.ItemIsEnabled);
160 } 160 }
161 161
162 Node getNode(int row, Node parent) 162 const (Node) getNode(int row, Node parent) const
163 { 163 {
164 if(parent !is null && parent.children.length == 0) { 164 if(parent !is null && parent.children.length == 0) {
165 for(int i = 0; i < rc; i++) 165 for(int i = 0; i < rc; i++)
166 parent.children ~= new Node(parent); 166 parent.children ~= new Node(parent);
167 } 167 }
168 168
169 Node[] v = parent !is null ? parent.children : tree; 169 auto v = parent !is null ? parent.children : tree;
170 return v[row]; 170 return v[row];
171 } 171 }
172 172
173 Node parent(Node child) 173 Node parent(Node child) const
174 { 174 {
175 return child !is null ? child.parent : null; 175 return child !is null ? child.parent : null;
176 } 176 }
177 177
178 int row(Node node) 178 int row(Node node) const
179 { 179 {
180 Node[] v = node.parent !is null ? node.parent.children : tree; 180 auto v = node.parent !is null ? node.parent.children : tree;
181 return find(v, node); 181 return find(v, node);
182 } 182 }
183 183
184 184
185 int rc, cc; 185 int rc, cc;