Libksieve

sievetemplateeditdialog.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "sievetemplateeditdialog.h"
21 #include "editor/sievetextedit.h"
22 
23 #include <KPIMTextEdit/PlainTextEditFindBar>
24 #include <KPIMTextEdit/SlideContainer>
25 #include <Libkdepim/LineEditCatchReturnKey>
26 
27 #include <KLocalizedString>
28 #include <QLineEdit>
29 
30 #include <QVBoxLayout>
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QShortcut>
34 #include <KSharedConfig>
35 #include <KConfigGroup>
36 #include <QDialogButtonBox>
37 #include <QPushButton>
38 
39 #include <editor/sievetexteditwidget.h>
40 
41 #include <KPIMTextEdit/TextToSpeechWidget>
42 
43 using namespace KSieveUi;
44 
45 SieveTemplateEditDialog::SieveTemplateEditDialog(QWidget *parent, bool defaultTemplate)
46  : QDialog(parent)
47 {
48  setWindowTitle(defaultTemplate ? i18n("Default template") : i18n("Template"));
49  QVBoxLayout *mainLayout = new QVBoxLayout(this);
50  QDialogButtonBox *buttonBox = nullptr;
51  if (defaultTemplate) {
52  buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
53  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveTemplateEditDialog::reject);
54  } else {
56  mOkButton = buttonBox->button(QDialogButtonBox::Ok);
57  mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
58  connect(buttonBox, &QDialogButtonBox::accepted, this, &SieveTemplateEditDialog::accept);
59  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveTemplateEditDialog::reject);
60  mOkButton->setDefault(true);
61  }
62  QWidget *w = new QWidget;
63 
64  QVBoxLayout *vbox = new QVBoxLayout;
65  vbox->setContentsMargins(0, 0, 0, 0);
66 
67  QHBoxLayout *hbox = new QHBoxLayout;
68  QLabel *label = new QLabel(i18n("Name:"));
69  hbox->addWidget(label);
70 
71  mTemplateNameEdit = new QLineEdit;
72  mTemplateNameEdit->setReadOnly(defaultTemplate);
73  new KPIM::LineEditCatchReturnKey(mTemplateNameEdit, this);
74  mTemplateNameEdit->setClearButtonEnabled(!defaultTemplate);
75  hbox->addWidget(mTemplateNameEdit);
76 
77  vbox->addLayout(hbox);
78 
80  vbox->addWidget(textToSpeechWidget);
81 
82  mTextEditWidget = new KSieveUi::SieveTextEditWidget;
83  mTextEditWidget->textEdit()->setShowHelpMenu(false);
84  mTextEditWidget->setReadOnly(defaultTemplate);
85  vbox->addWidget(mTextEditWidget);
86  connect(mTextEditWidget->textEdit(), &SieveTextEdit::say, textToSpeechWidget, &KPIMTextEdit::TextToSpeechWidget::say);
87 
88  QShortcut *shortcut = new QShortcut(this);
89  shortcut->setKey(Qt::Key_F + Qt::CTRL);
90  connect(shortcut, &QShortcut::activated, mTextEditWidget, &SieveTextEditWidget::slotFind);
91 
92  shortcut = new QShortcut(this);
93  shortcut->setKey(Qt::Key_R + Qt::CTRL);
94  connect(shortcut, &QShortcut::activated, mTextEditWidget, &SieveTextEditWidget::slotReplace);
95 
96  w->setLayout(vbox);
97  mainLayout->addWidget(w);
98  if (!defaultTemplate) {
99  if (mOkButton) {
100  mOkButton->setEnabled(false);
101  }
102  connect(mTemplateNameEdit, &QLineEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
103  connect(mTextEditWidget->textEdit(), &SieveTextEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
104  mTemplateNameEdit->setFocus();
105  }
106  mainLayout->addWidget(buttonBox);
107 
108  readConfig();
109 }
110 
111 SieveTemplateEditDialog::~SieveTemplateEditDialog()
112 {
113  writeConfig();
114  disconnect(mTemplateNameEdit, &QLineEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
115  disconnect(mTextEditWidget->textEdit(), &SieveTextEdit::textChanged, this, &SieveTemplateEditDialog::slotTemplateChanged);
116 }
117 
118 void SieveTemplateEditDialog::writeConfig()
119 {
120  KConfigGroup group(KSharedConfig::openConfig(), "SieveTemplateEditDialog");
121  group.writeEntry("Size", size());
122 }
123 
124 void SieveTemplateEditDialog::readConfig()
125 {
126  KConfigGroup group(KSharedConfig::openConfig(), "SieveTemplateEditDialog");
127  const QSize sizeDialog = group.readEntry("Size", QSize(600, 400));
128  if (sizeDialog.isValid()) {
129  resize(sizeDialog);
130  }
131 }
132 
133 void SieveTemplateEditDialog::slotTemplateChanged()
134 {
135  mOkButton->setEnabled(!mTemplateNameEdit->text().trimmed().isEmpty() && !mTextEditWidget->textEdit()->toPlainText().trimmed().isEmpty());
136 }
137 
138 void SieveTemplateEditDialog::setScript(const QString &text)
139 {
140  mTextEditWidget->textEdit()->setPlainText(text);
141 }
142 
143 QString SieveTemplateEditDialog::script() const
144 {
145  return mTextEditWidget->textEdit()->toPlainText();
146 }
147 
148 void SieveTemplateEditDialog::setSieveCapabilities(const QStringList &capabilities)
149 {
150  mTextEditWidget->textEdit()->setSieveCapabilities(capabilities);
151 }
152 
153 void SieveTemplateEditDialog::setTemplateName(const QString &name)
154 {
155  mTemplateNameEdit->setText(name);
156 }
157 
158 QString SieveTemplateEditDialog::templateName() const
159 {
160  return mTemplateNameEdit->text();
161 }
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)
void setReadOnly(bool)
QSize size() const const
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setLayout(QLayout *layout)
QString label(StandardShortcut id)
void setShortcut(const QKeySequence &key)
void activated()
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
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
void addLayout(QLayout *layout, int stretch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 11 2020 23:08:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.