on 07aedbf help sign in
unity8/tests/mocks/Unity/Indicators/fakeindicatorsmodel.cpp
145 lines 3.6 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)
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()));
31/*! \internal */
32FakeIndicatorsModel::~FakeIndicatorsModel()
36int FakeIndicatorsModel::count() const
38 return rowCount();
41void FakeIndicatorsModel::load(const QString&)
45void FakeIndicatorsModel::unload()
47 beginResetModel();
49 m_modelData.clear();
50 Q_EMIT modelDataChanged();
52 endResetModel();
56void FakeIndicatorsModel::append(const QVariantMap& data)
58 QList<QVariant> allData = m_modelData.toList();
59 beginInsertRows(QModelIndex(), allData.count(), allData.count());
61 allData.append(data);
62 m_modelData = allData;
63 Q_EMIT modelDataChanged();
65 endInsertRows();
68void FakeIndicatorsModel::insert(int row, const QVariantMap& data)
70 QList<QVariant> allData = m_modelData.toList();
71 row = qMax(0, qMin(row, allData.count()));
73 beginInsertRows(QModelIndex(), row, row);
75 allData.insert(row, data);
76 m_modelData = allData;
77 Q_EMIT modelDataChanged();
79 endInsertRows();
82void FakeIndicatorsModel::remove(int row)
84 QList<QVariant> allData = m_modelData.toList();
85 row = qMax(0, qMin(row, allData.count()));
87 beginRemoveRows(QModelIndex(), row, row);
89 allData.removeAt(row);
90 m_modelData = allData;
91 Q_EMIT modelDataChanged();
93 endRemoveRows();
96void FakeIndicatorsModel::setModelData(const QVariant& modelData)
98 beginResetModel();
100 m_modelData = modelData;
101 Q_EMIT modelDataChanged();
103 endResetModel();
106QHash<int, QByteArray> FakeIndicatorsModel::roleNames() const
108 static QHash<int, QByteArray> roles;
109 if (roles.isEmpty())
110 {
111 roles[IndicatorsModelRole::Identifier] = "identifier";
112 roles[IndicatorsModelRole::Position] = "position";
113 roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
114 }
115 return roles;
118int FakeIndicatorsModel::columnCount(const QModelIndex &) const
120 return 1;
123QVariant FakeIndicatorsModel::data(int row, int role) const
125 return data(index(row, 0), role);
128QVariant FakeIndicatorsModel::data(const QModelIndex &index, int role) const
130 QList<QVariant> dataList = m_modelData.toList();
131 if (!index.isValid() || index.row() >= dataList.size())
132 return QVariant();
134 return dataList[index.row()].toMap()[roleNames()[role]];
137QModelIndex FakeIndicatorsModel::parent(const QModelIndex&) const
139 return QModelIndex();
142int FakeIndicatorsModel::rowCount(const QModelIndex&) const
144 return m_modelData.toList().count();