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