comparison ui/certificatetabledelegate.cpp @ 347:dde533ba4fcc

Added table item delegate for certificates.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 10 Apr 2014 14:13:34 +0200
parents
children c9315b24b055
comparison
equal deleted inserted replaced
346:a54925d41056 347:dde533ba4fcc
1 #include <QtWidgets>
2 #include <QComboBox>
3
4 #include "certificatetabledelegate.h"
5
6 void CertificateTableDelegate::paint(QPainter *painter,
7 const QStyleOptionViewItem &option, const QModelIndex &index) const
8 {
9 painter->save();
10
11 if (index.column() == 0) {
12 if (option.state & QStyle::State_Selected) {
13 painter->fillRect(option.rect, option.palette.highlight());
14 }
15 bool value = index.data(Qt::DisplayRole).toBool();
16 QIcon icon = value ? QIcon(":/img/list-add.png"):QIcon(":/img/list-remove.png") ;
17 icon.paint(painter, option.rect, Qt::AlignVCenter|Qt::AlignHCenter);
18 }
19 else {
20 QStyledItemDelegate::paint(painter, option, index);
21 }
22 painter->restore();
23 }
24
25 QWidget *CertificateTableDelegate::createEditor(QWidget *parent,
26 const QStyleOptionViewItem &option, const QModelIndex &index) const
27 {
28 if (index.column() == 0) {
29 // Draw a combobox in the first column for install/remove certificate
30 // selection.
31 return drawComboBox(parent, option, index);
32 }
33 else {
34 return QStyledItemDelegate::createEditor(parent, option, index);
35 }
36 }
37
38 QWidget *CertificateTableDelegate::drawComboBox(QWidget *parent,
39 const QStyleOptionViewItem &option, const QModelIndex &index) const
40 {
41 // Create a combobox and add two items for install/remove.
42 QComboBox *comboBox = new QComboBox(parent);
43 comboBox->addItem(QIcon(":/img/list-add.png"), QString(""), QVariant("true"));
44 comboBox->addItem(QIcon(":/img/list-remove.png"), QString(""), QVariant("false"));
45 return comboBox;
46 }
47
48 void CertificateTableDelegate::setEditorData(QWidget *editor,
49 const QModelIndex &index) const
50 {
51 if (index.column() != 0) {
52 return;
53 }
54 // Get the current value from the model.
55 QString value = index.data(Qt::DisplayRole).toString();
56
57 qDebug() << "model value is: " + index.data(Qt::DisplayRole).toString();
58 // Find the index in comboxbox items and set the current index.
59 QComboBox *comboBox = static_cast<QComboBox*>(editor);
60 int ndx = comboBox->findData(value, Qt::UserRole);
61 comboBox->setCurrentIndex(ndx);
62 }
63
64 void CertificateTableDelegate::setModelData(QWidget *editor,
65 QAbstractItemModel *model, const QModelIndex &index) const
66 {
67 if (index.column() != 0) {
68 return;
69 }
70 QComboBox *comboBox = static_cast<QComboBox*>(editor);
71 bool value = comboBox->currentData().toBool();
72 model->setData(index, value, Qt::EditRole);
73 }
74
75 void CertificateTableDelegate::updateEditorGeometry(QWidget *editor,
76 const QStyleOptionViewItem &option, const QModelIndex &index) const
77 {
78 if (index.column() != 0) {
79 return;
80 }
81 editor->setGeometry(option.rect);
82 }

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