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"
9
10#include <KLocalizedString>
11#include <KMessageBox>
12#include <KStandardGuiItem>
13#include <QInputDialog>
14
15#include <QDesktopServices>
16#include <QDialogButtonBox>
17#include <QPushButton>
18#include <QTimer>
19#include <QUrlQuery>
20#include <QVBoxLayout>
21
22using namespace IncidenceEditorNG;
23
24TemplateManagementDialog::TemplateManagementDialog(QWidget *parent, const QStringList &templates, const QString &incidenceType)
25 : QDialog(parent)
26 , m_templates(templates)
27 , m_type(incidenceType)
28{
29 QString m_type_translated = i18n(qPrintable(m_type));
30 setWindowTitle(i18nc("@title:window", "Manage %1 Templates", m_type_translated));
32 auto mainLayout = new QVBoxLayout(this);
33 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
34 okButton->setDefault(true);
36 connect(buttonBox, &QDialogButtonBox::rejected, this, &TemplateManagementDialog::reject);
37 setObjectName(QLatin1StringView("template_management_dialog"));
38 connect(buttonBox->button(QDialogButtonBox::Help), &QPushButton::clicked, this, &TemplateManagementDialog::slotHelp);
39 auto widget = new QWidget(this);
40 mainLayout->addWidget(widget);
41 mainLayout->addWidget(buttonBox);
42
43 widget->setObjectName(QLatin1StringView("template_management_dialog_base"));
44 m_base.setupUi(widget);
45
46 m_base.m_listBox->addItems(m_templates);
47 m_base.m_listBox->setSelectionMode(QAbstractItemView::SingleSelection);
48
49 connect(m_base.m_buttonAdd, &QPushButton::clicked, this, &TemplateManagementDialog::slotAddTemplate);
50 connect(m_base.m_buttonRemove, &QPushButton::clicked, this, &TemplateManagementDialog::slotRemoveTemplate);
51 connect(m_base.m_buttonApply, &QPushButton::clicked, this, &TemplateManagementDialog::slotApplyTemplate);
52
53 connect(m_base.m_listBox, &QListWidget::itemSelectionChanged, this, &TemplateManagementDialog::slotItemSelected);
54 connect(m_base.m_listBox, &QListWidget::itemDoubleClicked, this, &TemplateManagementDialog::slotApplyTemplate);
55 connect(okButton, &QPushButton::clicked, this, &TemplateManagementDialog::slotOk);
56
57 m_base.m_buttonRemove->setEnabled(false);
58 m_base.m_buttonApply->setEnabled(false);
59}
60
61void TemplateManagementDialog::slotHelp()
62{
63 QUrl url = QUrl(QStringLiteral("help:/")).resolved(QUrl(QStringLiteral("korganizer/entering-data.html")));
64 QUrlQuery query(url);
65 query.addQueryItem(QStringLiteral("anchor"), QStringLiteral("entering-data-events-template-buttons"));
66 url.setQuery(query);
67 // launch khelpcenter, or a browser for URIs not handled by khelpcenter
69}
70
71void TemplateManagementDialog::slotItemSelected()
72{
73 m_base.m_buttonRemove->setEnabled(true);
74 m_base.m_buttonApply->setEnabled(true);
75}
76
77void TemplateManagementDialog::slotAddTemplate()
78{
79 bool ok;
80 bool duplicate = false;
81 QString m_type_translated = i18n(qPrintable(m_type));
82 const QString newTemplate = QInputDialog::getText(this,
83 i18n("Template Name"),
84 i18n("Please enter a name for the new template:"),
86 i18n("New %1 Template", m_type_translated),
87 &ok);
88 if (newTemplate.isEmpty() || !ok) {
89 return;
90 }
91
92 if (m_templates.contains(newTemplate)) {
94 i18n("A template with that name already exists, do you want to overwrite it?"),
95 i18nc("@title:window", "Duplicate Template Name"),
97 if (rc == KMessageBox::Cancel) {
98 QTimer::singleShot(0, this, &TemplateManagementDialog::slotAddTemplate);
99 return;
100 }
101 duplicate = true;
102 }
103
104 if (!duplicate) {
105 int count = m_base.m_listBox->count();
106 m_templates.append(newTemplate);
107 m_base.m_listBox->addItem(newTemplate);
108 QListWidgetItem *item = m_base.m_listBox->item(count);
109 item->setSelected(true);
110 }
111 m_newTemplate = newTemplate;
112 m_changed = true;
113
114 // From this point on we need to keep the original event around until the
115 // user has closed the dialog, applying a template would make little sense
116 // buttonBox->button(QDialogButtonBox::Apply)->setEnabled( false );
117 // neither does adding it again
118 m_base.m_buttonAdd->setEnabled(false);
119}
120
121void TemplateManagementDialog::slotRemoveTemplate()
122{
123 QListWidgetItem *const item = m_base.m_listBox->selectedItems().first();
124 if (!item) {
125 return; // can't happen (TM)
126 }
127
129 i18n("Are you sure that you want to remove the template <b>%1</b>?", item->text()),
130 i18nc("@title:window", "Remove Template"),
132
133 if (rc == KMessageBox::Cancel) {
134 return;
135 }
136
137 int current = m_base.m_listBox->row(item);
138
139 m_templates.removeAll(item->text());
140 m_base.m_listBox->takeItem(current);
141 QListWidgetItem *newItem = m_base.m_listBox->item(qMax(current - 1, 0));
142 if (newItem) {
143 newItem->setSelected(true);
144 }
145
146 updateButtons();
147
148 m_changed = true;
149}
150
151void TemplateManagementDialog::updateButtons()
152{
153 m_base.m_buttonAdd->setEnabled(true);
154 const bool isNotEmpty = m_base.m_listBox->count() != 0;
155 m_base.m_buttonRemove->setEnabled(isNotEmpty);
156 m_base.m_buttonApply->setEnabled(isNotEmpty);
157}
158
159void TemplateManagementDialog::slotApplyTemplate()
160{
161 // Once the user has applied the current template to the event,
162 // it makes no sense to add it again
163 m_base.m_buttonAdd->setEnabled(false);
164 QListWidgetItem *item = m_base.m_listBox->currentItem();
165 if (item) {
166 const QString &cur = item->text();
167 if (!cur.isEmpty() && cur != m_newTemplate) {
168 Q_EMIT loadTemplate(cur);
169 slotOk();
170 }
171 }
172}
173
174void TemplateManagementDialog::slotOk()
175{
176 // failure is not an option *cough*
177 if (!m_newTemplate.isEmpty()) {
178 Q_EMIT saveTemplate(m_newTemplate);
179 }
180 if (m_changed) {
181 Q_EMIT templatesChanged(m_templates);
182 }
183 accept();
184}
185
186#include "moc_templatemanagementdialog.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
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
T qobject_cast(QObject *object)
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 Tue Mar 26 2024 11:19:37 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.