Commit 3b70f2c3 authored by Laurent Montel's avatar Laurent Montel 😁

Use QToolButton. Optimization don't check all the time if we have kregexpeditor installed

parent f4058bde
......@@ -20,8 +20,9 @@
#include "regexpeditorlineedittest.h"
#include "../regexpeditorlineedit.h"
#include <QLineEdit>
#include <QPushButton>
#include <QToolButton>
#include <QTest>
#include <QHBoxLayout>
RegexpEditorLineEditTest::RegexpEditorLineEditTest(QObject *parent)
: QObject(parent)
......@@ -32,11 +33,16 @@ RegexpEditorLineEditTest::RegexpEditorLineEditTest(QObject *parent)
void RegexpEditorLineEditTest::shouldHaveDefaultValue()
{
RegexpEditorLineEdit w;
QHBoxLayout *mainLayout = w.findChild<QHBoxLayout *>(QStringLiteral("mainlayout"));
QVERIFY(mainLayout);
QCOMPARE(mainLayout->margin(), 0);
QLineEdit *mLineEdit = w.findChild<QLineEdit *>(QStringLiteral("lineedit"));
QVERIFY(mLineEdit);
QVERIFY(mLineEdit->text().isEmpty());
QPushButton *mRegExpEditorButton = w.findChild<QPushButton *>(QStringLiteral("regexpbutton"));
QToolButton *mRegExpEditorButton = w.findChild<QToolButton *>(QStringLiteral("regexpbutton"));
QVERIFY(mRegExpEditorButton);
QVERIFY(!mRegExpEditorButton->text().isEmpty());
QVERIFY(!mRegExpEditorButton->toolTip().isEmpty());
......
......@@ -24,7 +24,7 @@
#include <QStackedWidget>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QToolButton>
#include <KLocalizedString>
#include <KServiceTypeTrader>
#include <QDialog>
......@@ -36,27 +36,52 @@
K_PLUGIN_FACTORY_WITH_JSON(RegexpEditorLineEditFactory, "regexepeditorlineedit.json", registerPlugin<RegexpEditorLineEdit>();
)
struct InfoRegExp
{
InfoRegExp()
: status(Unknown)
{
}
enum RegexpEditorStatus {
Unknown = 0,
Installed,
NotInstalled
};
RegexpEditorStatus status;
};
Q_GLOBAL_STATIC(InfoRegExp, s_regexpeditorinstalled)
RegexpEditorLineEdit::RegexpEditorLineEdit(QWidget *parent, const QList<QVariant> &)
: KSieveUi::AbstractRegexpEditorLineEdit(parent)
, mEditorDialog(nullptr)
, mIsRegExpMode(false)
, mRegexEditorInstalled(false)
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setObjectName(QStringLiteral("mainlayout"));
mainLayout->setMargin(0);
mLineEdit = new QLineEdit(this);
connect(mLineEdit, &QLineEdit::textChanged, this, &RegexpEditorLineEdit::textChanged);
mLineEdit->setObjectName(QStringLiteral("lineedit"));
mainLayout->addWidget(mLineEdit);
mRegExpEditorButton = new QPushButton(i18n("..."), this);
mRegExpEditorButton = new QToolButton(this);
mRegExpEditorButton->setText(i18n("..."));
mRegExpEditorButton->setObjectName(QStringLiteral("regexpbutton"));
mRegExpEditorButton->setToolTip(i18n("Create Regular Expression"));
mainLayout->addWidget(mRegExpEditorButton);
mRegexEditorInstalled = !KServiceTypeTrader::self()->query(QStringLiteral("KRegExpEditor/KRegExpEditor")).isEmpty();
if (mRegexEditorInstalled) {
connect(mRegExpEditorButton, &QPushButton::clicked, this, &RegexpEditorLineEdit::slotOpenRegexpEditor);
if (s_regexpeditorinstalled->status == InfoRegExp::Unknown) {
if (KServiceTypeTrader::self()->query(QStringLiteral("KRegExpEditor/KRegExpEditor")).isEmpty()) {
s_regexpeditorinstalled->status = InfoRegExp::NotInstalled;
} else {
s_regexpeditorinstalled->status = InfoRegExp::Installed;
}
}
if (s_regexpeditorinstalled->status == InfoRegExp::Installed) {
connect(mRegExpEditorButton, &QToolButton::clicked, this, &RegexpEditorLineEdit::slotOpenRegexpEditor);
} else {
qCWarning(REGEXPEDITORLINEEDITPLUGIN_LOG) << "KRegExpEditor is not installed on system.";
}
......@@ -70,29 +95,30 @@ RegexpEditorLineEdit::~RegexpEditorLineEdit()
void RegexpEditorLineEdit::slotOpenRegexpEditor()
{
QString error;
if (!mEditorDialog) {
QString error;
QDialog *editorDialog = KServiceTypeTrader::createInstanceFromQuery<QDialog>(QStringLiteral("KRegExpEditor/KRegExpEditor"), this, this, {}, {}, &error);
qDebug() << " editorDialog"<<editorDialog;
if ( editorDialog ) {
KRegExpEditorInterface* iface = qobject_cast<KRegExpEditorInterface*>(editorDialog);
Q_ASSERT( iface ); // This should not fail!
mEditorDialog = KServiceTypeTrader::createInstanceFromQuery<QDialog>(QStringLiteral("KRegExpEditor/KRegExpEditor"), this, this, {}, {}, &error);
if ( !mEditorDialog ) {
qCWarning(REGEXPEDITORLINEEDITPLUGIN_LOG) << " Impossible to create regexpeditor " << error;
return;
}
}
KRegExpEditorInterface* iface = qobject_cast<KRegExpEditorInterface*>(mEditorDialog);
Q_ASSERT( iface ); // This should not fail!
// now use the editor.
iface->setRegExp(mLineEdit->text());
// now use the editor.
iface->setRegExp(mLineEdit->text());
if(editorDialog->exec() == QDialog::Accepted) {
mLineEdit->setText(iface->regExp());
}
} else {
qCWarning(REGEXPEDITORLINEEDITPLUGIN_LOG) << " Impossible to create regexpeditor " << error;
if(mEditorDialog->exec() == QDialog::Accepted) {
mLineEdit->setText(iface->regExp());
}
}
void RegexpEditorLineEdit::switchToRegexpEditorLineEdit(bool regexpEditor)
{
mIsRegExpMode = regexpEditor;
if (mRegexEditorInstalled) {
if (s_regexpeditorinstalled->status == InfoRegExp::Installed) {
mRegExpEditorButton->setVisible(mIsRegExpMode);
}
}
......
......@@ -23,7 +23,7 @@
#include <QWidget>
#include <KSieveUi/AbstractRegexpEditorLineEdit>
class QLineEdit;
class QPushButton;
class QToolButton;
class RegexpEditorLineEdit : public KSieveUi::AbstractRegexpEditorLineEdit
{
Q_OBJECT
......@@ -38,9 +38,9 @@ public:
private:
void slotOpenRegexpEditor();
QLineEdit *mLineEdit;
QPushButton *mRegExpEditorButton;
QToolButton *mRegExpEditorButton;
QDialog *mEditorDialog;
bool mIsRegExpMode;
bool mRegexEditorInstalled;
};
#endif // REGEXPEDITORLINEEDIT_H
......@@ -27,13 +27,13 @@ RegExpEditorWidget::RegExpEditorWidget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
QCheckBox *switchToRegexp = new QCheckBox(QStringLiteral("Switch to regexp line edit"), this);
layout->addWidget(switchToRegexp);
mLineEdit = new RegexpEditorLineEdit(this);
layout->addWidget(mLineEdit);
for (int i = 0; i < 4; ++i) {
QCheckBox *switchToRegexp = new QCheckBox(QStringLiteral("Switch to regexp line edit"), this);
layout->addWidget(switchToRegexp);
RegexpEditorLineEdit *lineEdit = new RegexpEditorLineEdit(this);
layout->addWidget(lineEdit);
connect(switchToRegexp, &QCheckBox::toggled, lineEdit, &RegexpEditorLineEdit::switchToRegexpEditorLineEdit);
}
layout->addStretch(1);
connect(switchToRegexp, &QCheckBox::toggled, mLineEdit, &RegexpEditorLineEdit::switchToRegexpEditorLineEdit);
}
......@@ -21,15 +21,12 @@
#define REGEXPEDITORWIDGET_H
#include <QWidget>
class RegexpEditorLineEdit;
class RegExpEditorWidget : public QWidget
{
Q_OBJECT
public:
explicit RegExpEditorWidget(QWidget *parent = nullptr);
~RegExpEditorWidget() = default;
private:
RegexpEditorLineEdit *mLineEdit;
};
#endif // REGEXPEDITORWIDGET_H
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