Warning, file /pim/mailcommon/src/snippets/snippetvariabledialog.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: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003   SPDX-FileContributor: Tobias Koenig <tokoe@kdab.com>
0004 
0005   SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0006 
0007   SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "snippetvariabledialog.h"
0011 
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 #include <KSharedConfig>
0015 #include <KWindowConfig>
0016 #include <QCheckBox>
0017 #include <QDialogButtonBox>
0018 #include <QLabel>
0019 #include <QMap>
0020 #include <QPushButton>
0021 #include <QVBoxLayout>
0022 #include <QWindow>
0023 
0024 #include <TextCustomEditor/PlainTextEditorWidget>
0025 
0026 using namespace MailCommon;
0027 namespace
0028 {
0029 static const char mySnippetVariableDialogConfigGroupName[] = "SnippetVariableDialog";
0030 }
0031 SnippetVariableDialog::SnippetVariableDialog(const QString &variableName, QMap<QString, QString> *variables, QWidget *parent)
0032     : QDialog(parent)
0033     , mVariableName(variableName)
0034     , mVariables(variables)
0035     , mVariableValueText(new TextCustomEditor::PlainTextEditorWidget(this))
0036 {
0037     setWindowTitle(i18nc("@title:window", "Enter Values for Variables"));
0038     auto mainLayout = new QVBoxLayout(this);
0039 
0040     auto label = new QLabel(i18n("Enter the replacement values for '%1':", variableName), this);
0041     mainLayout->addWidget(label);
0042 
0043     mainLayout->addWidget(mVariableValueText);
0044 
0045     mSaveVariable = new QCheckBox(i18n("Make value &default"), this);
0046     mSaveVariable->setChecked(false);
0047     mSaveVariable->setToolTip(i18nc("@info:tooltip",
0048                                     "Enable this to save the value entered to the right "
0049                                     "as the default value for this variable"));
0050     mSaveVariable->setWhatsThis(i18nc("@info:whatsthis",
0051                                       "If you enable this option, the value entered to the right will be saved. "
0052                                       "If you use the same variable later, even in another snippet, the value entered "
0053                                       "to the right will be the default value for that variable."));
0054     mainLayout->addWidget(mSaveVariable);
0055 
0056     if (mVariables->contains(variableName)) {
0057         mSaveVariable->setChecked(true);
0058         mVariableValueText->setPlainText(mVariables->value(variableName));
0059     }
0060     mVariableValueText->setFocus();
0061 
0062     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0063     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0064     okButton->setDefault(true);
0065     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0066     connect(buttonBox, &QDialogButtonBox::accepted, this, &SnippetVariableDialog::slotAccepted);
0067     connect(buttonBox, &QDialogButtonBox::rejected, this, &SnippetVariableDialog::reject);
0068 
0069     mainLayout->addWidget(buttonBox);
0070     readConfig();
0071 }
0072 
0073 SnippetVariableDialog::~SnippetVariableDialog()
0074 {
0075     writeConfig();
0076 }
0077 
0078 QString SnippetVariableDialog::variableValue() const
0079 {
0080     return mVariableValueText->toPlainText();
0081 }
0082 
0083 bool SnippetVariableDialog::saveVariableIsChecked() const
0084 {
0085     return mSaveVariable->isChecked();
0086 }
0087 
0088 void SnippetVariableDialog::slotAccepted()
0089 {
0090     if (mSaveVariable->isChecked()) {
0091         mVariables->insert(mVariableName, mVariableValueText->toPlainText());
0092     } else {
0093         mVariables->remove(mVariableName);
0094     }
0095 
0096     accept();
0097 }
0098 
0099 void SnippetVariableDialog::readConfig()
0100 {
0101     create(); // ensure a window is created
0102     windowHandle()->resize(QSize(300, 350));
0103     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySnippetVariableDialogConfigGroupName));
0104     KWindowConfig::restoreWindowSize(windowHandle(), group);
0105     resize(windowHandle()->size()); // workaround for QTBUG-40584
0106 }
0107 
0108 void SnippetVariableDialog::writeConfig()
0109 {
0110     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySnippetVariableDialogConfigGroupName));
0111     KWindowConfig::saveWindowSize(windowHandle(), group);
0112     group.sync();
0113 }
0114 
0115 #include "moc_snippetvariabledialog.cpp"