on 0487745 help sign in
unity8/tests/mocks/Unity/fake_filters.cpp
136 lines 4.5 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_expandablefilterwidget.h"
24Filters::Filters(QObject* parent)
25 : unity::shell::scopes::FiltersInterface(parent)
29void Filters::addFakeFilters()
31 addFilter(new FakeOptionSelectorFilter("OSF1", "Tag1", "Which Cake you like More", false, QStringList() << "cheese" << "carrot" << "chocolate", this));
33 FakeRangeInputFilter *rif = new FakeRangeInputFilter("RIF1", "Tag3", this);
34 rif->setTitle("How much do you want to walk?");
35 rif->setCentralLabel("to");
36 rif->setStartPostfixLabel("m");
37 rif->setEndPostfixLabel("m");
38 addFilter(rif);
40 QMap<double, QString> labels;
41 labels[15] = "Default";
42 labels[50] = "50";
43 labels[100] = "100";
44 FakeValueSliderFilter *vsf = new FakeValueSliderFilter("VS1", "Tag4", 15, 10, 150, labels, this);
45 addFilter(vsf);
47 addFilter(new FakeOptionSelectorFilter("OSF2", "Tag2", "Which Countries have you been to?", true, QStringList() << "Germany" << "UK" << "New Zealand", this));
49 auto expandableFilter = new FakeExpandbleFilterWidget("EFW", "Tag5", "Advanced Settings", this);
50 expandableFilter->addFilter(new FakeValueSliderFilter("VS2", "Tag6", 15, 10, 150, labels, this));
52 addFilter(expandableFilter);
55void Filters::addFilter(unity::shell::scopes::FilterBaseInterface *f)
57 switch (f->filterType()) {
58 case FiltersInterface::OptionSelectorFilter: {
59 FakeOptionSelectorFilter *osf = static_cast<FakeOptionSelectorFilter *>(f);
60 connect(osf, &FakeOptionSelectorFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
61 }
62 break;
64 case FiltersInterface::RangeInputFilter: {
65 FakeRangeInputFilter *rif = static_cast<FakeRangeInputFilter *>(f);
66 connect(rif, &FakeRangeInputFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
67 }
68 break;
70 case FiltersInterface::ExpandableFilterWidget:
71 // Can't be active
72 case FiltersInterface::ValueSliderFilter:
73 // Not counting value as active in the mock
75 case Invalid:
76 break;
77 }
78 m_filters << f;
81int Filters::rowCount(const QModelIndex &parent) const
83 if (parent.isValid())
84 return 0;
86 return m_filters.count();
89QVariant Filters::data(const QModelIndex &index, int role) const
91 const int row = index.row();
92 if (row < 0 || row >= m_filters.count())
93 return QVariant();
95 unity::shell::scopes::FilterBaseInterface *filter = m_filters[row];
97 switch (role) {
98 case RoleFilterId:
99 return filter->filterId();
100 case RoleFilterType:
101 return filter->filterType();
102 case RoleFilter:
103 return QVariant::fromValue<unity::shell::scopes::FilterBaseInterface *>(filter);
104 default:
105 return QVariant();
106 }
109int Filters::activeFiltersCount() const
111 int active = 0;
112 Q_FOREACH(unity::shell::scopes::FilterBaseInterface *f, m_filters) {
113 switch (f->filterType()) {
114 case FiltersInterface::OptionSelectorFilter: {
115 FakeOptionSelectorFilter *osf = static_cast<FakeOptionSelectorFilter *>(f);
116 if (osf->isActive()) ++active;
117 }
118 break;
120 case FiltersInterface::RangeInputFilter: {
121 FakeRangeInputFilter *rif = static_cast<FakeRangeInputFilter *>(f);
122 if (rif->isActive()) ++active;
123 }
124 break;
126 case FiltersInterface::ExpandableFilterWidget:
127 // Can't be active
128 case FiltersInterface::ValueSliderFilter:
129 // Not counting value as active in the mock
131 case Invalid:
132 break;
133 }
134 }
135 return active;