~nick-dedekind/unity8/indicators.hint-interval

« back to all changes in this revision

Viewing changes to tests/mocks/Unity/Indicators/fakeindicatorsmodel.cpp

  • Committer: Nick Dedekind
  • Date: 2014-03-07 15:54:57 UTC
  • mfrom: (638.1.118 unity8)
  • Revision ID: nicholas.dedekind@gmail.com-20140307155457-f0s1zu5ll2czt3rq
merged with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2014 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
 * Authors:
 
17
 *      Nick Dedekind <nick.dedekind@canonical.com>
 
18
 */
 
19
 
 
20
#include "fakeindicatorsmodel.h"
 
21
#include "indicators.h"
 
22
 
 
23
FakeIndicatorsModel::FakeIndicatorsModel(QObject *parent)
 
24
    : QAbstractListModel(parent)
 
25
{
 
26
    QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
 
27
    QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
 
28
    QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
 
29
}
 
30
 
 
31
/*! \internal */
 
32
FakeIndicatorsModel::~FakeIndicatorsModel()
 
33
{
 
34
    unload();
 
35
}
 
36
 
 
37
int FakeIndicatorsModel::count() const
 
38
{
 
39
    return rowCount();
 
40
}
 
41
 
 
42
void FakeIndicatorsModel::load(const QString&)
 
43
{
 
44
}
 
45
 
 
46
void FakeIndicatorsModel::unload()
 
47
{
 
48
    beginResetModel();
 
49
 
 
50
    qDeleteAll(m_indicators);
 
51
    m_indicators.clear();
 
52
 
 
53
    endResetModel();
 
54
}
 
55
 
 
56
 
 
57
void FakeIndicatorsModel::append(const QVariantMap& row)
 
58
{
 
59
    Indicator* new_row = new QHash<int, QVariant>();
 
60
    for (auto iter = row.begin(); iter != row.end(); ++iter )
 
61
    {
 
62
        int key = roleNames().key(iter.key().toUtf8(), -1);
 
63
        if (key != -1) {
 
64
            new_row->insert(key, iter.value());
 
65
        }
 
66
    }
 
67
 
 
68
    beginInsertRows(QModelIndex(), m_indicators.count(), m_indicators.count());
 
69
 
 
70
    m_indicators.append(new_row);
 
71
 
 
72
    endInsertRows();
 
73
}
 
74
 
 
75
QHash<int, QByteArray> FakeIndicatorsModel::roleNames() const
 
76
{
 
77
    static QHash<int, QByteArray> roles;
 
78
    if (roles.isEmpty())
 
79
    {
 
80
        roles[IndicatorsModelRole::Identifier] = "identifier";
 
81
        roles[IndicatorsModelRole::Position] = "position";
 
82
        roles[IndicatorsModelRole::WidgetSource] = "widgetSource";
 
83
        roles[IndicatorsModelRole::PageSource] = "pageSource";
 
84
        roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
 
85
    }
 
86
    return roles;
 
87
}
 
88
 
 
89
int FakeIndicatorsModel::columnCount(const QModelIndex &) const
 
90
{
 
91
    return 1;
 
92
}
 
93
 
 
94
Q_INVOKABLE QVariant FakeIndicatorsModel::data(int row, int role) const
 
95
{
 
96
    return data(index(row, 0), role);
 
97
}
 
98
 
 
99
QVariant FakeIndicatorsModel::data(const QModelIndex &index, int role) const
 
100
{
 
101
    if (!index.isValid() || index.row() >= m_indicators.size())
 
102
        return QVariant();
 
103
 
 
104
    Indicator* indicator = m_indicators[index.row()];
 
105
    return indicator->value(role, QVariant());
 
106
}
 
107
 
 
108
QModelIndex FakeIndicatorsModel::parent(const QModelIndex&) const
 
109
{
 
110
    return QModelIndex();
 
111
}
 
112
 
 
113
int FakeIndicatorsModel::rowCount(const QModelIndex&) const
 
114
{
 
115
    return m_indicators.count();
 
116
}