on eda83a4 help sign in
unity8/tests/mocks/Unity/fake_optionselectoroptions.cpp
77 lines 2.4 kB history raw
1/*
2 * Copyright (C) 2015 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 */
17#include "fake_optionselectoroptions.h"
19#include "fake_optionselectorfilter.h"
21FakeOptionSelectorOptions::FakeOptionSelectorOptions(const QStringList &optionLabels, FakeOptionSelectorFilter* parent)
22 : unity::shell::scopes::OptionSelectorOptionsInterface(parent),
23 m_optionLabels(optionLabels)
27int FakeOptionSelectorOptions::rowCount(const QModelIndex &parent) const
29 if (parent.isValid())
30 return 0;
32 return m_optionLabels.count();
35QVariant FakeOptionSelectorOptions::data(const QModelIndex &index, int role) const
37 const int row = index.row();
38 if (row < 0 || row >= m_optionLabels.count())
39 return QVariant();
41 switch (role) {
42 case RoleOptionId: {
43 const QString id = static_cast<FakeOptionSelectorFilter*>(parent())->filterId() + QString::number(row);
44 return id;
45 }
46 case RoleOptionLabel:
47 return m_optionLabels[row];
48 case RoleOptionChecked:
49 return m_checkedIndexes.contains(row);
50 default:
51 return QVariant();
52 }
56void FakeOptionSelectorOptions::setChecked(int row, bool checked)
58 if (checked) {
59 if (!static_cast<FakeOptionSelectorFilter*>(parent())->multiSelect()) {
60 if (!m_checkedIndexes.isEmpty()) {
61 setChecked(*m_checkedIndexes.begin(), false);
62 }
63 Q_ASSERT(m_checkedIndexes.isEmpty());
64 }
65 m_checkedIndexes << row;
66 } else {
67 m_checkedIndexes.remove(row);
68 }
69 const QModelIndex idx = index(row, 0);
70 Q_EMIT dataChanged(idx, idx, QVector<int>() << RoleOptionChecked);
71 Q_EMIT anyCheckedChanged();
74bool FakeOptionSelectorOptions::anyChecked() const
76 return !m_checkedIndexes.isEmpty();