2 * templatedlg.cpp - dialog to create, edit and delete alarm templates
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.
22 #include "templatedlg.moc"
25 #include "alarmcalendar.h"
27 #include "alarmresources.h"
28 #include "eventlistmodel.h"
29 #include "templatelistfiltermodel.h"
31 #include "functions.h"
32 #include "messagebox.h"
33 #include "newalarmaction.h"
34 #include "shellprocess.h"
35 #include "templatelistview.h"
41 #include <kstandardaction.h>
42 #include <kactioncollection.h>
46 #include <QPushButton>
48 #include <QVBoxLayout>
49 #include <QHBoxLayout>
51 #include <QResizeEvent>
55 static const char TMPL_DIALOG_NAME
[] = "TemplateDialog";
58 TemplateDlg
* TemplateDlg::mInstance
= 0;
61 TemplateDlg::TemplateDlg(QWidget
* parent
)
64 QWidget
* topWidget
= new QWidget(this);
65 setMainWidget(topWidget
);
67 setDefaultButton(Close
);
69 setCaption(i18nc("@title:window", "Alarm Templates"));
70 showButtonSeparator(true);
71 QBoxLayout
* topLayout
= new QHBoxLayout(topWidget
);
72 topLayout
->setMargin(0);
73 topLayout
->setSpacing(spacingHint());
75 QBoxLayout
* layout
= new QVBoxLayout();
77 topLayout
->addLayout(layout
);
79 mListFilterModel
= new TemplateListModel(this);
80 if (!ShellProcess::authorised())
81 mListFilterModel
->setAlarmActionFilter(static_cast<KAEvent::Actions
>(KAEvent::ACT_ALL
& ~KAEvent::ACT_COMMAND
));
83 mListFilterModel
= new TemplateListFilterModel(EventListModel::templates());
84 if (!ShellProcess::authorised())
85 mListFilterModel
->setTypeFilter(static_cast<KAEvent::Actions
>(KAEvent::ACT_ALL
& ~KAEvent::ACT_COMMAND
));
87 mListView
= new TemplateListView(topWidget
);
88 mListView
->setModel(mListFilterModel
);
90 mListView
->sortByColumn(TemplateListModel::TemplateNameColumn
, Qt::AscendingOrder
);
92 mListView
->sortByColumn(TemplateListFilterModel::TemplateNameColumn
, Qt::AscendingOrder
);
94 mListView
->setSizePolicy(QSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Expanding
));
95 mListView
->setWhatsThis(i18nc("@info:whatsthis", "The list of alarm templates"));
96 mListView
->setItemDelegate(new TemplateListDelegate(mListView
));
97 connect(mListView
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
,QItemSelection
)), SLOT(slotSelectionChanged()));
98 layout
->addWidget(mListView
);
100 layout
= new QVBoxLayout();
101 layout
->setMargin(0);
102 topLayout
->addLayout(layout
);
103 QPushButton
* button
= new QPushButton(i18nc("@action:button", "New"), topWidget
);
104 mNewAction
= new NewAlarmAction(true, i18nc("@action", "New"), this);
105 button
->setMenu(mNewAction
->menu());
106 connect(mNewAction
, SIGNAL(selected(EditAlarmDlg::Type
)), SLOT(slotNew(EditAlarmDlg::Type
)));
107 button
->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template"));
108 layout
->addWidget(button
);
110 mEditButton
= new QPushButton(i18nc("@action:button", "Edit..."), topWidget
);
111 connect(mEditButton
, SIGNAL(clicked()), SLOT(slotEdit()));
112 mEditButton
->setWhatsThis(i18nc("@info:whatsthis", "Edit the currently highlighted alarm template"));
113 layout
->addWidget(mEditButton
);
115 mCopyButton
= new QPushButton(i18nc("@action:button", "Copy"), topWidget
);
116 connect(mCopyButton
, SIGNAL(clicked()), SLOT(slotCopy()));
117 mCopyButton
->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template based on a copy of the currently highlighted template"));
118 layout
->addWidget(mCopyButton
);
120 mDeleteButton
= new QPushButton(i18nc("@action:button", "Delete"), topWidget
);
121 connect(mDeleteButton
, SIGNAL(clicked()), SLOT(slotDelete()));
122 mDeleteButton
->setWhatsThis(i18nc("@info:whatsthis", "Delete the currently highlighted alarm template"));
123 layout
->addWidget(mDeleteButton
);
125 KActionCollection
* actions
= new KActionCollection(this);
126 KAction
* act
= KStandardAction::selectAll(mListView
, SLOT(selectAll()), actions
);
127 topLevelWidget()->addAction(act
);
128 act
= KStandardAction::deselect(mListView
, SLOT(clearSelection()), actions
);
129 topLevelWidget()->addAction(act
);
131 slotSelectionChanged(); // enable/disable buttons as appropriate
134 if (KAlarm::readConfigWindowSize(TMPL_DIALOG_NAME
, s
))
138 /******************************************************************************
141 TemplateDlg::~TemplateDlg()
146 /******************************************************************************
147 * Create an instance, if none already exists.
149 TemplateDlg
* TemplateDlg::create(QWidget
* parent
)
153 mInstance
= new TemplateDlg(parent
);
157 /******************************************************************************
158 * Called when the New Template button is clicked to create a new template.
160 void TemplateDlg::slotNew(EditAlarmDlg::Type type
)
162 KAlarm::editNewTemplate(type
, mListView
);
165 /******************************************************************************
166 * Called when the Copy button is clicked to edit a copy of an existing alarm,
167 * to add to the list.
169 void TemplateDlg::slotCopy()
172 KAEvent event
= mListView
->selectedEvent();
174 KAlarm::editNewTemplate(&event
, mListView
);
176 KAEvent
* event
= mListView
->selectedEvent();
178 KAlarm::editNewTemplate(event
, mListView
);
182 /******************************************************************************
183 * Called when the Modify button is clicked to edit the currently highlighted
186 void TemplateDlg::slotEdit()
189 KAEvent event
= mListView
->selectedEvent();
191 KAlarm::editTemplate(&event
, mListView
);
193 KAEvent
* event
= mListView
->selectedEvent();
195 KAlarm::editTemplate(event
, mListView
);
199 /******************************************************************************
200 * Called when the Delete button is clicked to delete the currently highlighted
201 * alarms in the list.
203 void TemplateDlg::slotDelete()
206 QVector
<KAEvent
> events
= mListView
->selectedEvents();
208 KAEvent::List events
= mListView
->selectedEvents();
210 int n
= events
.count();
211 if (KAMessageBox::warningContinueCancel(this, i18ncp("@info", "Do you really want to delete the selected alarm template?",
212 "Do you really want to delete the %1 selected alarm templates?", n
),
213 i18ncp("@title:window", "Delete Alarm Template", "Delete Alarm Templates", n
),
214 KGuiItem(i18nc("@action:button", "&Delete"), "edit-delete"))
215 != KMessageBox::Continue
)
219 KAEvent::List delEvents
;
221 QStringList delEvents
;
223 Undo::EventList undos
;
224 AlarmCalendar
* resources
= AlarmCalendar::resources();
225 for (int i
= 0; i
< n
; ++i
)
228 KAEvent
* event
= &events
[i
];
229 delEvents
.append(event
);
230 Akonadi::Collection c
= resources
->collectionForEvent(event
->itemId());
231 undos
.append(*event
, c
);
233 const KAEvent
* event
= events
[i
];
234 delEvents
.append(event
->id());
235 undos
.append(*event
, resources
->resourceForEvent(event
->id()));
238 KAlarm::deleteTemplates(delEvents
, this);
239 Undo::saveDeletes(undos
);
242 /******************************************************************************
243 * Called when the group of items selected changes.
244 * Enable/disable the buttons depending on whether/how many templates are
245 * currently highlighted.
247 void TemplateDlg::slotSelectionChanged()
249 AlarmCalendar
* resources
= AlarmCalendar::resources();
251 QVector
<KAEvent
> events
= mListView
->selectedEvents();
253 KAEvent::List events
= mListView
->selectedEvents();
255 int count
= events
.count();
256 bool readOnly
= false;
257 for (int i
= 0; i
< count
; ++i
)
260 const KAEvent
* event
= &events
[i
];
261 if (resources
->eventReadOnly(event
->itemId()))
263 const KAEvent
* event
= events
[i
];
264 if (resources
->eventReadOnly(event
->id()))
271 mEditButton
->setEnabled(count
== 1);
272 mCopyButton
->setEnabled(count
== 1);
273 mDeleteButton
->setEnabled(count
&& !readOnly
);
276 /******************************************************************************
277 * Called when the dialog's size has changed.
278 * Records the new size in the config file.
280 void TemplateDlg::resizeEvent(QResizeEvent
* re
)
283 KAlarm::writeConfigWindowSize(TMPL_DIALOG_NAME
, re
->size());
284 KDialog::resizeEvent(re
);