summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Heinecke <aheinecke@intevation.de>2016-04-28 14:13:24 (GMT)
committerAndre Heinecke <aheinecke@intevation.de>2016-08-02 09:50:25 (GMT)
commiteeb042f1febebdefe4836f4e88a2ae02659780c6 (patch)
tree070ed3983498611e23f1fb2de0e339ae29109442
parentcdead29b6bfaa8bd6c5a938073e84bd324854fcb (diff)
Factor out combobox into certificatecombobox
For now this is mostly needed to have a kind of shadow text when nothing is selected. It also does not offer the dropdown and becomes flat if there is only one certificate for an email.
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/crypto/gui/certificatecombobox.cpp116
-rw-r--r--src/crypto/gui/certificatecombobox.h74
-rw-r--r--src/crypto/gui/certificateselectionline.cpp121
-rw-r--r--src/crypto/gui/certificateselectionline.h6
5 files changed, 210 insertions, 108 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 1ad70e0..7a7578d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -145,6 +145,7 @@ set(_kleopatra_SRCS
crypto/gui/wizardpage.cpp
crypto/gui/certificateselectionline.cpp
+ crypto/gui/certificatecombobox.cpp
crypto/gui/signingcertificateselectionwidget.cpp
crypto/gui/signingcertificateselectiondialog.cpp
diff --git a/src/crypto/gui/certificatecombobox.cpp b/src/crypto/gui/certificatecombobox.cpp
new file mode 100644
index 0000000..a9fac6e
--- /dev/null
+++ b/src/crypto/gui/certificatecombobox.cpp
@@ -0,0 +1,116 @@
+/* crypto/gui/certificatecombobox.cpp
+
+ This file is part of Kleopatra, the KDE keymanager
+ Copyright (c) 2016 Intevation GmbH
+
+ Kleopatra is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Kleopatra is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+ In addition, as a special exception, the copyright holders give
+ permission to link the code of this program with any edition of
+ the Qt library by Trolltech AS, Norway (or with modified versions
+ of Qt that use the same license as Qt), and distribute linked
+ combinations including the two. You must obey the GNU General
+ Public License in all respects for all of the code used other than
+ Qt. If you modify this file, you may extend this exception to
+ your version of the file, but you are not obligated to do so. If
+ you do not wish to do so, delete this exception statement from
+ your version.
+*/
+
+#include "certificatecombobox.h"
+#include "models/keylistmodelinterface.h"
+
+#include <QStylePainter>
+#include <QStyleOptionComboBox>
+#include <QStyle>
+
+#include <gpgme++/key.h>
+using namespace Kleo;
+
+Q_DECLARE_METATYPE(GpgME::Key);
+
+CertificateComboBox::CertificateComboBox(const QIcon &initialIcon,
+ const QString &initialText,
+ QWidget *parent)
+ : QComboBox(parent),
+ m_initialText(initialText),
+ m_initialIcon(initialIcon)
+{
+}
+
+QString CertificateComboBox::initialText() const
+{
+ return m_initialText;
+}
+
+QIcon CertificateComboBox::initialIcon() const
+{
+ return m_initialIcon;
+}
+
+void CertificateComboBox::setInitialText(const QString &txt)
+{
+ if (txt == m_initialText) {
+ return;
+ }
+ m_initialText = txt;
+ if (currentIndex() == -1) {
+ update();
+ }
+}
+
+void CertificateComboBox::setInitialIcon(const QIcon &icon)
+{
+ if (icon.cacheKey() == m_initialIcon.cacheKey()) {
+ return;
+ }
+ m_initialIcon = icon;
+ if (currentIndex() == -1) {
+ update();
+ }
+}
+
+void CertificateComboBox::paintEvent(QPaintEvent *)
+{
+ QStylePainter p(this);
+ p.setPen(palette().color(QPalette::Text));
+ QStyleOptionComboBox opt;
+ initStyleOption(&opt);
+ if (currentIndex() == -1)
+ {
+ opt.currentText = m_initialText;
+ opt.currentIcon = m_initialIcon;
+ }
+ if (count() > 1) {
+ p.drawComplexControl(QStyle::CC_ComboBox, opt);
+ p.drawControl(QStyle::CE_ComboBoxLabel, opt);
+ } else {
+ style()->drawPrimitive (QStyle::PE_Frame, &opt, &p, this);
+ style()->drawItemText (&p, rect(), Qt::AlignLeft, palette(), isEnabled(), opt.currentText);
+ }
+}
+
+void CertificateComboBox::showPopup()
+{
+ if (count() <= 1) {
+ return;
+ }
+ QComboBox::showPopup();
+}
+
+GpgME::Key CertificateComboBox::key() const
+{
+ return currentData(KeyListModelInterface::KeyRole).value<GpgME::Key>();
+}
diff --git a/src/crypto/gui/certificatecombobox.h b/src/crypto/gui/certificatecombobox.h
new file mode 100644
index 0000000..9989c50
--- /dev/null
+++ b/src/crypto/gui/certificatecombobox.h
@@ -0,0 +1,74 @@
+/* crypto/gui/certificatecombobox.h
+
+ This file is part of Kleopatra, the KDE keymanager
+ Copyright (c) 2016 Intevation GmbH
+
+ Kleopatra is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Kleopatra is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+ In addition, as a special exception, the copyright holders give
+ permission to link the code of this program with any edition of
+ the Qt library by Trolltech AS, Norway (or with modified versions
+ of Qt that use the same license as Qt), and distribute linked
+ combinations including the two. You must obey the GNU General
+ Public License in all respects for all of the code used other than
+ Qt. If you modify this file, you may extend this exception to
+ your version of the file, but you are not obligated to do so. If
+ you do not wish to do so, delete this exception statement from
+ your version.
+*/
+#ifndef CRYPTO_GUI_CERTIFICATECOMBOBOX_H
+#define CRYPTO_GUI_CERTIFICATECOMBOBOX_H
+
+#include <QComboBox>
+#include <QIcon>
+#include <QString>
+
+namespace GpgME
+{
+ class Key;
+}
+
+namespace Kleo {
+/** A QComboBox with an initial text for certificates.
+ *
+ * Becomes flat and without arrow if there is only one element.
+ */
+class CertificateComboBox : public QComboBox
+{
+ Q_OBJECT
+public:
+ explicit CertificateComboBox(const QIcon &initialIcon, const QString &initialText, QWidget *parent = Q_NULLPTR);
+ explicit CertificateComboBox(QWidget *parent = Q_NULLPTR) :
+ CertificateComboBox(QIcon(), QString(), parent) {}
+ explicit CertificateComboBox(const QString &initialText, QWidget *parent = Q_NULLPTR) :
+ CertificateComboBox(QIcon(), initialText, parent) {}
+
+ QString initialText() const;
+ QIcon initialIcon() const;
+
+ GpgME::Key key() const;
+
+public Q_SLOTS:
+ void setInitialText(const QString &txt);
+ void setInitialIcon(const QIcon &icon);
+ void showPopup() Q_DECL_OVERRIDE;
+protected:
+ void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
+private:
+ QString m_initialText;
+ QIcon m_initialIcon;
+};
+} // namespace kleo
+#endif // CRYPTO_GUI_CERTIFICATECOMBOBOX_H
diff --git a/src/crypto/gui/certificateselectionline.cpp b/src/crypto/gui/certificateselectionline.cpp
index 9f697eb..d834793 100644
--- a/src/crypto/gui/certificateselectionline.cpp
+++ b/src/crypto/gui/certificateselectionline.cpp
@@ -35,18 +35,15 @@
#include <QLabel>
#include <QStackedWidget>
#include <QComboBox>
-#include <QStylePainter>
-#include <QStyleOptionComboBox>
-#include <QStyle>
#include "utils/formatting.h"
#include "utils/kleo_assert.h"
+#include "certificatecombobox.h"
#include <KLocalizedString>
#include <Libkleo/Predicates>
-
using namespace Kleo;
using namespace GpgME;
@@ -64,104 +61,18 @@ static QString make_initial_text(const std::vector<Key> &keys)
}
}
-// A QComboBox with an initial text (as known from web browsers)
-//
-// only works with read-only QComboBoxen, doesn't affect sizeHint
-// as it should...
-//
-class ComboBox : public QComboBox
-{
- Q_OBJECT
- Q_PROPERTY(QString initialText READ initialText WRITE setInitialText)
- Q_PROPERTY(QIcon initialIcon READ initialIcon WRITE setInitialIcon)
-public:
- explicit ComboBox(QWidget *parent = Q_NULLPTR)
- : QComboBox(parent),
- m_initialText(),
- m_initialIcon()
- {
-
- }
-
- explicit ComboBox(const QString &initialText, QWidget *parent = Q_NULLPTR)
- : QComboBox(parent),
- m_initialText(initialText),
- m_initialIcon()
- {
-
- }
-
- explicit ComboBox(const QIcon &initialIcon, const QString &initialText, QWidget *parent = Q_NULLPTR)
- : QComboBox(parent),
- m_initialText(initialText),
- m_initialIcon(initialIcon)
- {
-
- }
-
- QString initialText() const
- {
- return m_initialText;
- }
- QIcon initialIcon() const
- {
- return m_initialIcon;
- }
-
-public Q_SLOTS:
- void setInitialText(const QString &txt)
- {
- if (txt == m_initialText) {
- return;
- }
- m_initialText = txt;
- if (currentIndex() == -1) {
- update();
- }
- }
- void setInitialIcon(const QIcon &icon)
- {
- if (icon.cacheKey() == m_initialIcon.cacheKey()) {
- return;
- }
- m_initialIcon = icon;
- if (currentIndex() == -1) {
- update();
- }
- }
-
-protected:
- void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE {
- QStylePainter p(this);
- p.setPen(palette().color(QPalette::Text));
- QStyleOptionComboBox opt;
- initStyleOption(&opt);
- p.drawComplexControl(QStyle::CC_ComboBox, opt);
-
- if (currentIndex() == -1)
- {
- opt.currentText = m_initialText;
- opt.currentIcon = m_initialIcon;
- }
- p.drawControl(QStyle::CE_ComboBoxLabel, opt);
- }
-
-private:
- QString m_initialText;
- QIcon m_initialIcon;
-};
-} // anonymous namespace
+}
-class Kleo::KeysComboBox : public ComboBox
+class Kleo::MyCertificateComboBox : public CertificateComboBox
{
Q_OBJECT
public:
- explicit KeysComboBox(QWidget *parent = Q_NULLPTR)
- : ComboBox(parent) {}
- explicit KeysComboBox(const QString &initialText, QWidget *parent = Q_NULLPTR)
- : ComboBox(initialText, parent) {}
- explicit KeysComboBox(const std::vector<Key> &keys, QWidget *parent = Q_NULLPTR)
- : ComboBox(make_initial_text(keys), parent)
+ explicit MyCertificateComboBox(QWidget *parent = Q_NULLPTR)
+ : CertificateComboBox(parent) {}
+ explicit MyCertificateComboBox(const QString &initialText, QWidget *parent = Q_NULLPTR)
+ : CertificateComboBox(initialText, parent) {}
+ explicit MyCertificateComboBox(const std::vector<Key> &keys, QWidget *parent = Q_NULLPTR)
+ : CertificateComboBox(make_initial_text(keys), parent)
{
setKeys(keys);
}
@@ -212,9 +123,9 @@ CertificateSelectionLine::CertificateSelectionLine(const QString &toFrom, const
mToFromLB(new QLabel(toFrom, q)),
mMailboxLB(new QLabel(mailbox, q)),
mSbox(new QStackedWidget(q)),
- mPgpCB(new KeysComboBox(pgp, mSbox)),
- mCmsCB(new KeysComboBox(cms, mSbox)),
- noProtocolCB(new KeysComboBox(i18n("(please choose between OpenPGP and S/MIME first)"), mSbox)),
+ mPgpCB(new MyCertificateComboBox(pgp, mSbox)),
+ mCmsCB(new MyCertificateComboBox(cms, mSbox)),
+ noProtocolCB(new MyCertificateComboBox(i18n("(please choose between OpenPGP and S/MIME first)"), mSbox)),
mToolTB(new QToolButton(q))
{
QFont bold;
@@ -263,7 +174,7 @@ QString CertificateSelectionLine::mailboxText() const
void CertificateSelectionLine::addAndSelectCertificate(const Key &key) const
{
- if (KeysComboBox *const cb = comboBox(key.protocol())) {
+ if (MyCertificateComboBox *const cb = comboBox(key.protocol())) {
cb->addAndSelectCertificate(key);
cb->setEnabled(true);
}
@@ -303,14 +214,14 @@ bool CertificateSelectionLine::wasInitiallyAmbiguous(Protocol proto) const
bool CertificateSelectionLine::isStillAmbiguous(Protocol proto) const
{
kleo_assert(proto == OpenPGP || proto == CMS);
- const KeysComboBox *const cb = comboBox(proto);
+ const MyCertificateComboBox *const cb = comboBox(proto);
return cb->currentIndex() == -1;
}
Key CertificateSelectionLine::key(Protocol proto) const
{
kleo_assert(proto == OpenPGP || proto == CMS);
- const KeysComboBox *const cb = comboBox(proto);
+ const MyCertificateComboBox *const cb = comboBox(proto);
return cb->currentKey();
}
@@ -327,7 +238,7 @@ void CertificateSelectionLine::kill()
delete mToolTB;
}
-KeysComboBox *CertificateSelectionLine::comboBox(Protocol proto) const
+MyCertificateComboBox *CertificateSelectionLine::comboBox(Protocol proto) const
{
if (proto == OpenPGP) {
return mPgpCB;
diff --git a/src/crypto/gui/certificateselectionline.h b/src/crypto/gui/certificateselectionline.h
index fdf0628..6c877f6 100644
--- a/src/crypto/gui/certificateselectionline.h
+++ b/src/crypto/gui/certificateselectionline.h
@@ -46,7 +46,7 @@ class QStackedWidget;
namespace Kleo
{
-class KeysComboBox;
+class MyCertificateComboBox;
class CertificateSelectionLine
{
@@ -92,7 +92,7 @@ public:
void kill();
- KeysComboBox *comboBox(GpgME::Protocol proto) const;
+ MyCertificateComboBox *comboBox(GpgME::Protocol proto) const;
private:
bool pgpAmbiguous : 1;
@@ -101,7 +101,7 @@ private:
QLabel *mToFromLB;
QLabel *mMailboxLB;
QStackedWidget *mSbox;
- KeysComboBox *mPgpCB,
+ MyCertificateComboBox *mPgpCB,
*mCmsCB,
*noProtocolCB;
QToolButton *mToolTB;