Incidenceeditor

templatemanagementdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2005 Till Adam <adam@kde.org>
3 SPDX-FileCopyrightText: 2009 Allen Winter <winter@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "templatemanagementdialog.h"
9using namespace Qt::Literals::StringLiterals;
10
11#include <KLocalizedString>
12#include <KMessageBox>
13#include <KStandardGuiItem>
14#include <QInputDialog>
15
16#include <QDesktopServices>
17#include <QDialogButtonBox>
18#include <QPushButton>
19#include <QTimer>
20#include <QUrlQuery>
21#include <QVBoxLayout>
22
23using namespace IncidenceEditorNG;
24
25TemplateManagementDialog::TemplateManagementDialog(QWidget *parent, const QStringList &templates, const QString &incidenceType)
26 : QDialog(parent)
27 , m_templates(templates)
28 , m_type(incidenceType)
29{
30 QString m_type_translated = i18n(qPrintable(m_type));
31 setWindowTitle(i18nc("@title:window", "Manage %1 Templates", m_type_translated));
33 auto mainLayout = new QVBoxLayout(this);
34 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
35 okButton->setDefault(true);
37 connect(buttonBox, &QDialogButtonBox::rejected, this, &TemplateManagementDialog::reject);
38 setObjectName("template_management_dialog"_L1);
39 connect(buttonBox->button(QDialogButtonBox::Help), &QPushButton::clicked, this, &TemplateManagementDialog::slotHelp);
40 auto widget = new QWidget(this);
41 mainLayout->addWidget(widget);
42 mainLayout->addWidget(buttonBox);
43
44 widget->setObjectName("template_management_dialog_base"_L1);
45 m_base.setupUi(widget);
46
47 m_base.m_listBox->addItems(m_templates);
48 m_base.m_listBox->setSelectionMode(QAbstractItemView::SingleSelection);
49
50 connect(m_base.m_buttonAdd, &QPushButton::clicked, this, &TemplateManagementDialog::slotAddTemplate);
51 connect(m_base.m_buttonRemove, &QPushButton::clicked, this, &TemplateManagementDialog::slotRemoveTemplate);
52 connect(m_base.m_buttonApply, &QPushButton::clicked, this, &TemplateManagementDialog::slotApplyTemplate);
53
54 connect(m_base.m_listBox, &QListWidget::itemSelectionChanged, this, &TemplateManagementDialog::slotItemSelected);
55 connect(m_base.m_listBox, &QListWidget::itemDoubleClicked, this, &TemplateManagementDialog::slotApplyTemplate);
56 connect(okButton, &QPushButton::clicked, this, &TemplateManagementDialog::slotOk);
57
58 m_base.m_buttonRemove->setEnabled(false);
59 m_base.m_buttonApply->setEnabled(false);
60}
61
62void TemplateManagementDialog::slotHelp()
63{
64 QUrl url = QUrl(QStringLiteral("help:/")).resolved(QUrl(QStringLiteral("korganizer/entering-data.html")));
65 QUrlQuery query(url);
66 query.addQueryItem(QStringLiteral("anchor"), QStringLiteral("entering-data-events-template-buttons"));
67 url.setQuery(query);
68 // launch khelpcenter, or a browser for URIs not handled by khelpcenter
70}
71
72void TemplateManagementDialog::slotItemSelected()
73{
74 m_base.m_buttonRemove->setEnabled(true);
75 m_base.m_buttonApply->setEnabled(true);
76}
77
78void TemplateManagementDialog::slotAddTemplate()
79{
80 bool ok;
81 bool duplicate = false;
82 QString m_type_translated = i18n(qPrintable(m_type));
83 const QString newTemplate = QInputDialog::getText(this,
84 i18n("Template Name"),
85 i18n("Please enter a name for the new template:"),
87 i18n("New %1 Template", m_type_translated),
88 &ok);
89 if (newTemplate.isEmpty() || !ok) {
90 return;
91 }
92
93 if (m_templates.contains(newTemplate)) {
95 i18n("A template with that name already exists, do you want to overwrite it?"),
96 i18nc("@title:window", "Duplicate Template Name"),
98 if (rc == KMessageBox::Cancel) {
99 QTimer::singleShot(0, this, &TemplateManagementDialog::slotAddTemplate);
100 return;
101 }
102 duplicate = true;
103 }
104
105 if (!duplicate) {
106 int count = m_base.m_listBox->count();
107 m_templates.append(newTemplate);
108 m_base.m_listBox->addItem(newTemplate);
109 QListWidgetItem *item = m_base.m_listBox->item(count);
110 item->setSelected(true);
111 }
112 m_newTemplate = newTemplate;
113 m_changed = true;
114
115 // From this point on we need to keep the original event around until the
116 // user has closed the dialog, applying a template would make little sense
117 // buttonBox->button(QDialogButtonBox::Apply)->setEnabled( false );
118 // neither does adding it again
119 m_base.m_buttonAdd->setEnabled(false);
120}
121
122void TemplateManagementDialog::slotRemoveTemplate()
123{
124 QListWidgetItem *const item = m_base.m_listBox->selectedItems().first();
125 if (!item) {
126 return; // can't happen (TM)
127 }
128
130 i18n("Are you sure that you want to remove the template <b>%1</b>?", item->text()),
131 i18nc("@title:window", "Remove Template"),
133
134 if (rc == KMessageBox::Cancel) {
135 return;
136 }
137
138 int current = m_base.m_listBox->row(item);
139
140 m_templates.removeAll(item->text());
141 m_base.m_listBox->takeItem(current);
142 QListWidgetItem *newItem = m_base.m_listBox->item(qMax(current - 1, 0));
143 if (newItem) {
144 newItem->setSelected(true);
145 }
146
147 updateButtons();
148
149 m_changed = true;
150}
151
152void TemplateManagementDialog::updateButtons()
153{
154 m_base.m_buttonAdd->setEnabled(true);
155 const bool isNotEmpty = m_base.m_listBox->count() != 0;
156 m_base.m_buttonRemove->setEnabled(isNotEmpty);
157 m_base.m_buttonApply->setEnabled(isNotEmpty);
158}
159
160void TemplateManagementDialog::slotApplyTemplate()
161{
162 // Once the user has applied the current template to the event,
163 // it makes no sense to add it again
164 m_base.m_buttonAdd->setEnabled(false);
165 QListWidgetItem *item = m_base.m_listBox->currentItem();
166 if (item) {
167 const QString &cur = item->text();
168 if (!cur.isEmpty() && cur != m_newTemplate) {
169 Q_EMIT loadTemplate(cur);
170 slotOk();
171 }
172 }
173}
174
175void TemplateManagementDialog::slotOk()
176{
177 // failure is not an option *cough*
178 if (!m_newTemplate.isEmpty()) {
179 Q_EMIT saveTemplate(m_newTemplate);
180 }
181 if (m_changed) {
182 Q_EMIT templatesChanged(m_templates);
183 }
184 accept();
185}
186
187#include "moc_templatemanagementdialog.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
std::optional< QSqlQuery > query(const QString &queryStatement)
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &title=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem remove()
KGuiItem overwrite()
void clicked(bool checked)
void setShortcut(const QKeySequence &key)
bool openUrl(const QUrl &url)
virtual void accept()
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
void append(QList< T > &&value)
qsizetype removeAll(const AT &t)
void itemDoubleClicked(QListWidgetItem *item)
void itemSelectionChanged()
void setSelected(bool select)
QString text() const const
Q_EMITQ_EMIT
void setDefault(bool)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl resolved(const QUrl &relative) const const
void setQuery(const QString &query, ParsingMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.