Rev 138 | Rev 169 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
123 | pingvin | 1 | |
2 | |||
3 | /**************************************************************************** |
||
4 | ** |
||
5 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
||
6 | ** All rights reserved. |
||
7 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
||
8 | ** |
||
9 | ** This file is part of the examples of the Qt Toolkit. |
||
10 | ** |
||
11 | ** $QT_BEGIN_LICENSE:LGPL$ |
||
12 | ** Commercial Usage |
||
13 | ** Licensees holding valid Qt Commercial licenses may use this file in |
||
14 | ** accordance with the Qt Commercial License Agreement provided with the |
||
15 | ** Software or, alternatively, in accordance with the terms contained in |
||
16 | ** a written agreement between you and Nokia. |
||
17 | ** |
||
18 | ** GNU Lesser General Public License Usage |
||
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 |
||
21 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
||
22 | ** packaging of this file. Please review the following information to |
||
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. |
||
25 | ** |
||
26 | ** In addition, as a special exception, Nokia gives you certain additional |
||
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. |
||
29 | ** |
||
30 | ** GNU General Public License Usage |
||
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 |
||
33 | ** Foundation and appearing in the file LICENSE.GPL included in the |
||
34 | ** packaging of this file. Please review the following information to |
||
35 | ** ensure the GNU General Public License version 3.0 requirements will be |
||
36 | ** met: http://www.gnu.org/copyleft/gpl.html. |
||
37 | ** |
||
38 | ** If you have questions regarding the use of this file, please contact |
||
39 | ** Nokia at qt-info@nokia.com. |
||
40 | ** $QT_END_LICENSE$ |
||
41 | ** |
||
42 | ****************************************************************************/ |
||
43 | |||
44 | /* |
||
45 | delegate.cpp |
||
46 | |||
47 | A delegate that allows the user to change integer values from the model |
||
48 | using a spin box widget. |
||
49 | */ |
||
50 | |||
51 | #include <QtGui> |
||
52 | |||
53 | #include "delegate.h" |
||
54 | |||
55 | |||
56 | //! [0] |
||
57 | SpinBoxDelegate::SpinBoxDelegate(QObject *parent) |
||
58 | : QItemDelegate(parent) |
||
59 | { |
||
60 | } |
||
61 | //! [0] |
||
62 | |||
126 | pingvin | 63 | void SpinBoxDelegate::setInctance(QString newInctance){ |
64 | inctance = newInctance; |
||
65 | } |
||
66 | |||
67 | void SpinBoxDelegate::getItems(){ |
||
68 | QMap <QString, QString> result_map; |
||
69 | QSqlQuery q; |
||
70 | QString query_str; |
||
71 | bool ok; |
||
72 | |||
73 | if (SpinBoxDelegate::inctance.isEmpty()) |
||
74 | { |
||
75 | items.clear(); |
||
76 | return; |
||
77 | } |
||
78 | |||
79 | query_str = tr("select * from `"); |
||
80 | query_str.append(inctance); |
||
81 | query_str.append( tr("`")); |
||
82 | |||
83 | q.prepare(query_str); |
||
84 | |||
85 | |||
86 | ok = q.exec(); |
||
87 | |||
88 | |||
89 | |||
90 | |||
91 | if (!ok) { |
||
92 | /* |
||
93 | QString error_str; |
||
94 | error_str = tr(" "); |
||
95 | error_str.append(inctance); |
||
96 | QMessageBox::critical( // . |
||
97 | this, // . |
||
98 | QObject::tr("Database Error"), // . |
||
99 | q.lastError().text()); // . |
||
100 | // tr(" ")); // . |
||
101 | |||
102 | */ |
||
103 | items.clear(); |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | |||
108 | // field_inctance = q.record().indexOf(tr("TableWhithInstance")); |
||
129 | pingvin | 109 | items.clear(); |
126 | pingvin | 110 | while(q.next()){ |
111 | |||
112 | QString ID; |
||
113 | QString Name; |
||
114 | ID = q.value(0).toString(); // ID |
||
115 | Name = q.value(1).toString(); // Name |
||
116 | items.insert(ID, Name); |
||
117 | } |
||
118 | |||
119 | |||
120 | // return result_map; |
||
121 | } |
||
122 | |||
123 | |||
123 | pingvin | 124 | //! [1] |
125 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, |
||
126 | const QStyleOptionViewItem &/* option */, |
||
127 | const QModelIndex &/* index */) const |
||
128 | { |
||
129 | //QSpinBox *editor = new QSpinBox(parent); |
||
130 | //editor->setMinimum(0); |
||
131 | //editor->setMaximum(100); |
||
126 | pingvin | 132 | QStringList ID_list; |
133 | QStringList Name_list; |
||
134 | |||
135 | // getItems(); |
||
136 | |||
137 | |||
138 | |||
139 | |||
140 | |||
141 | ID_list = items.keys(); |
||
142 | Name_list = items.values(); |
||
143 | |||
123 | pingvin | 144 | QComboBox *editor = new QComboBox(parent); |
126 | pingvin | 145 | |
146 | /* |
||
123 | pingvin | 147 | editor->addItem("0"); |
148 | editor->addItem("1"); |
||
149 | editor->addItem("2"); |
||
150 | editor->addItem("3"); |
||
151 | editor->addItem("4"); |
||
152 | editor->addItem("5"); |
||
153 | editor->addItem("6"); |
||
154 | editor->addItem("7"); |
||
155 | editor->addItem("8"); |
||
156 | editor->addItem("9"); |
||
126 | pingvin | 157 | */ |
123 | pingvin | 158 | |
126 | pingvin | 159 | // editor->addItem("0"); |
160 | |||
161 | editor->addItems(Name_list); |
||
123 | pingvin | 162 | return editor; |
163 | } |
||
164 | //! [1] |
||
165 | |||
166 | //! [2] |
||
167 | void SpinBoxDelegate::setEditorData(QWidget *editor, |
||
168 | const QModelIndex &index) const |
||
169 | { |
||
170 | // int value = index.model()->data(index, Qt::EditRole).toInt(); |
||
171 | |||
172 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
||
173 | // spinBox->setValue(value); |
||
174 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
||
136 | pingvin | 175 | |
123 | pingvin | 176 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
126 | pingvin | 177 | |
178 | QStringList ID_list; |
||
179 | QStringList Name_list; |
||
180 | int curr_index; |
||
181 | |||
182 | ID_list = items.keys(); |
||
183 | Name_list = items.values(); |
||
184 | |||
185 | curr_index = ID_list.indexOf(value); |
||
136 | pingvin | 186 | if (curr_index==-1) return; |
126 | pingvin | 187 | comboBox->setCurrentIndex(curr_index); |
188 | /**************************************************** |
||
123 | pingvin | 189 | if (value == "0") comboBox->setCurrentIndex(0); |
190 | if (value == "1") comboBox->setCurrentIndex(1); |
||
191 | if (value == "2") comboBox->setCurrentIndex(2); |
||
192 | if (value == "3") comboBox->setCurrentIndex(3); |
||
193 | if (value == "4") comboBox->setCurrentIndex(4); |
||
194 | if (value == "5") comboBox->setCurrentIndex(5); |
||
195 | if (value == "6") comboBox->setCurrentIndex(6); |
||
196 | if (value == "7") comboBox->setCurrentIndex(7); |
||
197 | if (value == "8") comboBox->setCurrentIndex(8); |
||
198 | if (value == "9") comboBox->setCurrentIndex(9); |
||
126 | pingvin | 199 | ******************************************************/ |
123 | pingvin | 200 | comboBox->setEditable(true); |
201 | |||
202 | |||
203 | //comboBox->setItemText(0, value); |
||
204 | |||
205 | } |
||
206 | //! [2] |
||
207 | |||
208 | //! [3] |
||
209 | void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
||
210 | const QModelIndex &index) const |
||
211 | { |
||
212 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
||
213 | // spinBox->interpretText(); |
||
214 | // int value = spinBox->value(); |
||
215 | |||
216 | // model->setData(index, value, Qt::EditRole); |
||
126 | pingvin | 217 | QStringList ID_list; |
218 | QStringList Name_list; |
||
123 | pingvin | 219 | |
220 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
||
221 | int currIndex; |
||
222 | currIndex = comboBox->currentIndex(); |
||
136 | pingvin | 223 | if (currIndex==-1) return; |
126 | pingvin | 224 | // QString value = comboBox->itemText(currIndex); |
225 | |||
226 | ID_list = items.keys(); |
||
227 | Name_list = items.values(); |
||
228 | |||
229 | QString value = ID_list.at(currIndex); |
||
123 | pingvin | 230 | model->setData(index, value, Qt::EditRole); |
231 | } |
||
232 | //! [3] |
||
233 | |||
234 | //! [4] |
||
235 | void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
||
236 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
||
237 | { |
||
238 | editor->setGeometry(option.rect); |
||
239 | } |
||
240 | //! [4] |
||
241 | MyDEDelegate::MyDEDelegate( |
||
242 | bool calpopup, |
||
243 | QObject *parent) |
||
244 | : QItemDelegate(parent), |
||
245 | m_calpopup(calpopup) { |
||
246 | } |
||
247 | |||
248 | QWidget *MyDEDelegate::createEditor( |
||
249 | QWidget *parent, |
||
250 | const QStyleOptionViewItem& /* option */, |
||
251 | const QModelIndex& /* index */) const { |
||
252 | QDateEdit *editor = new QDateEdit(parent); |
||
253 | editor->setCalendarPopup(m_calpopup); |
||
254 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
||
255 | return editor; |
||
256 | } |
||
257 | |||
258 | void MyDEDelegate::setEditorData( |
||
259 | QWidget *editor, |
||
260 | const QModelIndex &index) const { |
||
261 | QDate value = index.model()->data( |
||
262 | index, Qt::EditRole).toDate(); |
||
263 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
||
264 | de->setDate(value); |
||
265 | } |
||
266 | |||
267 | void MyDEDelegate::setModelData( |
||
268 | QWidget *editor, |
||
269 | QAbstractItemModel *model, |
||
270 | const QModelIndex& index) const { |
||
271 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
||
272 | de->interpretText(); |
||
273 | QDate value = de->date(); |
||
274 | model->setData(index, value); |
||
275 | } |
||
276 | |||
277 | void MyDEDelegate::updateEditorGeometry( |
||
278 | QWidget *editor, |
||
279 | const QStyleOptionViewItem &option, |
||
280 | const QModelIndex& /* index */) const { |
||
281 | editor->setGeometry(option.rect); |
||
282 | } |
||
138 | pingvin | 283 | |
284 | |||
285 | |||
286 | |||
287 | |||
288 | CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent) |
||
289 | { |
||
290 | } |
||
291 | |||
292 | void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
||
293 | { |
||
294 | m_pxPicture.fill( QColor(Qt::white) ); |
||
295 | |||
296 | const QAbstractItemModel * model = index.model(); |
||
297 | QString sFileName = model->data( index, Qt::DisplayRole ).toString(); |
||
298 | |||
299 | if ( !sFileName.isEmpty() ) |
||
300 | m_pxPicture.load( sFileName ); |
||
301 | else { |
||
302 | //QItemDelegate::paint(painter, option, index); |
||
303 | return; |
||
304 | } |
||
305 | |||
306 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
||
307 | ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) : |
||
308 | QPalette::Disabled; |
||
309 | |||
310 | if (option.state & QStyle::State_Selected) |
||
311 | painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight)); |
||
312 | |||
313 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
||
314 | int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 ); |
||
315 | painter->drawPixmap( nX, nY, m_pxPicture ); |
||
316 | |||
317 | |||
318 | //// drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves |
||
319 | |||
320 | /* |
||
321 | QPen pen = painter->pen(); |
||
322 | painter->setPen(option.palette.color(QPalette::Mid)); |
||
323 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); |
||
324 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); |
||
325 | painter->setPen(pen); |
||
326 | */ |
||
327 | } |
||
158 | pingvin | 328 | |
329 | |||
330 | |||
331 | |||
332 | void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
||
333 | const QModelIndex &index) const |
||
334 | { |
||
335 | int datetime = index.model()->data(index, Qt::DisplayRole).toInt(); |
||
336 | |||
337 | QString indexvalue = ""; |
||
338 | |||
339 | if (datetime > 0) |
||
340 | { |
||
341 | QDateTime dateTime2 = QDateTime(); |
||
342 | dateTime2.setTime_t(datetime); |
||
343 | indexvalue = dateTime2.toString(this->timeformat); |
||
344 | } |
||
345 | else |
||
346 | { |
||
347 | indexvalue = tr("Date not set"); |
||
348 | } |
||
349 | |||
350 | Q_ASSERT(index.isValid()); |
||
351 | |||
352 | QStyleOptionViewItemV3 opt = setOptions(index, option); |
||
353 | |||
354 | const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option); |
||
355 | opt.features = v2 ? v2->features |
||
356 | : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None); |
||
357 | const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option); |
||
358 | opt.locale = v3 ? v3->locale : QLocale(); |
||
359 | opt.widget = v3 ? v3->widget : 0; |
||
360 | |||
361 | // prepare |
||
362 | painter->save(); |
||
363 | |||
364 | painter->setClipRect(opt.rect); |
||
365 | |||
366 | // get the data and the rectangles |
||
367 | QVariant value; |
||
368 | |||
369 | QPixmap pixmap; |
||
370 | QRect decorationRect; |
||
371 | value = index.data(Qt::DecorationRole); |
||
372 | |||
373 | QString text; |
||
374 | QRect displayRect; |
||
375 | value = index.data(Qt::DisplayRole); |
||
376 | if (value.isValid()) { |
||
377 | text = indexvalue; |
||
378 | displayRect = textRectangle(painter, option.rect, opt.font, text); |
||
379 | } |
||
380 | |||
381 | QRect checkRect; |
||
382 | Qt::CheckState checkState = Qt::Unchecked; |
||
383 | value = index.data(Qt::CheckStateRole); |
||
384 | if (value.isValid()) { |
||
385 | checkState = static_cast<Qt::CheckState>(value.toInt()); |
||
386 | checkRect = check(opt, opt.rect, value); |
||
387 | } |
||
388 | |||
389 | // do the layout |
||
390 | doLayout(opt, &checkRect, &decorationRect, &displayRect, false); |
||
391 | // draw the item |
||
392 | |||
393 | drawBackground(painter, opt, index); |
||
394 | drawCheck(painter, opt, checkRect, checkState); |
||
395 | drawDecoration(painter, opt, decorationRect, pixmap); |
||
396 | drawDisplay(painter, opt, displayRect, text); |
||
397 | drawFocus(painter, opt, displayRect); |
||
398 | |||
399 | // done |
||
400 | painter->restore(); |
||
401 | } |