Редакция 201 | Редакция 205 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
123 | pingvin | 1 | |
2 | |||
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
||
4 | ** All rights reserved. |
||
5 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
||
6 | ** |
||
7 | ** This file is part of the examples of the Qt Toolkit. |
||
8 | ** |
||
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
||
10 | ** Commercial Usage |
||
11 | ** Licensees holding valid Qt Commercial licenses may use this file in |
||
12 | ** accordance with the Qt Commercial License Agreement provided with the |
||
13 | ** Software or, alternatively, in accordance with the terms contained in |
||
14 | ** a written agreement between you and Nokia. |
||
15 | ** |
||
16 | ** GNU Lesser General Public License Usage |
||
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
||
18 | ** General Public License version 2.1 as published by the Free Software |
||
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
||
20 | ** packaging of this file. Please review the following information to |
||
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
||
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
||
23 | ** |
||
24 | ** In addition, as a special exception, Nokia gives you certain additional |
||
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
||
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
||
27 | ** |
||
28 | ** GNU General Public License Usage |
||
29 | ** Alternatively, this file may be used under the terms of the GNU |
||
30 | ** General Public License version 3.0 as published by the Free Software |
||
31 | ** Foundation and appearing in the file LICENSE.GPL included in the |
||
32 | ** packaging of this file. Please review the following information to |
||
33 | ** ensure the GNU General Public License version 3.0 requirements will be |
||
34 | ** met: http://www.gnu.org/copyleft/gpl.html. |
||
35 | ** |
||
36 | ** If you have questions regarding the use of this file, please contact |
||
37 | ** Nokia at qt-info@nokia.com. |
||
38 | ** $QT_END_LICENSE$ |
||
39 | ** |
||
40 | ****************************************************************************/ |
||
41 | |||
42 | /* |
||
43 | |||
44 | |||
45 | A delegate that allows the user to change integer values from the model |
||
46 | |||
47 | */ |
||
48 | |||
49 | #include <QtGui> |
||
50 | |||
51 | #include "delegate.h" |
||
52 | |||
53 | |||
54 | |||
55 | |||
56 | : QItemDelegate(parent) |
||
57 | { |
||
58 | } |
||
59 | //! [0] |
||
60 | |||
61 | void SpinBoxDelegate::setInctance(QString newInctance){ |
||
62 | |||
126 | pingvin | 63 | } |
64 | |||
65 | void SpinBoxDelegate::getItems(){ |
||
66 | |||
67 | QSqlQuery q; |
||
68 | QString query_str; |
||
69 | bool ok; |
||
70 | |||
71 | if (SpinBoxDelegate::inctance.isEmpty()) |
||
72 | |||
73 | items.clear(); |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | query_str = tr("select * from `"); |
||
78 | |||
79 | query_str.append( tr("`")); |
||
80 | |||
81 | q.prepare(query_str); |
||
82 | |||
83 | |||
84 | |||
85 | |||
86 | |||
87 | |||
88 | |||
89 | |||
90 | |||
91 | QString error_str; |
||
92 | error_str = tr(" "); |
||
93 | error_str.append(inctance); |
||
94 | QMessageBox::critical( // . |
||
95 | this, // . |
||
96 | QObject::tr("Database Error"), // . |
||
97 | q.lastError().text()); // . |
||
98 | // tr(" ")); // . |
||
99 | |||
100 | */ |
||
101 | |||
102 | return; |
||
103 | } |
||
104 | |||
105 | |||
106 | |||
107 | |||
108 | while(q.next()){ |
||
129 | pingvin | 109 | |
126 | pingvin | 110 | QString ID; |
111 | |||
112 | ID = q.value(0).toString(); // ID |
||
113 | Name = q.value(1).toString(); // Name |
||
114 | items.insert(ID, Name); |
||
115 | } |
||
116 | |||
117 | |||
118 | |||
119 | |||
120 | |||
121 | |||
122 | |||
123 | |||
123 | pingvin | 124 | const QStyleOptionViewItem &/* option */, |
125 | const QModelIndex &/* index */) const |
||
126 | { |
||
127 | //QSpinBox *editor = new QSpinBox(parent); |
||
128 | //editor->setMinimum(0); |
||
129 | //editor->setMaximum(100); |
||
130 | QStringList ID_list; |
||
131 | QStringList Name_list; |
||
126 | pingvin | 132 | |
133 | // getItems(); |
||
134 | |||
135 | |||
136 | |||
137 | |||
138 | |||
139 | |||
140 | |||
141 | |||
142 | QComboBox *editor = new QComboBox(parent); |
||
143 | |||
123 | pingvin | 144 | /* |
126 | pingvin | 145 | |
146 | editor->addItem("1"); |
||
123 | pingvin | 147 | editor->addItem("2"); |
148 | editor->addItem("3"); |
||
149 | editor->addItem("4"); |
||
150 | editor->addItem("5"); |
||
151 | editor->addItem("6"); |
||
152 | editor->addItem("7"); |
||
153 | editor->addItem("8"); |
||
154 | editor->addItem("9"); |
||
155 | */ |
||
156 | |||
126 | pingvin | 157 | // editor->addItem("0"); |
123 | pingvin | 158 | |
126 | pingvin | 159 | editor->addItems(Name_list); |
160 | |||
161 | return editor; |
||
179 | pingvin | 162 | } |
123 | pingvin | 163 | //! [1] |
164 | |||
165 | //! [2] |
||
166 | |||
167 | const QModelIndex &index) const |
||
168 | { |
||
169 | // int value = index.model()->data(index, Qt::EditRole).toInt(); |
||
170 | |||
171 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
||
172 | |||
173 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
||
174 | |||
175 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
||
136 | pingvin | 176 | |
123 | pingvin | 177 | QStringList ID_list; |
126 | pingvin | 178 | |
179 | int curr_index; |
||
180 | |||
181 | ID_list = items.keys(); |
||
182 | |||
183 | |||
184 | curr_index = ID_list.indexOf(value); |
||
185 | |||
186 | comboBox->setCurrentIndex(curr_index); |
||
136 | pingvin | 187 | /**************************************************** |
126 | pingvin | 188 | if (value == "0") comboBox->setCurrentIndex(0); |
189 | if (value == "1") comboBox->setCurrentIndex(1); |
||
123 | pingvin | 190 | if (value == "2") comboBox->setCurrentIndex(2); |
191 | if (value == "3") comboBox->setCurrentIndex(3); |
||
192 | if (value == "4") comboBox->setCurrentIndex(4); |
||
193 | if (value == "5") comboBox->setCurrentIndex(5); |
||
194 | if (value == "6") comboBox->setCurrentIndex(6); |
||
195 | if (value == "7") comboBox->setCurrentIndex(7); |
||
196 | if (value == "8") comboBox->setCurrentIndex(8); |
||
197 | if (value == "9") comboBox->setCurrentIndex(9); |
||
198 | ******************************************************/ |
||
199 | comboBox->setEditable(false); |
||
126 | pingvin | 200 | |
179 | pingvin | 201 | |
123 | pingvin | 202 | |
203 | |||
204 | } |
||
205 | |||
206 | |||
207 | //! [3] |
||
208 | |||
209 | const QModelIndex &index) const |
||
210 | { |
||
211 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
||
212 | // spinBox->interpretText(); |
||
213 | // int value = spinBox->value(); |
||
214 | |||
215 | // model->setData(index, value, Qt::EditRole); |
||
216 | |||
217 | QStringList Name_list; |
||
126 | pingvin | 218 | |
219 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
||
123 | pingvin | 220 | |
221 | currIndex = comboBox->currentIndex(); |
||
222 | if (currIndex==-1) return; |
||
223 | // QString value = comboBox->itemText(currIndex); |
||
136 | pingvin | 224 | |
126 | pingvin | 225 | ID_list = items.keys(); |
226 | |||
227 | |||
228 | QString value = ID_list.at(currIndex); |
||
229 | |||
230 | } |
||
123 | pingvin | 231 | //! [3] |
232 | |||
233 | //! [4] |
||
234 | |||
235 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
||
236 | { |
||
237 | editor->setGeometry(option.rect); |
||
238 | } |
||
239 | //! [4] |
||
240 | MyDEDelegate::MyDEDelegate( |
||
241 | bool calpopup, |
||
242 | QObject *parent) |
||
243 | : QItemDelegate(parent), |
||
244 | m_calpopup(calpopup) { |
||
245 | } |
||
246 | |||
247 | QWidget *MyDEDelegate::createEditor( |
||
248 | |||
249 | const QStyleOptionViewItem& /* option */, |
||
250 | const QModelIndex& /* index */) const { |
||
251 | QDateEdit *editor = new QDateEdit(parent); |
||
252 | editor->setCalendarPopup(m_calpopup); |
||
253 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
||
254 | return editor; |
||
255 | } |
||
256 | |||
257 | void MyDEDelegate::setEditorData( |
||
258 | |||
259 | const QModelIndex &index) const { |
||
260 | QDate value = index.model()->data( |
||
261 | index, Qt::EditRole).toDate(); |
||
262 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
||
263 | de->setDate(value); |
||
264 | } |
||
265 | |||
266 | void MyDEDelegate::setModelData( |
||
267 | |||
268 | QAbstractItemModel *model, |
||
269 | const QModelIndex& index) const { |
||
270 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
||
271 | de->interpretText(); |
||
272 | QDate value = de->date(); |
||
273 | model->setData(index, value); |
||
274 | } |
||
275 | |||
276 | void MyDEDelegate::updateEditorGeometry( |
||
277 | |||
278 | const QStyleOptionViewItem &option, |
||
279 | const QModelIndex& /* index */) const { |
||
280 | |||
281 | |||
169 | pingvin | 282 | |
283 | |||
284 | |||
123 | pingvin | 285 | |
286 | |||
138 | pingvin | 287 | |
288 | |||
289 | |||
290 | |||
291 | |||
292 | } |
||
293 | |||
294 | void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
||
295 | |||
296 | m_pxPicture.fill( QColor(Qt::white) ); |
||
297 | |||
298 | const QAbstractItemModel * model = index.model(); |
||
299 | |||
300 | |||
301 | if ( !sFileName.isEmpty() ) |
||
302 | |||
303 | else { |
||
304 | //QItemDelegate::paint(painter, option, index); |
||
305 | return; |
||
306 | } |
||
307 | |||
308 | QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ? |
||
309 | |||
310 | QPalette::Disabled; |
||
311 | |||
312 | if (option.state & QStyle::State_Selected) |
||
313 | |||
314 | |||
315 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
||
316 | |||
317 | painter->drawPixmap( nX, nY, m_pxPicture ); |
||
318 | |||
319 | |||
320 | |||
321 | |||
322 | /* |
||
323 | |||
324 | painter->setPen(option.palette.color(QPalette::Mid)); |
||
325 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); |
||
326 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); |
||
327 | painter->setPen(pen); |
||
328 | */ |
||
329 | } |
||
330 | |||
331 | |||
158 | pingvin | 332 | |
333 | |||
334 | |||
335 | |||
336 | { |
||
337 | int datetime = index.model()->data(index, Qt::DisplayRole).toInt(); |
||
338 | |||
339 | QString indexvalue = ""; |
||
340 | |||
341 | if (datetime > 0) |
||
342 | |||
343 | QDateTime dateTime2 = QDateTime(); |
||
344 | dateTime2.setTime_t(datetime); |
||
345 | indexvalue = dateTime2.toString(this->timeformat); |
||
346 | } |
||
347 | else |
||
348 | { |
||
349 | indexvalue = tr("Date not set"); |
||
350 | } |
||
351 | |||
352 | Q_ASSERT(index.isValid()); |
||
353 | |||
354 | QStyleOptionViewItemV3 opt = setOptions(index, option); |
||
355 | |||
356 | const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option); |
||
357 | |||
358 | : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None); |
||
359 | const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option); |
||
360 | opt.locale = v3 ? v3->locale : QLocale(); |
||
361 | opt.widget = v3 ? v3->widget : 0; |
||
362 | |||
363 | // prepare |
||
364 | |||
365 | |||
366 | painter->setClipRect(opt.rect); |
||
367 | |||
368 | // get the data and the rectangles |
||
369 | |||
370 | |||
371 | QPixmap pixmap; |
||
372 | |||
373 | value = index.data(Qt::DecorationRole); |
||
374 | |||
375 | QString text; |
||
376 | |||
377 | value = index.data(Qt::DisplayRole); |
||
378 | if (value.isValid()) { |
||
379 | text = indexvalue; |
||
380 | displayRect = textRectangle(painter, option.rect, opt.font, text); |
||
381 | } |
||
382 | |||
383 | QRect checkRect; |
||
384 | |||
385 | value = index.data(Qt::CheckStateRole); |
||
386 | if (value.isValid()) { |
||
387 | checkState = static_cast<Qt::CheckState>(value.toInt()); |
||
388 | checkRect = check(opt, opt.rect, value); |
||
389 | } |
||
390 | |||
391 | // do the layout |
||
392 | |||
393 | // draw the item |
||
394 | |||
395 | drawBackground(painter, opt, index); |
||
396 | |||
397 | drawDecoration(painter, opt, decorationRect, pixmap); |
||
398 | drawDisplay(painter, opt, displayRect, text); |
||
399 | drawFocus(painter, opt, displayRect); |
||
400 | |||
401 | // done |
||
402 | |||
403 | } |
||
404 | |||
405 | |||
180 | pingvin | 406 | |
407 | |||
408 | |||
409 | |||
410 | |||
411 | |||
412 | } |
||
413 | |||
414 | |||
415 | |||
416 | |||
417 | |||
418 | |||
419 | |||
420 | const QModelIndex & /* index */) const |
||
421 | { |
||
422 | |||
423 | IconForm *editor = new IconForm(parent); |
||
424 | |||
425 | return editor; |
||
426 | |||
427 | } |
||
428 | |||
429 | |||
430 | |||
431 | |||
432 | |||
433 | |||
434 | { |
||
435 | |||
436 | |||
437 | |||
438 | |||
439 | QVariant currentImage = model->data(index,0); |
||
440 | QByteArray bytes = currentImage.toByteArray(); |
||
441 | if (currentImage.isValid()) { |
||
442 | m_pxPicture.loadFromData(bytes); |
||
443 | } |
||
444 | else { |
||
445 | //QItemDelegate::paint(painter, option, index); |
||
446 | |||
447 | |||
448 | |||
449 | |||
450 | icnFrm->setPixmap(m_pxPicture); |
||
451 | // tblView->setRowHeight(index.row(), icnFrm->geometry().height()); |
||
452 | } |
||
453 | |||
454 | |||
455 | |||
456 | |||
457 | |||
458 | |||
459 | QAbstractItemModel *model, |
||
460 | const QModelIndex& index) const { |
||
461 | IconForm *icnFrm = static_cast<IconForm*>(editor); |
||
462 | if (!(icnFrm->dataIsChanged())) return; |
||
463 | |||
464 | m_pxPicture = icnFrm->pixmap(); |
||
465 | |||
466 | QByteArray bytes; |
||
467 | QBuffer buffer(&bytes); |
||
468 | buffer.open(QIODevice::WriteOnly); |
||
469 | currentImage.save(&buffer, "PNG"); |
||
470 | |||
471 | |||
472 | |||
473 | |||
474 | |||
475 | |||
476 | // int widht = m_pxPicture.width(); |
||
477 | |||
478 | // tblView->setRowHeight(index.row(), heigh + 10); |
||
479 | // model->submitAll(); |
||
480 | |||
481 | |||
482 | |||
483 | |||
484 | |||
485 | |||
486 | |||
487 | |||
488 | |||
489 | |||
490 | |||
491 | const QStyleOptionViewItem &option, |
||
492 | const QModelIndex& /* index */) const { |
||
493 | |||
494 | |||
495 | |||
496 | |||
497 | |||
498 | editor->setGeometry(option.rect); |
||
499 | |||
500 | |||
501 | |||
502 | |||
503 | |||
504 | |||
505 | |||
506 | |||
507 | |||
508 | |||
509 | |||
510 | const QAbstractItemModel * model = index.model(); |
||
511 | |||
512 | |||
513 | QVariant currentImage = model->data(index,0); |
||
514 | |||
515 | |||
516 | |||
517 | |||
518 | // image.loadFromData(bytes); |
||
519 | |||
520 | |||
521 | |||
522 | |||
523 | |||
524 | } |
||
525 | |||
526 | //QItemDelegate::paint(painter, option, index); |
||
527 | return; |
||
528 | } |
||
529 | |||
530 | |||
531 | |||
532 | |||
533 | |||
534 | |||
535 | |||
536 | if ( !sFileName.isEmpty() ) |
||
537 | |||
538 | else { |
||
539 | //QItemDelegate::paint(painter, option, index); |
||
540 | return; |
||
541 | } |
||
542 | */ |
||
543 | |||
544 | |||
545 | |||
546 | |||
547 | QPalette::Disabled; |
||
548 | |||
549 | if (option.state & QStyle::State_Selected) |
||
550 | |||
551 | |||
552 | |||
553 | |||
554 | int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 ); |
||
555 | |||
556 | painter->drawPixmap( nX, nY, m_pxPicture ); |
||
557 | |||
558 | |||
559 | |||
560 | |||
561 | /* |
||
562 | |||
563 | painter->setPen(option.palette.color(QPalette::Mid)); |
||
564 | painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight()); |
||
565 | painter->drawLine(option.rect.topRight(), option.rect.bottomRight()); |
||
566 | painter->setPen(pen); |
||
567 | */ |
||
568 | } |
||
569 | |||
570 | |||
571 | |||
572 | |||
573 | } |
||
574 | |||
575 | |||
201 | pingvin | 576 | |
577 | |||
578 | |||
579 | |||
580 | |||
581 | |||
582 | |||
583 | |||
584 | } |
||
585 | |||
586 | |||
587 | |||
588 | |||
589 | |||
590 | |||
591 | |||
592 | const QModelIndex & /* index */) const |
||
593 | { |
||
594 | |||
595 | FileForm *editor = new FileForm(parent); |
||
596 | |||
597 | return editor; |
||
598 | |||
599 | } |
||
600 | |||
601 | |||
602 | |||
603 | |||
604 | |||
605 | |||
606 | { |
||
607 | |||
608 | |||
609 | |||
610 | |||
611 | QVariant currentData = model->data(index,Qt::EditRole); |
||
612 | |||
202 | pingvin | 613 | // QVariant currentData = QSqlTableModel::data(index, 0); |
614 | |||
615 | if (currentData.isValid()) flFrm->setData(bytes); |
||
201 | pingvin | 616 | else return; |
617 | |||
618 | } |
||
619 | |||
620 | |||
621 | |||
622 | |||
623 | |||
624 | |||
625 | QAbstractItemModel *model, |
||
626 | const QModelIndex& index) const { |
||
627 | FileForm *flFrm = static_cast<FileForm*>(editor); |
||
628 | if (!(flFrm->dataIsChanged())) return; |
||
629 | |||
630 | m_Data = flFrm->data(); |
||
631 | |||
632 | model->setData(index, QVariant (m_Data), Qt::EditRole); |
||
633 | |||
634 | } |
||
635 | |||
636 | QVariant val_null; |
||
637 | model->setData(index, val_null, Qt::EditRole); |
||
638 | |||
639 | } |
||
640 | |||
641 | |||
642 | |||
643 | |||
644 | |||
645 | |||
646 | |||
647 | |||
648 | |||
649 | const QStyleOptionViewItem &option, |
||
650 | const QModelIndex& /* index */) const { |
||
651 | |||
652 | |||
653 | |||
654 | |||
655 | |||
656 | editor->setGeometry(option.rect); |
||
657 | |||
658 |