on xenial help sign in
Value slider filter

Approved by: Andrea Cimitan

commit490b9e3c0ee264c9d1554761cdab35849e654c64 authorAlbert Astals Cid <albert.astals@canonical.com> dateWed 23 Mar 2016, 09:52 (11 years ago) parent66ee68474cbf86
12 files changed+443−4
qml/Dash/Filters/FilterValueSlider.qml+87 qml/Dash/Filters/FilterWidgetFactory.qml+1 tests/mocks/Unity/CMakeLists.txt+4 tests/mocks/Unity/fake_filters.cpp+18−2 tests/mocks/Unity/fake_unity_plugin.cpp+3−1 tests/mocks/Unity/fake_valuesliderfilter.cpp+84 tests/mocks/Unity/fake_valuesliderfilter.h+55 tests/mocks/Unity/fake_valueslidervalues.cpp+51 tests/mocks/Unity/fake_valueslidervalues.h+36 tests/qmltests/CMakeLists.txt+1 tests/qmltests/Dash/Filters/tst_FilterValueSlider.qml+101 tests/qmltests/Dash/Filters/tst_FilterWidgetFactory.qml+2−1
qml/Dash/Filters/FilterValueSlider.qmlnew file+87
@@ -0,0 +1,87 @@
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+ */
16+
17+import QtQuick 2.4
18+import QtQuick.Layouts 1.1
19+import Ubuntu.Components 1.3
20+
21+/*! Value Slider Filter Widget. */
22+
23+FilterWidget {
24+ id: root
25+
26+ implicitHeight: childrenRect.height + units.gu(2)
27+
28+ Connections {
29+ target: widgetData
30+ // One would think that this is not needed since
31+ // we have value: widgetData.value on the slider
32+ // but it is, otherwise reset doesn't seem to work
33+ onValueChanged: {
34+ if (widgetData.value !== slider.value) {
35+ slider.value = widgetData.value;
36+ }
37+ }
38+ }
39+
40+ Slider {
41+ id: slider
42+ objectName: "slider"
43+
44+ anchors {
45+ top: parent.top
46+ topMargin: units.gu(1)
47+ left: parent.left
48+ leftMargin: units.gu(2)
49+ right: parent.right
50+ rightMargin: units.gu(2)
51+ }
52+
53+ minimumValue: widgetData.minValue
54+ maximumValue: widgetData.maxValue
55+ value: widgetData.value
56+ onValueChanged: {
57+ widgetData.value = value;
58+ }
59+ onPressedChanged: {
60+ if (pressed) forceActiveFocus();
61+ }
62+
63+ readonly property Item thumb: __internals.thumb
64+ readonly property real barMinusThumb: __internals.barMinusThumb
65+ }
66+
67+ Repeater {
68+ objectName: "repeater"
69+ model: widgetData.values
70+ delegate: Label {
71+ anchors {
72+ top: slider.bottom
73+ // The slider is too tall, so move a bit up
74+ topMargin: -units.gu(1)
75+ }
76+ text: label
77+ visible: value <= widgetData.maxValue && value >= widgetData.minValue
78+ x: {
79+ var halfThumbDifference = (width - slider.thumb.width) / 2;
80+ var result = (value - widgetData.minValue) * slider.barMinusThumb / (widgetData.maxValue - widgetData.minValue) - halfThumbDifference;
81+ result = Math.max(result, 0);
82+ result = Math.min(result, slider.width - width);
83+ return units.gu(2) + result;
84+ }
85+ }
86+ }
87+}
qml/Dash/Filters/FilterWidgetFactory.qml+1
@@ -66,6 +66,7 @@ Item {
6666 switch (widgetType) {
6767 case Filters.OptionSelectorFilter: return "FilterOptionSelector.qml";
6868 case Filters.RangeInputFilter: return "FilterRangeInput.qml";
69+ case Filters.ValueSliderFilter: return "FilterValueSlider.qml";
6970 default: return "";
7071 }
7172 }
tests/mocks/Unity/CMakeLists.txt+4
@@ -33,6 +33,8 @@ set(UnityQML_SOURCES
3333 fake_optionselectorfilter.cpp
3434 fake_optionselectoroptions.cpp
3535 fake_rangeinputfilter.cpp
36+ fake_valuesliderfilter.cpp
37+ fake_valueslidervalues.cpp
3638 fake_unity_plugin.cpp
3739 ${SCOPES_API_INCLUDEDIR}/unity/shell/scopes/CategoriesInterface.h
3840 ${SCOPES_API_INCLUDEDIR}/unity/shell/scopes/FilterBaseInterface.h
@@ -47,6 +49,8 @@ set(UnityQML_SOURCES
4749 ${SCOPES_API_INCLUDEDIR}/unity/shell/scopes/ScopeInterface.h
4850 ${SCOPES_API_INCLUDEDIR}/unity/shell/scopes/ScopesInterface.h
4951 ${SCOPES_API_INCLUDEDIR}/unity/shell/scopes/SettingsModelInterface.h
52+ ${SCOPES_API_INCLUDEDIR}/unity/shell/scopes/ValueSliderFilterInterface.h
53+ ${SCOPES_API_INCLUDEDIR}/unity/shell/scopes/ValueSliderValuesInterface.h
5054)
5155
5256# Workaround for gcc failure LP: #1417664
tests/mocks/Unity/fake_filters.cpp+18−2
@@ -18,6 +18,7 @@
1818
1919#include "fake_optionselectorfilter.h"
2020#include "fake_rangeinputfilter.h"
21+#include "fake_valuesliderfilter.h"
2122#include "fake_scope.h"
2223
2324Filters::Filters(Scope* parent)
@@ -32,24 +33,35 @@ Filters::Filters(Scope* parent)
3233 rif->setEndPostfixLabel("m");
3334 addFilter(rif);
3435
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);
42+
3543 addFilter(new FakeOptionSelectorFilter("OSF2", "Tag2", "Which Countries have you been to?", true, QStringList() << "Germany" << "UK" << "New Zealand", this));
3644}
3745
3846void Filters::addFilter(unity::shell::scopes::FilterBaseInterface *f)
3947{
4048 switch (f->filterType()) {
41− case FiltersInterface::OptionSelectorFilter: {
49+ case FiltersInterface::OptionSelectorFilter: {
4250 FakeOptionSelectorFilter *osf = static_cast<FakeOptionSelectorFilter *>(f);
4351 connect(osf, &FakeOptionSelectorFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
4452 }
4553 break;
4654
47− case FiltersInterface::RangeInputFilter: {
55+ case FiltersInterface::RangeInputFilter: {
4856 FakeRangeInputFilter *rif = static_cast<FakeRangeInputFilter *>(f);
4957 connect(rif, &FakeRangeInputFilter::isActiveChanged, this, &Filters::activeFiltersCountChanged);
5058 }
5159 break;
5260
61+ case FiltersInterface::ValueSliderFilter: {
62+ // Not counting value slider as active in the mock
63+ }
64+
5365 case Invalid:
5466 break;
5567 }
@@ -101,6 +113,10 @@ int Filters::activeFiltersCount() const
101113 }
102114 break;
103115
116+ case FiltersInterface::ValueSliderFilter: {
117+ // Not counting value slider as active in the mock
118+ }
119+
104120 case Invalid:
105121 break;
106122 }
tests/mocks/Unity/fake_unity_plugin.cpp+3−1
@@ -29,6 +29,7 @@
2929#include "fake_previewwidgetmodel.h"
3030#include "fake_resultsmodel.h"
3131#include "fake_settingsmodel.h"
32+#include "fake_valueslidervalues.h"
3233
3334// External
3435#include <glib-object.h>
@@ -56,5 +57,6 @@ void FakeUnityPlugin::registerTypes(const char *uri)
5657 qmlRegisterUncreatableType<PreviewWidgetModel>(uri, 0, 2, "PreviewWidgetModel", "Can't create new PreviewWidgetModel in QML. Get them from PreviewModel instance.");
5758 qmlRegisterUncreatableType<unity::shell::scopes::FiltersInterface>(uri, 0, 2, "Filters", "Can't create Filters object in QML. Get them from Scope instance.");
5859 qmlRegisterUncreatableType<unity::shell::scopes::FilterBaseInterface>(uri, 0, 2, "Filter", "Can't create Filter object in QML. Get them from Scope instance.");
59− qmlRegisterUncreatableType<unity::shell::scopes::OptionSelectorOptionsInterface>(uri, 0, 2, "OptionSelectorOptions", "Can't create Filters object in QML. Get them from OptionSelector instance.");
60+ qmlRegisterUncreatableType<unity::shell::scopes::OptionSelectorOptionsInterface>(uri, 0, 2, "OptionSelectorOptions", "Can't create OptionSelectorOptions object in QML");
61+ qmlRegisterUncreatableType<unity::shell::scopes::ValueSliderValuesInterface>(uri, 0, 2, "ValueSliderValues", "Can't create ValueSliderValuesInterface object in QML");
6062}
tests/mocks/Unity/fake_valuesliderfilter.cppnew file+84
@@ -0,0 +1,84 @@
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+ */
16+
17+#include "fake_valuesliderfilter.h"
18+#include "fake_valueslidervalues.h"
19+
20+FakeValueSliderFilter::FakeValueSliderFilter(const QString &id, const QString &tag, double value, double minValue, double maxValue, const QMap<double, QString> &labels, QObject* parent)
21+ : unity::shell::scopes::ValueSliderFilterInterface(parent),
22+ m_filterId(id),
23+ m_filterTag(tag),
24+ m_value(value),
25+ m_minValue(minValue),
26+ m_maxValue(maxValue),
27+ m_values(new FakeValueSliderValues(labels, this))
28+{
29+}
30+
31+QString FakeValueSliderFilter::filterId() const
32+{
33+ return m_filterId;
34+}
35+
36+QString FakeValueSliderFilter::filterTag() const
37+{
38+ return m_filterTag;
39+}
40+
41+QString FakeValueSliderFilter::title() const
42+{
43+ return m_title;
44+}
45+
46+double FakeValueSliderFilter::value() const
47+{
48+ return m_value;
49+}
50+
51+void FakeValueSliderFilter::setValue(double value)
52+{
53+ if (value != m_value) {
54+ m_value = value;
55+ Q_EMIT valueChanged();
56+ }
57+}
58+
59+double FakeValueSliderFilter::minValue() const
60+{
61+ return m_minValue;
62+}
63+
64+double FakeValueSliderFilter::maxValue() const
65+{
66+ return m_maxValue;
67+}
68+
69+unity::shell::scopes::ValueSliderValuesInterface* FakeValueSliderFilter::values() const
70+{
71+ return m_values;
72+}
73+
74+bool FakeValueSliderFilter::isActive() const
75+{
76+ // Doesn't really matter
77+ return false;
78+}
79+
80+void FakeValueSliderFilter::setTitle(const QString &title)
81+{
82+ m_title = title;
83+ Q_EMIT titleChanged();
84+}
tests/mocks/Unity/fake_valuesliderfilter.hnew file+55
@@ -0,0 +1,55 @@
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+ */
16+
17+#ifndef FAKE_VALUESLIDERFILTER_H
18+#define FAKE_VALUESLIDERFILTER_H
19+
20+#include <unity/shell/scopes/ValueSliderFilterInterface.h>
21+
22+class FakeValueSliderFilter : public unity::shell::scopes::ValueSliderFilterInterface
23+{
24+ Q_OBJECT
25+
26+public:
27+ FakeValueSliderFilter(const QString &id, const QString &tag, double value, double minValue, double maxValue, const QMap<double, QString> &labels, QObject* parent);
28+
29+ QString filterId() const override;
30+ QString filterTag() const override;
31+ QString title() const override;
32+
33+ double value() const override;
34+ void setValue(double value) override;
35+ double minValue() const override;
36+ double maxValue() const override;
37+ unity::shell::scopes::ValueSliderValuesInterface* values() const override;
38+
39+ // Not part of the iface, for mock/testing purposes
40+ bool isActive() const;
41+
42+ void setTitle(const QString &title);
43+
44+private:
45+ QString m_filterId;
46+ QString m_filterTag;
47+ QString m_title;
48+
49+ double m_value;
50+ double m_minValue;
51+ double m_maxValue;
52+ unity::shell::scopes::ValueSliderValuesInterface *m_values;
53+};
54+
55+#endif
tests/mocks/Unity/fake_valueslidervalues.cppnew file+51
@@ -0,0 +1,51 @@
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+ */
16+
17+#include "fake_valueslidervalues.h"
18+
19+FakeValueSliderValues::FakeValueSliderValues(const QMap<double, QString> &labels, QObject* parent)
20+ : unity::shell::scopes::ValueSliderValuesInterface(parent)
21+ , m_labels(labels)
22+{
23+}
24+
25+int FakeValueSliderValues::rowCount(const QModelIndex &parent) const
26+{
27+ if (parent.isValid())
28+ return 0;
29+
30+ return m_labels.count();
31+}
32+
33+QVariant FakeValueSliderValues::data(const QModelIndex &index, int role) const
34+{
35+ if (!index.isValid())
36+ return QVariant();
37+
38+ const int row = index.row();
39+ auto it = m_labels.begin();
40+ it += row;
41+
42+ switch (role) {
43+ case RoleValue:
44+ return it.key();
45+
46+ case RoleLabel:
47+ return it.value();
48+ }
49+
50+ return QVariant();
51+}
tests/mocks/Unity/fake_valueslidervalues.hnew file+36
@@ -0,0 +1,36 @@
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+ */
16+
17+#ifndef FAKE_VALUESLIDERVALUES_H
18+#define FAKE_VALUESLIDERVALUES_H
19+
20+#include <unity/shell/scopes/ValueSliderValuesInterface.h>
21+
22+class FakeValueSliderValues : public unity::shell::scopes::ValueSliderValuesInterface
23+{
24+ Q_OBJECT
25+
26+public:
27+ FakeValueSliderValues(const QMap<double, QString> &labels, QObject* parent);
28+
29+ int rowCount(const QModelIndex &parent) const override;
30+ QVariant data(const QModelIndex &index, int role) const override;
31+
32+private:
33+ const QMap<double, QString> m_labels;
34+};
35+
36+#endif
tests/qmltests/CMakeLists.txt+1
@@ -55,6 +55,7 @@ add_unity8_qmltest(Dash/Previews PreviewWidgetFactory)
5555add_unity8_qmltest(Dash/Previews PreviewZoomableImage)
5656add_unity8_qmltest(Dash/Filters FilterOptionSelector)
5757add_unity8_qmltest(Dash/Filters FilterRangeInput)
58+add_unity8_qmltest(Dash/Filters FilterValueSlider)
5859add_unity8_qmltest(Dash/Filters FilterWidgetFactory)
5960add_unity8_qmltest(Dash/ScopeSettings ScopeSettingBoolean)
6061add_unity8_qmltest(Dash/ScopeSettings ScopeSettingList)
tests/qmltests/Dash/Filters/tst_FilterValueSlider.qmlnew file+101
@@ -0,0 +1,101 @@
1+/*
2+ * Copyright 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+ */
16+
17+import QtQuick 2.4
18+import QtTest 1.0
19+import Ubuntu.Components 1.3
20+import "../../../../qml/Dash/Filters"
21+import Unity.Test 0.1 as UT
22+import Unity 0.2
23+
24+Rectangle {
25+ id: root
26+ width: units.gu(60)
27+ height: units.gu(80)
28+ color: theme.palette.selected.background
29+
30+ QtObject {
31+ id: widgetData1
32+
33+ property string title
34+ property int value: 30
35+ property int minValue: 10
36+ property int maxValue: 150
37+
38+ property ListModel values: ListModel {
39+ ListElement {
40+ value: 10
41+ label: "10"
42+ }
43+ ListElement {
44+ value: 30
45+ label: "30"
46+ }
47+ ListElement {
48+ value: 100
49+ label: "100"
50+ }
51+ ListElement {
52+ value: 150
53+ label: "150"
54+ }
55+ }
56+ }
57+
58+ FilterWidgetFactory {
59+ id: factory
60+ widgetId: "testRangeValueSlider"
61+ widgetType: Filters.ValueSliderFilter
62+ widgetData: widgetData1;
63+ y: 100
64+ anchors {
65+ left: parent.left
66+ right: parent.right
67+ }
68+ }
69+
70+ UT.UnityTestCase {
71+ name: "FilterValueSlider"
72+ when: windowShown
73+
74+ function test_valueChanges() {
75+ var slider = findChild(factory, "slider");
76+ compare(slider.value, 30);
77+
78+ slider.value = 50
79+ compare(widgetData1.value, 50);
80+ }
81+
82+ function test_labelPositions() {
83+ var slider = findChild(factory, "slider");
84+ var repeater = findChild(factory, "repeater");
85+
86+ // It's very hard to check the position of the
87+ // labels is right, but at least we can check
88+ // they all have different X positions and the
89+ // same Y one
90+ compare(repeater.count, 4);
91+ for (var i = 0; i < repeater.count; ++i) {
92+ var itemI = repeater.itemAt(i);
93+ for (var j = i + 1; j < repeater.count; ++j) {
94+ var itemJ = repeater.itemAt(j);
95+ verify(itemI.x != itemJ.x);
96+ verify(itemI.y == itemJ.y);
97+ }
98+ }
99+ }
100+ }
101+}
tests/qmltests/Dash/Filters/tst_FilterWidgetFactory.qml+2−1
@@ -42,7 +42,8 @@ Rectangle {
4242 function test_mapping_data() {
4343 return [
4444 { tag: "OptionSelector", type: Filters.OptionSelectorFilter, source: "FilterOptionSelector.qml" },
45− { tag: "RangeInputFilter", type: Filters.RangeInputFilter, source: "FilterRangeInput.qml" }
45+ { tag: "RangeInputFilter", type: Filters.RangeInputFilter, source: "FilterRangeInput.qml" },
46+ { tag: "ValueSliderFilter", type: Filters.ValueSliderFilter, source: "FilterValueSlider.qml" }
4647 ];
4748 }
4849