~ci-train-bot/unity8/unity8-ubuntu-zesty-2167

« back to all changes in this revision

Viewing changes to tests/mocks/Ubuntu/Application/ApplicationListModel.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 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 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 General Public License for more details.
 
12
 *
 
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/>.
 
15
 */
 
16
 
 
17
#include "ApplicationListModel.h"
 
18
#include "ApplicationInfo.h"
 
19
 
 
20
ApplicationListModel::ApplicationListModel(QObject* parent)
 
21
    : QAbstractListModel(parent), m_applications()
 
22
{
 
23
    m_roleNames.insert(0, "application");
 
24
}
 
25
 
 
26
ApplicationListModel::~ApplicationListModel()
 
27
{
 
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();
 
32
}
 
33
 
 
34
int ApplicationListModel::rowCount(const QModelIndex& parent) const
 
35
{
 
36
    return !parent.isValid() ? m_applications.size() : 0;
 
37
}
 
38
 
 
39
QVariant ApplicationListModel::data(const QModelIndex& index, int role) const
 
40
{
 
41
    if (index.row() >= 0 && index.row() < m_applications.size() && role == 0)
 
42
        return QVariant::fromValue(m_applications.at(index.row()));
 
43
    else
 
44
        return QVariant();
 
45
}
 
46
 
 
47
QVariant ApplicationListModel::get(int row) const
 
48
{
 
49
    return data(index(row), 0);
 
50
}
 
51
 
 
52
void ApplicationListModel::move(int from, int to)
 
53
{
 
54
    if (from >= 0 && from < m_applications.size() && to >= 0 && to < m_applications.size()) {
 
55
        QModelIndex parent;
 
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);
 
61
        endMoveRows();
 
62
    }
 
63
}
 
64
 
 
65
void ApplicationListModel::add(ApplicationInfo* application)
 
66
{
 
67
    beginInsertRows(QModelIndex(), m_applications.size(), m_applications.size());
 
68
    m_applications.append(application);
 
69
    endInsertRows();
 
70
    Q_EMIT countChanged();
 
71
}
 
72
 
 
73
void ApplicationListModel::remove(ApplicationInfo* application)
 
74
{
 
75
    int i = m_applications.indexOf(application);
 
76
    if (i != -1) {
 
77
        beginRemoveRows(QModelIndex(), i, i);
 
78
        m_applications.removeAt(i);
 
79
        endRemoveRows();
 
80
        Q_EMIT countChanged();
 
81
    }
 
82
}
 
83
 
 
84
bool ApplicationListModel::contains(ApplicationInfo* application) const {
 
85
    return m_applications.contains(application);
 
86
}
 
87
 
 
88
void ApplicationListModel::clear()
 
89
{
 
90
    beginRemoveRows(QModelIndex(), 0, m_applications.size()-1);
 
91
    m_applications.clear();
 
92
    endRemoveRows();
 
93
    Q_EMIT countChanged();
 
94
}
 
95
 
 
96
QQmlListProperty<ApplicationInfo> ApplicationListModel::applications()
 
97
{
 
98
    return QQmlListProperty<ApplicationInfo>(this, 0,
 
99
        &ApplicationListModel::appendApplication,
 
100
        &ApplicationListModel::countApplications,
 
101
        &ApplicationListModel::atApplication,
 
102
        &ApplicationListModel::clearApplications);
 
103
}
 
104
 
 
105
void ApplicationListModel::appendApplication(QQmlListProperty<ApplicationInfo> *list,
 
106
                                             ApplicationInfo *application)
 
107
{
 
108
    ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);
 
109
    if (self) {
 
110
        application->setParent(self);
 
111
        self->add(application);
 
112
    }
 
113
}
 
114
 
 
115
int ApplicationListModel::countApplications(QQmlListProperty<ApplicationInfo> *list)
 
116
{
 
117
    ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);
 
118
    if (self) {
 
119
        return self->m_applications.size();
 
120
    } else {
 
121
        return 0;
 
122
    }
 
123
}
 
124
 
 
125
ApplicationInfo* ApplicationListModel::atApplication(QQmlListProperty<ApplicationInfo> *list,
 
126
                                                 int index)
 
127
{
 
128
    ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);
 
129
    if (!self) { return 0; }
 
130
 
 
131
    if (index >= 0 && index < self->m_applications.size()) {
 
132
        return self->m_applications.at(index);
 
133
    } else {
 
134
        return 0;
 
135
    }
 
136
}
 
137
 
 
138
void ApplicationListModel::clearApplications(QQmlListProperty<ApplicationInfo> *list)
 
139
{
 
140
    ApplicationListModel *self = qobject_cast<ApplicationListModel *>(list->object);
 
141
    if (self) {
 
142
        self->clear();
 
143
    }
 
144
}