comparison ui/certificateitemwidget.cpp @ 1106:6f7b7d88f048

(issue115) Rework CertificateListWidgets to have a ToolButton instead of a combobox
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 15 Sep 2014 17:43:56 +0200
parents 2b3526ef2d69
children 9bb9932bb819
comparison
equal deleted inserted replaced
1105:0c60ec9c2461 1106:6f7b7d88f048
8 #include "certificateitemwidget.h" 8 #include "certificateitemwidget.h"
9 9
10 #include <QHBoxLayout> 10 #include <QHBoxLayout>
11 #include <QDebug> 11 #include <QDebug>
12 #include <QStyleFactory> 12 #include <QStyleFactory>
13 #include <QToolButton>
13 14
14 CertificateItemWidget::CertificateItemWidget(QWidget *parent, 15 CertificateItemWidget::CertificateItemWidget(QWidget *parent,
15 const Certificate &cert, 16 const Certificate &cert,
16 bool state, 17 bool state,
17 bool editable, 18 QToolButton *btn) :
18 const QString &installLabel,
19 const QString &removeLabel) :
20 QWidget(parent), 19 QWidget(parent),
21 mInstallLabel (installLabel), 20 mButton(btn)
22 mRemoveLabel (removeLabel)
23 { 21 {
24 if (mInstallLabel.isEmpty()) {
25 mInstallLabel = tr("Install");
26 }
27 if (mRemoveLabel.isEmpty()) {
28 mRemoveLabel = tr("Remove");
29 }
30 mCertificate = cert; 22 mCertificate = cert;
31 mState = state; 23 mState = state;
32 mEditable = editable; 24 /* We carry the state explicitly to be better prepared for future
25 * changes */
26 btn->setCheckable(true);
27 btn->setChecked(!state);
33 setupGUI(); 28 setupGUI();
34 } 29 }
35
36
37 /* We use the label as data to hide it in the normal dropdown menu and only
38 * show it when the popup is shown.*/
39 30
40 void CertificateItemWidget::setupGUI() 31 void CertificateItemWidget::setupGUI()
41 { 32 {
42 mLabel = new QLabel; 33 mLabel = new QLabel;
43 mComboBox = new IconOnlyTextPopupBox;
44 QStyle *fusionStyle = QStyleFactory::create("Fusion");
45 if (!fusionStyle) {
46 qDebug() << "Failed to create fusion style";
47 } else {
48 mComboBox->setStyle(fusionStyle);
49 }
50
51 mComboBox->setIconSize(QSize(32, 32));
52 mComboBox->setFixedWidth(64);
53 34
54 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); 35 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
55 36
56 const QString validity = tr("Validity: %1 until %2").arg( 37 const QString validity = tr("Validity: %1 until %2").arg(
57 QLocale::system().toString(mCertificate.validFrom().date(), QLocale::ShortFormat)).arg( 38 QLocale::system().toString(mCertificate.validFrom().date(), QLocale::ShortFormat)).arg(
65 mLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); 46 mLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
66 47
67 mLabel->setTextInteractionFlags( 48 mLabel->setTextInteractionFlags(
68 Qt::TextSelectableByMouse | 49 Qt::TextSelectableByMouse |
69 Qt::TextSelectableByKeyboard); 50 Qt::TextSelectableByKeyboard);
70 mComboBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); 51 mButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
71 connect(mComboBox, SIGNAL(currentIndexChanged(int)), 52 connect(mButton, SIGNAL(toggled (bool)),
72 this, SLOT(currentStateChanged(int))); 53 this, SLOT(currentStateChanged(bool)));
73 54
74 QHBoxLayout *layout = new QHBoxLayout; 55 QHBoxLayout *layout = new QHBoxLayout;
56 layout->addWidget(mButton);
57 mButton->setFixedSize(64, 64);
58 mButton->setIconSize(QSize(48, 48));
59 /*
75 if (mCertificate.isInstallCert()) { 60 if (mCertificate.isInstallCert()) {
76 mComboBox->addItem(QIcon(":/img/security-high.png"), QString(), mInstallLabel); 61 mComboBox->addItem(QIcon(":/img/security-high.png"), QString(), mInstallLabel);
77 mComboBox->addItem(QIcon(":/img/security-low.png"), QString(), mRemoveLabel); 62 mComboBox->addItem(QIcon(":/img/security-low.png"), QString(), mRemoveLabel);
78 if (mState) { 63 if (mState) {
79 mComboBox->setCurrentIndex(0); 64 mComboBox->setCurrentIndex(0);
80 mComboBox->setToolTip(tr("This certificate is currently installed.")); 65 mComboBox->setToolTip(tr("This certificate is currently installed."));
81 } 66 }
82 else { 67 else {
83 mComboBox->setCurrentIndex(1); 68 mComboBox->setCurrentIndex(1);
84 mComboBox->setToolTip(tr("This certificate is currently not installed.")); 69 mComboBox->setToolTip(tr("This certificate is currently not installed."));
85 } 70 }
86 layout->addWidget(mComboBox); 71 layout->addWidget(mComboBox);
103 else { 88 else {
104 mComboBox->setCurrentIndex(1); 89 mComboBox->setCurrentIndex(1);
105 } 90 }
106 layout->addWidget(mComboBox); 91 layout->addWidget(mComboBox);
107 } 92 }
93 */
108 layout->addWidget(mLabel); 94 layout->addWidget(mLabel);
109 this->setLayout(layout); 95 this->setLayout(layout);
110 } 96 }
111 97
112 bool CertificateItemWidget::state() 98 bool CertificateItemWidget::state()
113 { 99 {
114 if (!mEditable) { 100 if (!mButton->isEnabled()) {
115 return true; 101 return true;
116 } 102 }
117 103
104 /*
118 const QString currentString = mComboBox->currentData().toString(); 105 const QString currentString = mComboBox->currentData().toString();
119 106
120 if (!mCertificate.isInstallCert()) { 107 if (!mCertificate.isInstallCert()) {
121 return currentString == tr("uninstall"); 108 return currentString == tr("uninstall");
122 } 109 }
123 110
124 return currentString == mInstallLabel; 111 return currentString == mInstallLabel;
112 */
113 return mState;
125 } 114 }
126 115
127 void CertificateItemWidget::setState(bool state) 116 void CertificateItemWidget::setState(bool state)
128 { 117 {
129 disconnect(mComboBox, SIGNAL(currentIndexChanged(int)), 118 mState = state;
130 this, SLOT(currentStateChanged(int))); 119 mButton->setChecked(!state);
131
132 if (state) {
133 mComboBox->setCurrentIndex(0);
134 }
135 else {
136 mComboBox->setCurrentIndex(1);
137 }
138 connect(mComboBox, SIGNAL(currentIndexChanged(int)),
139 this, SLOT(currentStateChanged(int)));
140 } 120 }
141 121
142 Certificate* CertificateItemWidget::certificate() 122 Certificate* CertificateItemWidget::certificate()
143 { 123 {
144 return &mCertificate; 124 return &mCertificate;
145 } 125 }
146 126
147 void CertificateItemWidget::currentStateChanged(int) 127 void CertificateItemWidget::currentStateChanged(bool state)
148 { 128 {
149 bool state = !mComboBox->currentIndex(); 129 mState = !state;
150 emit stateChanged(state, mCertificate); 130 emit stateChanged(mState, mCertificate);
151 } 131 }
152
153 void IconOnlyTextPopupBox::showPopup() {
154 for (int i = 0; i < count(); i++) {
155 setItemText(i, itemData(i).toString());
156 }
157 QComboBox::showPopup();
158 }
159
160 void IconOnlyTextPopupBox::hidePopup() {
161 for (int i = 0; i < count(); i++) {
162 setItemText(i, QString());
163 }
164 QComboBox::hidePopup();
165 }

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