Libksieve

sievetemplateeditdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "sievetemplateeditdialog.h"
8 #include "editor/sievetextedit.h"
9 
10 #include <KPIMTextEdit/PlainTextEditFindBar>
11 #include <KPIMTextEdit/SlideContainer>
12 #include <Libkdepim/LineEditCatchReturnKey>
13 
14 #include <KLocalizedString>
15 #include <QLineEdit>
16 
17 #include <KConfigGroup>
18 #include <KSharedConfig>
19 #include <QDialogButtonBox>
20 #include <QHBoxLayout>
21 #include <QLabel>
22 #include <QPushButton>
23 #include <QShortcut>
24 #include <QVBoxLayout>
25 
26 #include <editor/sievetexteditwidget.h>
27 
28 #include <KPIMTextEdit/TextToSpeechWidget>
29 
30 using namespace KSieveUi;
31 
32 SieveTemplateEditDialog::SieveTemplateEditDialog(QWidget *parent, bool defaultTemplate)
33  : QDialog(parent)
34 {
35  setWindowTitle(defaultTemplate ? i18n("Default template") : i18n("Template"));
36  auto mainLayout = new QVBoxLayout(this);
37  QDialogButtonBox *buttonBox = nullptr;
38  if (defaultTemplate) {
39  buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
40  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveTemplateEditDialog::reject);
41  } else {
43  mOkButton = buttonBox->button(QDialogButtonBox::Ok);
44  mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
45  connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveTemplateEditDialog::accept);
46  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveTemplateEditDialog::reject);
47  mOkButton->setDefault(true);
48  }
49  auto w = new QWidget;
50 
51  auto vbox = new QVBoxLayout;
52  vbox->setContentsMargins({});
53 
54  auto hbox = new QHBoxLayout;
55  auto label = new QLabel(i18n("Name:"), this);
56  hbox->addWidget(label);
57 
58  mTemplateNameEdit = new QLineEdit(this);
59  mTemplateNameEdit->setReadOnly(defaultTemplate);
60  new KPIM::LineEditCatchReturnKey(mTemplateNameEdit, this);
61  mTemplateNameEdit->setClearButtonEnabled(!defaultTemplate);
62  hbox->addWidget(mTemplateNameEdit);
63 
64  vbox->addLayout(hbox);
65 
66  auto textToSpeechWidget = new KPIMTextEdit::TextToSpeechWidget(this);
67  vbox->addWidget(textToSpeechWidget);
68 
69  mTextEditWidget = new KSieveUi::SieveTextEditWidget;
70  mTextEditWidget->textEdit()->setShowHelpMenu(false);
71  mTextEditWidget->setReadOnly(defaultTemplate);
72  vbox->addWidget(mTextEditWidget);
73  connect(mTextEditWidget->textEdit(), &SieveTextEdit::say, textToSpeechWidget, &KPIMTextEdit::TextToSpeechWidget::say);
74 
75  auto shortcut = new QShortcut(this);
76  shortcut->setKey(Qt::Key_F | Qt::CTRL);
77  connect(shortcut, &QShortcut::activated, mTextEditWidget, &SieveTextEditWidget::slotFind);
78 
79  shortcut = new QShortcut(this);
80  shortcut->setKey(Qt::Key_R | Qt::CTRL);
81  connect(shortcut, &QShortcut::activated, mTextEditWidget, &SieveTextEditWidget::slotReplace);
82 
83  w->setLayout(vbox);
84  mainLayout->addWidget(w);
85  if (!defaultTemplate) {
86  if (mOkButton) {
87  mOkButton->setEnabled(false);
88  }
89  connect(mTemplateNameEdit, &QLineEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
90  connect(mTextEditWidget->textEdit(), &SieveTextEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
91  mTemplateNameEdit->setFocus();
92  }
93  mainLayout->addWidget(buttonBox);
94 
95  readConfig();
96 }
97 
98 SieveTemplateEditDialog::~SieveTemplateEditDialog()
99 {
100  writeConfig();
101  disconnect(mTemplateNameEdit, &QLineEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
102  disconnect(mTextEditWidget->textEdit(), &SieveTextEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
103 }
104 
105 void SieveTemplateEditDialog::writeConfig()
106 {
107  KConfigGroup group(KSharedConfig::openStateConfig(), "SieveTemplateEditDialog");
108  group.writeEntry("Size", size());
109 }
110 
111 void SieveTemplateEditDialog::readConfig()
112 {
113  KConfigGroup group(KSharedConfig::openStateConfig(), "SieveTemplateEditDialog");
114  const QSize sizeDialog = group.readEntry("Size", QSize(600, 400));
115  if (sizeDialog.isValid()) {
116  resize(sizeDialog);
117  }
118 }
119 
120 void SieveTemplateEditDialog::slotTemplateChanged()
121 {
122  mOkButton->setEnabled(!mTemplateNameEdit->text().trimmed().isEmpty() && !mTextEditWidget->textEdit()->toPlainText().trimmed().isEmpty());
123 }
124 
125 void SieveTemplateEditDialog::setScript(const QString &text)
126 {
127  mTextEditWidget->textEdit()->setPlainText(text);
128 }
129 
130 QString SieveTemplateEditDialog::script() const
131 {
132  return mTextEditWidget->textEdit()->toPlainText();
133 }
134 
135 void SieveTemplateEditDialog::setSieveCapabilities(const QStringList &capabilities)
136 {
137  mTextEditWidget->textEdit()->setSieveCapabilities(capabilities);
138 }
139 
140 void SieveTemplateEditDialog::setTemplateName(const QString &name)
141 {
142  mTemplateNameEdit->setText(name);
143 }
144 
145 QString SieveTemplateEditDialog::templateName() const
146 {
147  return mTemplateNameEdit->text();
148 }
void resize(int w, int h)
bool isValid() const const
void setContentsMargins(int left, int top, int right, int bottom)
void textChanged()
void textChanged(const QString &text)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QSize size() const const
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString label(StandardShortcut id)
void setShortcut(const QKeySequence &key)
void activated()
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
void setWindowTitle(const QString &)
QWidget(QWidget *parent, Qt::WindowFlags f)
QPushButton * button(QDialogButtonBox::StandardButton which) const const
const QList< QKeySequence > & shortcut(StandardShortcut id)
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 Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.