Pimcommon

templateeditdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "templateeditdialog.h"
8#include <KLineEditEventHandler>
9#include <TextCustomEditor/PlainTextEditor>
10#include <TextCustomEditor/PlainTextEditorWidget>
11
12#include <KLocalizedString>
13#include <QLineEdit>
14
15#include <KConfigGroup>
16#include <KSharedConfig>
17#include <KWindowConfig>
18#include <QDialogButtonBox>
19#include <QHBoxLayout>
20#include <QLabel>
21#include <QPushButton>
22#include <QVBoxLayout>
23#include <QWindow>
24
25using namespace PimCommon;
26namespace
27{
28static const char myTemplateEditDialogConfigGroupName[] = "TemplateEditDialog";
29}
30TemplateEditDialog::TemplateEditDialog(QWidget *parent, bool defaultTemplate)
31 : QDialog(parent)
32 , mTextEdit(new TextCustomEditor::PlainTextEditorWidget(this))
33 , mTemplateNameEdit(new QLineEdit(this))
34{
35 setWindowTitle(defaultTemplate ? i18nc("@title:window", "Default template") : i18nc("@title:window", "Template"));
36 auto mainLayout = new QVBoxLayout(this);
37 QDialogButtonBox *buttonBox = nullptr;
38 if (defaultTemplate) {
39 buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
40 connect(buttonBox, &QDialogButtonBox::rejected, this, &TemplateEditDialog::reject);
41 } else {
43 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
44 mOkButton->setDefault(true);
45 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
46 connect(buttonBox, &QDialogButtonBox::accepted, this, &TemplateEditDialog::accept);
47 connect(buttonBox, &QDialogButtonBox::rejected, this, &TemplateEditDialog::reject);
48 mOkButton->setFocus();
49 }
50 auto w = new QWidget;
51 mainLayout->addWidget(w);
52 mainLayout->addWidget(buttonBox);
53
54 auto vbox = new QVBoxLayout;
55 vbox->setContentsMargins({});
56
57 auto hbox = new QHBoxLayout;
58 auto label = new QLabel(i18nc("@label:textbox", "Name:"));
59 hbox->addWidget(label);
60
62 mTemplateNameEdit->setClearButtonEnabled(!defaultTemplate);
63 mTemplateNameEdit->setReadOnly(defaultTemplate);
64 hbox->addWidget(mTemplateNameEdit);
65
66 vbox->addLayout(hbox);
67
68 mTextEdit->setReadOnly(defaultTemplate);
69 vbox->addWidget(mTextEdit);
70
71 w->setLayout(vbox);
72 if (!defaultTemplate) {
73 if (mOkButton) {
74 mOkButton->setEnabled(false);
75 }
76 connect(mTemplateNameEdit, &QLineEdit::textChanged, this, &TemplateEditDialog::slotTemplateChanged);
77 connect(mTextEdit->editor(), &QPlainTextEdit::textChanged, this, &TemplateEditDialog::slotTemplateChanged);
78 mTemplateNameEdit->setFocus();
79 }
80 readConfig();
81}
82
83TemplateEditDialog::~TemplateEditDialog()
84{
85 writeConfig();
86}
87
88void TemplateEditDialog::readConfig()
89{
90 create(); // ensure a window is created
91 windowHandle()->resize(QSize(300, 200));
92 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myTemplateEditDialogConfigGroupName));
94 resize(windowHandle()->size()); // workaround for QTBUG-40584
95}
96
97void TemplateEditDialog::writeConfig()
98{
99 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myTemplateEditDialogConfigGroupName));
101}
102
103void TemplateEditDialog::slotTemplateChanged()
104{
105 mOkButton->setEnabled(!mTemplateNameEdit->text().trimmed().isEmpty() && !mTextEdit->editor()->toPlainText().trimmed().isEmpty());
106}
107
108void TemplateEditDialog::setScript(const QString &text)
109{
110 mTextEdit->setPlainText(text);
111}
112
113QString TemplateEditDialog::script() const
114{
115 return mTextEdit->toPlainText();
116}
117
118void TemplateEditDialog::setTemplateName(const QString &name)
119{
120 mTemplateNameEdit->setText(name);
121}
122
123QString TemplateEditDialog::templateName() const
124{
125 return mTemplateNameEdit->text();
126}
127
128#include "moc_templateeditdialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
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)
folderdialogacltab.h
QPushButton * button(StandardButton which) const const
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
void textChanged()
void setDefault(bool)
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void setEnabled(bool)
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 Mon Nov 4 2024 16:39:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.