Rev 205 | Rev 224 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 205 | Rev 219 | ||
---|---|---|---|
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 | ||
- | 242 | ||
- | 243 | ||
- | 244 | ||
- | 245 | ||
- | 246 | ||
- | 247 | ||
- | 248 | ||
- | 249 | ||
- | 250 | ||
- | 251 | ||
- | 252 | //! [0]
|
|
- | 253 | FilterSpinBoxDelegate::FilterSpinBoxDelegate(QObject *parent) |
|
- | 254 | : QItemDelegate(parent) |
|
- | 255 | {
|
|
- | 256 | }
|
|
- | 257 | //! [0]
|
|
- | 258 | ||
- | 259 | void FilterSpinBoxDelegate::setClassID(QString newClassID){ |
|
- | 260 | class_id = newClassID; |
|
- | 261 | }
|
|
- | 262 | ||
- | 263 | void FilterSpinBoxDelegate::getItems(){ |
|
- | 264 | QMap <QString, QString> result_map; |
|
- | 265 | QSqlQuery q;
|
|
- | 266 | QString query_str;
|
|
- | 267 | bool ok; |
|
- | 268 | ||
- | 269 | if (FilterSpinBoxDelegate::class_id.isEmpty()) |
|
- | 270 | {
|
|
- | 271 | items.clear(); |
|
- | 272 | return; |
|
- | 273 | }
|
|
- | 274 | ||
- | 275 | query_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '"); |
|
- | 276 | query_str.append(class_id); |
|
- | 277 | query_str.append( tr("'")); |
|
- | 278 | ||
- | 279 | q.prepare(query_str); |
|
- | 280 | ||
- | 281 | ||
- | 282 | ok = q.exec(); |
|
- | 283 | ||
- | 284 | ||
- | 285 | ||
- | 286 | ||
- | 287 | if (!ok) { |
|
- | 288 | /*
|
|
- | 289 | QString error_str;
|
|
- | 290 | error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
|
|
- | 291 | error_str.append(inctance);
|
|
- | 292 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
|
- | 293 | this, // Ðîäèòåëüñêèé âèäæåò.
|
|
- | 294 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
|
- | 295 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
|
- | 296 | // tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
|
|
- | 297 | ||
- | 298 | */
|
|
- | 299 | items.clear(); |
|
- | 300 | return; |
|
- | 301 | }
|
|
- | 302 | ||
- | 303 | ||
- | 304 | // field_inctance = q.record().indexOf(tr("TableWhithInstance"));
|
|
- | 305 | items.clear(); |
|
- | 306 | while(q.next()){ |
|
- | 307 | ||
- | 308 | QString ID;
|
|
- | 309 | QString Name;
|
|
- | 310 | ID = q.value(0).toString(); // îïðåäåëÿåì ID |
|
- | 311 | Name = q.value(1).toString(); // îïðåäåëÿåì Name |
|
- | 312 | items.insert(ID, Name); |
|
- | 313 | }
|
|
- | 314 | ||
- | 315 | ||
- | 316 | // return result_map;
|
|
- | 317 | }
|
|
- | 318 | ||
- | 319 | ||
- | 320 | //! [1]
|
|
- | 321 | QWidget *FilterSpinBoxDelegate::createEditor(QWidget *parent, |
|
- | 322 | const QStyleOptionViewItem &/* option */, |
|
- | 323 | const QModelIndex &/* index */) const |
|
- | 324 | {
|
|
- | 325 | //QSpinBox *editor = new QSpinBox(parent);
|
|
- | 326 | //editor->setMinimum(0);
|
|
- | 327 | //editor->setMaximum(100);
|
|
- | 328 | QStringList ID_list;
|
|
- | 329 | QStringList Name_list;
|
|
- | 330 | ||
- | 331 | // getItems();
|
|
- | 332 | ||
- | 333 | ||
- | 334 | ||
- | 335 | ||
- | 336 | ||
- | 337 | ID_list = items.keys(); |
|
- | 338 | Name_list = items.values(); |
|
- | 339 | ||
- | 340 | QComboBox *editor = new QComboBox(parent); |
|
- | 341 | ||
- | 342 | /*
|
|
- | 343 | editor->addItem("0");
|
|
- | 344 | editor->addItem("1");
|
|
- | 345 | editor->addItem("2");
|
|
- | 346 | editor->addItem("3");
|
|
- | 347 | editor->addItem("4");
|
|
- | 348 | editor->addItem("5");
|
|
- | 349 | editor->addItem("6");
|
|
- | 350 | editor->addItem("7");
|
|
- | 351 | editor->addItem("8");
|
|
- | 352 | editor->addItem("9");
|
|
- | 353 | */
|
|
- | 354 | ||
- | 355 | // editor->addItem("0");
|
|
- | 356 | ||
- | 357 | editor->addItems(Name_list); |
|
- | 358 | // editor->setEditable(false);
|
|
- | 359 | return editor; |
|
- | 360 | }
|
|
- | 361 | //! [1]
|
|
- | 362 | ||
- | 363 | //! [2]
|
|
- | 364 | void FilterSpinBoxDelegate::setEditorData(QWidget *editor, |
|
- | 365 | const QModelIndex &index) const |
|
- | 366 | {
|
|
- | 367 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
|
- | 368 | ||
- | 369 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
|
- | 370 | // spinBox->setValue(value);
|
|
- | 371 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
|
- | 372 | ||
- | 373 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
|
- | 374 | ||
- | 375 | QStringList ID_list;
|
|
- | 376 | QStringList Name_list;
|
|
- | 377 | int curr_index; |
|
- | 378 | ||
- | 379 | ID_list = items.keys(); |
|
- | 380 | Name_list = items.values(); |
|
- | 381 | ||
- | 382 | curr_index = ID_list.indexOf(value); |
|
- | 383 | if (curr_index==-1) return; |
|
- | 384 | comboBox->setCurrentIndex(curr_index); |
|
- | 385 | /****************************************************
|
|
- | 386 | if (value == "0") comboBox->setCurrentIndex(0);
|
|
- | 387 | if (value == "1") comboBox->setCurrentIndex(1);
|
|
- | 388 | if (value == "2") comboBox->setCurrentIndex(2);
|
|
- | 389 | if (value == "3") comboBox->setCurrentIndex(3);
|
|
- | 390 | if (value == "4") comboBox->setCurrentIndex(4);
|
|
- | 391 | if (value == "5") comboBox->setCurrentIndex(5);
|
|
- | 392 | if (value == "6") comboBox->setCurrentIndex(6);
|
|
- | 393 | if (value == "7") comboBox->setCurrentIndex(7);
|
|
- | 394 | if (value == "8") comboBox->setCurrentIndex(8);
|
|
- | 395 | if (value == "9") comboBox->setCurrentIndex(9);
|
|
- | 396 | ******************************************************/
|
|
- | 397 | comboBox->setEditable(false); |
|
- | 398 | ||
- | 399 | ||
- | 400 | //comboBox->setItemText(0, value);
|
|
- | 401 | ||
- | 402 | }
|
|
- | 403 | //! [2]
|
|
- | 404 | ||
- | 405 | //! [3]
|
|
- | 406 | void FilterSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
|
- | 407 | const QModelIndex &index) const |
|
- | 408 | {
|
|
- | 409 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
|
- | 410 | // spinBox->interpretText();
|
|
- | 411 | // int value = spinBox->value();
|
|
- | 412 | ||
- | 413 | // model->setData(index, value, Qt::EditRole);
|
|
- | 414 | QStringList ID_list;
|
|
- | 415 | QStringList Name_list;
|
|
- | 416 | ||
- | 417 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
|
- | 418 | int currIndex; |
|
- | 419 | currIndex = comboBox->currentIndex(); |
|
- | 420 | if (currIndex==-1) return; |
|
- | 421 | // QString value = comboBox->itemText(currIndex);
|
|
- | 422 | ||
- | 423 | ID_list = items.keys(); |
|
- | 424 | Name_list = items.values(); |
|
- | 425 | ||
- | 426 | // QString value = ID_list.at(currIndex);
|
|
- | 427 | QString value = Name_list.at(currIndex); |
|
- | 428 | model->setData(index, value, Qt::EditRole); |
|
- | 429 | }
|
|
- | 430 | //! [3]
|
|
- | 431 | ||
- | 432 | //! [4]
|
|
- | 433 | void FilterSpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
|
- | 434 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
|
- | 435 | {
|
|
- | 436 | editor->setGeometry(option.rect); |
|
- | 437 | }
|
|
- | 438 | ||
- | 439 | ||
- | 440 | ||
- | 441 | ||
- | 442 | ||
- | 443 | ||
- | 444 | ||
- | 445 | ||
- | 446 | ||
- | 447 | ||
- | 448 | //! [0]
|
|
- | 449 | FilterConditionDelegate::FilterConditionDelegate(QObject *parent) |
|
- | 450 | : QItemDelegate(parent) |
|
- | 451 | {
|
|
- | 452 | }
|
|
- | 453 | //! [0]
|
|
- | 454 | ||
- | 455 | void FilterConditionDelegate::setClassID(QString newClassID){ |
|
- | 456 | class_id = newClassID; |
|
- | 457 | }
|
|
- | 458 | ||
- | 459 | ||
- | 460 | ||
- | 461 | //! [1]
|
|
- | 462 | QWidget *FilterConditionDelegate::createEditor(QWidget *parent, |
|
- | 463 | const QStyleOptionViewItem &/* option */, |
|
- | 464 | const QModelIndex &/* index */) const |
|
- | 465 | {
|
|
- | 466 | QStringList Name_list;
|
|
- | 467 | ||
- | 468 | Name_list = items; |
|
- | 469 | ||
- | 470 | QComboBox *editor = new QComboBox(parent); |
|
- | 471 | ||
- | 472 | /*
|
|
- | 473 | editor->addItem("0");
|
|
- | 474 | editor->addItem("1");
|
|
- | 475 | editor->addItem("2");
|
|
- | 476 | editor->addItem("3");
|
|
- | 477 | editor->addItem("4");
|
|
- | 478 | editor->addItem("5");
|
|
- | 479 | editor->addItem("6");
|
|
- | 480 | editor->addItem("7");
|
|
- | 481 | editor->addItem("8");
|
|
- | 482 | editor->addItem("9");
|
|
- | 483 | */
|
|
- | 484 | ||
- | 485 | // editor->addItem("0");
|
|
- | 486 | ||
- | 487 | editor->addItems(Name_list); |
|
- | 488 | // editor->setEditable(false);
|
|
- | 489 | return editor; |
|
- | 490 | }
|
|
- | 491 | //! [1]
|
|
- | 492 | ||
- | 493 | //! [2]
|
|
- | 494 | void FilterConditionDelegate::setEditorData(QWidget *editor, |
|
- | 495 | const QModelIndex &index) const |
|
- | 496 | {
|
|
- | 497 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
|
- | 498 | ||
- | 499 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
|
- | 500 | // spinBox->setValue(value);
|
|
- | 501 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
|
- | 502 | ||
- | 503 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
|
- | 504 | ||
- | 505 | //QStringList ID_list;
|
|
- | 506 | //QStringList Name_list;
|
|
- | 507 | int curr_index; |
|
- | 508 | ||
- | 509 | //ID_list = items.keys();
|
|
- | 510 | //Name_list = items.values();
|
|
- | 511 | ||
- | 512 | curr_index = items.indexOf(value); |
|
- | 513 | if (curr_index==-1) return; |
|
- | 514 | comboBox->setCurrentIndex(curr_index); |
|
- | 515 | /****************************************************
|
|
- | 516 | if (value == "0") comboBox->setCurrentIndex(0);
|
|
- | 517 | if (value == "1") comboBox->setCurrentIndex(1);
|
|
- | 518 | if (value == "2") comboBox->setCurrentIndex(2);
|
|
- | 519 | if (value == "3") comboBox->setCurrentIndex(3);
|
|
- | 520 | if (value == "4") comboBox->setCurrentIndex(4);
|
|
- | 521 | if (value == "5") comboBox->setCurrentIndex(5);
|
|
- | 522 | if (value == "6") comboBox->setCurrentIndex(6);
|
|
- | 523 | if (value == "7") comboBox->setCurrentIndex(7);
|
|
- | 524 | if (value == "8") comboBox->setCurrentIndex(8);
|
|
- | 525 | if (value == "9") comboBox->setCurrentIndex(9);
|
|
- | 526 | ******************************************************/
|
|
- | 527 | comboBox->setEditable(false); |
|
- | 528 | ||
- | 529 | ||
- | 530 | //comboBox->setItemText(0, value);
|
|
- | 531 | ||
- | 532 | }
|
|
- | 533 | //! [2]
|
|
- | 534 | ||
- | 535 | //! [3]
|
|
- | 536 | void FilterConditionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
|
- | 537 | const QModelIndex &index) const |
|
- | 538 | {
|
|
- | 539 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
|
- | 540 | // spinBox->interpretText();
|
|
- | 541 | // int value = spinBox->value();
|
|
- | 542 | ||
- | 543 | // model->setData(index, value, Qt::EditRole);
|
|
- | 544 | QStringList ID_list;
|
|
- | 545 | QStringList Name_list;
|
|
- | 546 | ||
- | 547 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
|
- | 548 | int currIndex; |
|
- | 549 | currIndex = comboBox->currentIndex(); |
|
- | 550 | if (currIndex==-1) return; |
|
- | 551 | // QString value = comboBox->itemText(currIndex);
|
|
- | 552 | ||
- | 553 | // ID_list = items.keys();
|
|
- | 554 | // Name_list = items.values();
|
|
- | 555 | ||
- | 556 | // QString value = ID_list.at(currIndex);
|
|
- | 557 | QString value = items.at(currIndex); |
|
- | 558 | model->setData(index, value, Qt::EditRole); |
|
- | 559 | }
|
|
- | 560 | //! [3]
|
|
- | 561 | ||
- | 562 | //! [4]
|
|
- | 563 | void FilterConditionDelegate::updateEditorGeometry(QWidget *editor, |
|
- | 564 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
|
- | 565 | {
|
|
- | 566 | editor->setGeometry(option.rect); |
|
- | 567 | }
|
|
- | 568 | ||
- | 569 | ||
- | 570 | ||
- | 571 | ||
- | 572 | ||
- | 573 | ||
- | 574 | ||
- | 575 | ||
- | 576 | ||
- | 577 | ||
241 | //! [4]
|
578 | //! [4]
|
242 | MyDEDelegate::MyDEDelegate( |
579 | MyDEDelegate::MyDEDelegate( |
243 | bool calpopup,
|
580 | bool calpopup,
|
244 | QObject *parent) |
581 | QObject *parent) |
245 | : QItemDelegate(parent), |
582 | : QItemDelegate(parent), |
246 | m_calpopup(calpopup) { |
583 | m_calpopup(calpopup) { |
247 | }
|
584 | }
|
248 | 585 | ||
249 | QWidget *MyDEDelegate::createEditor( |
586 | QWidget *MyDEDelegate::createEditor( |
250 | QWidget *parent,
|
587 | QWidget *parent,
|
251 | const QStyleOptionViewItem& /* option */, |
588 | const QStyleOptionViewItem& /* option */, |
252 | const QModelIndex& /* index */) const { |
589 | const QModelIndex& /* index */) const { |
253 | QDateEdit *editor = new QDateEdit(parent); |
590 | QDateEdit *editor = new QDateEdit(parent); |
254 | editor->setCalendarPopup(m_calpopup); |
591 | editor->setCalendarPopup(m_calpopup); |
255 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
592 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
256 | return editor; |
593 | return editor; |
257 | }
|
594 | }
|
258 | 595 | ||
259 | void MyDEDelegate::setEditorData( |
596 | void MyDEDelegate::setEditorData( |
260 | QWidget *editor,
|
597 | QWidget *editor,
|
261 | const QModelIndex &index) const { |
598 | const QModelIndex &index) const { |
262 | QDate value = index.model()->data( |
599 | QDate value = index.model()->data( |
263 | index, Qt::EditRole).toDate(); |
600 | index, Qt::EditRole).toDate(); |
264 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
601 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
265 | de->setDate(value); |
602 | de->setDate(value); |
266 | }
|
603 | }
|
267 | 604 | ||
268 | void MyDEDelegate::setModelData( |
605 | void MyDEDelegate::setModelData( |
269 | QWidget *editor,
|
606 | QWidget *editor,
|
270 | QAbstractItemModel *model,
|
607 | QAbstractItemModel *model,
|
271 | const QModelIndex& index) const { |
608 | const QModelIndex& index) const { |
272 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
609 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
273 | de->interpretText(); |
610 | de->interpretText(); |
274 | QDate value = de->date(); |
611 | QDate value = de->date(); |
275 | model->setData(index, value); |
612 | model->setData(index, value); |
276 | }
|
613 | }
|
277 | 614 | ||
278 | void MyDEDelegate::updateEditorGeometry( |
615 | void MyDEDelegate::updateEditorGeometry( |
279 | QWidget *editor,
|
616 | QWidget *editor,
|
280 | const QStyleOptionViewItem &option, |
617 | const QStyleOptionViewItem &option, |
281 | const QModelIndex& /* index */) const { |
618 | const QModelIndex& /* index */) const { |
282 | 619 | ||
283 | 620 | ||
284 | 621 | ||
285 | editor->setGeometry(option.rect); |
622 | editor->setGeometry(option.rect); |
286 | }
|
623 | }
|
287 | 624 | ||
288 | 625 | ||
289 | 626 | ||
290 | 627 | ||
291 | 628 | ||
292 | CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent) |
629 | CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent) |
293 | {
|
630 | {
|
294 | }
|
631 | }
|
295 | 632 | ||
296 | void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
633 | void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
297 | {
|
634 | {
|
298 | m_pxPicture.fill( QColor(Qt::white) ); |
635 | m_pxPicture.fill( QColor(Qt::white) ); |
299 | 636 | ||
300 | const QAbstractItemModel * model = index.model(); |
637 | const QAbstractItemModel * model = index.model(); |
301 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
638 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
302 | 639 | ||
303 | if ( !sFileName.isEmpty() ) |
640 | if ( !sFileName.isEmpty() ) |
304 | m_pxPicture.load( sFileName ); |
641 | m_pxPicture.load( sFileName ); |
305 | else { |
642 | else { |
306 | //QItemDelegate::paint(painter, option, index);
|
643 | //QItemDelegate::paint(painter, option, index);
|
307 | return; |
644 | return; |
308 | }
|
645 | }
|
309 | 646 | ||
310 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
647 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
311 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
648 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
312 | QPalette::Disabled; |
649 | QPalette::Disabled; |
313 | 650 | ||
314 | if (option.state & QStyle::State_Selected) |
651 | if (option.state & QStyle::State_Selected) |
315 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
652 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
316 | 653 | ||
317 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
654 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
318 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
655 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
319 | painter->drawPixmap( nX, nY, m_pxPicture ); |
656 | painter->drawPixmap( nX, nY, m_pxPicture ); |
320 | 657 | ||
321 | 658 | ||
322 | //// drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
659 | //// drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
323 | 660 | ||
324 | /*
|
661 | /*
|
325 | QPen pen = painter->pen();
|
662 | QPen pen = painter->pen();
|
326 | painter->setPen(option.palette.color(QPalette::Mid));
|
663 | painter->setPen(option.palette.color(QPalette::Mid));
|
327 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
664 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
328 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
665 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
329 | painter->setPen(pen);
|
666 | painter->setPen(pen);
|
330 | */
|
667 | */
|
331 | }
|
668 | }
|
332 | 669 | ||
333 | 670 | ||
334 | 671 | ||
335 | 672 | ||
336 | void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
673 | void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
337 | const QModelIndex &index) const |
674 | const QModelIndex &index) const |
338 | {
|
675 | {
|
339 | int datetime = index.model()->data(index, Qt::DisplayRole).toInt(); |
676 | int datetime = index.model()->data(index, Qt::DisplayRole).toInt(); |
340 | 677 | ||
341 | QString indexvalue = ""; |
678 | QString indexvalue = ""; |
342 | 679 | ||
343 | if (datetime > 0) |
680 | if (datetime > 0) |
344 | {
|
681 | {
|
345 | QDateTime dateTime2 = QDateTime(); |
682 | QDateTime dateTime2 = QDateTime(); |
346 | dateTime2.setTime_t(datetime); |
683 | dateTime2.setTime_t(datetime); |
347 | indexvalue = dateTime2.toString(this->timeformat); |
684 | indexvalue = dateTime2.toString(this->timeformat); |
348 | }
|
685 | }
|
349 | else
|
686 | else
|
350 | {
|
687 | {
|
351 | indexvalue = tr("Date not set"); |
688 | indexvalue = tr("Date not set"); |
352 | }
|
689 | }
|
353 | 690 | ||
354 | Q_ASSERT(index.isValid()); |
691 | Q_ASSERT(index.isValid()); |
355 | 692 | ||
356 | QStyleOptionViewItemV3 opt = setOptions(index, option); |
693 | QStyleOptionViewItemV3 opt = setOptions(index, option); |
357 | 694 | ||
358 | const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option); |
695 | const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option); |
359 | opt.features = v2 ? v2->features |
696 | opt.features = v2 ? v2->features |
360 | : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None); |
697 | : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None); |
361 | const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option); |
698 | const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option); |
362 | opt.locale = v3 ? v3->locale : QLocale(); |
699 | opt.locale = v3 ? v3->locale : QLocale(); |
363 | opt.widget = v3 ? v3->widget : 0; |
700 | opt.widget = v3 ? v3->widget : 0; |
364 | 701 | ||
365 | // prepare
|
702 | // prepare
|
366 | painter->save(); |
703 | painter->save(); |
367 | 704 | ||
368 | painter->setClipRect(opt.rect); |
705 | painter->setClipRect(opt.rect); |
369 | 706 | ||
370 | // get the data and the rectangles
|
707 | // get the data and the rectangles
|
371 | QVariant value;
|
708 | QVariant value;
|
372 | 709 | ||
373 | QPixmap pixmap;
|
710 | QPixmap pixmap;
|
374 | QRect decorationRect;
|
711 | QRect decorationRect;
|
375 | value = index.data(Qt::DecorationRole); |
712 | value = index.data(Qt::DecorationRole); |
376 | 713 | ||
377 | QString text;
|
714 | QString text;
|
378 | QRect displayRect;
|
715 | QRect displayRect;
|
379 | value = index.data(Qt::DisplayRole); |
716 | value = index.data(Qt::DisplayRole); |
380 | if (value.isValid()) { |
717 | if (value.isValid()) { |
381 | text = indexvalue; |
718 | text = indexvalue; |
382 | displayRect = textRectangle(painter, option.rect, opt.font, text); |
719 | displayRect = textRectangle(painter, option.rect, opt.font, text); |
383 | }
|
720 | }
|
384 | 721 | ||
385 | QRect checkRect;
|
722 | QRect checkRect;
|
386 | Qt::CheckState checkState = Qt::Unchecked; |
723 | Qt::CheckState checkState = Qt::Unchecked; |
387 | value = index.data(Qt::CheckStateRole); |
724 | value = index.data(Qt::CheckStateRole); |
388 | if (value.isValid()) { |
725 | if (value.isValid()) { |
389 | checkState = static_cast<Qt::CheckState>(value.toInt()); |
726 | checkState = static_cast<Qt::CheckState>(value.toInt()); |
390 | checkRect = check(opt, opt.rect, value); |
727 | checkRect = check(opt, opt.rect, value); |
391 | }
|
728 | }
|
392 | 729 | ||
393 | // do the layout
|
730 | // do the layout
|
394 | doLayout(opt, &checkRect, &decorationRect, &displayRect, false); |
731 | doLayout(opt, &checkRect, &decorationRect, &displayRect, false); |
395 | // draw the item
|
732 | // draw the item
|
396 | 733 | ||
397 | drawBackground(painter, opt, index); |
734 | drawBackground(painter, opt, index); |
398 | drawCheck(painter, opt, checkRect, checkState); |
735 | drawCheck(painter, opt, checkRect, checkState); |
399 | drawDecoration(painter, opt, decorationRect, pixmap); |
736 | drawDecoration(painter, opt, decorationRect, pixmap); |
400 | drawDisplay(painter, opt, displayRect, text); |
737 | drawDisplay(painter, opt, displayRect, text); |
401 | drawFocus(painter, opt, displayRect); |
738 | drawFocus(painter, opt, displayRect); |
402 | 739 | ||
403 | // done
|
740 | // done
|
404 | painter->restore(); |
741 | painter->restore(); |
405 | }
|
742 | }
|
406 | 743 | ||
407 | 744 | ||
408 | 745 | ||
409 | 746 | ||
410 | 747 | ||
411 | 748 | ||
412 | IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent) |
749 | IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent) |
413 | {
|
750 | {
|
414 | }
|
751 | }
|
415 | 752 | ||
416 | 753 | ||
417 | 754 | ||
418 | 755 | ||
419 | 756 | ||
420 | QWidget *IconDelegate::createEditor(QWidget *parent, |
757 | QWidget *IconDelegate::createEditor(QWidget *parent, |
421 | const QStyleOptionViewItem &/* option */, |
758 | const QStyleOptionViewItem &/* option */, |
422 | const QModelIndex & /* index */) const |
759 | const QModelIndex & /* index */) const |
423 | {
|
760 | {
|
424 | 761 | ||
425 | IconForm *editor = new IconForm(parent); |
762 | IconForm *editor = new IconForm(parent); |
426 | 763 | ||
427 | return editor; |
764 | return editor; |
428 | 765 | ||
429 | }
|
766 | }
|
430 | 767 | ||
431 | 768 | ||
432 | 769 | ||
433 | 770 | ||
434 | void IconDelegate::setEditorData(QWidget *editor, |
771 | void IconDelegate::setEditorData(QWidget *editor, |
435 | const QModelIndex &index) const |
772 | const QModelIndex &index) const |
436 | {
|
773 | {
|
437 | 774 | ||
438 | 775 | ||
439 | const QAbstractItemModel * model = index.model(); |
776 | const QAbstractItemModel * model = index.model(); |
440 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
777 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
441 | QVariant currentImage = model->data(index,0); |
778 | QVariant currentImage = model->data(index,0); |
442 | QByteArray bytes = currentImage.toByteArray(); |
779 | QByteArray bytes = currentImage.toByteArray(); |
443 | if (currentImage.isValid()) { |
780 | if (currentImage.isValid()) { |
444 | m_pxPicture.loadFromData(bytes); |
781 | m_pxPicture.loadFromData(bytes); |
445 | }
|
782 | }
|
446 | else { |
783 | else { |
447 | //QItemDelegate::paint(painter, option, index);
|
784 | //QItemDelegate::paint(painter, option, index);
|
448 | 785 | ||
449 | 786 | ||
450 | return; |
787 | return; |
451 | }
|
788 | }
|
452 | icnFrm->setPixmap(m_pxPicture); |
789 | icnFrm->setPixmap(m_pxPicture); |
453 | // tblView->setRowHeight(index.row(), icnFrm->geometry().height());
|
790 | // tblView->setRowHeight(index.row(), icnFrm->geometry().height());
|
454 | }
|
791 | }
|
455 | 792 | ||
456 | 793 | ||
457 | 794 | ||
458 | 795 | ||
459 | void IconDelegate::setModelData( |
796 | void IconDelegate::setModelData( |
460 | QWidget *editor,
|
797 | QWidget *editor,
|
461 | QAbstractItemModel *model,
|
798 | QAbstractItemModel *model,
|
462 | const QModelIndex& index) const { |
799 | const QModelIndex& index) const { |
463 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
800 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
464 | if (!(icnFrm->dataIsChanged())) return; |
801 | if (!(icnFrm->dataIsChanged())) return; |
465 | 802 | ||
466 | m_pxPicture = icnFrm->pixmap(); |
803 | m_pxPicture = icnFrm->pixmap(); |
467 | QImage currentImage = m_pxPicture.toImage(); |
804 | QImage currentImage = m_pxPicture.toImage(); |
468 | QByteArray bytes;
|
805 | QByteArray bytes;
|
469 | QBuffer buffer(&bytes); |
806 | QBuffer buffer(&bytes); |
470 | buffer.open(QIODevice::WriteOnly); |
807 | buffer.open(QIODevice::WriteOnly); |
471 | currentImage.save(&buffer, "PNG"); |
808 | currentImage.save(&buffer, "PNG"); |
472 | 809 | ||
473 | 810 | ||
474 | 811 | ||
475 | 812 | ||
476 | model->setData(index, QVariant (bytes), Qt::EditRole); |
813 | model->setData(index, QVariant (bytes), Qt::EditRole); |
477 | 814 | ||
478 | // int widht = m_pxPicture.width();
|
815 | // int widht = m_pxPicture.width();
|
479 | // int heigh = m_pxPicture.height();
|
816 | // int heigh = m_pxPicture.height();
|
480 | // tblView->setRowHeight(index.row(), heigh + 10);
|
817 | // tblView->setRowHeight(index.row(), heigh + 10);
|
481 | // model->submitAll();
|
818 | // model->submitAll();
|
482 | 819 | ||
483 | 820 | ||
484 | 821 | ||
485 | 822 | ||
486 | // model->setData(index, value);
|
823 | // model->setData(index, value);
|
487 | }
|
824 | }
|
488 | 825 | ||
489 | 826 | ||
490 | 827 | ||
491 | void IconDelegate::updateEditorGeometry( |
828 | void IconDelegate::updateEditorGeometry( |
492 | QWidget *editor,
|
829 | QWidget *editor,
|
493 | const QStyleOptionViewItem &option, |
830 | const QStyleOptionViewItem &option, |
494 | const QModelIndex& /* index */) const { |
831 | const QModelIndex& /* index */) const { |
495 | 832 | ||
496 | 833 | ||
497 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
834 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
498 | // editor->setGeometry(r);
|
835 | // editor->setGeometry(r);
|
499 | 836 | ||
500 | editor->setGeometry(option.rect); |
837 | editor->setGeometry(option.rect); |
501 | }
|
838 | }
|
502 | 839 | ||
503 | 840 | ||
504 | 841 | ||
505 | 842 | ||
506 | void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
843 | void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
507 | {
|
844 | {
|
508 | 845 | ||
509 | 846 | ||
510 | m_pxPicture.fill( QColor(Qt::white) ); |
847 | m_pxPicture.fill( QColor(Qt::white) ); |
511 | 848 | ||
512 | const QAbstractItemModel * model = index.model(); |
849 | const QAbstractItemModel * model = index.model(); |
513 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
850 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
514 | 851 | ||
515 | QVariant currentImage = model->data(index,0); |
852 | QVariant currentImage = model->data(index,0); |
516 | 853 | ||
517 | 854 | ||
518 | QByteArray bytes = currentImage.toByteArray(); |
855 | QByteArray bytes = currentImage.toByteArray(); |
519 | // QImage image;
|
856 | // QImage image;
|
520 | // image.loadFromData(bytes);
|
857 | // image.loadFromData(bytes);
|
521 | 858 | ||
522 | 859 | ||
523 | if (currentImage.isValid()) { |
860 | if (currentImage.isValid()) { |
524 | m_pxPicture.loadFromData(bytes); |
861 | m_pxPicture.loadFromData(bytes); |
525 | 862 | ||
526 | }
|
863 | }
|
527 | else { |
864 | else { |
528 | //QItemDelegate::paint(painter, option, index);
|
865 | //QItemDelegate::paint(painter, option, index);
|
529 | return; |
866 | return; |
530 | }
|
867 | }
|
531 | 868 | ||
532 | 869 | ||
533 | 870 | ||
534 | 871 | ||
535 | 872 | ||
536 | /*
|
873 | /*
|
537 | 874 | ||
538 | if ( !sFileName.isEmpty() )
|
875 | if ( !sFileName.isEmpty() )
|
539 | m_pxPicture.load( sFileName );
|
876 | m_pxPicture.load( sFileName );
|
540 | else {
|
877 | else {
|
541 | //QItemDelegate::paint(painter, option, index);
|
878 | //QItemDelegate::paint(painter, option, index);
|
542 | return;
|
879 | return;
|
543 | }
|
880 | }
|
544 | */
|
881 | */
|
545 | 882 | ||
546 | 883 | ||
547 | 884 | ||
548 | 885 | ||
549 | 886 | ||
550 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
887 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
551 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
888 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
552 | QPalette::Disabled; |
889 | QPalette::Disabled; |
553 | 890 | ||
554 | if (option.state & QStyle::State_Selected) |
891 | if (option.state & QStyle::State_Selected) |
555 | 892 | ||
556 | 893 | ||
557 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
894 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
558 | 895 | ||
559 | 896 | ||
560 | 897 | ||
561 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
898 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
562 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
899 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
563 | // painter->setRenderHint(QPainter::Antialiasing);
|
900 | // painter->setRenderHint(QPainter::Antialiasing);
|
564 | painter->drawPixmap( nX, nY, m_pxPicture ); |
901 | painter->drawPixmap( nX, nY, m_pxPicture ); |
565 | 902 | ||
566 | // drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
903 | // drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
|
567 | 904 | ||
568 | /*
|
905 | /*
|
569 | QPen pen = painter->pen();
|
906 | QPen pen = painter->pen();
|
570 | painter->setPen(option.palette.color(QPalette::Mid));
|
907 | painter->setPen(option.palette.color(QPalette::Mid));
|
571 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
908 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
|
572 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
909 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
573 | painter->setPen(pen);
|
910 | painter->setPen(pen);
|
574 | */
|
911 | */
|
575 | }
|
912 | }
|
576 | 913 | ||
577 | 914 | ||
578 | void IconDelegate::setTableView(QTableView* table){ |
915 | void IconDelegate::setTableView(QTableView* table){ |
579 | tblView = table; |
916 | tblView = table; |
580 | }
|
917 | }
|
581 | 918 | ||
582 | 919 | ||
583 | 920 | ||
584 | 921 | ||
585 | 922 | ||
586 | 923 | ||
587 | 924 | ||
588 | 925 | ||
589 | FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent) |
926 | FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent) |
590 | {
|
927 | {
|
591 | }
|
928 | }
|
592 | 929 | ||
593 | 930 | ||
594 | 931 | ||
595 | 932 | ||
596 | 933 | ||
597 | QWidget *FileDelegate::createEditor(QWidget *parent, |
934 | QWidget *FileDelegate::createEditor(QWidget *parent, |
598 | const QStyleOptionViewItem &/* option */, |
935 | const QStyleOptionViewItem &/* option */, |
599 | const QModelIndex & /* index */) const |
936 | const QModelIndex & /* index */) const |
600 | {
|
937 | {
|
601 | 938 | ||
602 | FileForm *editor = new FileForm(parent); |
939 | FileForm *editor = new FileForm(parent); |
603 | 940 | ||
604 | return editor; |
941 | return editor; |
605 | 942 | ||
606 | }
|
943 | }
|
607 | 944 | ||
608 | 945 | ||
609 | 946 | ||
610 | 947 | ||
611 | void FileDelegate::setEditorData(QWidget *editor, |
948 | void FileDelegate::setEditorData(QWidget *editor, |
612 | const QModelIndex &index) const |
949 | const QModelIndex &index) const |
613 | {
|
950 | {
|
614 | 951 | ||
615 | 952 | ||
616 | const QAbstractItemModel * model = index.model(); |
953 | const QAbstractItemModel * model = index.model(); |
617 | FileForm *flFrm = static_cast<FileForm*>(editor); |
954 | FileForm *flFrm = static_cast<FileForm*>(editor); |
618 | QVariant currentData = model->data(index,Qt::EditRole); |
955 | QVariant currentData = model->data(index,Qt::EditRole); |
619 | 956 | ||
620 | // QVariant currentData = QSqlTableModel::data(index, 0);
|
957 | // QVariant currentData = QSqlTableModel::data(index, 0);
|
621 | QByteArray bytes = currentData.toByteArray(); |
958 | QByteArray bytes = currentData.toByteArray(); |
622 | if (currentData.isValid()) flFrm->setData(bytes); |
959 | if (currentData.isValid()) flFrm->setData(bytes); |
623 | else return; |
960 | else return; |
624 | 961 | ||
625 | }
|
962 | }
|
626 | 963 | ||
627 | 964 | ||
628 | 965 | ||
629 | 966 | ||
630 | void FileDelegate::setModelData( |
967 | void FileDelegate::setModelData( |
631 | QWidget *editor,
|
968 | QWidget *editor,
|
632 | QAbstractItemModel *model,
|
969 | QAbstractItemModel *model,
|
633 | const QModelIndex& index) const { |
970 | const QModelIndex& index) const { |
634 | FileForm *flFrm = static_cast<FileForm*>(editor); |
971 | FileForm *flFrm = static_cast<FileForm*>(editor); |
635 | if (!(flFrm->dataIsChanged())) return; |
972 | if (!(flFrm->dataIsChanged())) return; |
636 | 973 | ||
637 | m_Data = flFrm->data(); |
974 | m_Data = flFrm->data(); |
638 | if (!m_Data.isEmpty()) { // åñëè ìàññèâ íå ïóñòîé |
975 | if (!m_Data.isEmpty()) { // åñëè ìàññèâ íå ïóñòîé |
639 | model->setData(index, QVariant (m_Data), Qt::EditRole); |
976 | model->setData(index, QVariant (m_Data), Qt::EditRole); |
640 | 977 | ||
641 | }
|
978 | }
|
642 | else { // åñëè ìàññèâ ïóñòîé (ôàéë óäàëèëè) |
979 | else { // åñëè ìàññèâ ïóñòîé (ôàéë óäàëèëè) |
643 | QVariant val_null;
|
980 | QVariant val_null;
|
644 | model->setData(index, val_null, Qt::EditRole); |
981 | model->setData(index, val_null, Qt::EditRole); |
645 | 982 | ||
646 | }
|
983 | }
|
647 | 984 | ||
648 | 985 | ||
649 | 986 | ||
650 | }
|
987 | }
|
651 | 988 | ||
652 | 989 | ||
653 | 990 | ||
654 | void FileDelegate::updateEditorGeometry( |
991 | void FileDelegate::updateEditorGeometry( |
655 | QWidget *editor,
|
992 | QWidget *editor,
|
656 | const QStyleOptionViewItem &option, |
993 | const QStyleOptionViewItem &option, |
657 | const QModelIndex& /* index */) const { |
994 | const QModelIndex& /* index */) const { |
658 | 995 | ||
659 | 996 | ||
660 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
997 | // QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
|
661 | // editor->setGeometry(r);
|
998 | // editor->setGeometry(r);
|
662 | 999 | ||
663 | editor->setGeometry(option.rect); |
1000 | editor->setGeometry(option.rect); |
664 | }
|
1001 | }
|
665 | 1002 | ||
666 | 1003 | ||
667 | 1004 | ||
668 | 1005 | ||
669 | 1006 |