Commit f7241ec6 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Add a helper class for managing a QWidget with associated QLabel

The helper makes the widget the label's buddy and it simplifies enabling
and disabling both widgets.

GnuPG-bug-id: 5791
parent f7e4efff
......@@ -39,6 +39,8 @@ set(kcm_kleopatra_PART_SRCS
appearanceconfigpage.h
appearanceconfigwidget.h
gnupgsystemconfigurationpage.h
labelledwidget.cpp
labelledwidget.cpp
${kleopatra_BINARY_DIR}/src/kleopatra_debug.cpp
${_kcm_kleopatra_libkleopatraclient_extra_SRCS}
)
......
......@@ -11,6 +11,8 @@
#include "dirservconfigpage.h"
#include "labelledwidget.h"
#include <settings.h>
#include <Libkleo/Compat>
......@@ -152,13 +154,13 @@ DirectoryServicesConfigurationPage::DirectoryServicesConfigurationPage(QWidget *
auto l = new QHBoxLayout{};
l->setContentsMargins(0, 0, 0, 0);
l->addWidget(new QLabel{i18n("OpenPGP keyserver:"), this});
mOpenPGPKeyserverEdit = new QLineEdit{this};
l->addWidget(mOpenPGPKeyserverEdit);
mOpenPGPKeyserverEdit.createWidgets(this);
mOpenPGPKeyserverEdit.label()->setText(i18n("OpenPGP keyserver:"));
l->addWidget(mOpenPGPKeyserverEdit.label());
l->addWidget(mOpenPGPKeyserverEdit.widget());
glay->addLayout(l, row, 0, 1, 3);
connect(mOpenPGPKeyserverEdit, &QLineEdit::textEdited, this, [this]() { Q_EMIT changed(true); });
connect(mOpenPGPKeyserverEdit.widget(), &QLineEdit::textEdited, this, [this]() { Q_EMIT changed(true); });
}
// X.509 servers
......@@ -186,23 +188,21 @@ DirectoryServicesConfigurationPage::DirectoryServicesConfigurationPage(QWidget *
// LDAP timeout
++row;
auto label = new QLabel(i18n("LDAP &timeout (minutes:seconds):"), this);
mTimeout = new QTimeEdit(this);
mTimeout->setDisplayFormat(QStringLiteral("mm:ss"));
connect(mTimeout, SIGNAL(timeChanged(QTime)), this, SLOT(changed()));
label->setBuddy(mTimeout);
glay->addWidget(label, row, 0);
glay->addWidget(mTimeout, row, 1);
mTimeout.createWidgets(this);
mTimeout.label()->setText(i18n("LDAP &timeout (minutes:seconds):"));
mTimeout.widget()->setDisplayFormat(QStringLiteral("mm:ss"));
connect(mTimeout.widget(), SIGNAL(timeChanged(QTime)), this, SLOT(changed()));
glay->addWidget(mTimeout.label(), row, 0);
glay->addWidget(mTimeout.widget(), row, 1);
// Max number of items returned by queries
++row;
mMaxItemsLabel = new QLabel(i18n("&Maximum number of items returned by query:"), this);
mMaxItems = new QSpinBox(this);
mMaxItems->setMinimum(0);
mMaxItemsLabel->setBuddy(mMaxItems);
connect(mMaxItems, SIGNAL(valueChanged(int)), this, SLOT(changed()));
glay->addWidget(mMaxItemsLabel, row, 0);
glay->addWidget(mMaxItems, row, 1);
mMaxItems.createWidgets(this);
mMaxItems.label()->setText(i18n("&Maximum number of items returned by query:"));
mMaxItems.widget()->setMinimum(0);
connect(mMaxItems.widget(), SIGNAL(valueChanged(int)), this, SLOT(changed()));
glay->addWidget(mMaxItems.label(), row, 0);
glay->addWidget(mMaxItems.widget(), row, 1);
#ifdef NOT_USEFUL_CURRENTLY
++row
......@@ -263,18 +263,18 @@ void DirectoryServicesConfigurationPage::load()
<< s_pgpservice_componentName << "/" << s_pgpservice_entryName;
}
mOpenPGPKeyserverEdit->setText(mOpenPGPServiceEntry && mOpenPGPServiceEntry->isSet() ? mOpenPGPServiceEntry->stringValue() : QString());
mOpenPGPKeyserverEdit->setEnabled(mOpenPGPServiceEntry && !mOpenPGPServiceEntry->isReadOnly());
mOpenPGPKeyserverEdit.widget()->setText(mOpenPGPServiceEntry && mOpenPGPServiceEntry->isSet() ? mOpenPGPServiceEntry->stringValue() : QString());
mOpenPGPKeyserverEdit.setEnabled(mOpenPGPServiceEntry && !mOpenPGPServiceEntry->isReadOnly());
#ifdef QGPGME_CRYPTOCONFIGENTRY_HAS_DEFAULT_VALUE
if (newEntry && !newEntry->defaultValue().isNull()) {
mOpenPGPKeyserverEdit->setPlaceholderText(newEntry->defaultValue().toString());
mOpenPGPKeyserverEdit.widget()->setPlaceholderText(newEntry->defaultValue().toString());
} else
#endif
{
if (GpgME::engineInfo(GpgME::GpgEngine).engineVersion() < "2.1.16") {
mOpenPGPKeyserverEdit->setPlaceholderText(QStringLiteral("hkp://keys.gnupg.net"));
mOpenPGPKeyserverEdit.widget()->setPlaceholderText(QStringLiteral("hkp://keys.gnupg.net"));
} else {
mOpenPGPKeyserverEdit->setPlaceholderText(QStringLiteral("hkps://hkps.pool.sks-keyservers.net"));
mOpenPGPKeyserverEdit.widget()->setPlaceholderText(QStringLiteral("hkps://hkps.pool.sks-keyservers.net"));
}
}
}
......@@ -292,9 +292,9 @@ void DirectoryServicesConfigurationPage::load()
static_cast<int>(mTimeoutConfigEntry->uintValue());
const QTime time = QTime(0, 0, 0, 0).addSecs(ldapTimeout);
//qCDebug(KLEOPATRA_LOG) <<"timeout:" << mTimeoutConfigEntry->uintValue() <<" ->" << time;
mTimeout->setTime(time);
mTimeout.widget()->setTime(time);
}
mTimeout->setEnabled(mTimeoutConfigEntry && !mTimeoutConfigEntry->isReadOnly());
mTimeout.setEnabled(mTimeoutConfigEntry && !mTimeoutConfigEntry->isReadOnly());
// read max-replies config entry
// first try to read the config entry as int (GnuPG 2.3)
......@@ -307,13 +307,11 @@ void DirectoryServicesConfigurationPage::load()
const int value = mMaxItemsConfigEntry->argType() == CryptoConfigEntry::ArgType_Int ?
mMaxItemsConfigEntry->intValue() :
static_cast<int>(mMaxItemsConfigEntry->uintValue());
mMaxItems->blockSignals(true); // KNumInput emits valueChanged from setValue!
mMaxItems->setValue(value);
mMaxItems->blockSignals(false);
mMaxItems.widget()->blockSignals(true); // KNumInput emits valueChanged from setValue!
mMaxItems.widget()->setValue(value);
mMaxItems.widget()->blockSignals(false);
}
const bool maxItemsEnabled = mMaxItemsConfigEntry && !mMaxItemsConfigEntry->isReadOnly();
mMaxItems->setEnabled(maxItemsEnabled);
mMaxItemsLabel->setEnabled(maxItemsEnabled);
mMaxItems.setEnabled(mMaxItemsConfigEntry && !mMaxItemsConfigEntry->isReadOnly());
#ifdef NOT_USEFUL_CURRENTLY
mAddNewServersConfigEntry = configEntry(s_addnewservers_componentName, s_addnewservers_groupName, s_addnewservers_entryName, CryptoConfigEntry::ArgType_None, SingleValue, DoShowError);
......@@ -353,7 +351,7 @@ void DirectoryServicesConfigurationPage::save()
}
if (mOpenPGPServiceEntry) {
const auto keyserver = mOpenPGPKeyserverEdit->text().trimmed();
const auto keyserver = mOpenPGPKeyserverEdit.widget()->text().trimmed();
if (keyserver.isEmpty()) {
mOpenPGPServiceEntry->resetToDefault();
} else {
......@@ -362,10 +360,10 @@ void DirectoryServicesConfigurationPage::save()
}
}
const QTime time{mTimeout->time()};
const QTime time{mTimeout.widget()->time()};
updateIntegerConfigEntry(mTimeoutConfigEntry, time.minute() * 60 + time.second());
updateIntegerConfigEntry(mMaxItemsConfigEntry, mMaxItems->value());
updateIntegerConfigEntry(mMaxItemsConfigEntry, mMaxItems.widget()->value());
#ifdef NOT_USEFUL_CURRENTLY
if (mAddNewServersConfigEntry && mAddNewServersConfigEntry->boolValue() != mAddNewServersCB->isChecked()) {
......
......@@ -9,6 +9,8 @@
#pragma once
#include <labelledwidget.h>
#include <KCModule>
#include <QGpgME/CryptoConfig>
......@@ -18,6 +20,7 @@ class QLabel;
class QLineEdit;
class QTimeEdit;
class QSpinBox;
namespace Kleo
{
class DirectoryServicesWidget;
......@@ -55,11 +58,10 @@ private:
ShowError showError);
private:
QLineEdit *mOpenPGPKeyserverEdit = nullptr;
Kleo::LabelledWidget<QLineEdit> mOpenPGPKeyserverEdit;
Kleo::DirectoryServicesWidget *mDirectoryServices = nullptr;
QTimeEdit *mTimeout = nullptr;
QSpinBox *mMaxItems = nullptr;
QLabel *mMaxItemsLabel = nullptr;
Kleo::LabelledWidget<QTimeEdit> mTimeout;
Kleo::LabelledWidget<QSpinBox> mMaxItems;
QCheckBox *mAddNewServersCB = nullptr;
QGpgME::CryptoConfigEntry *mX509ServicesEntry = nullptr;
......
/* conf/labelledwidget.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "labelledwidget.h"
namespace Kleo::_detail
{
QWidget *LabelledWidgetBase::widget() const
{
return mWidget;
}
QLabel *LabelledWidgetBase::label() const
{
return mLabel;
}
void LabelledWidgetBase::setWidgets(QWidget *widget, QLabel *label)
{
mWidget = widget;
mLabel = label;
if (mLabel) {
mLabel->setBuddy(mWidget);
}
}
void LabelledWidgetBase::setEnabled(bool enabled)
{
if (mLabel) {
mLabel->setEnabled(enabled);
}
if (mWidget) {
mWidget->setEnabled(enabled);
}
}
}
/* conf/labelledwidget.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QLabel>
#include <QPointer>
namespace Kleo
{
namespace _detail
{
class LabelledWidgetBase
{
protected:
LabelledWidgetBase() = default;
QWidget *widget() const;
public:
QLabel *label() const;
void setWidgets(QWidget *widget, QLabel *label);
void setEnabled(bool enabled);
private:
QPointer<QLabel> mLabel;
QPointer<QWidget> mWidget;
};
}
/**
* LabelledWidget is a small value-like class for simplifying the management
* of a QWidget with associated QLabel.
*/
template<class Widget>
class LabelledWidget : public _detail::LabelledWidgetBase
{
public:
LabelledWidget()
: _detail::LabelledWidgetBase{}
{}
void createWidgets(QWidget *parent)
{
setWidgets(new Widget{parent}, new QLabel{parent});
}
Widget *widget() const
{
return dynamic_cast<Widget *>(_detail::LabelledWidgetBase::widget());
}
};
}
......@@ -13,6 +13,8 @@
#include "ui_smimevalidationconfigurationwidget.h"
#include "labelledwidget.h"
#include "smimevalidationpreferences.h"
#include <Libkleo/Compat>
......@@ -58,11 +60,17 @@ private:
private:
struct UI : Ui_SMimeValidationConfigurationWidget {
LabelledWidget<KleopatraClientCopy::Gui::CertificateRequester> labelledOCSPResponderSignature;
LabelledWidget<QLineEdit> labelledOCSPResponderURL;
explicit UI(SMimeValidationConfigurationWidget *q)
: Ui_SMimeValidationConfigurationWidget()
{
setupUi(q);
labelledOCSPResponderURL.setWidgets(OCSPResponderURL, OCSPResponderURLLabel);
labelledOCSPResponderSignature.setWidgets(OCSPResponderSignature, OCSPResponderSignatureLabel);
if (QLayout *l = q->layout()) {
l->setContentsMargins(0, 0, 0, 0);
}
......@@ -256,13 +264,11 @@ void SMimeValidationConfigurationWidget::load()
if (e.mOCSPResponderURLConfigEntry) {
d->ui.OCSPResponderURL->setText(e.mOCSPResponderURLConfigEntry->stringValue());
}
d->ui.OCSPResponderURL->setEnabled(e.mOCSPResponderURLConfigEntry && !e.mOCSPResponderURLConfigEntry->isReadOnly());
d->ui.OCSPResponderURLLabel->setEnabled(e.mOCSPResponderURLConfigEntry && !e.mOCSPResponderURLConfigEntry->isReadOnly());
d->ui.labelledOCSPResponderURL.setEnabled(e.mOCSPResponderURLConfigEntry && !e.mOCSPResponderURLConfigEntry->isReadOnly());
if (e.mOCSPResponderSignature) {
d->ui.OCSPResponderSignature->setSelectedCertificate(e.mOCSPResponderSignature->stringValue());
}
d->ui.OCSPResponderSignature->setEnabled(e.mOCSPResponderSignature && !e.mOCSPResponderSignature->isReadOnly());
d->ui.OCSPResponderSignatureLabel->setEnabled(e.mOCSPResponderSignature && !e.mOCSPResponderSignature->isReadOnly());
d->ui.labelledOCSPResponderSignature.setEnabled(e.mOCSPResponderSignature && !e.mOCSPResponderSignature->isReadOnly());
// dirmngr-0.9.0 options
initializeDirmngrCheckbox(d->ui.ignoreServiceURLCB, e.mIgnoreServiceURLEntry);
......
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