Unity 8
FilterValueSlider.qml
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 }