regexpeditorlineedit.cpp 3.72 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
   Copyright (C) 2017 Montel Laurent <montel@kde.org>

   This program 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.

   This program 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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "regexpeditorlineedit.h"
Laurent Montel's avatar
Laurent Montel committed
21
#include "regexpeditorlineeditplugin_debug.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include <kpluginfactory.h>
Laurent Montel's avatar
Laurent Montel committed
23 24 25

#include <QStackedWidget>
#include <QHBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
26 27
#include <QLineEdit>
#include <QPushButton>
Laurent Montel's avatar
Laurent Montel committed
28
#include <KLocalizedString>
29
#include <KServiceTypeTrader>
Laurent Montel's avatar
Laurent Montel committed
30 31 32 33 34
#include <QDialog>
#include <QDebug>
#include <KPluginTrader>

#include <KTextWidgets/kregexpeditorinterface.h>
Laurent Montel's avatar
Laurent Montel committed
35 36


Laurent Montel's avatar
Laurent Montel committed
37 38
K_PLUGIN_FACTORY_WITH_JSON(RegexpEditorLineEditFactory, "regexepeditorlineedit.json", registerPlugin<RegexpEditorLineEdit>();
                           )
Laurent Montel's avatar
Laurent Montel committed
39

40
RegexpEditorLineEdit::RegexpEditorLineEdit(QWidget *parent, const QList<QVariant> &)
Laurent Montel's avatar
Laurent Montel committed
41 42
    : KSieveUi::AbstractRegexpEditorLineEdit(parent)
    , mIsRegExpMode(false)
43
    , mRegexEditorInstalled(false)
Laurent Montel's avatar
Laurent Montel committed
44
{
Laurent Montel's avatar
Laurent Montel committed
45 46 47
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setObjectName(QStringLiteral("mainlayout"));

Laurent Montel's avatar
Laurent Montel committed
48
    mLineEdit = new QLineEdit(this);
Laurent Montel's avatar
Laurent Montel committed
49
    connect(mLineEdit, &QLineEdit::textChanged, this, &RegexpEditorLineEdit::textChanged);
Laurent Montel's avatar
Laurent Montel committed
50 51 52 53 54
    mLineEdit->setObjectName(QStringLiteral("lineedit"));
    mainLayout->addWidget(mLineEdit);

    mRegExpEditorButton = new QPushButton(i18n("..."), this);
    mRegExpEditorButton->setObjectName(QStringLiteral("regexpbutton"));
Laurent Montel's avatar
Laurent Montel committed
55
    mRegExpEditorButton->setToolTip(i18n("Create Regular Expression"));
Laurent Montel's avatar
Laurent Montel committed
56
    mainLayout->addWidget(mRegExpEditorButton);
57
    mRegexEditorInstalled = !KServiceTypeTrader::self()->query(QStringLiteral("KRegExpEditor/KRegExpEditor")).isEmpty();
Laurent Montel's avatar
Laurent Montel committed
58 59
    if (mRegexEditorInstalled) {
        connect(mRegExpEditorButton, &QPushButton::clicked, this, &RegexpEditorLineEdit::slotOpenRegexpEditor);
Laurent Montel's avatar
Laurent Montel committed
60
    } else {
Laurent Montel's avatar
Laurent Montel committed
61
        qCWarning(REGEXPEDITORLINEEDITPLUGIN_LOG) << "KRegExpEditor is not installed on system.";
Laurent Montel's avatar
Laurent Montel committed
62
    }
63 64
    //Hidden by default
    mRegExpEditorButton->setVisible(false);
Laurent Montel's avatar
Laurent Montel committed
65 66 67 68 69 70
}

RegexpEditorLineEdit::~RegexpEditorLineEdit()
{
}

Laurent Montel's avatar
Laurent Montel committed
71 72
void RegexpEditorLineEdit::slotOpenRegexpEditor()
{
Laurent Montel's avatar
Laurent Montel committed
73
    QString error;
Laurent Montel's avatar
Laurent Montel committed
74

Laurent Montel's avatar
Laurent Montel committed
75
    QDialog *editorDialog = KServiceTypeTrader::createInstanceFromQuery<QDialog>(QStringLiteral("KRegExpEditor/KRegExpEditor"), this, this, {}, {}, &error);
Laurent Montel's avatar
Laurent Montel committed
76 77 78 79 80 81 82 83 84 85 86
    qDebug() << " editorDialog"<<editorDialog;
    if ( editorDialog ) {
        KRegExpEditorInterface* iface = qobject_cast<KRegExpEditorInterface*>(editorDialog);
        Q_ASSERT( iface ); // This should not fail!

        // now use the editor.
        iface->setRegExp(mLineEdit->text());

        if(editorDialog->exec() == QDialog::Accepted) {
            mLineEdit->setText(iface->regExp());
        }
Laurent Montel's avatar
Laurent Montel committed
87
    } else {
Laurent Montel's avatar
Laurent Montel committed
88
        qCWarning(REGEXPEDITORLINEEDITPLUGIN_LOG) << " Impossible to create regexpeditor " << error;
Laurent Montel's avatar
Laurent Montel committed
89
    }
Laurent Montel's avatar
Laurent Montel committed
90 91
}

Laurent Montel's avatar
Laurent Montel committed
92
void RegexpEditorLineEdit::switchToRegexpEditorLineEdit(bool regexpEditor)
93
{
Laurent Montel's avatar
Laurent Montel committed
94
    mIsRegExpMode = regexpEditor;
95 96 97
    if (mRegexEditorInstalled) {
        mRegExpEditorButton->setVisible(mIsRegExpMode);
    }
98 99 100 101
}

void RegexpEditorLineEdit::setCode(const QString &str)
{
Laurent Montel's avatar
Laurent Montel committed
102
    mLineEdit->setText(str);
103 104 105 106
}

QString RegexpEditorLineEdit::code() const
{
Laurent Montel's avatar
Laurent Montel committed
107
    return mLineEdit->text();
108 109
}

Laurent Montel's avatar
Laurent Montel committed
110
#include "regexpeditorlineedit.moc"