Warning, file /pim/mailcommon/src/snippets/snippetwidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "snippetwidget.h"
0008 #include "ui_snippetwidget.h"
0009 #include <MessageComposer/ConvertSnippetVariableMenu>
0010 #include <TextCustomEditor/PlainTextEditor>
0011 
0012 #include <KActionCollection>
0013 #include <KLineEdit>
0014 #include <KLocalizedString>
0015 #include <QComboBox>
0016 #include <QVBoxLayout>
0017 using namespace MailCommon;
0018 
0019 class Q_DECL_HIDDEN SnippetWidgetPrivate
0020 {
0021 public:
0022     Ui::SnippetWidget mUi;
0023     QWidget *wdg = nullptr;
0024     bool isSelectedGroup = false;
0025     bool wasChanged = false;
0026 };
0027 
0028 SnippetWidget::SnippetWidget(QWidget *parent)
0029     : QWidget(parent)
0030     , d(new SnippetWidgetPrivate)
0031 {
0032     auto layout = new QVBoxLayout(this);
0033     layout->setObjectName(QLatin1StringView("mainlayout"));
0034     layout->setContentsMargins({});
0035     d->wdg = new QWidget(this);
0036     d->mUi.setupUi(d->wdg);
0037     layout->addWidget(d->wdg);
0038 
0039     auto variableMenu = new MessageComposer::ConvertSnippetVariableMenu(false, this, this);
0040     d->mUi.pushButtonVariables->setMenu(variableMenu->menu());
0041     connect(variableMenu,
0042             &MessageComposer::ConvertSnippetVariableMenu::insertVariable,
0043             this,
0044             [this](MessageComposer::ConvertSnippetVariablesUtil::VariableType type) {
0045                 d->mUi.snippetText->editor()->insertPlainText(MessageComposer::ConvertSnippetVariablesUtil::snippetVariableFromEnum(type) + QLatin1Char(' '));
0046             });
0047 
0048     d->mUi.nameEdit->setTrapReturnKey(true);
0049     d->mUi.keyword->setTrapReturnKey(true);
0050     d->mUi.keyword->setClearButtonEnabled(true);
0051     d->mUi.nameEdit->setClearButtonEnabled(true);
0052     d->mUi.nameEdit->setFocus();
0053     d->mUi.snippetText->setMinimumSize(500, 300);
0054 
0055     d->mUi.keyword->setWhatsThis(
0056         i18n("Enter a keyword here to enable fast insertion of this snippet while writing "
0057              "an email. For instance if you choose \"greeting\" as the keyword, you can then "
0058              "type \\greeting in your email and then press the tab key, and it will be "
0059              "replaced with the contents of this snippet."));
0060 
0061     connect(d->mUi.nameEdit, &KLineEdit::textChanged, this, [this](const QString &str) {
0062         Q_EMIT textChanged(str);
0063         d->wasChanged = true;
0064     });
0065     connect(d->mUi.groupBox, &QComboBox::currentIndexChanged, this, [this](int index) {
0066         Q_EMIT groupChanged(index);
0067         d->wasChanged = true;
0068     });
0069     connect(d->mUi.keyword, &KLineEdit::textChanged, this, [this]() {
0070         d->wasChanged = true;
0071     });
0072     connect(d->mUi.snippetText->editor(), &TextCustomEditor::PlainTextEditor::textChanged, this, [this]() {
0073         d->wasChanged = true;
0074     });
0075     connect(d->mUi.keyWidget, &KKeySequenceWidget::keySequenceChanged, this, [this]() {
0076         d->wasChanged = true;
0077     });
0078     connect(d->mUi.cc, &Akonadi::EmailAddressRequester::textChanged, this, [this]() {
0079         d->wasChanged = true;
0080     });
0081     connect(d->mUi.to, &Akonadi::EmailAddressRequester::textChanged, this, [this]() {
0082         d->wasChanged = true;
0083     });
0084     connect(d->mUi.bcc, &Akonadi::EmailAddressRequester::textChanged, this, [this]() {
0085         d->wasChanged = true;
0086     });
0087     connect(d->mUi.subject, &QLineEdit::textChanged, this, [this]() {
0088         d->wasChanged = true;
0089     });
0090     connect(d->mUi.attachment, &MailCommon::SnippetAttachmentWidget::wasChanged, this, [this]() {
0091         d->wasChanged = true;
0092     });
0093 }
0094 
0095 SnippetWidget::~SnippetWidget() = default;
0096 
0097 void SnippetWidget::setName(const QString &name)
0098 {
0099     d->mUi.nameEdit->setText(name);
0100 }
0101 
0102 QString SnippetWidget::name() const
0103 {
0104     return d->mUi.nameEdit->text();
0105 }
0106 
0107 void SnippetWidget::setText(const QString &text)
0108 {
0109     d->mUi.snippetText->setPlainText(text);
0110 }
0111 
0112 QString SnippetWidget::text() const
0113 {
0114     return d->mUi.snippetText->toPlainText();
0115 }
0116 
0117 void SnippetWidget::setKeySequence(const QKeySequence &sequence)
0118 {
0119     d->mUi.keyWidget->setKeySequence(sequence);
0120 }
0121 
0122 QKeySequence SnippetWidget::keySequence() const
0123 {
0124     return d->mUi.keyWidget->keySequence();
0125 }
0126 
0127 void SnippetWidget::setKeyword(const QString &keyword)
0128 {
0129     d->mUi.keyword->setText(keyword);
0130 }
0131 
0132 QString SnippetWidget::keyword() const
0133 {
0134     return d->mUi.keyword->text();
0135 }
0136 
0137 void SnippetWidget::setTo(const QString &keyword)
0138 {
0139     d->mUi.to->setText(keyword);
0140 }
0141 
0142 QString SnippetWidget::to() const
0143 {
0144     return d->mUi.to->text();
0145 }
0146 
0147 void SnippetWidget::setCc(const QString &keyword)
0148 {
0149     d->mUi.cc->setText(keyword);
0150 }
0151 
0152 QString SnippetWidget::cc() const
0153 {
0154     return d->mUi.cc->text();
0155 }
0156 
0157 void SnippetWidget::setBcc(const QString &keyword)
0158 {
0159     d->mUi.bcc->setText(keyword);
0160 }
0161 
0162 QString SnippetWidget::bcc() const
0163 {
0164     return d->mUi.bcc->text();
0165 }
0166 
0167 void SnippetWidget::setGroupModel(QAbstractItemModel *model)
0168 {
0169     d->mUi.groupBox->setModel(model);
0170 }
0171 
0172 void SnippetWidget::setGroupIndex(const QModelIndex &index)
0173 {
0174     d->mUi.groupBox->setCurrentIndex(index.row());
0175 }
0176 
0177 QModelIndex SnippetWidget::groupIndex() const
0178 {
0179     return d->mUi.groupBox->model()->index(d->mUi.groupBox->currentIndex(), 0);
0180 }
0181 
0182 bool SnippetWidget::snippetIsValid() const
0183 {
0184     if (d->mUi.nameEdit->text().trimmed().isEmpty()) {
0185         return false;
0186     } else {
0187         if (d->mUi.formLayout->isRowVisible(1)) {
0188             return !d->mUi.groupBox->currentText().trimmed().isEmpty();
0189         }
0190     }
0191     return true;
0192 }
0193 
0194 void SnippetWidget::setCheckActionCollections(const QList<KActionCollection *> &lst)
0195 {
0196     d->mUi.keyWidget->setCheckActionCollections(lst);
0197 }
0198 
0199 void SnippetWidget::setGroupSelected(bool inGroupMode)
0200 {
0201     d->isSelectedGroup = inGroupMode;
0202     // Set all the other row but name to invisible
0203     for (int i = 1; i < d->mUi.formLayout->rowCount(); i++) {
0204         d->mUi.formLayout->setRowVisible(i, !inGroupMode);
0205     }
0206 }
0207 
0208 bool SnippetWidget::isGroupSelected() const
0209 {
0210     return d->isSelectedGroup;
0211 }
0212 
0213 void SnippetWidget::clear()
0214 {
0215     d->mUi.nameEdit->clear();
0216     d->mUi.keyword->clear();
0217     d->mUi.snippetText->clear();
0218     d->mUi.keyWidget->setKeySequence({});
0219     d->mUi.subject->clear();
0220     d->mUi.cc->clear();
0221     d->mUi.to->clear();
0222     d->mUi.bcc->clear();
0223     d->mUi.attachment->clear();
0224 }
0225 
0226 bool SnippetWidget::wasChanged() const
0227 {
0228     return d->wasChanged;
0229 }
0230 
0231 void SnippetWidget::setWasChanged(bool b)
0232 {
0233     d->wasChanged = b;
0234 }
0235 
0236 QString SnippetWidget::subject() const
0237 {
0238     return d->mUi.subject->text();
0239 }
0240 
0241 void SnippetWidget::setAttachment(const QString &keyword)
0242 {
0243     d->mUi.attachment->setText(keyword);
0244 }
0245 
0246 QString SnippetWidget::attachment() const
0247 {
0248     return d->mUi.attachment->text();
0249 }
0250 
0251 void SnippetWidget::setSubject(const QString &text)
0252 {
0253     d->mUi.subject->setText(text);
0254 }
0255 
0256 #include "moc_snippetwidget.cpp"