Mailcommon

snippetvariabledialog.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
3 SPDX-FileContributor: Tobias Koenig <tokoe@kdab.com>
4
5 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
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
26using namespace MailCommon;
27namespace
28{
29static const char mySnippetVariableDialogConfigGroupName[] = "SnippetVariableDialog";
30}
31SnippetVariableDialog::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(i18nc("@label:textbox", "Enter the replacement values for '%1':", variableName), this);
41 mainLayout->addWidget(label);
42
43 mainLayout->addWidget(mVariableValueText);
44
45 mSaveVariable = new QCheckBox(i18nc("@option:check", "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);
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
73SnippetVariableDialog::~SnippetVariableDialog()
74{
75 writeConfig();
76}
77
78QString SnippetVariableDialog::variableValue() const
79{
80 return mVariableValueText->toPlainText();
81}
82
83bool SnippetVariableDialog::saveVariableIsChecked() const
84{
85 return mSaveVariable->isChecked();
86}
87
88void 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
99void SnippetVariableDialog::readConfig()
100{
101 create(); // ensure a window is created
102 windowHandle()->resize(QSize(300, 350));
103 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySnippetVariableDialogConfigGroupName));
105 resize(windowHandle()->size()); // workaround for QTBUG-40584
106}
107
108void SnippetVariableDialog::writeConfig()
109{
110 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySnippetVariableDialogConfigGroupName));
112 group.sync();
113}
114
115#include "moc_snippetvariabledialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
The filter dialog.
bool isChecked() const const
void setShortcut(const QKeySequence &key)
virtual void accept()
iterator insert(const Key &key, const T &value)
size_type remove(const Key &key)
void setDefault(bool)
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.