on master help sign in
unity8/tests/mocks/Unity/Indicators/fakeindicatorsmodel.cpp
157 lines 3.9 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, &FakeIndicatorsModel::rowsInserted, this, &FakeIndicatorsModel::countChanged);
28 QObject::connect(this, &FakeIndicatorsModel::rowsRemoved, this, &FakeIndicatorsModel::countChanged);
29 QObject::connect(this, &FakeIndicatorsModel::modelReset, this, &FakeIndicatorsModel::countChanged);
32/*! \internal */
33FakeIndicatorsModel::~FakeIndicatorsModel()
37int FakeIndicatorsModel::count() const
39 return rowCount();
42QString FakeIndicatorsModel::profile() const
44 return m_profile;
47void FakeIndicatorsModel::setProfile(const QString& profile)
49 m_profile = profile;
50 Q_EMIT profileChanged();
53void FakeIndicatorsModel::load(const QString&)
57void FakeIndicatorsModel::unload()
59 beginResetModel();
61 m_modelData.clear();
62 Q_EMIT modelDataChanged();
64 endResetModel();
68void FakeIndicatorsModel::append(const QVariantMap& data)
70 QList<QVariant> allData = m_modelData.toList();
71 beginInsertRows(QModelIndex(), allData.count(), allData.count());
73 allData.append(data);
74 m_modelData = allData;
75 Q_EMIT modelDataChanged();
77 endInsertRows();
80void FakeIndicatorsModel::insert(int row, const QVariantMap& data)
82 QList<QVariant> allData = m_modelData.toList();
83 row = qMax(0, qMin(row, allData.count()));
85 beginInsertRows(QModelIndex(), row, row);
87 allData.insert(row, data);
88 m_modelData = allData;
89 Q_EMIT modelDataChanged();
91 endInsertRows();
94void FakeIndicatorsModel::remove(int row)
96 QList<QVariant> allData = m_modelData.toList();
97 row = qMax(0, qMin(row, allData.count()));
99 beginRemoveRows(QModelIndex(), row, row);
101 allData.removeAt(row);
102 m_modelData = allData;
103 Q_EMIT modelDataChanged();
105 endRemoveRows();
108void FakeIndicatorsModel::setModelData(const QVariant& modelData)
110 beginResetModel();
112 m_modelData = modelData;
113 Q_EMIT modelDataChanged();
115 endResetModel();
118QHash<int, QByteArray> FakeIndicatorsModel::roleNames() const
120 static QHash<int, QByteArray> roles;
121 if (roles.isEmpty())
122 {
123 roles[IndicatorsModelRole::Identifier] = "identifier";
124 roles[IndicatorsModelRole::Position] = "position";
125 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
126 }
127 return roles;
130int FakeIndicatorsModel::columnCount(const QModelIndex &) const
132 return 1;
135QVariant FakeIndicatorsModel::data(int row, int role) const
137 return data(index(row, 0), role);
140QVariant FakeIndicatorsModel::data(const QModelIndex &index, int role) const
142 QList<QVariant> dataList = m_modelData.toList();
143 if (!index.isValid() || index.row() >= dataList.size())
144 return QVariant();
146 return dataList[index.row()].toMap()[roleNames()[role]];
149QModelIndex FakeIndicatorsModel::parent(const QModelIndex&) const
151 return QModelIndex();
154int FakeIndicatorsModel::rowCount(const QModelIndex&) const
156 return m_modelData.toList().count();