SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / kalarm / templatedlg.cpp
blob7b12827b075a963027107710a50cd5b437c3539a
1 /*
2 * templatedlg.cpp - dialog to create, edit and delete alarm templates
3 * Program: kalarm
4 * Copyright © 2004-2011 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kalarm.h"
22 #include "templatedlg.h"
24 #include "editdlg.h"
25 #include "alarmcalendar.h"
26 #include "functions.h"
27 #include "messagebox.h"
28 #include "newalarmaction.h"
29 #include "shellprocess.h"
30 #include "templatelistview.h"
31 #include "undo.h"
33 #include <KLocalizedString>
34 #include <kguiitem.h>
35 #include <kstandardaction.h>
36 #include <kactioncollection.h>
37 #include <KSeparator>
39 #include <QAction>
40 #include <QPushButton>
41 #include <QVBoxLayout>
42 #include <QHBoxLayout>
43 #include <QBoxLayout>
44 #include <QResizeEvent>
45 #include "kalarm_debug.h"
47 using namespace KCal;
49 static const char TMPL_DIALOG_NAME[] = "TemplateDialog";
52 TemplateDlg* TemplateDlg::mInstance = Q_NULLPTR;
55 TemplateDlg::TemplateDlg(QWidget* parent)
56 : QDialog(parent)
58 setModal(false);
59 setWindowTitle(i18nc("@title:window", "Alarm Templates"));
61 QBoxLayout* topLayout = new QVBoxLayout(this);
63 QBoxLayout* hlayout = new QHBoxLayout(this);
64 topLayout->addLayout(hlayout);
66 QBoxLayout* layout = new QVBoxLayout();
67 layout->setMargin(0);
68 hlayout->addLayout(layout);
69 mListFilterModel = new TemplateListModel(this);
70 if (!ShellProcess::authorised())
71 mListFilterModel->setAlarmActionFilter(static_cast<KAEvent::Actions>(KAEvent::ACT_ALL & ~KAEvent::ACT_COMMAND));
72 mListView = new TemplateListView(this);
73 mListView->setModel(mListFilterModel);
74 mListView->sortByColumn(TemplateListModel::TemplateNameColumn, Qt::AscendingOrder);
75 mListView->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
76 mListView->setWhatsThis(i18nc("@info:whatsthis", "The list of alarm templates"));
77 mListView->setItemDelegate(new TemplateListDelegate(mListView));
78 connect(mListView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TemplateDlg::slotSelectionChanged);
79 layout->addWidget(mListView);
81 layout = new QVBoxLayout();
82 layout->setMargin(0);
83 hlayout->addLayout(layout);
84 QPushButton* button = new QPushButton(i18nc("@action:button", "New"));
85 mNewAction = new NewAlarmAction(true, i18nc("@action", "New"), this);
86 button->setMenu(mNewAction->menu());
87 connect(mNewAction, &NewAlarmAction::selected, this, &TemplateDlg::slotNew);
88 button->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template"));
89 layout->addWidget(button);
91 mEditButton = new QPushButton(i18nc("@action:button", "Edit..."));
92 connect(mEditButton, &QPushButton::clicked, this, &TemplateDlg::slotEdit);
93 mEditButton->setWhatsThis(i18nc("@info:whatsthis", "Edit the currently highlighted alarm template"));
94 layout->addWidget(mEditButton);
96 mCopyButton = new QPushButton(i18nc("@action:button", "Copy"));
97 connect(mCopyButton, &QPushButton::clicked, this, &TemplateDlg::slotCopy);
98 mCopyButton->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template based on a copy of the currently highlighted template"));
99 layout->addWidget(mCopyButton);
101 mDeleteButton = new QPushButton(i18nc("@action:button", "Delete"));
102 connect(mDeleteButton, &QPushButton::clicked, this, &TemplateDlg::slotDelete);
103 mDeleteButton->setWhatsThis(i18nc("@info:whatsthis", "Delete the currently highlighted alarm template"));
104 layout->addWidget(mDeleteButton);
106 layout->addStretch();
108 topLayout->addWidget(new KSeparator(Qt::Horizontal, this));
110 QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
111 buttonBox->addButton(QDialogButtonBox::Close);
112 connect(buttonBox, &QDialogButtonBox::rejected,
113 this, &QDialog::close);
114 topLayout->addWidget(buttonBox);
116 KActionCollection* actions = new KActionCollection(this);
117 QAction* act = KStandardAction::selectAll(mListView, SLOT(selectAll()), actions);
118 topLevelWidget()->addAction(act);
119 act = KStandardAction::deselect(mListView, SLOT(clearSelection()), actions);
120 topLevelWidget()->addAction(act);
121 slotSelectionChanged(); // enable/disable buttons as appropriate
123 QSize s;
124 if (KAlarm::readConfigWindowSize(TMPL_DIALOG_NAME, s))
125 resize(s);
128 /******************************************************************************
129 * Destructor.
131 TemplateDlg::~TemplateDlg()
133 mInstance = Q_NULLPTR;
136 /******************************************************************************
137 * Create an instance, if none already exists.
139 TemplateDlg* TemplateDlg::create(QWidget* parent)
141 if (mInstance)
142 return Q_NULLPTR;
143 mInstance = new TemplateDlg(parent);
144 return mInstance;
147 /******************************************************************************
148 * Called when the New Template button is clicked to create a new template.
150 void TemplateDlg::slotNew(EditAlarmDlg::Type type)
152 KAlarm::editNewTemplate(type, mListView);
155 /******************************************************************************
156 * Called when the Copy button is clicked to edit a copy of an existing alarm,
157 * to add to the list.
159 void TemplateDlg::slotCopy()
161 KAEvent event = mListView->selectedEvent();
162 if (event.isValid())
163 KAlarm::editNewTemplate(&event, mListView);
166 /******************************************************************************
167 * Called when the Modify button is clicked to edit the currently highlighted
168 * alarm in the list.
170 void TemplateDlg::slotEdit()
172 KAEvent event = mListView->selectedEvent();
173 if (event.isValid())
174 KAlarm::editTemplate(&event, mListView);
177 /******************************************************************************
178 * Called when the Delete button is clicked to delete the currently highlighted
179 * alarms in the list.
181 void TemplateDlg::slotDelete()
183 QVector<KAEvent> events = mListView->selectedEvents();
184 int n = events.count();
185 if (KAMessageBox::warningContinueCancel(this, i18ncp("@info", "Do you really want to delete the selected alarm template?",
186 "Do you really want to delete the %1 selected alarm templates?", n),
187 i18ncp("@title:window", "Delete Alarm Template", "Delete Alarm Templates", n),
188 KGuiItem(i18nc("@action:button", "&Delete"), QStringLiteral("edit-delete")))
189 != KMessageBox::Continue)
190 return;
192 KAEvent::List delEvents;
193 Undo::EventList undos;
194 AlarmCalendar* resources = AlarmCalendar::resources();
195 for (int i = 0; i < n; ++i)
197 KAEvent* event = &events[i];
198 delEvents.append(event);
199 Akonadi::Collection c = resources->collectionForEvent(event->itemId());
200 undos.append(*event, c);
202 KAlarm::deleteTemplates(delEvents, this);
203 Undo::saveDeletes(undos);
206 /******************************************************************************
207 * Called when the group of items selected changes.
208 * Enable/disable the buttons depending on whether/how many templates are
209 * currently highlighted.
211 void TemplateDlg::slotSelectionChanged()
213 AlarmCalendar* resources = AlarmCalendar::resources();
214 QVector<KAEvent> events = mListView->selectedEvents();
215 int count = events.count();
216 bool readOnly = false;
217 for (int i = 0; i < count; ++i)
219 const KAEvent* event = &events[i];
220 if (resources->eventReadOnly(event->itemId()))
222 readOnly = true;
223 break;
226 mEditButton->setEnabled(count == 1);
227 mCopyButton->setEnabled(count == 1);
228 mDeleteButton->setEnabled(count && !readOnly);
231 /******************************************************************************
232 * Called when the dialog's size has changed.
233 * Records the new size in the config file.
235 void TemplateDlg::resizeEvent(QResizeEvent* re)
237 if (isVisible())
238 KAlarm::writeConfigWindowSize(TMPL_DIALOG_NAME, re->size());
239 QDialog::resizeEvent(re);
242 // vim: et sw=4: