Редакция 225 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 225 | Редакция 226 | ||
---|---|---|---|
1 | 1 | ||
2 | 2 | ||
3 | /****************************************************************************
|
3 | /****************************************************************************
|
4 | **
|
4 | **
|
5 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
5 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
6 | ** All rights reserved.
|
6 | ** All rights reserved.
|
7 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
7 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
8 | **
|
8 | **
|
9 | ** This file is part of the examples of the Qt Toolkit.
|
9 | ** This file is part of the examples of the Qt Toolkit.
|
10 | **
|
10 | **
|
11 | ** $QT_BEGIN_LICENSE:LGPL$
|
11 | ** $QT_BEGIN_LICENSE:LGPL$
|
12 | ** Commercial Usage
|
12 | ** Commercial Usage
|
13 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
13 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
14 | ** accordance with the Qt Commercial License Agreement provided with the
|
14 | ** accordance with the Qt Commercial License Agreement provided with the
|
15 | ** Software or, alternatively, in accordance with the terms contained in
|
15 | ** Software or, alternatively, in accordance with the terms contained in
|
16 | ** a written agreement between you and Nokia.
|
16 | ** a written agreement between you and Nokia.
|
17 | **
|
17 | **
|
18 | ** GNU Lesser General Public License Usage
|
18 | ** GNU Lesser General Public License Usage
|
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
20 | ** General Public License version 2.1 as published by the Free Software
|
20 | ** General Public License version 2.1 as published by the Free Software
|
21 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
21 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
22 | ** packaging of this file. Please review the following information to
|
22 | ** packaging of this file. Please review the following information to
|
23 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
23 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
24 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
24 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
25 | **
|
25 | **
|
26 | ** In addition, as a special exception, Nokia gives you certain additional
|
26 | ** In addition, as a special exception, Nokia gives you certain additional
|
27 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
27 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
29 | **
|
29 | **
|
30 | ** GNU General Public License Usage
|
30 | ** GNU General Public License Usage
|
31 | ** Alternatively, this file may be used under the terms of the GNU
|
31 | ** Alternatively, this file may be used under the terms of the GNU
|
32 | ** General Public License version 3.0 as published by the Free Software
|
32 | ** General Public License version 3.0 as published by the Free Software
|
33 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
33 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
34 | ** packaging of this file. Please review the following information to
|
34 | ** packaging of this file. Please review the following information to
|
35 | ** ensure the GNU General Public License version 3.0 requirements will be
|
35 | ** ensure the GNU General Public License version 3.0 requirements will be
|
36 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
36 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
37 | **
|
37 | **
|
38 | ** If you have questions regarding the use of this file, please contact
|
38 | ** If you have questions regarding the use of this file, please contact
|
39 | ** Nokia at qt-info@nokia.com.
|
39 | ** Nokia at qt-info@nokia.com.
|
40 | ** $QT_END_LICENSE$
|
40 | ** $QT_END_LICENSE$
|
41 | **
|
41 | **
|
42 | ****************************************************************************/
|
42 | ****************************************************************************/
|
43 | 43 | ||
44 | /*
|
44 | /*
|
45 | delegate.cpp
|
45 | delegate.cpp
|
46 | 46 | ||
47 | A delegate that allows the user to change integer values from the model
|
47 | A delegate that allows the user to change integer values from the model
|
48 | using a spin box widget.
|
48 | using a spin box widget.
|
49 | */
|
49 | */
|
50 | 50 | ||
51 | #include <QtGui>
|
51 | #include <QtGui>
|
52 | 52 | ||
53 | #include "delegate.h"
|
53 | #include "delegate.h"
|
54 | 54 | ||
55 | 55 | ||
56 | //! [0]
|
56 | //! [0]
|
57 | SpinBoxDelegate::SpinBoxDelegate(QObject *parent) |
57 | SpinBoxDelegate::SpinBoxDelegate(QObject *parent) |
58 | : QItemDelegate(parent) |
58 | : QItemDelegate(parent) |
59 | {
|
59 | {
|
60 | }
|
60 | }
|
61 | //! [0]
|
61 | //! [0]
|
62 | 62 | ||
63 | void SpinBoxDelegate::setInctance(QString newInctance){ |
63 | void SpinBoxDelegate::setInctance(QString newInctance){ |
64 | inctance = newInctance; |
64 | inctance = newInctance; |
65 | }
|
65 | }
|
66 | 66 | ||
67 | void SpinBoxDelegate::getItems(){ |
67 | void SpinBoxDelegate::getItems(){ |
68 | QMap <QString, QString> result_map; |
68 | QMap <QString, QString> result_map; |
69 | QSqlQuery q;
|
69 | QSqlQuery q;
|
70 | QString query_str;
|
70 | QString query_str;
|
71 | bool ok; |
71 | bool ok; |
72 | 72 | ||
73 | if (SpinBoxDelegate::inctance.isEmpty()) |
73 | if (SpinBoxDelegate::inctance.isEmpty()) |
74 | {
|
74 | {
|
75 | items.clear(); |
75 | items.clear(); |
76 | return; |
76 | return; |
77 | }
|
77 | }
|
78 | 78 | ||
79 | query_str = tr("select * from `"); |
79 | query_str = tr("select * from `"); |
80 | query_str.append(inctance); |
80 | query_str.append(inctance); |
81 | query_str.append( tr("`")); |
81 | query_str.append( tr("`")); |
82 | 82 | ||
83 | q.prepare(query_str); |
83 | q.prepare(query_str); |
84 | 84 | ||
85 | 85 | ||
86 | ok = q.exec(); |
86 | ok = q.exec(); |
87 | 87 | ||
88 | 88 | ||
89 | 89 | ||
90 | 90 | ||
91 | if (!ok) { |
91 | if (!ok) { |
92 | /*
|
92 | /*
|
93 | QString error_str;
|
93 | QString error_str;
|
94 | error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
|
94 | error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
|
95 | error_str.append(inctance);
|
95 | error_str.append(inctance);
|
96 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
96 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
97 | this, // Ðîäèòåëüñêèé âèäæåò.
|
97 | this, // Ðîäèòåëüñêèé âèäæåò.
|
98 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
98 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
99 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
99 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
100 | // tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
|
100 | // tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
|
101 | 101 | ||
102 | */
|
102 | */
|
103 | items.clear(); |
103 | items.clear(); |
104 | return; |
104 | return; |
105 | }
|
105 | }
|
106 | 106 | ||
107 | 107 | ||
108 | // field_inctance = q.record().indexOf(tr("TableWhithInstance"));
|
108 | // field_inctance = q.record().indexOf(tr("TableWhithInstance"));
|
109 | items.clear(); |
109 | items.clear(); |
110 | while(q.next()){ |
110 | while(q.next()){ |
111 | 111 | ||
112 | QString ID;
|
112 | QString ID;
|
113 | QString Name;
|
113 | QString Name;
|
114 | ID = q.value(0).toString(); // îïðåäåëÿåì ID |
114 | ID = q.value(0).toString(); // îïðåäåëÿåì ID |
115 | Name = q.value(1).toString(); // îïðåäåëÿåì Name |
115 | Name = q.value(1).toString(); // îïðåäåëÿåì Name |
116 | items.insert(ID, Name); |
116 | items.insert(ID, Name); |
117 | }
|
117 | }
|
118 | 118 | ||
119 | 119 | ||
120 | // return result_map;
|
120 | // return result_map;
|
121 | }
|
121 | }
|
122 | 122 | ||
123 | 123 | ||
124 | //! [1]
|
124 | //! [1]
|
125 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, |
125 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, |
126 | const QStyleOptionViewItem &/* option */, |
126 | const QStyleOptionViewItem &/* option */, |
127 | const QModelIndex &/* index */) const |
127 | const QModelIndex &/* index */) const |
128 | {
|
128 | {
|
129 | //QSpinBox *editor = new QSpinBox(parent);
|
129 | //QSpinBox *editor = new QSpinBox(parent);
|
130 | //editor->setMinimum(0);
|
130 | //editor->setMinimum(0);
|
131 | //editor->setMaximum(100);
|
131 | //editor->setMaximum(100);
|
132 | QStringList ID_list;
|
132 | QStringList ID_list;
|
133 | QStringList Name_list;
|
133 | QStringList Name_list;
|
134 | 134 | ||
135 | // getItems();
|
135 | // getItems();
|
136 | 136 | ||
137 | 137 | ||
138 | 138 | ||
139 | 139 | ||
140 | 140 | ||
141 | ID_list = items.keys(); |
141 | ID_list = items.keys(); |
142 | Name_list = items.values(); |
142 | Name_list = items.values(); |
143 | 143 | ||
144 | QComboBox *editor = new QComboBox(parent); |
144 | QComboBox *editor = new QComboBox(parent); |
145 | 145 | ||
146 | /*
|
146 | /*
|
147 | editor->addItem("0");
|
147 | editor->addItem("0");
|
148 | editor->addItem("1");
|
148 | editor->addItem("1");
|
149 | editor->addItem("2");
|
149 | editor->addItem("2");
|
150 | editor->addItem("3");
|
150 | editor->addItem("3");
|
151 | editor->addItem("4");
|
151 | editor->addItem("4");
|
152 | editor->addItem("5");
|
152 | editor->addItem("5");
|
153 | editor->addItem("6");
|
153 | editor->addItem("6");
|
154 | editor->addItem("7");
|
154 | editor->addItem("7");
|
155 | editor->addItem("8");
|
155 | editor->addItem("8");
|
156 | editor->addItem("9");
|
156 | editor->addItem("9");
|
157 | */
|
157 | */
|
158 | 158 | ||
159 | // editor->addItem("0");
|
159 | // editor->addItem("0");
|
160 | 160 | ||
161 | editor->addItems(Name_list); |
161 | editor->addItems(Name_list); |
162 | // editor->setEditable(false);
|
162 | // editor->setEditable(false);
|
163 | return editor; |
163 | return editor; |
164 | }
|
164 | }
|
165 | //! [1]
|
165 | //! [1]
|
166 | 166 | ||
167 | //! [2]
|
167 | //! [2]
|
168 | void SpinBoxDelegate::setEditorData(QWidget *editor, |
168 | void SpinBoxDelegate::setEditorData(QWidget *editor, |
169 | const QModelIndex &index) const |
169 | const QModelIndex &index) const |
170 | {
|
170 | {
|
171 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
171 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
172 | 172 | ||
173 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
173 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
174 | // spinBox->setValue(value);
|
174 | // spinBox->setValue(value);
|
175 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
175 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
176 | 176 | ||
177 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
177 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
178 | 178 | ||
179 | QStringList ID_list;
|
179 | QStringList ID_list;
|
180 | QStringList Name_list;
|
180 | QStringList Name_list;
|
181 | int curr_index; |
181 | int curr_index; |
182 | 182 | ||
183 | ID_list = items.keys(); |
183 | ID_list = items.keys(); |
184 | Name_list = items.values(); |
184 | Name_list = items.values(); |
185 | 185 | ||
186 | curr_index = ID_list.indexOf(value); |
186 | curr_index = ID_list.indexOf(value); |
187 | if (curr_index==-1) return; |
187 | if (curr_index==-1) return; |
188 | comboBox->setCurrentIndex(curr_index); |
188 | comboBox->setCurrentIndex(curr_index); |
189 | /****************************************************
|
189 | /****************************************************
|
190 | if (value == "0") comboBox->setCurrentIndex(0);
|
190 | if (value == "0") comboBox->setCurrentIndex(0);
|
191 | if (value == "1") comboBox->setCurrentIndex(1);
|
191 | if (value == "1") comboBox->setCurrentIndex(1);
|
192 | if (value == "2") comboBox->setCurrentIndex(2);
|
192 | if (value == "2") comboBox->setCurrentIndex(2);
|
193 | if (value == "3") comboBox->setCurrentIndex(3);
|
193 | if (value == "3") comboBox->setCurrentIndex(3);
|
194 | if (value == "4") comboBox->setCurrentIndex(4);
|
194 | if (value == "4") comboBox->setCurrentIndex(4);
|
195 | if (value == "5") comboBox->setCurrentIndex(5);
|
195 | if (value == "5") comboBox->setCurrentIndex(5);
|
196 | if (value == "6") comboBox->setCurrentIndex(6);
|
196 | if (value == "6") comboBox->setCurrentIndex(6);
|
197 | if (value == "7") comboBox->setCurrentIndex(7);
|
197 | if (value == "7") comboBox->setCurrentIndex(7);
|
198 | if (value == "8") comboBox->setCurrentIndex(8);
|
198 | if (value == "8") comboBox->setCurrentIndex(8);
|
199 | if (value == "9") comboBox->setCurrentIndex(9);
|
199 | if (value == "9") comboBox->setCurrentIndex(9);
|
200 | ******************************************************/
|
200 | ******************************************************/
|
201 | comboBox->setEditable(false); |
201 | comboBox->setEditable(false); |
202 | 202 | ||
203 | 203 | ||
204 | //comboBox->setItemText(0, value);
|
204 | //comboBox->setItemText(0, value);
|
205 | 205 | ||
206 | }
|
206 | }
|
207 | //! [2]
|
207 | //! [2]
|
208 | 208 | ||
209 | //! [3]
|
209 | //! [3]
|
210 | void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
210 | void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
211 | const QModelIndex &index) const |
211 | const QModelIndex &index) const |
212 | {
|
212 | {
|
213 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
213 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
214 | // spinBox->interpretText();
|
214 | // spinBox->interpretText();
|
215 | // int value = spinBox->value();
|
215 | // int value = spinBox->value();
|
216 | 216 | ||
217 | // model->setData(index, value, Qt::EditRole);
|
217 | // model->setData(index, value, Qt::EditRole);
|
218 | QStringList ID_list;
|
218 | QStringList ID_list;
|
219 | QStringList Name_list;
|
219 | QStringList Name_list;
|
220 | 220 | ||
221 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
221 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
222 | int currIndex; |
222 | int currIndex; |
223 | currIndex = comboBox->currentIndex(); |
223 | currIndex = comboBox->currentIndex(); |
224 | if (currIndex==-1) return; |
224 | if (currIndex==-1) return; |
225 | // QString value = comboBox->itemText(currIndex);
|
225 | // QString value = comboBox->itemText(currIndex);
|
226 | 226 | ||
227 | ID_list = items.keys(); |
227 | ID_list = items.keys(); |
228 | Name_list = items.values(); |
228 | Name_list = items.values(); |
229 | 229 | ||
230 | QString value = ID_list.at(currIndex); |
230 | QString value = ID_list.at(currIndex); |
231 | model->setData(index, value, Qt::EditRole); |
231 | model->setData(index, value, Qt::EditRole); |
232 | }
|
232 | }
|
233 | //! [3]
|
233 | //! [3]
|
234 | 234 | ||
235 | //! [4]
|
235 | //! [4]
|
236 | void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
236 | void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
237 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
237 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
238 | {
|
238 | {
|
239 | editor->setGeometry(option.rect); |
239 | editor->setGeometry(option.rect); |
240 | }
|
240 | }
|
241 | 241 | ||
242 | 242 | ||
243 | 243 | ||
244 | 244 | ||
245 | 245 | ||
246 | 246 | ||
247 | 247 | ||
248 | 248 | ||
249 | 249 | ||
250 | 250 | ||
251 | 251 | ||
252 | //! [0]
|
252 | //! [0]
|
253 | FilterSpinBoxDelegate::FilterSpinBoxDelegate(QObject *parent) |
253 | FilterSpinBoxDelegate::FilterSpinBoxDelegate(QObject *parent) |
254 | : QItemDelegate(parent) |
254 | : QItemDelegate(parent) |
255 | {
|
255 | {
|
256 | }
|
256 | }
|
257 | //! [0]
|
257 | //! [0]
|
258 | 258 | ||
259 | void FilterSpinBoxDelegate::setClassID(QString newClassID){ |
259 | void FilterSpinBoxDelegate::setClassID(QString newClassID){ |
260 | class_id = newClassID; |
260 | class_id = newClassID; |
261 | }
|
261 | }
|
262 | 262 | ||
263 | void FilterSpinBoxDelegate::getItems(){ |
263 | void FilterSpinBoxDelegate::getItems(){ |
264 | QMap <QString, QString> result_map; |
264 | QMap <QString, QString> result_map; |
265 | QSqlQuery q;
|
265 | QSqlQuery q;
|
266 | QString query_str;
|
266 | QString query_str;
|
267 | bool ok; |
267 | bool ok; |
268 | 268 | ||
269 | if (FilterSpinBoxDelegate::class_id.isEmpty()) |
269 | if (FilterSpinBoxDelegate::class_id.isEmpty()) |
270 | {
|
270 | {
|
271 | items.clear(); |
271 | items.clear(); |
272 | return; |
272 | return; |
273 | }
|
273 | }
|
274 | 274 | ||
275 | query_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '"); |
275 | query_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '"); |
276 | query_str.append(class_id); |
276 | query_str.append(class_id); |
277 | query_str.append( tr("'")); |
277 | query_str.append( tr("'")); |
278 | 278 | ||
279 | q.prepare(query_str); |
279 | q.prepare(query_str); |
280 | 280 | ||
281 | 281 | ||
282 | ok = q.exec(); |
282 | ok = q.exec(); |
283 | 283 | ||
284 | 284 | ||
285 | 285 | ||
286 | 286 | ||
287 | if (!ok) { |
287 | if (!ok) { |
288 | /*
|
288 | /*
|
289 | QString error_str;
|
289 | QString error_str;
|
290 | error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
|
290 | error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
|
291 | error_str.append(inctance);
|
291 | error_str.append(inctance);
|
292 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
292 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
293 | this, // Ðîäèòåëüñêèé âèäæåò.
|
293 | this, // Ðîäèòåëüñêèé âèäæåò.
|
294 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
294 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
295 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
295 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
296 | // tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
|
296 | // tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
|
297 | 297 | ||
298 | */
|
298 | */
|
299 | items.clear(); |
299 | items.clear(); |
300 | return; |
300 | return; |
301 | }
|
301 | }
|
302 | 302 | ||
303 | 303 | ||
304 | // field_inctance = q.record().indexOf(tr("TableWhithInstance"));
|
304 | // field_inctance = q.record().indexOf(tr("TableWhithInstance"));
|
305 | items.clear(); |
305 | items.clear(); |
306 | while(q.next()){ |
306 | while(q.next()){ |
307 | 307 | ||
308 | QString ID;
|
308 | QString ID;
|
309 | QString Name;
|
309 | QString Name;
|
310 | ID = q.value(0).toString(); // îïðåäåëÿåì ID |
310 | ID = q.value(0).toString(); // îïðåäåëÿåì ID |
311 | Name = q.value(1).toString(); // îïðåäåëÿåì Name |
311 | Name = q.value(1).toString(); // îïðåäåëÿåì Name |
312 | items.insert(ID, Name); |
312 | items.insert(ID, Name); |
313 | }
|
313 | }
|
314 | 314 | ||
315 | 315 | ||
316 | // return result_map;
|
316 | // return result_map;
|
317 | }
|
317 | }
|
318 | 318 | ||
319 | 319 | ||
320 | //! [1]
|
320 | //! [1]
|
321 | QWidget *FilterSpinBoxDelegate::createEditor(QWidget *parent, |
321 | QWidget *FilterSpinBoxDelegate::createEditor(QWidget *parent, |
322 | const QStyleOptionViewItem &/* option */, |
322 | const QStyleOptionViewItem &/* option */, |
323 | const QModelIndex &/* index */) const |
323 | const QModelIndex &/* index */) const |
324 | {
|
324 | {
|
325 | //QSpinBox *editor = new QSpinBox(parent);
|
325 | //QSpinBox *editor = new QSpinBox(parent);
|
326 | //editor->setMinimum(0);
|
326 | //editor->setMinimum(0);
|
327 | //editor->setMaximum(100);
|
327 | //editor->setMaximum(100);
|
328 | QStringList ID_list;
|
328 | QStringList ID_list;
|
329 | QStringList Name_list;
|
329 | QStringList Name_list;
|
330 | 330 | ||
331 | // getItems();
|
331 | // getItems();
|
332 | 332 | ||
333 | 333 | ||
334 | 334 | ||
335 | 335 | ||
336 | 336 | ||
337 | ID_list = items.keys(); |
337 | ID_list = items.keys(); |
338 | Name_list = items.values(); |
338 | Name_list = items.values(); |
339 | 339 | ||
340 | QComboBox *editor = new QComboBox(parent); |
340 | QComboBox *editor = new QComboBox(parent); |
341 | 341 | ||
342 | /*
|
342 | /*
|
343 | editor->addItem("0");
|
343 | editor->addItem("0");
|
344 | editor->addItem("1");
|
344 | editor->addItem("1");
|
345 | editor->addItem("2");
|
345 | editor->addItem("2");
|
346 | editor->addItem("3");
|
346 | editor->addItem("3");
|
347 | editor->addItem("4");
|
347 | editor->addItem("4");
|
348 | editor->addItem("5");
|
348 | editor->addItem("5");
|
349 | editor->addItem("6");
|
349 | editor->addItem("6");
|
350 | editor->addItem("7");
|
350 | editor->addItem("7");
|
351 | editor->addItem("8");
|
351 | editor->addItem("8");
|
352 | editor->addItem("9");
|
352 | editor->addItem("9");
|
353 | */
|
353 | */
|
354 | 354 | ||
355 | // editor->addItem("0");
|
355 | // editor->addItem("0");
|
356 | 356 | ||
357 | editor->addItems(Name_list); |
357 | editor->addItems(Name_list); |
358 | // editor->setEditable(false);
|
358 | // editor->setEditable(false);
|
359 | return editor; |
359 | return editor; |
360 | }
|
360 | }
|
361 | //! [1]
|
361 | //! [1]
|
362 | 362 | ||
363 | //! [2]
|
363 | //! [2]
|
364 | void FilterSpinBoxDelegate::setEditorData(QWidget *editor, |
364 | void FilterSpinBoxDelegate::setEditorData(QWidget *editor, |
365 | const QModelIndex &index) const |
365 | const QModelIndex &index) const |
366 | {
|
366 | {
|
367 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
367 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
368 | 368 | ||
369 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
369 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
370 | // spinBox->setValue(value);
|
370 | // spinBox->setValue(value);
|
371 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
371 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
372 | 372 | ||
373 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
373 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
374 | 374 | ||
375 | QStringList ID_list;
|
375 | QStringList ID_list;
|
376 | QStringList Name_list;
|
376 | QStringList Name_list;
|
377 | int curr_index; |
377 | int curr_index; |
378 | 378 | ||
379 | ID_list = items.keys(); |
379 | ID_list = items.keys(); |
380 | Name_list = items.values(); |
380 | Name_list = items.values(); |
381 | 381 | ||
382 | curr_index = ID_list.indexOf(value); |
382 | curr_index = ID_list.indexOf(value); |
383 | if (curr_index==-1) return; |
383 | if (curr_index==-1) return; |
384 | comboBox->setCurrentIndex(curr_index); |
384 | comboBox->setCurrentIndex(curr_index); |
385 | /****************************************************
|
385 | /****************************************************
|
386 | if (value == "0") comboBox->setCurrentIndex(0);
|
386 | if (value == "0") comboBox->setCurrentIndex(0);
|
387 | if (value == "1") comboBox->setCurrentIndex(1);
|
387 | if (value == "1") comboBox->setCurrentIndex(1);
|
388 | if (value == "2") comboBox->setCurrentIndex(2);
|
388 | if (value == "2") comboBox->setCurrentIndex(2);
|
389 | if (value == "3") comboBox->setCurrentIndex(3);
|
389 | if (value == "3") comboBox->setCurrentIndex(3);
|
390 | if (value == "4") comboBox->setCurrentIndex(4);
|
390 | if (value == "4") comboBox->setCurrentIndex(4);
|
391 | if (value == "5") comboBox->setCurrentIndex(5);
|
391 | if (value == "5") comboBox->setCurrentIndex(5);
|
392 | if (value == "6") comboBox->setCurrentIndex(6);
|
392 | if (value == "6") comboBox->setCurrentIndex(6);
|
393 | if (value == "7") comboBox->setCurrentIndex(7);
|
393 | if (value == "7") comboBox->setCurrentIndex(7);
|
394 | if (value == "8") comboBox->setCurrentIndex(8);
|
394 | if (value == "8") comboBox->setCurrentIndex(8);
|
395 | if (value == "9") comboBox->setCurrentIndex(9);
|
395 | if (value == "9") comboBox->setCurrentIndex(9);
|
396 | ******************************************************/
|
396 | ******************************************************/
|
397 | comboBox->setEditable(false); |
397 | comboBox->setEditable(false); |
398 | 398 | ||
399 | 399 | ||
400 | //comboBox->setItemText(0, value);
|
400 | //comboBox->setItemText(0, value);
|
401 | 401 | ||
402 | }
|
402 | }
|
403 | //! [2]
|
403 | //! [2]
|
404 | 404 | ||
405 | //! [3]
|
405 | //! [3]
|
406 | void FilterSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
406 | void FilterSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
407 | const QModelIndex &index) const |
407 | const QModelIndex &index) const |
408 | {
|
408 | {
|
409 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
409 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
410 | // spinBox->interpretText();
|
410 | // spinBox->interpretText();
|
411 | // int value = spinBox->value();
|
411 | // int value = spinBox->value();
|
412 | 412 | ||
413 | // model->setData(index, value, Qt::EditRole);
|
413 | // model->setData(index, value, Qt::EditRole);
|
414 | QStringList ID_list;
|
414 | QStringList ID_list;
|
415 | QStringList Name_list;
|
415 | QStringList Name_list;
|
416 | 416 | ||
417 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
417 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
418 | int currIndex; |
418 | int currIndex; |
419 | currIndex = comboBox->currentIndex(); |
419 | currIndex = comboBox->currentIndex(); |
420 | if (currIndex==-1) return; |
420 | if (currIndex==-1) return; |
421 | // QString value = comboBox->itemText(currIndex);
|
421 | // QString value = comboBox->itemText(currIndex);
|
422 | 422 | ||
423 | ID_list = items.keys(); |
423 | ID_list = items.keys(); |
424 | Name_list = items.values(); |
424 | Name_list = items.values(); |
425 | 425 | ||
426 | // QString value = ID_list.at(currIndex);
|
426 | // QString value = ID_list.at(currIndex);
|
427 | QString value = Name_list.at(currIndex); |
427 | QString value = Name_list.at(currIndex); |
428 | model->setData(index, value, Qt::EditRole); |
428 | model->setData(index, value, Qt::EditRole); |
429 | }
|
429 | }
|
430 | //! [3]
|
430 | //! [3]
|
431 | 431 | ||
432 | //! [4]
|
432 | //! [4]
|
433 | void FilterSpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
433 | void FilterSpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
434 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
434 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
435 | {
|
435 | {
|
436 | editor->setGeometry(option.rect); |
436 | editor->setGeometry(option.rect); |
437 | }
|
437 | }
|
438 | 438 | ||
439 | 439 | ||
440 | 440 | ||
441 | 441 | ||
442 | 442 | ||
443 | 443 | ||
444 | 444 | ||
445 | 445 | ||
446 | 446 | ||
447 | 447 | ||
448 | //! [0]
|
448 | //! [0]
|
449 | FilterConditionDelegate::FilterConditionDelegate(QObject *parent) |
449 | FilterConditionDelegate::FilterConditionDelegate(QObject *parent) |
450 | : QItemDelegate(parent) |
450 | : QItemDelegate(parent) |
451 | {
|
451 | {
|
452 | }
|
452 | }
|
453 | //! [0]
|
453 | //! [0]
|
454 | 454 | ||
455 | void FilterConditionDelegate::setClassID(QString newClassID){ |
455 | void FilterConditionDelegate::setClassID(QString newClassID){ |
456 | class_id = newClassID; |
456 | class_id = newClassID; |
457 | }
|
457 | }
|
458 | 458 | ||
459 | 459 | ||
460 | 460 | ||
461 | //! [1]
|
461 | //! [1]
|
462 | QWidget *FilterConditionDelegate::createEditor(QWidget *parent, |
462 | QWidget *FilterConditionDelegate::createEditor(QWidget *parent, |
463 | const QStyleOptionViewItem &/* option */, |
463 | const QStyleOptionViewItem &/* option */, |
464 | const QModelIndex &/* index */) const |
464 | const QModelIndex &/* index */) const |
465 | {
|
465 | {
|
466 | QStringList Name_list;
|
466 | QStringList Name_list;
|
467 | 467 | ||
468 | Name_list = items; |
468 | Name_list = items; |
469 | 469 | ||
470 | QComboBox *editor = new QComboBox(parent); |
470 | QComboBox *editor = new QComboBox(parent); |
471 | 471 | ||
472 | /*
|
472 | /*
|
473 | editor->addItem("0");
|
473 | editor->addItem("0");
|
474 | editor->addItem("1");
|
474 | editor->addItem("1");
|
475 | editor->addItem("2");
|
475 | editor->addItem("2");
|
476 | editor->addItem("3");
|
476 | editor->addItem("3");
|
477 | editor->addItem("4");
|
477 | editor->addItem("4");
|
478 | editor->addItem("5");
|
478 | editor->addItem("5");
|
479 | editor->addItem("6");
|
479 | editor->addItem("6");
|
480 | editor->addItem("7");
|
480 | editor->addItem("7");
|
481 | editor->addItem("8");
|
481 | editor->addItem("8");
|
482 | editor->addItem("9");
|
482 | editor->addItem("9");
|
483 | */
|
483 | */
|
484 | 484 | ||
485 | // editor->addItem("0");
|
485 | // editor->addItem("0");
|
486 | 486 | ||
487 | editor->addItems(Name_list); |
487 | editor->addItems(Name_list); |
488 | // editor->setEditable(false);
|
488 | // editor->setEditable(false);
|
489 | return editor; |
489 | return editor; |
490 | }
|
490 | }
|
491 | //! [1]
|
491 | //! [1]
|
492 | 492 | ||
493 | //! [2]
|
493 | //! [2]
|
494 | void FilterConditionDelegate::setEditorData(QWidget *editor, |
494 | void FilterConditionDelegate::setEditorData(QWidget *editor, |
495 | const QModelIndex &index) const |
495 | const QModelIndex &index) const |
496 | {
|
496 | {
|
497 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
497 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
498 | 498 | ||
499 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
499 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
500 | // spinBox->setValue(value);
|
500 | // spinBox->setValue(value);
|
501 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
501 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
502 | 502 | ||
503 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
503 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
504 | 504 | ||
505 | //QStringList ID_list;
|
505 | //QStringList ID_list;
|
506 | //QStringList Name_list;
|
506 | //QStringList Name_list;
|
507 | int curr_index; |
507 | int curr_index; |
508 | 508 | ||
509 | //ID_list = items.keys();
|
509 | //ID_list = items.keys();
|
510 | //Name_list = items.values();
|
510 | //Name_list = items.values();
|
511 | 511 | ||
512 | curr_index = items.indexOf(value); |
512 | curr_index = items.indexOf(value); |
513 | if (curr_index==-1) return; |
513 | if (curr_index==-1) return; |
514 | comboBox->setCurrentIndex(curr_index); |
514 | comboBox->setCurrentIndex(curr_index); |
515 | /****************************************************
|
515 | /****************************************************
|
516 | if (value == "0") comboBox->setCurrentIndex(0);
|
516 | if (value == "0") comboBox->setCurrentIndex(0);
|
517 | if (value == "1") comboBox->setCurrentIndex(1);
|
517 | if (value == "1") comboBox->setCurrentIndex(1);
|
518 | if (value == "2") comboBox->setCurrentIndex(2);
|
518 | if (value == "2") comboBox->setCurrentIndex(2);
|
519 | if (value == "3") comboBox->setCurrentIndex(3);
|
519 | if (value == "3") comboBox->setCurrentIndex(3);
|
520 | if (value == "4") comboBox->setCurrentIndex(4);
|
520 | if (value == "4") comboBox->setCurrentIndex(4);
|
521 | if (value == "5") comboBox->setCurrentIndex(5);
|
521 | if (value == "5") comboBox->setCurrentIndex(5);
|
522 | if (value == "6") comboBox->setCurrentIndex(6);
|
522 | if (value == "6") comboBox->setCurrentIndex(6);
|
523 | if (value == "7") comboBox->setCurrentIndex(7);
|
523 | if (value == "7") comboBox->setCurrentIndex(7);
|
524 | if (value == "8") comboBox->setCurrentIndex(8);
|
524 | if (value == "8") comboBox->setCurrentIndex(8);
|
525 | if (value == "9") comboBox->setCurrentIndex(9);
|
525 | if (value == "9") comboBox->setCurrentIndex(9);
|
526 | ******************************************************/
|
526 | ******************************************************/
|
527 | comboBox->setEditable(false); |
527 | comboBox->setEditable(false); |
528 | 528 | ||
529 | 529 | ||
530 | //comboBox->setItemText(0, value);
|
530 | //comboBox->setItemText(0, value);
|
531 | 531 | ||
532 | }
|
532 | }
|
533 | //! [2]
|
533 | //! [2]
|
534 | 534 | ||
535 | //! [3]
|
535 | //! [3]
|
536 | void FilterConditionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
536 | void FilterConditionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
537 | const QModelIndex &index) const |
537 | const QModelIndex &index) const |
538 | {
|
538 | {
|
539 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
539 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
540 | // spinBox->interpretText();
|
540 | // spinBox->interpretText();
|
541 | // int value = spinBox->value();
|
541 | // int value = spinBox->value();
|
542 | 542 | ||
543 | // model->setData(index, value, Qt::EditRole);
|
543 | // model->setData(index, value, Qt::EditRole);
|
544 | QStringList ID_list;
|
544 | QStringList ID_list;
|
545 | QStringList Name_list;
|
545 | QStringList Name_list;
|
546 | 546 | ||
547 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
547 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
548 | int currIndex; |
548 | int currIndex; |
549 | currIndex = comboBox->currentIndex(); |
549 | currIndex = comboBox->currentIndex(); |
550 | if (currIndex==-1) return; |
550 | if (currIndex==-1) return; |
551 | // QString value = comboBox->itemText(currIndex);
|
551 | // QString value = comboBox->itemText(currIndex);
|
552 | 552 | ||
553 | // ID_list = items.keys();
|
553 | // ID_list = items.keys();
|
554 | // Name_list = items.values();
|
554 | // Name_list = items.values();
|
555 | 555 | ||
556 | // QString value = ID_list.at(currIndex);
|
556 | // QString value = ID_list.at(currIndex);
|
557 | QString value = items.at(currIndex); |
557 | QString value = items.at(currIndex); |
558 | model->setData(index, value, Qt::EditRole); |
558 | model->setData(index, value, Qt::EditRole); |
559 | }
|
559 | }
|
560 | //! [3]
|
560 | //! [3]
|
561 | 561 | ||
562 | //! [4]
|
562 | //! [4]
|
563 | void FilterConditionDelegate::updateEditorGeometry(QWidget *editor, |
563 | void FilterConditionDelegate::updateEditorGeometry(QWidget *editor, |
564 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
564 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
565 | {
|
565 | {
|
566 | editor->setGeometry(option.rect); |
566 | editor->setGeometry(option.rect); |
567 | }
|
567 | }
|
568 | 568 | ||
569 | 569 | ||
570 | 570 | ||
571 | 571 | ||
572 | 572 | ||
573 | 573 | ||
574 | 574 | ||
575 | 575 | ||
576 | 576 | ||
577 | 577 | ||
578 | FilterValueDelegate::FilterValueDelegate(QObject *parent) |
578 | FilterValueDelegate::FilterValueDelegate(QObject *parent) |
579 | : QItemDelegate(parent) |
579 | : QItemDelegate(parent) |
580 | {
|
580 | {
|
581 | }
|
581 | }
|
582 | //! [0]
|
582 | //! [0]
|
583 | 583 | ||
584 | void FilterValueDelegate::setClassID(QString newClassID){ |
584 | void FilterValueDelegate::setClassID(QString newClassID){ |
585 | class_id = newClassID; |
585 | class_id = newClassID; |
586 | }
|
586 | }
|
587 | 587 | ||
588 | 588 | ||
589 | 589 | ||
590 | // ïîëó÷àåì èìÿ òàáëèöû - õðàíèëèùà îáúåêòîâ êëàññà
|
590 | // ïîëó÷àåì èìÿ òàáëèöû - õðàíèëèùà îáúåêòîâ êëàññà
|
591 | QString FilterValueDelegate::getClassInctanc(QString class_id){ |
591 | QString FilterValueDelegate::getClassInctanc(QString class_id){ |
592 | 592 | ||
593 | 593 | ||
594 | 594 | ||
595 | 595 | ||
596 | QSqlQuery q;
|
596 | QSqlQuery q;
|
597 | QString sql_str;
|
597 | QString sql_str;
|
598 | QString result;
|
598 | QString result;
|
599 | QString classInctance;
|
599 | QString classInctance;
|
600 | int field_inctance; |
600 | int field_inctance; |
601 | bool ok; |
601 | bool ok; |
602 | sql_str = tr("select * " |
602 | sql_str = tr("select * " |
603 | " from ListOfClasses where ListOfClasses.ID = '" //îïðåäåëèì êëàññû, äëÿ êîòîðûõ êîðíåâîé ÿâëÿåòñÿ êîíòåéíåðîì |
603 | " from ListOfClasses where ListOfClasses.ID = '" //îïðåäåëèì êëàññû, äëÿ êîòîðûõ êîðíåâîé ÿâëÿåòñÿ êîíòåéíåðîì |
604 | ); |
604 | ); |
605 | sql_str.append(class_id); |
605 | sql_str.append(class_id); |
606 | sql_str.append(tr("'")); |
606 | sql_str.append(tr("'")); |
607 | q.prepare(sql_str); |
607 | q.prepare(sql_str); |
608 | 608 | ||
609 | ok = q.exec(); |
609 | ok = q.exec(); |
610 | 610 | ||
611 | if (!ok) return result; |
611 | if (!ok) return result; |
612 | 612 | ||
613 | /*
|
613 | /*
|
614 | if (!ok) {
|
614 | if (!ok) {
|
615 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
615 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
616 | this, // Ðîäèòåëüñêèé âèäæåò.
|
616 | this, // Ðîäèòåëüñêèé âèäæåò.
|
617 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
617 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
618 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
618 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
619 | return result;
|
619 | return result;
|
620 | }
|
620 | }
|
621 | 621 | ||
622 | */
|
622 | */
|
623 | 623 | ||
624 | 624 | ||
625 | 625 | ||
626 | 626 | ||
627 | field_inctance = q.record().indexOf(tr("TableWhithInstance")); |
627 | field_inctance = q.record().indexOf(tr("TableWhithInstance")); |
628 | while(q.next()){ |
628 | while(q.next()){ |
629 | 629 | ||
630 | classInctance = q.value(field_inctance).toString(); |
630 | classInctance = q.value(field_inctance).toString(); |
631 | result.append(classInctance); |
631 | result.append(classInctance); |
632 | }
|
632 | }
|
633 | 633 | ||
634 | return result; |
634 | return result; |
635 | 635 | ||
636 | 636 | ||
637 | 637 | ||
638 | 638 | ||
639 | 639 | ||
640 | }
|
640 | }
|
641 | 641 | ||
642 | 642 | ||
643 | 643 | ||
644 | QString grtFieldType(QString class_id, QString fieldName)//îïðåäåëÿåì òèï äàííûõ ïîëÿ |
644 | QString FilterValueDelegate::getFieldType(QString class_id, QString fieldName)//îïðåäåëÿåì òèï äàííûõ ïîëÿ |
645 | {
|
645 | {
|
646 | 646 | ||
647 | }
|
647 | }
|
648 | 648 | ||
649 | 649 | ||
- | 650 | ||
- | 651 | ||
- | 652 | ||
- | 653 | ||
- | 654 | ||
650 | //! [1]
|
655 | //! [1]
|
651 | QWidget *FilterValueDelegate::createEditor(QWidget *parent, |
656 | QWidget *FilterValueDelegate::createEditor(QWidget *parent, |
652 | const QStyleOptionViewItem &/* option */, |
657 | const QStyleOptionViewItem &/* option */, |
653 | const QModelIndex &/* index */) const |
658 | const QModelIndex &/* index */) const |
654 | {
|
659 | {
|
655 | QStringList Name_list;
|
660 | QStringList Name_list;
|
656 | 661 | ||
657 | Name_list = items; |
662 | Name_list = items; |
658 | 663 | ||
659 | QComboBox *editor = new QComboBox(parent); |
664 | QComboBox *editor = new QComboBox(parent); |
660 | 665 | ||
661 | /*
|
666 | /*
|
662 | editor->addItem("0");
|
667 | editor->addItem("0");
|
663 | editor->addItem("1");
|
668 | editor->addItem("1");
|
664 | editor->addItem("2");
|
669 | editor->addItem("2");
|
665 | editor->addItem("3");
|
670 | editor->addItem("3");
|
666 | editor->addItem("4");
|
671 | editor->addItem("4");
|
667 | editor->addItem("5");
|
672 | editor->addItem("5");
|
668 | editor->addItem("6");
|
673 | editor->addItem("6");
|
669 | editor->addItem("7");
|
674 | editor->addItem("7");
|
670 | editor->addItem("8");
|
675 | editor->addItem("8");
|
671 | editor->addItem("9");
|
676 | editor->addItem("9");
|
672 | */
|
677 | */
|
673 | 678 | ||
674 | // editor->addItem("0");
|
679 | // editor->addItem("0");
|
675 | 680 | ||
676 | // // // editor->addItems(Name_list);
|
681 | // // // editor->addItems(Name_list);
|
677 | editor->setEditable(true); |
682 | editor->setEditable(true); |
678 | return editor; |
683 | return editor; |
679 | }
|
684 | }
|
680 | //! [1]
|
685 | //! [1]
|
681 | 686 | ||
682 | 687 | ||
683 | 688 | ||
684 | 689 | ||
- | 690 | TField FilterValueDelegate::getField(const QModelIndex &index) // ïî èíäåêñó (â òàáëèöå ôèëüòðîâ) ïîëó÷èì èíôîðìàöèþ î ïîëå |
|
- | 691 | {
|
|
- | 692 | const QAbstractItemModel * model = index.model(); |
|
- | 693 | ||
- | 694 | TField field_result;
|
|
- | 695 | QSqlQuery q;
|
|
- | 696 | QString sql_str;
|
|
- | 697 | bool ok; |
|
- | 698 | ||
- | 699 | ||
- | 700 | // QString fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
|
|
- | 701 | // QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
|
|
- | 702 | field_result.FieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString(); |
|
- | 703 | field_result.ClassIdentifer = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString(); |
|
- | 704 | ||
- | 705 | sql_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '"); |
|
- | 706 | sql_str.append(field_result.ClassIdentifer); |
|
- | 707 | sql_str.append(tr("' and `FieldName` = '")); |
|
- | 708 | sql_str.append(field_result.FieldName); |
|
- | 709 | sql_str.append(tr("'")); |
|
- | 710 | ||
- | 711 | ok = q.prepare(sql_str); |
|
- | 712 | ||
- | 713 | if(!ok) return field_result; |
|
- | 714 | ||
- | 715 | ok = q.exec(); |
|
- | 716 | ||
- | 717 | if(!ok) return field_result; |
|
- | 718 | ||
- | 719 | int ID_index = q.record().indexOf(tr("ID")); |
|
685 | 720 | ||
- | 721 | int FieldAlias_index = q.record().indexOf(tr("FieldAlias")); |
|
- | 722 | ||
- | 723 | int FieldType_index = q.record().indexOf(tr("FieldType")); |
|
- | 724 | ||
- | 725 | int DefaultValue_index = q.record().indexOf(tr("DefaultValue")); |
|
- | 726 | ||
- | 727 | int Comment_index = q.record().indexOf(tr("Comment")); |
|
- | 728 | ||
- | 729 | ||
- | 730 | ||
- | 731 | while(q.next()){ |
|
- | 732 | field_result.ID = q.value(ID_index).asString(); |
|
- | 733 | field_result.FieldAlias = q.value(FieldAlias_index).asString(); |
|
- | 734 | field_result.FieldType = q.value(FieldType_index).asString(); |
|
- | 735 | field_result.DefaultValue = q.value(DefaultValue_index).asString(); |
|
- | 736 | field_result.Comment = q.value(Comment_index).asString(); |
|
- | 737 | }
|
|
- | 738 | ||
- | 739 | return field_result; |
|
- | 740 | ||
- | 741 | ||
- | 742 | ||
- | 743 | }
|
|
686 | 744 | ||
687 | 745 | ||
688 | 746 | ||
689 | 747 | ||
690 | 748 | ||
691 | //! [2]
|
749 | //! [2]
|
692 | void FilterValueDelegate::setEditorData(QWidget *editor, |
750 | void FilterValueDelegate::setEditorData(QWidget *editor, |
693 | const QModelIndex &index) const |
751 | const QModelIndex &index) const |
694 | {
|
752 | {
|
695 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
753 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
696 | 754 | ||
697 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
755 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
698 | // spinBox->setValue(value);
|
756 | // spinBox->setValue(value);
|
699 | 757 | ||
700 | 758 | ||
701 | const QAbstractItemModel * model = index.model(); |
759 | const QAbstractItemModel * model = index.model(); |
702 | 760 | ||
703 | 761 | ||
704 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
762 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
705 | QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString(); |
763 | QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString(); |
706 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
764 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
707 | QString inctance; //õðàíèëèùå îáúåêòîâ êëàññà |
765 | QString inctance; //õðàíèëèùå îáúåêòîâ êëàññà |
708 | QString fieldName;//èìÿ ïîëÿ |
766 | QString fieldName;//èìÿ ïîëÿ |
709 | QSqlQuery q;
|
767 | QSqlQuery q;
|
710 | QString sql_str;
|
768 | QString sql_str;
|
711 | QStringList itemsList;
|
769 | QStringList itemsList;
|
712 | bool ok; |
770 | bool ok; |
- | 771 | TField Field;
|
|
- | 772 | ||
- | 773 | Field = getField(index); |
|
- | 774 | ||
- | 775 | ||
- | 776 | ||
713 | 777 | ||
714 | 778 | ||
715 | 779 | ||
716 | fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString(); |
780 | fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString(); |
717 | 781 | ||
718 | 782 | ||
719 | inctance = FilterValueDelegate::getClassInctanc(class_id); |
783 | inctance = FilterValueDelegate::getClassInctanc(class_id); |
720 | if (!inctance.isEmpty() && !fieldName.isEmpty()){ |
784 | if (!inctance.isEmpty() && !fieldName.isEmpty()){ |
721 | sql_str = tr("select `"); |
785 | sql_str = tr("select `"); |
722 | sql_str.append(fieldName); |
786 | sql_str.append(fieldName); |
723 | sql_str.append(tr("` from `")); |
787 | sql_str.append(tr("` from `")); |
724 | sql_str.append(inctance); |
788 | sql_str.append(inctance); |
725 | sql_str.append(tr("`")); |
789 | sql_str.append(tr("`")); |
726 | ok = q.prepare(sql_str); |
790 | ok = q.prepare(sql_str); |
727 | ok = q.exec(); |
791 | ok = q.exec(); |
728 | 792 | ||
729 | while(q.next()){ |
793 | while(q.next()){ |
730 | QString tmp_str;
|
794 | QString tmp_str;
|
731 | tmp_str = q.record().value(fieldName).asString(); |
795 | tmp_str = q.record().value(fieldName).asString(); |
732 | itemsList.append(tmp_str); |
796 | itemsList.append(tmp_str); |
733 | FilterValueDelegate::items.append(tmp_str); |
797 | FilterValueDelegate::items.append(tmp_str); |
734 | }
|
798 | }
|
735 | if(!itemsList.isEmpty()) { |
799 | if(!itemsList.isEmpty()) { |
736 | comboBox->addItems(itemsList); |
800 | comboBox->addItems(itemsList); |
737 | items = itemsList; // ãëîáàëüãàÿ ïåðåìåííàÿ |
801 | items = itemsList; // ãëîáàëüãàÿ ïåðåìåííàÿ |
738 | }
|
802 | }
|
739 | 803 | ||
740 | }
|
804 | }
|
741 | 805 | ||
742 | 806 | ||
743 | 807 | ||
744 | 808 | ||
745 | 809 | ||
746 | //QStringList ID_list;
|
810 | //QStringList ID_list;
|
747 | //QStringList Name_list;
|
811 | //QStringList Name_list;
|
748 | int curr_index; |
812 | int curr_index; |
749 | 813 | ||
750 | //ID_list = items.keys();
|
814 | //ID_list = items.keys();
|
751 | //Name_list = items.values();
|
815 | //Name_list = items.values();
|
752 | 816 | ||
753 | curr_index = itemsList.indexOf(value); |
817 | curr_index = itemsList.indexOf(value); |
754 | if (curr_index==-1) return; |
818 | if (curr_index==-1) return; |
755 | comboBox->setCurrentIndex(curr_index); |
819 | comboBox->setCurrentIndex(curr_index); |
756 | /****************************************************
|
820 | /****************************************************
|
757 | if (value == "0") comboBox->setCurrentIndex(0);
|
821 | if (value == "0") comboBox->setCurrentIndex(0);
|
758 | if (value == "1") comboBox->setCurrentIndex(1);
|
822 | if (value == "1") comboBox->setCurrentIndex(1);
|
759 | if (value == "2") comboBox->setCurrentIndex(2);
|
823 | if (value == "2") comboBox->setCurrentIndex(2);
|
760 | if (value == "3") comboBox->setCurrentIndex(3);
|
824 | if (value == "3") comboBox->setCurrentIndex(3);
|
761 | if (value == "4") comboBox->setCurrentIndex(4);
|
825 | if (value == "4") comboBox->setCurrentIndex(4);
|
762 | if (value == "5") comboBox->setCurrentIndex(5);
|
826 | if (value == "5") comboBox->setCurrentIndex(5);
|
763 | if (value == "6") comboBox->setCurrentIndex(6);
|
827 | if (value == "6") comboBox->setCurrentIndex(6);
|
764 | if (value == "7") comboBox->setCurrentIndex(7);
|
828 | if (value == "7") comboBox->setCurrentIndex(7);
|
765 | if (value == "8") comboBox->setCurrentIndex(8);
|
829 | if (value == "8") comboBox->setCurrentIndex(8);
|
766 | if (value == "9") comboBox->setCurrentIndex(9);
|
830 | if (value == "9") comboBox->setCurrentIndex(9);
|
767 | ******************************************************/
|
831 | ******************************************************/
|
768 | comboBox->setEditable(true); |
832 | comboBox->setEditable(true); |
769 | 833 | ||
770 | 834 | ||
771 | //comboBox->setItemText(0, value);
|
835 | //comboBox->setItemText(0, value);
|
772 | 836 | ||
773 | }
|
837 | }
|
774 | //! [2]
|
838 | //! [2]
|
775 | 839 | ||
776 | //! [3]
|
840 | //! [3]
|
777 | void FilterValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
841 | void FilterValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
778 | const QModelIndex &index) const |
842 | const QModelIndex &index) const |
779 | {
|
843 | {
|
780 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
844 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
781 | // spinBox->interpretText();
|
845 | // spinBox->interpretText();
|
782 | // int value = spinBox->value();
|
846 | // int value = spinBox->value();
|
783 | 847 | ||
784 | // model->setData(index, value, Qt::EditRole);
|
848 | // model->setData(index, value, Qt::EditRole);
|
785 | 849 | ||
786 | QStringList ID_list;
|
850 | QStringList ID_list;
|
787 | QStringList Name_list;
|
851 | QStringList Name_list;
|
788 | 852 | ||
789 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
853 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
790 | int currIndex; |
854 | int currIndex; |
791 | currIndex = comboBox->currentIndex(); |
855 | currIndex = comboBox->currentIndex(); |
792 | if (currIndex==-1) return; |
856 | if (currIndex==-1) return; |
793 | // QString value = comboBox->itemText(currIndex);
|
857 | // QString value = comboBox->itemText(currIndex);
|
794 | 858 | ||
795 | // ID_list = items.keys();
|
859 | // ID_list = items.keys();
|
796 | // Name_list = items.values();
|
860 | // Name_list = items.values();
|
797 | 861 | ||
798 | // QString value = ID_list.at(currIndex);
|
862 | // QString value = ID_list.at(currIndex);
|
799 | QString value = items.at(currIndex); |
863 | QString value = items.at(currIndex); |
800 | model->setData(index, value, Qt::EditRole); |
864 | model->setData(index, value, Qt::EditRole); |
801 | 865 | ||
802 | 866 | ||
803 | }
|
867 | }
|
804 | 868 | ||
805 | 869 | ||
806 | 870 | ||
807 | 871 | ||
808 | 872 | ||
809 | 873 | ||
810 | 874 | ||
811 | 875 | ||
812 | //! [3]
|
876 | //! [3]
|
813 | 877 | ||
814 | //! [4]
|
878 | //! [4]
|
815 | void FilterValueDelegate::updateEditorGeometry(QWidget *editor, |
879 | void FilterValueDelegate::updateEditorGeometry(QWidget *editor, |
816 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
880 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
817 | {
|
881 | {
|
818 | editor->setGeometry(option.rect); |
882 | editor->setGeometry(option.rect); |
819 | }
|
883 | }
|
820 | 884 | ||
821 | 885 | ||
822 | 886 | ||
823 | 887 | ||
824 | 888 | ||
825 | 889 | ||
826 | 890 | ||
827 | 891 | ||
828 | 892 | ||
829 | 893 | ||
830 | 894 | ||
831 | 895 | ||
832 | 896 | ||
833 | 897 | ||
834 | 898 | ||
835 | //! [4]
|
899 | //! [4]
|
836 | MyDEDelegate::MyDEDelegate( |
900 | MyDEDelegate::MyDEDelegate( |
837 | bool calpopup,
|
901 | bool calpopup,
|
838 | QObject *parent) |
902 | QObject *parent) |
839 | : QItemDelegate(parent), |
903 | : QItemDelegate(parent), |
840 | m_calpopup(calpopup) { |
904 | m_calpopup(calpopup) { |
841 | }
|
905 | }
|
842 | 906 | ||
843 | QWidget *MyDEDelegate::createEditor( |
907 | QWidget *MyDEDelegate::createEditor( |
844 | QWidget *parent,
|
908 | QWidget *parent,
|
845 | const QStyleOptionViewItem& /* option */, |
909 | const QStyleOptionViewItem& /* option */, |
846 | const QModelIndex& /* index */) const { |
910 | const QModelIndex& /* index */) const { |
847 | QDateEdit *editor = new QDateEdit(parent); |
911 | QDateEdit *editor = new QDateEdit(parent); |
848 | editor->setCalendarPopup(m_calpopup); |
912 | editor->setCalendarPopup(m_calpopup); |
849 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
913 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
850 | return editor; |
914 | return editor; |
851 | }
|
915 | }
|
852 | 916 | ||
853 | void MyDEDelegate::setEditorData( |
917 | void MyDEDelegate::setEditorData( |
854 | QWidget *editor,
|
918 | QWidget *editor,
|
855 | const QModelIndex &index) const { |
919 | const QModelIndex &index) const { |
856 | QDate value = index.model()->data( |
920 | QDate value = index.model()->data( |
857 | index, Qt::EditRole).toDate(); |
921 | index, Qt::EditRole).toDate(); |
858 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
922 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
859 | de->setDate(value); |
923 | de->setDate(value); |
860 | }
|
924 | }
|
861 | 925 | ||
862 | void MyDEDelegate::setModelData( |
926 | void MyDEDelegate::setModelData( |
863 | QWidget *editor,
|
927 | QWidget *editor,
|
864 | QAbstractItemModel *model,
|
928 | QAbstractItemModel *model,
|
865 | const QModelIndex& index) const { |
929 | const QModelIndex& index) const { |
866 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
930 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
867 | de->interpretText(); |
931 | de->interpretText(); |
868 | QDate value = de->date(); |
932 | QDate value = de->date(); |
869 | model->setData(index, value); |
933 | model->setData(index, value); |
870 | }
|
934 | }
|
871 | 935 | ||
872 | void MyDEDelegate::updateEditorGeometry( |
936 | void MyDEDelegate::updateEditorGeometry( |
873 | QWidget *editor,
|
937 | QWidget *editor,
|
874 | const QStyleOptionViewItem &option, |
938 | const QStyleOptionViewItem &option, |
875 | const QModelIndex& /* index */) const { |
939 | const QModelIndex& /* index */) const { |
876 | 940 | ||
877 | 941 | ||
878 | 942 | ||
879 | editor->setGeometry(option.rect); |
943 | editor->setGeometry(option.rect); |
880 | }
|
944 | }
|
881 | 945 | ||
882 | 946 | ||
883 | 947 | ||
884 | 948 | ||
885 | 949 | ||
886 | CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent) |
950 | CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent) |
887 | {
|
951 | {
|
888 | }
|
952 | }
|
889 | 953 | ||
890 | void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
954 | void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
891 | {
|
955 | {
|
892 | m_pxPicture.fill( QColor(Qt::white) ); |
956 | m_pxPicture.fill( QColor(Qt::white) ); |
893 | 957 | ||
894 | const QAbstractItemModel * model = index.model(); |
958 | const QAbstractItemModel * model = index.model(); |
895 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
959 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
896 | 960 | ||
897 | if ( !sFileName.isEmpty() ) |
961 | if ( !sFileName.isEmpty() ) |
898 | m_pxPicture.load( sFileName ); |
962 | m_pxPicture.load( sFileName ); |
899 | else { |
963 | else { |
900 | //QItemDelegate::paint(painter, option, index);
|
964 | //QItemDelegate::paint(painter, option, index);
|
901 | return; |
965 | return; |
902 | }
|
966 | }
|
903 | 967 | ||
904 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
968 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
905 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
969 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
906 | QPalette::Disabled; |
970 | QPalette::Disabled; |
907 | 971 | ||
908 | if (option.state & QStyle::State_Selected) |
972 | if (option.state & QStyle::State_Selected) |
909 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
973 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
910 | 974 | ||
911 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
975 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
912 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
976 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
913 | painter->drawPixmap( nX, nY, m_pxPicture ); |
977 | painter->drawPixmap( nX, nY, m_pxPicture ); |
914 | 978 | ||
915 | 979 | ||
916 | //// drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
980 | //// drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
917 | 981 | ||
918 | /*
|
982 | /*
|
919 | QPen pen = painter->pen();
|
983 | QPen pen = painter->pen();
|
920 | painter->setPen(option.palette.color(QPalette::Mid));
|
984 | painter->setPen(option.palette.color(QPalette::Mid));
|
921 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
985 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
922 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
986 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
923 | painter->setPen(pen);
|
987 | painter->setPen(pen);
|
924 | */
|
988 | */
|
925 | }
|
989 | }
|
926 | 990 | ||
927 | 991 | ||
928 | 992 | ||
929 | 993 | ||
930 | void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
994 | void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
931 | const QModelIndex &index) const |
995 | const QModelIndex &index) const |
932 | {
|
996 | {
|
933 | int datetime = index.model()->data(index, Qt::DisplayRole).toInt(); |
997 | int datetime = index.model()->data(index, Qt::DisplayRole).toInt(); |
934 | 998 | ||
935 | QString indexvalue = ""; |
999 | QString indexvalue = ""; |
936 | 1000 | ||
937 | if (datetime > 0) |
1001 | if (datetime > 0) |
938 | {
|
1002 | {
|
939 | QDateTime dateTime2 = QDateTime(); |
1003 | QDateTime dateTime2 = QDateTime(); |
940 | dateTime2.setTime_t(datetime); |
1004 | dateTime2.setTime_t(datetime); |
941 | indexvalue = dateTime2.toString(this->timeformat); |
1005 | indexvalue = dateTime2.toString(this->timeformat); |
942 | }
|
1006 | }
|
943 | else
|
1007 | else
|
944 | {
|
1008 | {
|
945 | indexvalue = tr("Date not set"); |
1009 | indexvalue = tr("Date not set"); |
946 | }
|
1010 | }
|
947 | 1011 | ||
948 | Q_ASSERT(index.isValid()); |
1012 | Q_ASSERT(index.isValid()); |
949 | 1013 | ||
950 | QStyleOptionViewItemV3 opt = setOptions(index, option); |
1014 | QStyleOptionViewItemV3 opt = setOptions(index, option); |
951 | 1015 | ||
952 | const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option); |
1016 | const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option); |
953 | opt.features = v2 ? v2->features |
1017 | opt.features = v2 ? v2->features |
954 | : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None); |
1018 | : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None); |
955 | const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option); |
1019 | const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option); |
956 | opt.locale = v3 ? v3->locale : QLocale(); |
1020 | opt.locale = v3 ? v3->locale : QLocale(); |
957 | opt.widget = v3 ? v3->widget : 0; |
1021 | opt.widget = v3 ? v3->widget : 0; |
958 | 1022 | ||
959 | // prepare
|
1023 | // prepare
|
960 | painter->save(); |
1024 | painter->save(); |
961 | 1025 | ||
962 | painter->setClipRect(opt.rect); |
1026 | painter->setClipRect(opt.rect); |
963 | 1027 | ||
964 | // get the data and the rectangles
|
1028 | // get the data and the rectangles
|
965 | QVariant value;
|
1029 | QVariant value;
|
966 | 1030 | ||
967 | QPixmap pixmap;
|
1031 | QPixmap pixmap;
|
968 | QRect decorationRect;
|
1032 | QRect decorationRect;
|
969 | value = index.data(Qt::DecorationRole); |
1033 | value = index.data(Qt::DecorationRole); |
970 | 1034 | ||
971 | QString text;
|
1035 | QString text;
|
972 | QRect displayRect;
|
1036 | QRect displayRect;
|
973 | value = index.data(Qt::DisplayRole); |
1037 | value = index.data(Qt::DisplayRole); |
974 | if (value.isValid()) { |
1038 | if (value.isValid()) { |
975 | text = indexvalue; |
1039 | text = indexvalue; |
976 | displayRect = textRectangle(painter, option.rect, opt.font, text); |
1040 | displayRect = textRectangle(painter, option.rect, opt.font, text); |
977 | }
|
1041 | }
|
978 | 1042 | ||
979 | QRect checkRect;
|
1043 | QRect checkRect;
|
980 | Qt::CheckState checkState = Qt::Unchecked; |
1044 | Qt::CheckState checkState = Qt::Unchecked; |
981 | value = index.data(Qt::CheckStateRole); |
1045 | value = index.data(Qt::CheckStateRole); |
982 | if (value.isValid()) { |
1046 | if (value.isValid()) { |
983 | checkState = static_cast<Qt::CheckState>(value.toInt()); |
1047 | checkState = static_cast<Qt::CheckState>(value.toInt()); |
984 | checkRect = check(opt, opt.rect, value); |
1048 | checkRect = check(opt, opt.rect, value); |
985 | }
|
1049 | }
|
986 | 1050 | ||
987 | // do the layout
|
1051 | // do the layout
|
988 | doLayout(opt, &checkRect, &decorationRect, &displayRect, false); |
1052 | doLayout(opt, &checkRect, &decorationRect, &displayRect, false); |
989 | // draw the item
|
1053 | // draw the item
|
990 | 1054 | ||
991 | drawBackground(painter, opt, index); |
1055 | drawBackground(painter, opt, index); |
992 | drawCheck(painter, opt, checkRect, checkState); |
1056 | drawCheck(painter, opt, checkRect, checkState); |
993 | drawDecoration(painter, opt, decorationRect, pixmap); |
1057 | drawDecoration(painter, opt, decorationRect, pixmap); |
994 | drawDisplay(painter, opt, displayRect, text); |
1058 | drawDisplay(painter, opt, displayRect, text); |
995 | drawFocus(painter, opt, displayRect); |
1059 | drawFocus(painter, opt, displayRect); |
996 | 1060 | ||
997 | // done
|
1061 | // done
|
998 | painter->restore(); |
1062 | painter->restore(); |
999 | }
|
1063 | }
|
1000 | 1064 | ||
1001 | 1065 | ||
1002 | 1066 | ||
1003 | 1067 | ||
1004 | 1068 | ||
1005 | 1069 | ||
1006 | IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent) |
1070 | IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent) |
1007 | {
|
1071 | {
|
1008 | }
|
1072 | }
|
1009 | 1073 | ||
1010 | 1074 | ||
1011 | 1075 | ||
1012 | 1076 | ||
1013 | 1077 | ||
1014 | QWidget *IconDelegate::createEditor(QWidget *parent, |
1078 | QWidget *IconDelegate::createEditor(QWidget *parent, |
1015 | const QStyleOptionViewItem &/* option */, |
1079 | const QStyleOptionViewItem &/* option */, |
1016 | const QModelIndex & /* index */) const |
1080 | const QModelIndex & /* index */) const |
1017 | {
|
1081 | {
|
1018 | 1082 | ||
1019 | IconForm *editor = new IconForm(parent); |
1083 | IconForm *editor = new IconForm(parent); |
1020 | 1084 | ||
1021 | return editor; |
1085 | return editor; |
1022 | 1086 | ||
1023 | }
|
1087 | }
|
1024 | 1088 | ||
1025 | 1089 | ||
1026 | 1090 | ||
1027 | 1091 | ||
1028 | void IconDelegate::setEditorData(QWidget *editor, |
1092 | void IconDelegate::setEditorData(QWidget *editor, |
1029 | const QModelIndex &index) const |
1093 | const QModelIndex &index) const |
1030 | {
|
1094 | {
|
1031 | 1095 | ||
1032 | 1096 | ||
1033 | const QAbstractItemModel * model = index.model(); |
1097 | const QAbstractItemModel * model = index.model(); |
1034 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
1098 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
1035 | QVariant currentImage = model->data(index,0); |
1099 | QVariant currentImage = model->data(index,0); |
1036 | QByteArray bytes = currentImage.toByteArray(); |
1100 | QByteArray bytes = currentImage.toByteArray(); |
1037 | if (currentImage.isValid()) { |
1101 | if (currentImage.isValid()) { |
1038 | m_pxPicture.loadFromData(bytes); |
1102 | m_pxPicture.loadFromData(bytes); |
1039 | }
|
1103 | }
|
1040 | else { |
1104 | else { |
1041 | //QItemDelegate::paint(painter, option, index);
|
1105 | //QItemDelegate::paint(painter, option, index);
|
1042 | 1106 | ||
1043 | 1107 | ||
1044 | return; |
1108 | return; |
1045 | }
|
1109 | }
|
1046 | icnFrm->setPixmap(m_pxPicture); |
1110 | icnFrm->setPixmap(m_pxPicture); |
1047 | // tblView->setRowHeight(index.row(), icnFrm->geometry().height());
|
1111 | // tblView->setRowHeight(index.row(), icnFrm->geometry().height());
|
1048 | }
|
1112 | }
|
1049 | 1113 | ||
1050 | 1114 | ||
1051 | 1115 | ||
1052 | 1116 | ||
1053 | void IconDelegate::setModelData( |
1117 | void IconDelegate::setModelData( |
1054 | QWidget *editor,
|
1118 | QWidget *editor,
|
1055 | QAbstractItemModel *model,
|
1119 | QAbstractItemModel *model,
|
1056 | const QModelIndex& index) const { |
1120 | const QModelIndex& index) const { |
1057 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
1121 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
1058 | if (!(icnFrm->dataIsChanged())) return; |
1122 | if (!(icnFrm->dataIsChanged())) return; |
1059 | 1123 | ||
1060 | m_pxPicture = icnFrm->pixmap(); |
1124 | m_pxPicture = icnFrm->pixmap(); |
1061 | QImage currentImage = m_pxPicture.toImage(); |
1125 | QImage currentImage = m_pxPicture.toImage(); |
1062 | QByteArray bytes;
|
1126 | QByteArray bytes;
|
1063 | QBuffer buffer(&bytes); |
1127 | QBuffer buffer(&bytes); |
1064 | buffer.open(QIODevice::WriteOnly); |
1128 | buffer.open(QIODevice::WriteOnly); |
1065 | currentImage.save(&buffer, "PNG"); |
1129 | currentImage.save(&buffer, "PNG"); |
1066 | 1130 | ||
1067 | 1131 | ||
1068 | 1132 | ||
1069 | 1133 | ||
1070 | model->setData(index, QVariant (bytes), Qt::EditRole); |
1134 | model->setData(index, QVariant (bytes), Qt::EditRole); |
1071 | 1135 | ||
1072 | // int widht = m_pxPicture.width();
|
1136 | // int widht = m_pxPicture.width();
|
1073 | // int heigh = m_pxPicture.height();
|
1137 | // int heigh = m_pxPicture.height();
|
1074 | // tblView->setRowHeight(index.row(), heigh + 10);
|
1138 | // tblView->setRowHeight(index.row(), heigh + 10);
|
1075 | // model->submitAll();
|
1139 | // model->submitAll();
|
1076 | 1140 | ||
1077 | 1141 | ||
1078 | 1142 | ||
1079 | 1143 | ||
1080 | // model->setData(index, value);
|
1144 | // model->setData(index, value);
|
1081 | }
|
1145 | }
|
1082 | 1146 | ||
1083 | 1147 | ||
1084 | 1148 | ||
1085 | void IconDelegate::updateEditorGeometry( |
1149 | void IconDelegate::updateEditorGeometry( |
1086 | QWidget *editor,
|
1150 | QWidget *editor,
|
1087 | const QStyleOptionViewItem &option, |
1151 | const QStyleOptionViewItem &option, |
1088 | const QModelIndex& /* index */) const { |
1152 | const QModelIndex& /* index */) const { |
1089 | 1153 | ||
1090 | 1154 | ||
1091 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
1155 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
1092 | // editor->setGeometry(r);
|
1156 | // editor->setGeometry(r);
|
1093 | 1157 | ||
1094 | editor->setGeometry(option.rect); |
1158 | editor->setGeometry(option.rect); |
1095 | }
|
1159 | }
|
1096 | 1160 | ||
1097 | 1161 | ||
1098 | 1162 | ||
1099 | 1163 | ||
1100 | void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
1164 | void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
1101 | {
|
1165 | {
|
1102 | 1166 | ||
1103 | 1167 | ||
1104 | m_pxPicture.fill( QColor(Qt::white) ); |
1168 | m_pxPicture.fill( QColor(Qt::white) ); |
1105 | 1169 | ||
1106 | const QAbstractItemModel * model = index.model(); |
1170 | const QAbstractItemModel * model = index.model(); |
1107 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
1171 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
1108 | 1172 | ||
1109 | QVariant currentImage = model->data(index,0); |
1173 | QVariant currentImage = model->data(index,0); |
1110 | 1174 | ||
1111 | 1175 | ||
1112 | QByteArray bytes = currentImage.toByteArray(); |
1176 | QByteArray bytes = currentImage.toByteArray(); |
1113 | // QImage image;
|
1177 | // QImage image;
|
1114 | // image.loadFromData(bytes);
|
1178 | // image.loadFromData(bytes);
|
1115 | 1179 | ||
1116 | 1180 | ||
1117 | if (currentImage.isValid()) { |
1181 | if (currentImage.isValid()) { |
1118 | m_pxPicture.loadFromData(bytes); |
1182 | m_pxPicture.loadFromData(bytes); |
1119 | 1183 | ||
1120 | }
|
1184 | }
|
1121 | else { |
1185 | else { |
1122 | //QItemDelegate::paint(painter, option, index);
|
1186 | //QItemDelegate::paint(painter, option, index);
|
1123 | return; |
1187 | return; |
1124 | }
|
1188 | }
|
1125 | 1189 | ||
1126 | 1190 | ||
1127 | 1191 | ||
1128 | 1192 | ||
1129 | 1193 | ||
1130 | /*
|
1194 | /*
|
1131 | 1195 | ||
1132 | if ( !sFileName.isEmpty() )
|
1196 | if ( !sFileName.isEmpty() )
|
1133 | m_pxPicture.load( sFileName );
|
1197 | m_pxPicture.load( sFileName );
|
1134 | else {
|
1198 | else {
|
1135 | //QItemDelegate::paint(painter, option, index);
|
1199 | //QItemDelegate::paint(painter, option, index);
|
1136 | return;
|
1200 | return;
|
1137 | }
|
1201 | }
|
1138 | */
|
1202 | */
|
1139 | 1203 | ||
1140 | 1204 | ||
1141 | 1205 | ||
1142 | 1206 | ||
1143 | 1207 | ||
1144 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
1208 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
1145 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
1209 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
1146 | QPalette::Disabled; |
1210 | QPalette::Disabled; |
1147 | 1211 | ||
1148 | if (option.state & QStyle::State_Selected) |
1212 | if (option.state & QStyle::State_Selected) |
1149 | 1213 | ||
1150 | 1214 | ||
1151 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
1215 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
1152 | 1216 | ||
1153 | 1217 | ||
1154 | 1218 | ||
1155 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
1219 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
1156 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
1220 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
1157 | // painter->setRenderHint(QPainter::Antialiasing);
|
1221 | // painter->setRenderHint(QPainter::Antialiasing);
|
1158 | painter->drawPixmap( nX, nY, m_pxPicture ); |
1222 | painter->drawPixmap( nX, nY, m_pxPicture ); |
1159 | 1223 | ||
1160 | // drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
1224 | // drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
1161 | 1225 | ||
1162 | /*
|
1226 | /*
|
1163 | QPen pen = painter->pen();
|
1227 | QPen pen = painter->pen();
|
1164 | painter->setPen(option.palette.color(QPalette::Mid));
|
1228 | painter->setPen(option.palette.color(QPalette::Mid));
|
1165 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
1229 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
1166 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
1230 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
1167 | painter->setPen(pen);
|
1231 | painter->setPen(pen);
|
1168 | */
|
1232 | */
|
1169 | }
|
1233 | }
|
1170 | 1234 | ||
1171 | 1235 | ||
1172 | void IconDelegate::setTableView(QTableView* table){ |
1236 | void IconDelegate::setTableView(QTableView* table){ |
1173 | tblView = table; |
1237 | tblView = table; |
1174 | }
|
1238 | }
|
1175 | 1239 | ||
1176 | 1240 | ||
1177 | 1241 | ||
1178 | 1242 | ||
1179 | 1243 | ||
1180 | 1244 | ||
1181 | 1245 | ||
1182 | 1246 | ||
1183 | FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent) |
1247 | FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent) |
1184 | {
|
1248 | {
|
1185 | }
|
1249 | }
|
1186 | 1250 | ||
1187 | 1251 | ||
1188 | 1252 | ||
1189 | 1253 | ||
1190 | 1254 | ||
1191 | QWidget *FileDelegate::createEditor(QWidget *parent, |
1255 | QWidget *FileDelegate::createEditor(QWidget *parent, |
1192 | const QStyleOptionViewItem &/* option */, |
1256 | const QStyleOptionViewItem &/* option */, |
1193 | const QModelIndex & /* index */) const |
1257 | const QModelIndex & /* index */) const |
1194 | {
|
1258 | {
|
1195 | 1259 | ||
1196 | FileForm *editor = new FileForm(parent); |
1260 | FileForm *editor = new FileForm(parent); |
1197 | 1261 | ||
1198 | return editor; |
1262 | return editor; |
1199 | 1263 | ||
1200 | }
|
1264 | }
|
1201 | 1265 | ||
1202 | 1266 | ||
1203 | 1267 | ||
1204 | 1268 | ||
1205 | void FileDelegate::setEditorData(QWidget *editor, |
1269 | void FileDelegate::setEditorData(QWidget *editor, |
1206 | const QModelIndex &index) const |
1270 | const QModelIndex &index) const |
1207 | {
|
1271 | {
|
1208 | 1272 | ||
1209 | 1273 | ||
1210 | const QAbstractItemModel * model = index.model(); |
1274 | const QAbstractItemModel * model = index.model(); |
1211 | FileForm *flFrm = static_cast<FileForm*>(editor); |
1275 | FileForm *flFrm = static_cast<FileForm*>(editor); |
1212 | QVariant currentData = model->data(index,Qt::EditRole); |
1276 | QVariant currentData = model->data(index,Qt::EditRole); |
1213 | 1277 | ||
1214 | // QVariant currentData = QSqlTableModel::data(index, 0);
|
1278 | // QVariant currentData = QSqlTableModel::data(index, 0);
|
1215 | QByteArray bytes = currentData.toByteArray(); |
1279 | QByteArray bytes = currentData.toByteArray(); |
1216 | if (currentData.isValid()) flFrm->setData(bytes); |
1280 | if (currentData.isValid()) flFrm->setData(bytes); |
1217 | else return; |
1281 | else return; |
1218 | 1282 | ||
1219 | }
|
1283 | }
|
1220 | 1284 | ||
1221 | 1285 | ||
1222 | 1286 | ||
1223 | 1287 | ||
1224 | void FileDelegate::setModelData( |
1288 | void FileDelegate::setModelData( |
1225 | QWidget *editor,
|
1289 | QWidget *editor,
|
1226 | QAbstractItemModel *model,
|
1290 | QAbstractItemModel *model,
|
1227 | const QModelIndex& index) const { |
1291 | const QModelIndex& index) const { |
1228 | FileForm *flFrm = static_cast<FileForm*>(editor); |
1292 | FileForm *flFrm = static_cast<FileForm*>(editor); |
1229 | if (!(flFrm->dataIsChanged())) return; |
1293 | if (!(flFrm->dataIsChanged())) return; |
1230 | 1294 | ||
1231 | m_Data = flFrm->data(); |
1295 | m_Data = flFrm->data(); |
1232 | if (!m_Data.isEmpty()) { // åñëè ìàññèâ íå ïóñòîé |
1296 | if (!m_Data.isEmpty()) { // åñëè ìàññèâ íå ïóñòîé |
1233 | model->setData(index, QVariant (m_Data), Qt::EditRole); |
1297 | model->setData(index, QVariant (m_Data), Qt::EditRole); |
1234 | 1298 | ||
1235 | }
|
1299 | }
|
1236 | else { // åñëè ìàññèâ ïóñòîé (ôàéë óäàëèëè) |
1300 | else { // åñëè ìàññèâ ïóñòîé (ôàéë óäàëèëè) |
1237 | QVariant val_null;
|
1301 | QVariant val_null;
|
1238 | model->setData(index, val_null, Qt::EditRole); |
1302 | model->setData(index, val_null, Qt::EditRole); |
1239 | 1303 | ||
1240 | }
|
1304 | }
|
1241 | 1305 | ||
1242 | 1306 | ||
1243 | 1307 | ||
1244 | }
|
1308 | }
|
1245 | 1309 | ||
1246 | 1310 | ||
1247 | 1311 | ||
1248 | void FileDelegate::updateEditorGeometry( |
1312 | void FileDelegate::updateEditorGeometry( |
1249 | QWidget *editor,
|
1313 | QWidget *editor,
|
1250 | const QStyleOptionViewItem &option, |
1314 | const QStyleOptionViewItem &option, |
1251 | const QModelIndex& /* index */) const { |
1315 | const QModelIndex& /* index */) const { |
1252 | 1316 | ||
1253 | 1317 | ||
1254 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
1318 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
1255 | // editor->setGeometry(r);
|
1319 | // editor->setGeometry(r);
|
1256 | 1320 | ||
1257 | editor->setGeometry(option.rect); |
1321 | editor->setGeometry(option.rect); |
1258 | }
|
1322 | }
|
1259 | 1323 | ||
1260 | 1324 | ||
1261 | 1325 | ||
1262 | 1326 | ||
1263 | 1327 |