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