Mailcommon

snippetwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "snippetwidget.h"
8#include "ui_snippetwidget.h"
9#include <MessageComposer/ConvertSnippetVariableMenu>
10#include <TextCustomEditor/PlainTextEditor>
11
12#include <KActionCollection>
13#include <KLineEdit>
14#include <KLocalizedString>
15#include <QComboBox>
16#include <QVBoxLayout>
17using namespace MailCommon;
18
19class Q_DECL_HIDDEN SnippetWidgetPrivate
20{
21public:
22 Ui::SnippetWidget mUi;
23 QWidget *wdg = nullptr;
24 bool isSelectedGroup = false;
25 bool wasChanged = false;
26};
27
28SnippetWidget::SnippetWidget(QWidget *parent)
29 : QWidget(parent)
30 , d(new SnippetWidgetPrivate)
31{
32 auto layout = new QVBoxLayout(this);
33 layout->setObjectName(QLatin1StringView("mainlayout"));
34 layout->setContentsMargins({});
35 d->wdg = new QWidget(this);
36 d->mUi.setupUi(d->wdg);
37 layout->addWidget(d->wdg);
38
39 auto variableMenu = new MessageComposer::ConvertSnippetVariableMenu(false, this, this);
40 d->mUi.pushButtonVariables->setMenu(variableMenu->menu());
41 connect(variableMenu,
42 &MessageComposer::ConvertSnippetVariableMenu::insertVariable,
43 this,
44 [this](MessageComposer::ConvertSnippetVariablesUtil::VariableType type) {
45 d->mUi.snippetText->editor()->insertPlainText(MessageComposer::ConvertSnippetVariablesUtil::snippetVariableFromEnum(type) + QLatin1Char(' '));
46 });
47
48 d->mUi.nameEdit->setTrapReturnKey(true);
49 d->mUi.keyword->setTrapReturnKey(true);
50 d->mUi.keyword->setClearButtonEnabled(true);
51 d->mUi.nameEdit->setClearButtonEnabled(true);
52 d->mUi.nameEdit->setFocus();
53 d->mUi.snippetText->setMinimumSize(500, 300);
54
55 d->mUi.keyword->setWhatsThis(
56 i18n("Enter a keyword here to enable fast insertion of this snippet while writing "
57 "an email. For instance if you choose \"greeting\" as the keyword, you can then "
58 "type \\greeting in your email and then press the tab key, and it will be "
59 "replaced with the contents of this snippet."));
60
61 connect(d->mUi.nameEdit, &KLineEdit::textChanged, this, [this](const QString &str) {
62 Q_EMIT textChanged(str);
63 d->wasChanged = true;
64 });
65 connect(d->mUi.groupBox, &QComboBox::currentIndexChanged, this, [this](int index) {
66 Q_EMIT groupChanged(index);
67 d->wasChanged = true;
68 });
69 connect(d->mUi.keyword, &KLineEdit::textChanged, this, [this]() {
70 d->wasChanged = true;
71 });
72 connect(d->mUi.snippetText->editor(), &TextCustomEditor::PlainTextEditor::textChanged, this, [this]() {
73 d->wasChanged = true;
74 });
75 connect(d->mUi.keyWidget, &KKeySequenceWidget::keySequenceChanged, this, [this]() {
76 d->wasChanged = true;
77 });
78 connect(d->mUi.cc, &Akonadi::EmailAddressRequester::textChanged, this, [this]() {
79 d->wasChanged = true;
80 });
81 connect(d->mUi.to, &Akonadi::EmailAddressRequester::textChanged, this, [this]() {
82 d->wasChanged = true;
83 });
84 connect(d->mUi.bcc, &Akonadi::EmailAddressRequester::textChanged, this, [this]() {
85 d->wasChanged = true;
86 });
87 connect(d->mUi.subject, &QLineEdit::textChanged, this, [this]() {
88 d->wasChanged = true;
89 });
90 connect(d->mUi.attachment, &MailCommon::SnippetAttachmentWidget::wasChanged, this, [this]() {
91 d->wasChanged = true;
92 });
93}
94
95SnippetWidget::~SnippetWidget() = default;
96
97void SnippetWidget::setName(const QString &name)
98{
99 d->mUi.nameEdit->setText(name);
100}
101
102QString SnippetWidget::name() const
103{
104 return d->mUi.nameEdit->text();
105}
106
107void SnippetWidget::setText(const QString &text)
108{
109 d->mUi.snippetText->setPlainText(text);
110}
111
112QString SnippetWidget::text() const
113{
114 return d->mUi.snippetText->toPlainText();
115}
116
117void SnippetWidget::setKeySequence(const QKeySequence &sequence)
118{
119 d->mUi.keyWidget->setKeySequence(sequence);
120}
121
122QKeySequence SnippetWidget::keySequence() const
123{
124 return d->mUi.keyWidget->keySequence();
125}
126
127void SnippetWidget::setKeyword(const QString &keyword)
128{
129 d->mUi.keyword->setText(keyword);
130}
131
132QString SnippetWidget::keyword() const
133{
134 return d->mUi.keyword->text();
135}
136
137void SnippetWidget::setTo(const QString &keyword)
138{
139 d->mUi.to->setText(keyword);
140}
141
142QString SnippetWidget::to() const
143{
144 return d->mUi.to->text();
145}
146
147void SnippetWidget::setCc(const QString &keyword)
148{
149 d->mUi.cc->setText(keyword);
150}
151
152QString SnippetWidget::cc() const
153{
154 return d->mUi.cc->text();
155}
156
157void SnippetWidget::setBcc(const QString &keyword)
158{
159 d->mUi.bcc->setText(keyword);
160}
161
162QString SnippetWidget::bcc() const
163{
164 return d->mUi.bcc->text();
165}
166
167void SnippetWidget::setGroupModel(QAbstractItemModel *model)
168{
169 d->mUi.groupBox->setModel(model);
170}
171
172void SnippetWidget::setGroupIndex(const QModelIndex &index)
173{
174 d->mUi.groupBox->setCurrentIndex(index.row());
175}
176
177QModelIndex SnippetWidget::groupIndex() const
178{
179 return d->mUi.groupBox->model()->index(d->mUi.groupBox->currentIndex(), 0);
180}
181
182bool SnippetWidget::snippetIsValid() const
183{
184 if (d->mUi.nameEdit->text().trimmed().isEmpty()) {
185 return false;
186 } else {
187 if (d->mUi.formLayout->isRowVisible(1)) {
188 return !d->mUi.groupBox->currentText().trimmed().isEmpty();
189 }
190 }
191 return true;
192}
193
194void SnippetWidget::setCheckActionCollections(const QList<KActionCollection *> &lst)
195{
196 d->mUi.keyWidget->setCheckActionCollections(lst);
197}
198
199void SnippetWidget::setGroupSelected(bool inGroupMode)
200{
201 d->isSelectedGroup = inGroupMode;
202 // Set all the other row but name to invisible
203 for (int i = 1; i < d->mUi.formLayout->rowCount(); i++) {
204 d->mUi.formLayout->setRowVisible(i, !inGroupMode);
205 }
206}
207
208bool SnippetWidget::isGroupSelected() const
209{
210 return d->isSelectedGroup;
211}
212
213void SnippetWidget::clear()
214{
215 d->mUi.nameEdit->clear();
216 d->mUi.keyword->clear();
217 d->mUi.snippetText->clear();
218 d->mUi.keyWidget->setKeySequence({});
219 d->mUi.subject->clear();
220 d->mUi.cc->clear();
221 d->mUi.to->clear();
222 d->mUi.bcc->clear();
223 d->mUi.attachment->clear();
224}
225
226bool SnippetWidget::wasChanged() const
227{
228 return d->wasChanged;
229}
230
231void SnippetWidget::setWasChanged(bool b)
232{
233 d->wasChanged = b;
234}
235
236QString SnippetWidget::subject() const
237{
238 return d->mUi.subject->text();
239}
240
241void SnippetWidget::setAttachment(const QString &keyword)
242{
243 d->mUi.attachment->setText(keyword);
244}
245
246QString SnippetWidget::attachment() const
247{
248 return d->mUi.attachment->text();
249}
250
251void SnippetWidget::setSubject(const QString &text)
252{
253 d->mUi.subject->setText(text);
254}
255
256#include "moc_snippetwidget.cpp"
void keySequenceChanged(const QKeySequence &seq)
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
void currentIndexChanged(int index)
void textChanged(const QString &text)
int row() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:39:23 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.