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