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-2022 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 <QCheckBox>
16 #include <QDialogButtonBox>
17 #include <QLabel>
18 #include <QMap>
19 #include <QPushButton>
20 #include <QVBoxLayout>
21 
22 #include <KPIMTextEdit/PlainTextEditorWidget>
23 
24 using namespace MailCommon;
25 namespace
26 {
27 static const char mySnippetVariableDialogConfigGroupName[] = "SnippetVariableDialog";
28 }
29 SnippetVariableDialog::SnippetVariableDialog(const QString &variableName, QMap<QString, QString> *variables, QWidget *parent)
30  : QDialog(parent)
31  , mVariableName(variableName)
32  , mVariables(variables)
33  , mVariableValueText(new KPIMTextEdit::PlainTextEditorWidget(this))
34 {
35  setWindowTitle(i18nc("@title:window", "Enter Values for Variables"));
36  auto mainLayout = new QVBoxLayout(this);
37 
38  auto label = new QLabel(i18n("Enter the replacement values for '%1':", variableName), this);
39  mainLayout->addWidget(label);
40 
41  mainLayout->addWidget(mVariableValueText);
42 
43  mSaveVariable = new QCheckBox(i18n("Make value &default"), this);
44  mSaveVariable->setChecked(false);
45  mSaveVariable->setToolTip(i18nc("@info:tooltip",
46  "Enable this to save the value entered to the right "
47  "as the default value for this variable"));
48  mSaveVariable->setWhatsThis(i18nc("@info:whatsthis",
49  "If you enable this option, the value entered to the right will be saved. "
50  "If you use the same variable later, even in another snippet, the value entered "
51  "to the right will be the default value for that variable."));
52  mainLayout->addWidget(mSaveVariable);
53 
54  if (mVariables->contains(variableName)) {
55  mSaveVariable->setChecked(true);
56  mVariableValueText->setPlainText(mVariables->value(variableName));
57  }
58  mVariableValueText->setFocus();
59 
61  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
62  okButton->setDefault(true);
63  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
64  connect(buttonBox, &QDialogButtonBox::accepted, this, &SnippetVariableDialog::slotAccepted);
65  connect(buttonBox, &QDialogButtonBox::rejected, this, &SnippetVariableDialog::reject);
66 
67  mainLayout->addWidget(buttonBox);
68  readConfig();
69 }
70 
71 SnippetVariableDialog::~SnippetVariableDialog()
72 {
73  writeConfig();
74 }
75 
76 QString SnippetVariableDialog::variableValue() const
77 {
78  return mVariableValueText->toPlainText();
79 }
80 
81 bool SnippetVariableDialog::saveVariableIsChecked() const
82 {
83  return mSaveVariable->isChecked();
84 }
85 
86 void SnippetVariableDialog::slotAccepted()
87 {
88  if (mSaveVariable->isChecked()) {
89  mVariables->insert(mVariableName, mVariableValueText->toPlainText());
90  } else {
91  mVariables->remove(mVariableName);
92  }
93 
94  accept();
95 }
96 
97 void SnippetVariableDialog::writeConfig()
98 {
99  KConfigGroup group(KSharedConfig::openStateConfig(), mySnippetVariableDialogConfigGroupName);
100  group.writeEntry("Size", size());
101 }
102 
103 void SnippetVariableDialog::readConfig()
104 {
105  KConfigGroup group(KSharedConfig::openStateConfig(), mySnippetVariableDialogConfigGroupName);
106  const QSize sizeDialog = group.readEntry("Size", QSize(300, 350));
107  if (sizeDialog.isValid()) {
108  resize(sizeDialog);
109  }
110 }
bool isValid() const const
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)
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)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setDefault(bool)
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.