~tpeeters/ubuntu-ui-toolkit/ToolbarButton-disable

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/plugin/ucalarmmodel.cpp

  • Committer: tpeeters
  • Date: 2013-08-21 17:06:42 UTC
  • mfrom: (647.2.56 trunk)
  • Revision ID: tim.peeters@canonical.com-20130821170642-q2ly3ygsq32yy2vh
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Author: Zsombor Egri <zsombor.egri@canonical.com>
 
17
 */
 
18
 
 
19
#include "ucalarmmodel.h"
 
20
#include "ucalarm.h"
 
21
#include "ucalarm_p.h"
 
22
#include "alarmmanager_p.h"
 
23
 
 
24
/*!
 
25
 * \qmltype AlarmModel
 
26
 * \instantiates UCAlarmModel
 
27
 * \inqmlmodule Ubuntu.Components 0.1
 
28
 * \ingroup ubuntu-services
 
29
 * \brief AlarmModel holds the list of alarms defined.
 
30
 *
 
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.
 
34
 *
 
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.
 
37
 *
 
38
 * Example usage:
 
39
 * \qml
 
40
 * import QtQuick 2.0
 
41
 * import Ubuntu.Components 0.1
 
42
 * import Ubuntu.Components.ListItems 0.1
 
43
 * ListView {
 
44
 *     model: AlarmModel {}
 
45
 *     width: units.gu(80)
 
46
 *     height: units.gu(100)
 
47
 *     delegate: Subtitled {
 
48
 *         text: message
 
49
 *         subText: Qt.formatDateTime(date)
 
50
 *     }
 
51
 * }
 
52
 * \endqml
 
53
 */
 
54
 
 
55
UCAlarmModel::UCAlarmModel(QObject *parent) :
 
56
    QAbstractListModel(parent)
 
57
{
 
58
    // keep in sync with alarms collection changes
 
59
    connect(&AlarmManager::instance(), SIGNAL(alarmsChanged()), this, SLOT(refresh()));
 
60
}
 
61
 
 
62
int UCAlarmModel::rowCount(const QModelIndex &parent) const
 
63
{
 
64
    Q_UNUSED(parent);
 
65
    return AlarmManager::instance().alarms().count();
 
66
}
 
67
QVariant UCAlarmModel::data(const QModelIndex &index, int role) const
 
68
{
 
69
    if (!index.isValid()) {
 
70
        return QVariant();
 
71
    }
 
72
    int idx = index.row();
 
73
    const QList<AlarmData> alarms = AlarmManager::instance().alarms();
 
74
    if ((idx >= alarms.count()) || (idx < 0)) {
 
75
        return QVariant();
 
76
    }
 
77
    return alarms[idx].roleData(role);
 
78
}
 
79
 
 
80
QHash<int, QByteArray> UCAlarmModel::roleNames() const
 
81
{
 
82
    return AlarmData::roles();
 
83
}
 
84
 
 
85
/*!
 
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.
 
90
 *
 
91
 * \code
 
92
 * Component.onCompleted: {
 
93
 *     var alarm = alarmModel.get(0);
 
94
 *     alarm.message += " updated";
 
95
 *     alarm.save();
 
96
 * }
 
97
 * \endcode
 
98
 *
 
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:
 
103
 *
 
104
 * \code
 
105
 * Component.onCompleted: {
 
106
 *     alarmModel.get(0).message += " updated";
 
107
 *     alarm.message += " updated";
 
108
 * }
 
109
 * \endcode
 
110
 *
 
111
 * \b Warning: The returned object is not guarantied to remain valid, it should
 
112
 * not be used in property bindings.
 
113
 *
 
114
 * \sa Alarm
 
115
 */
 
116
UCAlarm* UCAlarmModel::get(int index)
 
117
{
 
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];
 
122
        return &m_alarm;
 
123
    }
 
124
    return 0;
 
125
}
 
126
 
 
127
/*!
 
128
 * \qmlproperty int AlarmModel::count
 
129
 * The number of data entries in the model.
 
130
 */
 
131
int UCAlarmModel::count() const
 
132
{
 
133
    return AlarmManager::instance().alarms().count();
 
134
}
 
135
 
 
136
/*!
 
137
 * \internal
 
138
 * The slot prepares the views for the dataChanged() signal.
 
139
 */
 
140
void UCAlarmModel::refresh()
 
141
{
 
142
    beginResetModel();
 
143
    endResetModel();
 
144
}