2 * templatepickdlg.cpp - dialog to choose an alarm template
4 * Copyright © 2004,2006-2010 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 "templatepickdlg.moc"
25 #include "eventlistmodel.h"
26 #include "templatelistfiltermodel.h"
28 #include "functions.h"
29 #include "shellprocess.h"
30 #include "templatelistview.h"
35 #include <QVBoxLayout>
36 #include <QResizeEvent>
38 static const char TMPL_PICK_DIALOG_NAME
[] = "TemplatePickDialog";
41 TemplatePickDlg::TemplatePickDlg(KAEvent::Actions type
, QWidget
* parent
)
44 QWidget
* topWidget
= new QWidget(this);
45 setMainWidget(topWidget
);
46 setCaption(i18nc("@title:window", "Choose Alarm Template"));
47 setButtons(Ok
|Cancel
);
49 QVBoxLayout
* topLayout
= new QVBoxLayout(topWidget
);
50 topLayout
->setMargin(0);
51 topLayout
->setSpacing(spacingHint());
53 // Display the list of templates, but exclude command alarms if in kiosk mode.
54 KAEvent::Actions shown
= KAEvent::ACT_ALL
;
55 if (!ShellProcess::authorised())
57 type
= static_cast<KAEvent::Actions
>(type
& ~KAEvent::ACT_COMMAND
);
58 shown
= static_cast<KAEvent::Actions
>(shown
& ~KAEvent::ACT_COMMAND
);
61 mListFilterModel
= new TemplateListModel(this);
62 mListFilterModel
->setAlarmActionsEnabled(type
);
63 mListFilterModel
->setAlarmActionFilter(shown
);
65 mListFilterModel
= new TemplateListFilterModel(EventListModel::templates());
66 mListFilterModel
->setTypesEnabled(type
);
67 mListFilterModel
->setTypeFilter(shown
);
69 mListView
= new TemplateListView(topWidget
);
70 mListView
->setModel(mListFilterModel
);
72 mListView
->sortByColumn(TemplateListModel::TemplateNameColumn
, Qt::AscendingOrder
);
74 mListView
->sortByColumn(TemplateListFilterModel::TemplateNameColumn
, Qt::AscendingOrder
);
76 mListView
->setSelectionMode(QAbstractItemView::SingleSelection
);
77 mListView
->setWhatsThis(i18nc("@info:whatsthis", "Select a template to base the new alarm on."));
78 connect(mListView
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
,QItemSelection
)), SLOT(slotSelectionChanged()));
79 // Require a real double click (even if KDE is in single-click mode) to accept the selection
80 connect(mListView
, SIGNAL(doubleClicked(QModelIndex
)), SLOT(accept()));
81 topLayout
->addWidget(mListView
);
83 slotSelectionChanged(); // enable or disable the OK button
86 if (KAlarm::readConfigWindowSize(TMPL_PICK_DIALOG_NAME
, s
))
90 /******************************************************************************
91 * Return the currently selected alarm template, or 0 if none.
94 KAEvent
TemplatePickDlg::selectedTemplate() const
96 const KAEvent
* TemplatePickDlg::selectedTemplate() const
99 return mListView
->selectedEvent();
102 /******************************************************************************
103 * Called when the template selection changes.
104 * Enable/disable the OK button depending on whether anything is selected.
106 void TemplatePickDlg::slotSelectionChanged()
108 bool enable
= !mListView
->selectionModel()->selectedRows().isEmpty();
110 enable
= mListView
->model()->flags(mListView
->selectedIndex()) & Qt::ItemIsEnabled
;
111 enableButtonOk(enable
);
114 /******************************************************************************
115 * Called when the dialog's size has changed.
116 * Records the new size in the config file.
118 void TemplatePickDlg::resizeEvent(QResizeEvent
* re
)
121 KAlarm::writeConfigWindowSize(TMPL_PICK_DIALOG_NAME
, re
->size());
122 KDialog::resizeEvent(re
);