Libksieve

sievescriptdebuggerdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2021 Laurent Montel <[email protected]>
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 <KLocalizedString>
12 #include <KSharedConfig>
13 
14 #include <QDialogButtonBox>
15 #include <QPushButton>
16 #include <QVBoxLayout>
17 
18 using namespace KSieveUi;
19 
20 SieveScriptDebuggerDialog::SieveScriptDebuggerDialog(QWidget *parent)
21  : QDialog(parent)
22 {
23  auto mainLayout = new QVBoxLayout(this);
24  setWindowTitle(i18nc("@title:window", "Debug Sieve Script"));
25 
26  mSieveScriptDebuggerWidget = new SieveScriptDebuggerWidget(this);
27  mSieveScriptDebuggerWidget->setObjectName(QStringLiteral("sievescriptdebuggerwidget"));
28  mainLayout->addWidget(mSieveScriptDebuggerWidget);
29  connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::sieveTestNotFound, this, &SieveScriptDebuggerDialog::updateButtons);
30 
32  mainLayout->addWidget(buttonBox);
33  buttonBox->setObjectName(QStringLiteral("buttonbox"));
34  mOkButton = buttonBox->button(QDialogButtonBox::Ok);
35  mOkButton->setObjectName(QStringLiteral("okbutton"));
36  mOkButton->setText(i18n("Apply Changes"));
37  mOkButton->setDefault(true);
38  mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
39  mOkButton->setEnabled(false);
40  connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveScriptDebuggerDialog::slotAccepted);
41  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveScriptDebuggerDialog::reject);
42  connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
43  connect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugButtonEnabled, this, &SieveScriptDebuggerDialog::debugButtonEnableStateChanged);
44 
45  mDebugScriptButton = new QPushButton(i18n("Debug"), this);
46  mDebugScriptButton->setObjectName(QStringLiteral("debug_button"));
47  mDebugScriptButton->setEnabled(false);
48  connect(mDebugScriptButton, &QPushButton::clicked, mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::debugScriptButtonClicked);
49  buttonBox->addButton(mDebugScriptButton, QDialogButtonBox::ActionRole);
50  readConfig();
51 }
52 
53 SieveScriptDebuggerDialog::~SieveScriptDebuggerDialog()
54 {
55  disconnect(mSieveScriptDebuggerWidget, &SieveScriptDebuggerWidget::scriptTextChanged, this, &SieveScriptDebuggerDialog::slotScriptTextChanged);
56  writeConfig();
57 }
58 
59 void SieveScriptDebuggerDialog::updateButtons()
60 {
61  mOkButton->setEnabled(false);
62 }
63 
64 void SieveScriptDebuggerDialog::debugButtonEnableStateChanged(bool state)
65 {
66  mDebugScriptButton->setEnabled(state);
67 }
68 
69 void SieveScriptDebuggerDialog::slotScriptTextChanged()
70 {
71  if (mSieveScriptDebuggerWidget->haveDebugApps()) {
72  mOkButton->setEnabled(mOriginScript != mSieveScriptDebuggerWidget->script());
73  }
74 }
75 
76 void SieveScriptDebuggerDialog::slotAccepted()
77 {
78  if (mSieveScriptDebuggerWidget->canAccept()) {
79  accept();
80  }
81 }
82 
83 void SieveScriptDebuggerDialog::setScript(const QString &script)
84 {
85  mOriginScript = script;
86  mSieveScriptDebuggerWidget->setScript(script);
87  mOkButton->setEnabled(false);
88 }
89 
90 QString SieveScriptDebuggerDialog::script() const
91 {
92  return mSieveScriptDebuggerWidget->script();
93 }
94 
95 void SieveScriptDebuggerDialog::writeConfig()
96 {
97  KConfigGroup group(KSharedConfig::openStateConfig(), "SieveScriptDebuggerDialog");
98  group.writeEntry("Size", size());
99  group.writeEntry("Splitter", mSieveScriptDebuggerWidget->splitterSizes());
100 }
101 
102 void SieveScriptDebuggerDialog::readConfig()
103 {
104  KConfigGroup group(KSharedConfig::openStateConfig(), "SieveScriptDebuggerDialog");
105  const QSize sizeDialog = group.readEntry("Size", QSize(800, 600));
106  if (sizeDialog.isValid()) {
107  resize(sizeDialog);
108  }
109  const QList<int> size{100, 400};
110  mSieveScriptDebuggerWidget->setSplitterSizes(group.readEntry("Splitter", size));
111 }
bool isValid() const const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(bool checked)
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
The SieveScriptDebuggerWidget class.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Key_Return
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.