on dbb8563 help sign in
unity8/tests/mocks/Unity/fake_filters.cpp
125 lines 4.1 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_valuesliderfilter.h"
22#include "fake_scope.h"
24Filters::Filters(Scope* parent)
25 : unity::shell::scopes::FiltersInterface(parent)
27 addFilter(new FakeOptionSelectorFilter("OSF1", "Tag1", "Which Cake you like More", false, QStringList() << "cheese" << "carrot" << "chocolate", this));
29 FakeRangeInputFilter *rif = new FakeRangeInputFilter("RIF1", "Tag3", this);
30 rif->setTitle("How much do you want to walk?");
31 rif->setCentralLabel("to");
32 rif->setStartPostfixLabel("m");
33 rif->setEndPostfixLabel("m");
34 addFilter(rif);
36 QMap<double, QString> labels;
37 labels[15] = "Default";
38 labels[50] = "50";
39 labels[100] = "100";
40 FakeValueSliderFilter *vsf = new FakeValueSliderFilter("VS1", "Tag4", 15, 10, 150, labels, this);
41 addFilter(vsf);
43 addFilter(new FakeOptionSelectorFilter("OSF2", "Tag2", "Which Countries have you been to?", true, QStringList() << "Germany" << "UK" << "New Zealand", this));
46void Filters::addFilter(unity::shell::scopes::FilterBaseInterface *f)
48 switch (f->filterType()) {
49 case FiltersInterface::OptionSelectorFilter: {
50 FakeOptionSelectorFilter *osf = static_cast<FakeOptionSelectorFilter *>(f);
51 connect(osf, &FakeOptionSelectorFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
52 }
53 break;
55 case FiltersInterface::RangeInputFilter: {
56 FakeRangeInputFilter *rif = static_cast<FakeRangeInputFilter *>(f);
57 connect(rif, &FakeRangeInputFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
58 }
59 break;
61 case FiltersInterface::ValueSliderFilter: {
62 // Not counting value slider as active in the mock
63 }
65 case Invalid:
66 break;
67 }
68 m_filters << f;
71int Filters::rowCount(const QModelIndex &parent) const
73 if (parent.isValid())
74 return 0;
76 return m_filters.count();
79QVariant Filters::data(const QModelIndex &index, int role) const
81 const int row = index.row();
82 if (row < 0 || row >= m_filters.count())
83 return QVariant();
85 unity::shell::scopes::FilterBaseInterface *filter = m_filters[row];
87 switch (role) {
88 case RoleFilterId:
89 return filter->filterId();
90 case RoleFilterType:
91 return filter->filterType();
92 case RoleFilter:
93 return QVariant::fromValue<unity::shell::scopes::FilterBaseInterface *>(filter);
94 default:
95 return QVariant();
96 }
99int Filters::activeFiltersCount() const
101 int active = 0;
102 Q_FOREACH(unity::shell::scopes::FilterBaseInterface *f, m_filters) {
103 switch (f->filterType()) {
104 case FiltersInterface::OptionSelectorFilter: {
105 FakeOptionSelectorFilter *osf = static_cast<FakeOptionSelectorFilter *>(f);
106 if (osf->isActive()) ++active;
107 }
108 break;
110 case FiltersInterface::RangeInputFilter: {
111 FakeRangeInputFilter *rif = static_cast<FakeRangeInputFilter *>(f);
112 if (rif->isActive()) ++active;
113 }
114 break;
116 case FiltersInterface::ValueSliderFilter: {
117 // Not counting value slider as active in the mock
118 }
120 case Invalid:
121 break;
122 }
123 }
124 return active;