Mailcommon

snippetvariabledialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
3  SPDX-FileContributor: Tobias Koenig <[email protected]>
4 
5  SPDX-FileCopyrightText: 2019-2023 Laurent Montel <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "snippetvariabledialog.h"
11 
12 #include <KConfigGroup>
13 #include <KLocalizedString>
14 #include <KSharedConfig>
15 #include <KWindowConfig>
16 #include <QCheckBox>
17 #include <QDialogButtonBox>
18 #include <QLabel>
19 #include <QMap>
20 #include <QPushButton>
21 #include <QVBoxLayout>
22 #include <QWindow>
23 
24 #include <TextCustomEditor/PlainTextEditorWidget>
25 
26 using namespace MailCommon;
27 namespace
28 {
29 static const char mySnippetVariableDialogConfigGroupName[] = "SnippetVariableDialog";
30 }
31 SnippetVariableDialog::SnippetVariableDialog(const QString &variableName, QMap<QString, QString> *variables, QWidget *parent)
32  : QDialog(parent)
33  , mVariableName(variableName)
34  , mVariables(variables)
35  , mVariableValueText(new TextCustomEditor::PlainTextEditorWidget(this))
36 {
37  setWindowTitle(i18nc("@title:window", "Enter Values for Variables"));
38  auto mainLayout = new QVBoxLayout(this);
39 
40  auto label = new QLabel(i18n("Enter the replacement values for '%1':", variableName), this);
41  mainLayout->addWidget(label);
42 
43  mainLayout->addWidget(mVariableValueText);
44 
45  mSaveVariable = new QCheckBox(i18n("Make value &default"), this);
46  mSaveVariable->setChecked(false);
47  mSaveVariable->setToolTip(i18nc("@info:tooltip",
48  "Enable this to save the value entered to the right "
49  "as the default value for this variable"));
50  mSaveVariable->setWhatsThis(i18nc("@info:whatsthis",
51  "If you enable this option, the value entered to the right will be saved. "
52  "If you use the same variable later, even in another snippet, the value entered "
53  "to the right will be the default value for that variable."));
54  mainLayout->addWidget(mSaveVariable);
55 
56  if (mVariables->contains(variableName)) {
57  mSaveVariable->setChecked(true);
58  mVariableValueText->setPlainText(mVariables->value(variableName));
59  }
60  mVariableValueText->setFocus();
61 
63  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
64  okButton->setDefault(true);
65  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
66  connect(buttonBox, &QDialogButtonBox::accepted, this, &SnippetVariableDialog::slotAccepted);
67  connect(buttonBox, &QDialogButtonBox::rejected, this, &SnippetVariableDialog::reject);
68 
69  mainLayout->addWidget(buttonBox);
70  readConfig();
71 }
72 
73 SnippetVariableDialog::~SnippetVariableDialog()
74 {
75  writeConfig();
76 }
77 
78 QString SnippetVariableDialog::variableValue() const
79 {
80  return mVariableValueText->toPlainText();
81 }
82 
83 bool SnippetVariableDialog::saveVariableIsChecked() const
84 {
85  return mSaveVariable->isChecked();
86 }
87 
88 void SnippetVariableDialog::slotAccepted()
89 {
90  if (mSaveVariable->isChecked()) {
91  mVariables->insert(mVariableName, mVariableValueText->toPlainText());
92  } else {
93  mVariables->remove(mVariableName);
94  }
95 
96  accept();
97 }
98 
99 void SnippetVariableDialog::readConfig()
100 {
101  create(); // ensure a window is created
102  windowHandle()->resize(QSize(300, 350));
103  KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(mySnippetVariableDialogConfigGroupName));
104  KWindowConfig::restoreWindowSize(windowHandle(), group);
105  resize(windowHandle()->size()); // workaround for QTBUG-40584
106 }
107 
108 void SnippetVariableDialog::writeConfig()
109 {
110  KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(mySnippetVariableDialogConfigGroupName));
111  KWindowConfig::saveWindowSize(windowHandle(), group);
112  group.sync();
113 }
114 
115 #include "moc_snippetvariabledialog.cpp"
void setShortcut(const QKeySequence &key)
void readConfig()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QAction * create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent)
QString i18n(const char *text, const TYPE &arg...)
void setWindowTitle(const QString &)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
Key_Return
QString & remove(int position, int n)
QString label(StandardShortcut id)
QString & insert(int position, QChar ch)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setDefault(bool)
The filter dialog.
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 3 2023 04:04:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.