Libksieve

sievescriptdebuggerdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "sievescriptdebuggerdialog.h"
8#include "sievescriptdebuggerwidget.h"
9
10#include <KConfigGroup>
11#include <KGuiItem>
12#include <KLocalizedString>
13#include <KSharedConfig>
14
15#include <KWindowConfig>
16#include <QDialogButtonBox>
17#include <QPushButton>
18#include <QVBoxLayout>
19#include <QWindow>
20
21using namespace KSieveUi;
22namespace
23{
24static const char mySieveScriptDebuggerDialog[] = "SieveScriptDebuggerDialog";
25}
26SieveScriptDebuggerDialog::SieveScriptDebuggerDialog(QWidget *parent)
27 : QDialog(parent)
28{
29 auto mainLayout = new QVBoxLayout(this);
30 setWindowTitle(i18nc("@title:window", "Debug Sieve Script"));
31
32 mSieveScriptDebuggerWidget = new SieveScriptDebuggerWidget(this);
33 mSieveScriptDebuggerWidget->setObjectName(QLatin1StringView("sievescriptdebuggerwidget"));
34 mainLayout->addWidget(mSieveScriptDebuggerWidget);
35 connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::sieveTestNotFound, this, &SieveScriptDebuggerDialog::updateButtons);
36
38 mainLayout->addWidget(buttonBox);
39 buttonBox->setObjectName(QLatin1StringView("buttonbox"));
40 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
41 mOkButton->setObjectName(QLatin1StringView("okbutton"));
42 mOkButton->setText(i18n("Apply Changes"));
43 mOkButton->setDefault(true);
44 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
45 mOkButton->setEnabled(false);
46 connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveScriptDebuggerDialog::slotAccepted);
47 connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveScriptDebuggerDialog::reject);
48 connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
49 connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugButtonEnabled, this, &SieveScriptDebuggerDialog::debugButtonEnableStateChanged);
50
51 mDebugScriptButton = new QPushButton(i18nc("@action:button", "Debug"), this);
52 const KGuiItem item(i18n("Debug"), QIcon::fromTheme(QStringLiteral("debug-run")));
53 KGuiItem::assign(mDebugScriptButton, item);
54
55 mDebugScriptButton->setObjectName(QLatin1StringView("debug_button"));
56 mDebugScriptButton->setEnabled(false);
57 connect(mDebugScriptButton, &QPushButton::clicked, mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugScriptButtonClicked);
58 buttonBox->addButton(mDebugScriptButton, QDialogButtonBox::ActionRole);
59 readConfig();
60}
61
62SieveScriptDebuggerDialog::~SieveScriptDebuggerDialog()
63{
64 disconnect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
65 writeConfig();
66}
67
68void SieveScriptDebuggerDialog::updateButtons()
69{
70 mOkButton->setEnabled(false);
71}
72
73void SieveScriptDebuggerDialog::debugButtonEnableStateChanged(bool state)
74{
75 mDebugScriptButton->setEnabled(state);
76}
77
78void SieveScriptDebuggerDialog::slotScriptTextChanged()
79{
80 if (mSieveScriptDebuggerWidget->haveDebugApps()) {
81 mOkButton->setEnabled(mOriginScript != mSieveScriptDebuggerWidget->script());
82 }
83}
84
85void SieveScriptDebuggerDialog::slotAccepted()
86{
87 if (mSieveScriptDebuggerWidget->canAccept()) {
88 accept();
89 }
90}
91
92void SieveScriptDebuggerDialog::setScript(const QString &script)
93{
94 mOriginScript = script;
95 mSieveScriptDebuggerWidget->setScript(script);
96 mOkButton->setEnabled(false);
97}
98
99QString SieveScriptDebuggerDialog::script() const
100{
101 return mSieveScriptDebuggerWidget->script();
102}
103
104void SieveScriptDebuggerDialog::readConfig()
105{
106 create(); // ensure a window is created
107 windowHandle()->resize(QSize(800, 600));
108 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveScriptDebuggerDialog));
110 resize(windowHandle()->size()); // workaround for QTBUG-40584
111 const QList<int> size{100, 400};
112 mSieveScriptDebuggerWidget->setSplitterSizes(group.readEntry("Splitter", size));
113}
114
115void SieveScriptDebuggerDialog::writeConfig()
116{
117 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveScriptDebuggerDialog));
119 group.writeEntry("Splitter", mSieveScriptDebuggerWidget->splitterSizes());
120 group.sync();
121}
122
123#include "moc_sievescriptdebuggerdialog.cpp"
static void assign(QPushButton *button, const KGuiItem &item)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
The SieveScriptDebuggerWidget class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void clicked(bool checked)
virtual void accept()
QIcon fromTheme(const QString &name)
bool disconnect(const QMetaObject::Connection &connection)
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void setEnabled(bool)
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.