Libksieve

sievetemplateeditdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "sievetemplateeditdialog.h"
8#include "editor/sievetextedit.h"
9
10#include <KLineEditEventHandler>
11
12#include <KLocalizedString>
13#include <QLineEdit>
14
15#include <KConfigGroup>
16#include <KSharedConfig>
17#include <KWindowConfig>
18#include <QDialogButtonBox>
19#include <QHBoxLayout>
20#include <QLabel>
21#include <QPushButton>
22#include <QShortcut>
23#include <QVBoxLayout>
24#include <QWindow>
25
26#include "editor/sievetexteditwidget.h"
27#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
28#include <TextEditTextToSpeech/TextToSpeechContainerWidget>
29#endif
30
31using namespace KSieveUi;
32namespace
33{
34static const char mySieveTemplateEditDialogGroupName[] = "SieveTemplateEditDialog";
35}
36SieveTemplateEditDialog::SieveTemplateEditDialog(QWidget *parent, bool defaultTemplate)
37 : QDialog(parent)
38{
39 setWindowTitle(defaultTemplate ? i18nc("@title:window", "Default template") : i18nc("@title:window", "Template"));
40 auto mainLayout = new QVBoxLayout(this);
41 QDialogButtonBox *buttonBox = nullptr;
42 if (defaultTemplate) {
43 buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
44 connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveTemplateEditDialog::reject);
45 } else {
47 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
49 connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveTemplateEditDialog::accept);
50 connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveTemplateEditDialog::reject);
51 mOkButton->setDefault(true);
52 }
53 auto w = new QWidget;
54
55 auto vbox = new QVBoxLayout;
56 vbox->setContentsMargins({});
57
58 auto hbox = new QHBoxLayout;
59 auto label = new QLabel(i18nc("@label:textbox", "Name:"), this);
60 hbox->addWidget(label);
61
62 mTemplateNameEdit = new QLineEdit(this);
63 mTemplateNameEdit->setReadOnly(defaultTemplate);
65 mTemplateNameEdit->setClearButtonEnabled(!defaultTemplate);
66 hbox->addWidget(mTemplateNameEdit);
67
68 vbox->addLayout(hbox);
69#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
70 auto textToSpeechWidget = new TextEditTextToSpeech::TextToSpeechContainerWidget(this);
71 vbox->addWidget(textToSpeechWidget);
72#endif
73
74 mTextEditWidget = new KSieveUi::SieveTextEditWidget;
75 mTextEditWidget->textEdit()->setShowHelpMenu(false);
76 mTextEditWidget->setReadOnly(defaultTemplate);
77 vbox->addWidget(mTextEditWidget);
78#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
79 connect(mTextEditWidget->textEdit(), &SieveTextEdit::say, textToSpeechWidget, &TextEditTextToSpeech::TextToSpeechContainerWidget::say);
80#endif
81
82 auto shortcut = new QShortcut(this);
83 shortcut->setKey(Qt::Key_F | Qt::CTRL);
84 connect(shortcut, &QShortcut::activated, mTextEditWidget, &SieveTextEditWidget::slotFind);
85
86 shortcut = new QShortcut(this);
87 shortcut->setKey(Qt::Key_R | Qt::CTRL);
88 connect(shortcut, &QShortcut::activated, mTextEditWidget, &SieveTextEditWidget::slotReplace);
89
90 w->setLayout(vbox);
91 mainLayout->addWidget(w);
92 if (!defaultTemplate) {
93 if (mOkButton) {
94 mOkButton->setEnabled(false);
95 }
96 connect(mTemplateNameEdit, &QLineEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
97 connect(mTextEditWidget->textEdit(), &SieveTextEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
98 mTemplateNameEdit->setFocus();
99 }
100 mainLayout->addWidget(buttonBox);
101
102 readConfig();
103}
104
105SieveTemplateEditDialog::~SieveTemplateEditDialog()
106{
107 writeConfig();
108 disconnect(mTemplateNameEdit, &QLineEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
109 disconnect(mTextEditWidget->textEdit(), &SieveTextEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
110}
111
112void SieveTemplateEditDialog::readConfig()
113{
114 create(); // ensure a window is created
115 windowHandle()->resize(QSize(600, 400));
116 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveTemplateEditDialogGroupName));
118 resize(windowHandle()->size()); // workaround for QTBUG-40584
119}
120
121void SieveTemplateEditDialog::writeConfig()
122{
123 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveTemplateEditDialogGroupName));
125 group.sync();
126}
127
128void SieveTemplateEditDialog::slotTemplateChanged()
129{
130 mOkButton->setEnabled(!mTemplateNameEdit->text().trimmed().isEmpty() && !mTextEditWidget->textEdit()->toPlainText().trimmed().isEmpty());
131}
132
133void SieveTemplateEditDialog::setScript(const QString &text)
134{
135 mTextEditWidget->textEdit()->setPlainText(text);
136}
137
138QString SieveTemplateEditDialog::script() const
139{
140 return mTextEditWidget->textEdit()->toPlainText();
141}
142
143void SieveTemplateEditDialog::setSieveCapabilities(const QStringList &capabilities)
144{
145 mTextEditWidget->textEdit()->setSieveCapabilities(capabilities);
146}
147
148void SieveTemplateEditDialog::setTemplateName(const QString &name)
149{
150 mTemplateNameEdit->setText(name);
151}
152
153QString SieveTemplateEditDialog::templateName() const
154{
155 return mTemplateNameEdit->text();
156}
157
158#include "moc_sievetemplateeditdialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
QString label(StandardShortcut id)
const QList< QKeySequence > & shortcut(StandardShortcut id)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void setShortcut(const QKeySequence &key)
QPushButton * button(StandardButton which) const const
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
bool disconnect(const QMetaObject::Connection &connection)
void setPlainText(const QString &text)
void textChanged()
QString toPlainText() const const
void activated()
bool isEmpty() const const
QString trimmed() const const
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 Fri Nov 22 2024 12:09:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.