2
* Copyright 2013 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Zsombor Egri <zsombor.egri@canonical.com>
19
#include "ucalarmmodel.h"
21
#include "ucalarm_p.h"
22
#include "alarmmanager_p.h"
26
* \instantiates UCAlarmModel
27
* \inqmlmodule Ubuntu.Components 0.1
28
* \ingroup ubuntu-services
29
* \brief AlarmModel holds the list of alarms defined.
31
* The AlarmModel is a simple container of \l Alarm definitions stored in the alarm
32
* collection. The data provided by the model are read only, adding, modifying or
33
* removing data is only possible through \l Alarm functions.
35
* The number of alarm events can be obtained from the \l count property. To get
36
* a specific alarm event data from the model, use the \l get function.
41
* import Ubuntu.Components 0.1
42
* import Ubuntu.Components.ListItems 0.1
44
* model: AlarmModel {}
46
* height: units.gu(100)
47
* delegate: Subtitled {
49
* subText: Qt.formatDateTime(date)
55
UCAlarmModel::UCAlarmModel(QObject *parent) :
56
QAbstractListModel(parent)
58
// keep in sync with alarms collection changes
59
connect(&AlarmManager::instance(), SIGNAL(alarmsChanged()), this, SLOT(refresh()));
62
int UCAlarmModel::rowCount(const QModelIndex &parent) const
65
return AlarmManager::instance().alarms().count();
67
QVariant UCAlarmModel::data(const QModelIndex &index, int role) const
69
if (!index.isValid()) {
72
int idx = index.row();
73
const QList<AlarmData> alarms = AlarmManager::instance().alarms();
74
if ((idx >= alarms.count()) || (idx < 0)) {
77
return alarms[idx].roleData(role);
80
QHash<int, QByteArray> UCAlarmModel::roleNames() const
82
return AlarmData::roles();
86
* \qmlmethod Alarm AlarmModel::get(int index)
87
* Returns the copy of the alarm event at \a index in the model. This allows the
88
* alarm data to be modified and updated either through normal component binding
89
* or in Javascript functions.
92
* Component.onCompleted: {
93
* var alarm = alarmModel.get(0);
94
* alarm.message += " updated";
99
* This Javascript code returns the alarm properties from the first index of the
100
* model, updates its message and updates the alarm. Note that the alarm must be
101
* saved in order to have the changes visible. The follwoing code will not update
102
* the alarm in the model/collection:
105
* Component.onCompleted: {
106
* alarmModel.get(0).message += " updated";
107
* alarm.message += " updated";
111
* \b Warning: The returned object is not guarantied to remain valid, it should
112
* not be used in property bindings.
116
UCAlarm* UCAlarmModel::get(int index)
118
QList<AlarmData> alarms = AlarmManager::instance().alarms();
119
if ((index >= 0) && (index < alarms.count())) {
120
UCAlarmPrivate *pAlarm = UCAlarmPrivate::get(&m_alarm);
121
pAlarm->rawData = alarms[index];
128
* \qmlproperty int AlarmModel::count
129
* The number of data entries in the model.
131
int UCAlarmModel::count() const
133
return AlarmManager::instance().alarms().count();
138
* The slot prepares the views for the dataChanged() signal.
140
void UCAlarmModel::refresh()