Commit ca707754 authored by Andre Heinecke's avatar Andre Heinecke

Replace certificatecombobox with KeySelectionCombo

This moves back the certificatecombo code into the
certificateselectionline and removes it as an extra class.
parent 80988b90
......@@ -9,7 +9,7 @@ option(DISABLE_KWATCHGNUPG "Don't build the kwatchgnupg tool [default=OFF]" OFF)
set(KF5_VERSION "5.17.0")
set(GPGMEPP_VERSION "5.2.81")
set(KMIME_VERSION "5.1.40")
set(LIBKLEO_VERSION "5.3.41")
set(LIBKLEO_VERSION "5.3.42")
set(QT_REQUIRED_VERSION "5.4.0")
find_package(ECM ${KF5_VERSION} REQUIRED NO_MODULE)
......
......@@ -136,7 +136,6 @@ set(_kleopatra_SRCS
crypto/gui/wizardpage.cpp
crypto/gui/certificateselectionline.cpp
crypto/gui/certificatecombobox.cpp
crypto/gui/certificatelineedit.cpp
crypto/gui/signingcertificateselectionwidget.cpp
crypto/gui/signingcertificateselectiondialog.cpp
......
/* 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 <Libkleo/KeyListModelInterface>
#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>();
}
/* 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
......@@ -35,9 +35,11 @@
#include <QLabel>
#include <QStackedWidget>
#include <QComboBox>
#include <QStylePainter>
#include <QStyleOptionComboBox>
#include <QStyle>
#include "utils/kleo_assert.h"
#include "certificatecombobox.h"
#include <KLocalizedString>
......@@ -61,18 +63,104 @@ 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::MyCertificateComboBox : public CertificateComboBox
class Kleo::KeysComboBox : public ComboBox
{
Q_OBJECT
public:
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)
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)
{
setKeys(keys);
}
......@@ -123,9 +211,9 @@ CertificateSelectionLine::CertificateSelectionLine(const QString &toFrom, const
mToFromLB(new QLabel(toFrom, q)),
mMailboxLB(new QLabel(mailbox, q)),
mSbox(new QStackedWidget(q)),
mPgpCB(new MyCertificateComboBox(pgp, mSbox)),
mCmsCB(new MyCertificateComboBox(cms, mSbox)),
noProtocolCB(new MyCertificateComboBox(i18n("(please choose between OpenPGP and S/MIME first)"), mSbox)),
mPgpCB(new KeysComboBox(pgp, mSbox)),
mCmsCB(new KeysComboBox(cms, mSbox)),
noProtocolCB(new KeysComboBox(i18n("(please choose between OpenPGP and S/MIME first)"), mSbox)),
mToolTB(new QToolButton(q))
{
QFont bold;
......@@ -174,7 +262,7 @@ QString CertificateSelectionLine::mailboxText() const
void CertificateSelectionLine::addAndSelectCertificate(const Key &key) const
{
if (MyCertificateComboBox *const cb = comboBox(key.protocol())) {
if (KeysComboBox *const cb = comboBox(key.protocol())) {
cb->addAndSelectCertificate(key);
cb->setEnabled(true);
}
......@@ -214,14 +302,14 @@ bool CertificateSelectionLine::wasInitiallyAmbiguous(Protocol proto) const
bool CertificateSelectionLine::isStillAmbiguous(Protocol proto) const
{
kleo_assert(proto == OpenPGP || proto == CMS);
const MyCertificateComboBox *const cb = comboBox(proto);
const KeysComboBox *const cb = comboBox(proto);
return cb->currentIndex() == -1;
}
Key CertificateSelectionLine::key(Protocol proto) const
{
kleo_assert(proto == OpenPGP || proto == CMS);
const MyCertificateComboBox *const cb = comboBox(proto);
const KeysComboBox *const cb = comboBox(proto);
return cb->currentKey();
}
......@@ -238,7 +326,7 @@ void CertificateSelectionLine::kill()
delete mToolTB;
}
MyCertificateComboBox *CertificateSelectionLine::comboBox(Protocol proto) const
KeysComboBox *CertificateSelectionLine::comboBox(Protocol proto) const
{
if (proto == OpenPGP) {
return mPgpCB;
......
......@@ -46,7 +46,7 @@ class QStackedWidget;
namespace Kleo
{
class MyCertificateComboBox;
class KeysComboBox;
class CertificateSelectionLine
{
......@@ -92,7 +92,7 @@ public:
void kill();
MyCertificateComboBox *comboBox(GpgME::Protocol proto) const;
KeysComboBox *comboBox(GpgME::Protocol proto) const;
private:
bool pgpAmbiguous : 1;
......@@ -101,7 +101,7 @@ private:
QLabel *mToFromLB;
QLabel *mMailboxLB;
QStackedWidget *mSbox;
MyCertificateComboBox *mPgpCB,
KeysComboBox *mPgpCB,
*mCmsCB,
*noProtocolCB;
QToolButton *mToolTB;
......
......@@ -33,7 +33,6 @@
#include "kleopatra_debug.h"
#include "certificatecombobox.h"
#include "certificatelineedit.h"
#include <QVBoxLayout>
......@@ -44,6 +43,7 @@
#include <Libkleo/DefaultKeyFilter>
#include <Libkleo/KeyListModel>
#include <Libkleo/KeySelectionCombo>
#include <Libkleo/KeyListSortFilterProxyModel>
#include <KLocalizedString>
......@@ -105,14 +105,8 @@ SignEncryptWidget::SignEncryptWidget(QWidget *parent)
QCheckBox *sigChk = new QCheckBox(QStringLiteral("Sign as:"));
sigChk->setChecked(true);
KeyListSortFilterProxyModel *sigModel = new KeyListSortFilterProxyModel(this);
sigModel->setKeyFilter(boost::shared_ptr<KeyFilter>(new SignCertificateFilter()));
sigModel->setSourceModel(mModel);
mModel->setParent(this);
mSigSelect = new CertificateComboBox(i18n("No valid secret keys found."));
mSigSelect->setModel(sigModel);
mSigSelect->setModelColumn(KeyListModelInterface::Summary);
mSigSelect = new KeySelectionCombo();
mSigSelect->setKeyFilter(boost::shared_ptr<KeyFilter>(new SignCertificateFilter()));
sigLay->addWidget(sigChk);
sigLay->addStretch(1);
......@@ -122,7 +116,7 @@ SignEncryptWidget::SignEncryptWidget(QWidget *parent)
connect(sigChk, &QCheckBox::toggled, mSigSelect, &QWidget::setEnabled);
connect(sigChk, &QCheckBox::toggled, this, &SignEncryptWidget::updateOp);
connect(mSigSelect, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
connect(mSigSelect, &KeySelectionCombo::currentKeyChanged,
this, &SignEncryptWidget::updateOp);
/* Recipient selection */
......@@ -148,14 +142,7 @@ SignEncryptWidget::SignEncryptWidget(QWidget *parent)
QCheckBox *encSelfChk = new QCheckBox(QStringLiteral("Own certificate:"));
encSelfChk->setChecked(true);
KeyListSortFilterProxyModel *encModel = new KeyListSortFilterProxyModel(this);
encModel->setKeyFilter(boost::shared_ptr<KeyFilter>(new EncryptSelfCertificateFilter()));
encModel->setSourceModel(mModel);
mModel->setParent(this);
mSelfSelect = new CertificateComboBox(i18n("No valid secret keys found."));
mSelfSelect->setModel(encModel);
mSelfSelect->setModelColumn(KeyListModelInterface::Summary);
mSelfSelect = new KeySelectionCombo();
encSelfLay->addWidget(encSelfChk);
encSelfLay->addWidget(mSelfSelect);
......@@ -163,7 +150,7 @@ SignEncryptWidget::SignEncryptWidget(QWidget *parent)
connect(encSelfChk, &QCheckBox::toggled, mSelfSelect, &QWidget::setEnabled);
connect(encSelfChk, &QCheckBox::toggled, this, &SignEncryptWidget::updateOp);
connect(mSelfSelect, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
connect(mSelfSelect, &KeySelectionCombo::currentKeyChanged,
this, &SignEncryptWidget::updateOp);
lay->addWidget(encBox);
......@@ -217,7 +204,7 @@ void SignEncryptWidget::recipientsChanged()
Key SignEncryptWidget::signKey() const
{
if (mSigSelect->isEnabled()) {
return mSigSelect->key();
return mSigSelect->currentKey();
}
return Key();
}
......@@ -225,7 +212,7 @@ Key SignEncryptWidget::signKey() const
Key SignEncryptWidget::selfKey() const
{
if (mSelfSelect->isEnabled()) {
return mSelfSelect->key();
return mSelfSelect->currentKey();
}
return Key();
}
......
......@@ -41,7 +41,7 @@ class QGridLayout;
namespace Kleo
{
class CertificateLineEdit;
class CertificateComboBox;
class KeySelectionCombo;
class AbstractKeyListModel;
class SignEncryptWidget: public QWidget
......@@ -83,8 +83,8 @@ Q_SIGNALS:
void keysChanged();
private:
CertificateComboBox *mSigSelect,
*mSelfSelect;
KeySelectionCombo *mSigSelect,
*mSelfSelect;
QVector<CertificateLineEdit *> mRecpWidgets;
QGridLayout *mRecpLayout;
QString mOp;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment