2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU 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 General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "ApplicationListModel.h"
18
#include "ApplicationInfo.h"
20
ApplicationListModel::ApplicationListModel(QObject* parent)
21
: QAbstractListModel(parent), m_applications()
23
m_roleNames.insert(0, "application");
26
ApplicationListModel::~ApplicationListModel()
28
const int kSize = m_applications.size();
29
for (int i = 0; i < kSize; i++)
30
delete m_applications.at(i);
31
m_applications.clear();
34
int ApplicationListModel::rowCount(const QModelIndex& parent) const
36
return !parent.isValid() ? m_applications.size() : 0;
39
QVariant ApplicationListModel::data(const QModelIndex& index, int role) const
41
if (index.row() >= 0 && index.row() < m_applications.size() && role == 0)
42
return QVariant::fromValue(m_applications.at(index.row()));
47
QVariant ApplicationListModel::get(int row) const
49
return data(index(row), 0);
52
void ApplicationListModel::move(int from, int to)
54
if (from >= 0 && from < m_applications.size() && to >= 0 && to < m_applications.size()) {
56
/* When moving an item down, the destination index needs to be incremented
57
by one, as explained in the documentation:
58
http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
59
beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
60
m_applications.move(from, to);
65
void ApplicationListModel::add(ApplicationInfo* application)
67
beginInsertRows(QModelIndex(), m_applications.size(), m_applications.size());
68
m_applications.append(application);
70
Q_EMIT countChanged();
73
void ApplicationListModel::remove(ApplicationInfo* application)
75
int i = m_applications.indexOf(application);
77
beginRemoveRows(QModelIndex(), i, i);
78
m_applications.removeAt(i);
80
Q_EMIT countChanged();
84
bool ApplicationListModel::contains(ApplicationInfo* application) const {
85
return m_applications.contains(application);
88
void ApplicationListModel::clear()
90
beginRemoveRows(QModelIndex(), 0, m_applications.size()-1);
91
m_applications.clear();
93
Q_EMIT countChanged();
96
QQmlListProperty<ApplicationInfo> ApplicationListModel::applications()
98
return QQmlListProperty<ApplicationInfo>(this, 0,
99
&ApplicationListModel::appendApplication,
100
&ApplicationListModel::countApplications,
101
&ApplicationListModel::atApplication,
102
&ApplicationListModel::clearApplications);
105
void ApplicationListModel::appendApplication(QQmlListProperty<ApplicationInfo> *list,
106
ApplicationInfo *application)
108
ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);
110
application->setParent(self);
111
self->add(application);
115
int ApplicationListModel::countApplications(QQmlListProperty<ApplicationInfo> *list)
117
ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);
119
return self->m_applications.size();
125
ApplicationInfo* ApplicationListModel::atApplication(QQmlListProperty<ApplicationInfo> *list,
128
ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);
129
if (!self) { return 0; }
131
if (index >= 0 && index < self->m_applications.size()) {
132
return self->m_applications.at(index);
138
void ApplicationListModel::clearApplications(QQmlListProperty<ApplicationInfo> *list)
140
ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);