annotate ui/certificateitemdelegate.cpp @ 371:6b78a89c1e17

Fix NSS test in release build and add verify's to avoid segfault with invalid list.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 15 Apr 2014 11:04:34 +0200
parents dd6a09d2699f
children 6cc124e79066
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"
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
4 #include "certificateitemdelegate.h"
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
5
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
6 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
7 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
8 {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
9 // Save the current painter.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
10 painter->save();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
11 int status = index.data(Qt::UserRole + 1).toInt();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
12 if (status == 0) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
13 // 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
14 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
15 painter->restore();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
16 return;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
17 }
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 if (status == Certificate::InstallNew) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
20 //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
21 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
22 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
23 font->setBold(true);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
24 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
25 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
26 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
27 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
28 // 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
29 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
30 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
31 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
32 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
33 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
34 //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
35 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
36 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
37 font->setBold(true);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
38 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
39 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
40 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
41 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
42 // 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
43 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
44 QFont *font = new QFont();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
45 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
46 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
47 else {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
48 // Draw the default item.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
49 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
50 }
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
51 // 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
52 // action.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
53 painter->restore();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
54 return;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
55 }
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 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
58 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
59 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
60 {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
61 // 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
62 QStyleOptionViewItem opt = option;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
63 // 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
64 initStyleOption(&opt, index);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
65 // 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
66 opt.text.clear();
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
67
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
68 // Draw highlighted background.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
69 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
70 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
71 }
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 // 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
74 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
75
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
76 // Draw the icon.
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
77 int iconSpace = 25;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
78 if (!icon->isNull()) {
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
79 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
80 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
81 iconSpace = 50;
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
82 }
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 // 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
85 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
86 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
87 painter->setFont(*font);
dd6a09d2699f Added delegate for certificate list items to render a custom ui.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff changeset
88 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
89 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
90 }

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