Mailcommon

snippetwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2019-2022 Laurent Montel <[email protected]>
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 <KPIMTextEdit/PlainTextEditor>
10 #include <MessageComposer/ConvertSnippetVariableMenu>
11 
12 #include <KActionCollection>
13 #include <KLineEdit>
14 #include <KLocalizedString>
15 #include <QComboBox>
16 #include <QVBoxLayout>
17 using namespace MailCommon;
18 
19 class Q_DECL_HIDDEN SnippetWidgetPrivate
20 {
21 public:
22  Ui::SnippetWidget mUi;
23  QWidget *wdg = nullptr;
24  bool isSelectedGroup = false;
25  bool wasChanged = false;
26 };
27 
28 SnippetWidget::SnippetWidget(QWidget *parent)
29  : QWidget(parent)
30  , d(new SnippetWidgetPrivate)
31 {
32  auto layout = new QVBoxLayout(this);
33  layout->setObjectName(QStringLiteral("mainlayout"));
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(), &KPIMTextEdit::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 
95 SnippetWidget::~SnippetWidget() = default;
96 
97 void SnippetWidget::setName(const QString &name)
98 {
99  d->mUi.nameEdit->setText(name);
100 }
101 
102 QString SnippetWidget::name() const
103 {
104  return d->mUi.nameEdit->text();
105 }
106 
107 void SnippetWidget::setText(const QString &text)
108 {
109  d->mUi.snippetText->setPlainText(text);
110 }
111 
112 QString SnippetWidget::text() const
113 {
114  return d->mUi.snippetText->toPlainText();
115 }
116 
117 void SnippetWidget::setKeySequence(const QKeySequence &sequence)
118 {
119  d->mUi.keyWidget->setKeySequence(sequence);
120 }
121 
122 QKeySequence SnippetWidget::keySequence() const
123 {
124  return d->mUi.keyWidget->keySequence();
125 }
126 
127 void SnippetWidget::setKeyword(const QString &keyword)
128 {
129  d->mUi.keyword->setText(keyword);
130 }
131 
132 QString SnippetWidget::keyword() const
133 {
134  return d->mUi.keyword->text();
135 }
136 
137 void SnippetWidget::setTo(const QString &keyword)
138 {
139  d->mUi.to->setText(keyword);
140 }
141 
142 QString SnippetWidget::to() const
143 {
144  return d->mUi.to->text();
145 }
146 
147 void SnippetWidget::setCc(const QString &keyword)
148 {
149  d->mUi.cc->setText(keyword);
150 }
151 
152 QString SnippetWidget::cc() const
153 {
154  return d->mUi.cc->text();
155 }
156 
157 void SnippetWidget::setBcc(const QString &keyword)
158 {
159  d->mUi.bcc->setText(keyword);
160 }
161 
162 QString SnippetWidget::bcc() const
163 {
164  return d->mUi.bcc->text();
165 }
166 
167 void SnippetWidget::setGroupModel(QAbstractItemModel *model)
168 {
169  d->mUi.groupBox->setModel(model);
170 }
171 
172 void SnippetWidget::setGroupIndex(const QModelIndex &index)
173 {
174  d->mUi.groupBox->setCurrentIndex(index.row());
175 }
176 
177 QModelIndex SnippetWidget::groupIndex() const
178 {
179  return d->mUi.groupBox->model()->index(d->mUi.groupBox->currentIndex(), 0);
180 }
181 
182 bool SnippetWidget::snippetIsValid() const
183 {
184  if (d->mUi.nameEdit->text().trimmed().isEmpty()) {
185  return false;
186  } else {
187  if (d->mUi.groupWidget->isVisible()) {
188  return !d->mUi.groupBox->currentText().trimmed().isEmpty();
189  }
190  }
191  return true;
192 }
193 
194 void SnippetWidget::setCheckActionCollections(const QList<KActionCollection *> &lst)
195 {
196  d->mUi.keyWidget->setCheckActionCollections(lst);
197 }
198 
199 void SnippetWidget::setGroupSelected(bool inGroupMode)
200 {
201  d->isSelectedGroup = inGroupMode;
202  d->mUi.groupWidget->setVisible(!inGroupMode);
203 }
204 
205 bool SnippetWidget::isGroupSelected() const
206 {
207  return d->isSelectedGroup;
208 }
209 
210 void SnippetWidget::clear()
211 {
212  d->mUi.nameEdit->clear();
213  d->mUi.keyword->clear();
214  d->mUi.snippetText->clear();
215  d->mUi.keyWidget->setKeySequence({});
216  d->mUi.subject->clear();
217  d->mUi.cc->clear();
218  d->mUi.to->clear();
219  d->mUi.bcc->clear();
220  d->mUi.attachment->clear();
221 }
222 
223 bool SnippetWidget::wasChanged() const
224 {
225  return d->wasChanged;
226 }
227 
228 void SnippetWidget::setWasChanged(bool b)
229 {
230  d->wasChanged = b;
231 }
232 
233 QString SnippetWidget::subject() const
234 {
235  return d->mUi.subject->text();
236 }
237 
238 void SnippetWidget::setAttachment(const QString &keyword)
239 {
240  d->mUi.attachment->setText(keyword);
241 }
242 
243 QString SnippetWidget::attachment() const
244 {
245  return d->mUi.attachment->text();
246 }
247 
248 void SnippetWidget::setSubject(const QString &text)
249 {
250  d->mUi.subject->setText(text);
251 }
QWidget(QWidget *parent, Qt::WindowFlags f)
QLayout * layout() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString i18n(const char *text, const TYPE &arg...)
void textChanged(const QString &text)
void textChanged()
void keySequenceChanged(const QKeySequence &seq)
int row() const const
void setObjectName(const QString &name)
void addWidget(QWidget *w)
void currentIndexChanged(int index)
void setContentsMargins(int left, int top, int right, int bottom)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.