on ceb6203 help sign in
unity8/tests/mocks/Unity/fake_filters.cpp
109 lines 3.6 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_filters.h"
19#include "fake_optionselectorfilter.h"
20#include "fake_rangeinputfilter.h"
21#include "fake_scope.h"
23Filters::Filters(Scope* parent)
24 : unity::shell::scopes::FiltersInterface(parent)
26 addFilter(new FakeOptionSelectorFilter("OSF1", "Tag1", "Which Cake you like More", false, QStringList() << "cheese" << "carrot" << "chocolate", this));
28 FakeRangeInputFilter *rif = new FakeRangeInputFilter("RIF1", "Tag3", this);
29 rif->setTitle("How much do you want to walk?");
30 rif->setCentralLabel("to");
31 rif->setStartPostfixLabel("m");
32 rif->setEndPostfixLabel("m");
33 addFilter(rif);
35 addFilter(new FakeOptionSelectorFilter("OSF2", "Tag2", "Which Countries have you been to?", true, QStringList() << "Germany" << "UK" << "New Zealand", this));
38void Filters::addFilter(unity::shell::scopes::FilterBaseInterface *f)
40 switch (f->filterType()) {
41 case FiltersInterface::OptionSelectorFilter: {
42 FakeOptionSelectorFilter *osf = static_cast<FakeOptionSelectorFilter *>(f);
43 connect(osf, &FakeOptionSelectorFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
44 }
45 break;
47 case FiltersInterface::RangeInputFilter: {
48 FakeRangeInputFilter *rif = static_cast<FakeRangeInputFilter *>(f);
49 connect(rif, &FakeRangeInputFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
50 }
51 break;
53 case Invalid:
54 break;
55 }
56 m_filters << f;
59int Filters::rowCount(const QModelIndex &parent) const
61 if (parent.isValid())
62 return 0;
64 return m_filters.count();
67QVariant Filters::data(const QModelIndex &index, int role) const
69 const int row = index.row();
70 if (row < 0 || row >= m_filters.count())
71 return QVariant();
73 unity::shell::scopes::FilterBaseInterface *filter = m_filters[row];
75 switch (role) {
76 case RoleFilterId:
77 return filter->filterId();
78 case RoleFilterType:
79 return filter->filterType();
80 case RoleFilter:
81 return QVariant::fromValue<unity::shell::scopes::FilterBaseInterface *>(filter);
82 default:
83 return QVariant();
84 }
87int Filters::activeFiltersCount() const
89 int active = 0;
90 Q_FOREACH(unity::shell::scopes::FilterBaseInterface *f, m_filters) {
91 switch (f->filterType()) {
92 case FiltersInterface::OptionSelectorFilter: {
93 FakeOptionSelectorFilter *osf = static_cast<FakeOptionSelectorFilter *>(f);
94 if (osf->isActive()) ++active;
95 }
96 break;
98 case FiltersInterface::RangeInputFilter: {
99 FakeRangeInputFilter *rif = static_cast<FakeRangeInputFilter *>(f);
100 if (rif->isActive()) ++active;
101 }
102 break;
104 case Invalid:
105 break;
106 }
107 }
108 return active;