~mhr3/unity8/fix-1297246

« back to all changes in this revision

Viewing changes to tests/qmltests/plugins/Unity/fake_lenses.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
 * Author: Nick Dedekind <nick.dedekind@canonical.com>
 
17
 */
 
18
 
 
19
// Self
 
20
#include "fake_lenses.h"
 
21
 
 
22
// TODO: Implement remaining pieces, like Categories (i.e. LensView now gives warnings)
 
23
 
 
24
// Qt
 
25
#include <QTimer>
 
26
 
 
27
Lenses::Lenses(QObject *parent)
 
28
: QAbstractListModel(parent)
 
29
, m_loaded(false)
 
30
, timer(this)
 
31
{
 
32
    m_roles[Lenses::RoleLens] = "lens";
 
33
    m_roles[Lenses::RoleId] = "id";
 
34
    m_roles[Lenses::RoleVisible] = "visible";
 
35
 
 
36
    QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
 
37
    QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
 
38
    QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
 
39
 
 
40
    timer.setSingleShot(true);
 
41
    timer.setInterval(100);
 
42
    QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(updateLenses()));
 
43
    load();
 
44
}
 
45
 
 
46
Lenses::~Lenses()
 
47
{
 
48
}
 
49
 
 
50
void Lenses::updateLenses()
 
51
{
 
52
    clear();
 
53
    addLens(new Lens("MockLens1", "People", true, this));
 
54
    addLens(new Lens("MockLens2", "Music", false, this));
 
55
    addLens(new Lens("MockLens3", "Home", true, this));
 
56
    addLens(new Lens("MockLens4", "Applications", true, this));
 
57
    addLens(new Lens("MockLens5", "Videos", true, this));
 
58
 
 
59
    if (!m_loaded) {
 
60
        m_loaded = true;
 
61
        Q_EMIT loadedChanged(m_loaded);
 
62
    }
 
63
}
 
64
 
 
65
void Lenses::clear()
 
66
{
 
67
    timer.stop();
 
68
    if (m_lenses.size() > 0) {
 
69
        beginRemoveRows(QModelIndex(), 0, m_lenses.count()-1);
 
70
        qDeleteAll(m_lenses);
 
71
        m_lenses.clear();
 
72
        endRemoveRows();
 
73
    }
 
74
 
 
75
    if (m_loaded) {
 
76
        m_loaded = false;
 
77
        Q_EMIT loadedChanged(m_loaded);
 
78
    }
 
79
}
 
80
 
 
81
void Lenses::load()
 
82
{
 
83
    timer.start();
 
84
}
 
85
 
 
86
QHash<int, QByteArray> Lenses::roleNames() const
 
87
{
 
88
    return m_roles;
 
89
}
 
90
 
 
91
int Lenses::rowCount(const QModelIndex&) const
 
92
{
 
93
    return m_lenses.count();
 
94
}
 
95
 
 
96
QVariant Lenses::data(const QModelIndex& index, int role) const
 
97
{
 
98
    if (!index.isValid() || index.row() >= m_lenses.size()) {
 
99
        return QVariant();
 
100
    }
 
101
 
 
102
    Lens* lens = m_lenses.at(index.row());
 
103
 
 
104
    if (role == Lenses::RoleLens) {
 
105
        return QVariant::fromValue(lens);
 
106
    } else if (role == Lenses::RoleId) {
 
107
        return QVariant::fromValue(lens->id());
 
108
    } else if (role == Lenses::RoleVisible) {
 
109
        return QVariant::fromValue(lens->visible());
 
110
    } else {
 
111
        return QVariant();
 
112
    }
 
113
}
 
114
 
 
115
QVariant Lenses::get(int row) const
 
116
{
 
117
    return data(QAbstractListModel::index(row), 0);
 
118
}
 
119
 
 
120
QVariant Lenses::get(QString const&) const
 
121
{
 
122
    return QVariant();
 
123
}
 
124
 
 
125
QModelIndex Lenses::parent(const QModelIndex&) const
 
126
{
 
127
    return QModelIndex();
 
128
}
 
129
 
 
130
bool Lenses::loaded() const
 
131
{
 
132
    return m_loaded;
 
133
}
 
134
 
 
135
int Lenses::count() const
 
136
{
 
137
    return rowCount();
 
138
}
 
139
 
 
140
void Lenses::addLens(Lens* lens)
 
141
{
 
142
    int index = rowCount();
 
143
    beginInsertRows(QModelIndex(), index, index);
 
144
    m_lenses.append(lens);
 
145
    endInsertRows();
 
146
}