on d6de6bc help sign in
unity8/tests/mocks/Unity/Indicators/fakeindicatorsmodel.cpp
128 lines 3.4 kB history raw
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 */
20#include "fakeindicatorsmodel.h"
21#include "indicators.h"
23FakeIndicatorsModel::FakeIndicatorsModel(QObject *parent)
24 : QAbstractListModel(parent),
25 m_profile("phone")
27 QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
28 QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
29 QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
32/*! \internal */
33FakeIndicatorsModel::~FakeIndicatorsModel()
35 qDeleteAll(m_indicators);
38int FakeIndicatorsModel::count() const
40 return rowCount();
43QString FakeIndicatorsModel::profile() const
45 return m_profile;
48void FakeIndicatorsModel::setProfile(const QString& profile)
50 m_profile = profile;
51 Q_EMIT profileChanged();
54void FakeIndicatorsModel::load(const QString&)
58void FakeIndicatorsModel::unload()
60 beginResetModel();
62 qDeleteAll(m_indicators);
63 m_indicators.clear();
65 endResetModel();
69void FakeIndicatorsModel::append(const QVariantMap& row)
71 Indicator* new_row = new QHash<int, QVariant>();
72 for (auto iter = row.begin(); iter != row.end(); ++iter )
73 {
74 int key = roleNames().key(iter.key().toUtf8(), -1);
75 if (key != -1) {
76 new_row->insert(key, iter.value());
77 }
78 }
80 beginInsertRows(QModelIndex(), m_indicators.count(), m_indicators.count());
82 m_indicators.append(new_row);
84 endInsertRows();
87QHash<int, QByteArray> FakeIndicatorsModel::roleNames() const
89 static QHash<int, QByteArray> roles;
90 if (roles.isEmpty())
91 {
92 roles[IndicatorsModelRole::Identifier] = "identifier";
93 roles[IndicatorsModelRole::Position] = "position";
94 roles[IndicatorsModelRole::WidgetSource] = "widgetSource";
95 roles[IndicatorsModelRole::PageSource] = "pageSource";
96 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
97 }
98 return roles;
101int FakeIndicatorsModel::columnCount(const QModelIndex &) const
103 return 1;
106Q_INVOKABLE QVariant FakeIndicatorsModel::data(int row, int role) const
108 return data(index(row, 0), role);
111QVariant FakeIndicatorsModel::data(const QModelIndex &index, int role) const
113 if (!index.isValid() || index.row() >= m_indicators.size())
114 return QVariant();
116 Indicator* indicator = m_indicators[index.row()];
117 return indicator->value(role, QVariant());
120QModelIndex FakeIndicatorsModel::parent(const QModelIndex&) const
122 return QModelIndex();
125int FakeIndicatorsModel::rowCount(const QModelIndex&) const
127 return m_indicators.count();