annotate ui/certificateitemdelegate.cpp @ 399:55cbe0a482ce

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 16 Apr 2014 10:01:02 +0200
parents 9e38a4bcd44e
children 17e1c8f37d72
rev   line source
266
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
1 #include <QtWidgets>
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
2
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
3 #include "certificate.h"
372
6cc124e79066 Add save/load unselected and add role enumerator
Andre Heinecke <andre.heinecke@intevation.de>
parents: 266
diff changeset
4 #include "mainwindow.h"
266
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
5 #include "certificateitemdelegate.h"
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
6
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
7 void CertificateItemDelegate::paint(QPainter *painter,
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
8 const QStyleOptionViewItem &option, const QModelIndex &index) const
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
9 {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
10 // Save the current painter.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
11 painter->save();
375
9e38a4bcd44e Move certificateitem role into the certificateitemdelagate where it belongs.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 372
diff changeset
12 int status = index.data(StatusRole).toInt();
266
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
13 if (status == 0) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
14 // This status is not known, so draw the default item.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
15 QStyledItemDelegate::paint(painter, option, index);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
16 painter->restore();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
17 return;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
18 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
19
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
20 if (status == Certificate::InstallNew) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
21 //Set the icon and use bold and bigger font to highlight this item.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
22 QIcon *icon = new QIcon(":/img/list-add.png");
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
23 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
24 font->setBold(true);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
25 font->setPointSize(font->pointSize() + 1);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
26 drawItem(painter, option, index, icon, font);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
27 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
28 else if (status == Certificate::InstallOld) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
29 // Set the icon and use the default font for this item.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
30 QIcon *icon = new QIcon(":/img/list-add.png");
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
31 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
32 drawItem(painter, option, index, icon, font);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
33 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
34 else if (status == Certificate::RemoveNew) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
35 //Set the icon and use bold and bigger font to highlight this item.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
36 QIcon *icon = new QIcon(":/img/list-remove.png");
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
37 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
38 font->setBold(true);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
39 font->setPointSize(font->pointSize() + 1);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
40 drawItem(painter, option, index, icon, font);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
42 else if (status == Certificate::RemoveOld) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
43 // Set the icon and use the default font for this item.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
44 QIcon *icon = new QIcon(":/img/list-remove.png");
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
45 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
46 drawItem(painter, option, index, icon, font);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
47 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
48 else {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49 // Draw the default item.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
50 QStyledItemDelegate::paint(painter, option, index);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
51 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
52 // Restore the painter to have an unmodified painter for the next draw
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
53 // action.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
54 painter->restore();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
55 return;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
56 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
57
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
58 void CertificateItemDelegate::drawItem(QPainter *painter,
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
59 const QStyleOptionViewItem &option, const QModelIndex &index,
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
60 QIcon *icon, QFont *font) const
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
61 {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
62 // Get temporary style option to draw a checkbox only.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
63 QStyleOptionViewItem opt = option;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
64 // Initialize the style options with the temporary option object.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
65 initStyleOption(&opt, index);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
66 // Clear all text to draw the checkbox only.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
67 opt.text.clear();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
68
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
69 // Draw highlighted background.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
70 if (option.state & QStyle::State_Selected) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
71 painter->fillRect(option.rect, option.palette.highlight());
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
72 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
73
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
74 // Draw the checkbox control with the temporary options.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
75 QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
76
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
77 // Draw the icon.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
78 int iconSpace = 25;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
79 if (!icon->isNull()) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
80 QRect rect = option.rect.adjusted(25, 0, 0, 0);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
81 icon->paint(painter, rect, Qt::AlignVCenter|Qt::AlignLeft);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
82 iconSpace = 50;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
83 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
84
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
85 // Draw the text using the given font.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
86 QString text = index.data().toString();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
87 QRect rect = option.rect.adjusted(iconSpace, 0, 0, 0);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
88 painter->setFont(*font);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
89 painter->drawText(rect.left(), rect.top(), rect.width(), rect.height(),
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
90 Qt::AlignVCenter|Qt::AlignLeft, text, &rect);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
91 }

http://wald.intevation.org/projects/trustbridge/