Unity 8
FilterRangeInput.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 /*! Range Input Filter Widget. */
22 
23 FilterWidget {
24  id: root
25 
26  implicitHeight: field1.height + units.gu(2)
27 
28  function setFieldValue(field, hasValue, value) {
29  if (hasValue) {
30  // Need this othewise if we are on 4.5 and backspace instead of
31  // having 4. in the text field we end up with 4 which is confusing
32  if (field.text != value) {
33  field.text = value;
34  }
35  } else {
36  field.text = "";
37  }
38  }
39 
40  Connections {
41  target: widgetData
42  onStartValueChanged: root.setFieldValue(field1, widgetData.hasStartValue, widgetData.startValue);
43  onHasStartValueChanged: root.setFieldValue(field1, widgetData.hasStartValue, widgetData.startValue);
44  onEndValueChanged: root.setFieldValue(field2, widgetData.hasEndValue, widgetData.endValue);
45  onHasEndValueChanged: root.setFieldValue(field2, widgetData.hasEndValue, widgetData.endValue);
46  }
47 
48  onWidgetDataChanged: {
49  if (widgetData) {
50  root.setFieldValue(field1, widgetData.hasStartValue, widgetData.startValue);
51  root.setFieldValue(field2, widgetData.hasEndValue, widgetData.endValue);
52  } else {
53  root.setFieldValue(field1, false, -1);
54  root.setFieldValue(field2, false, -1);
55  }
56  }
57 
58  RowLayout {
59  anchors {
60  fill: parent
61  topMargin: units.gu(1)
62  bottomMargin: units.gu(1)
63  }
64 
65  Item {
66  Layout.fillWidth: true
67  }
68 
69  Label {
70  text: widgetData.startPrefixLabel
71  verticalAlignment: Text.AlignVCenter
72  }
73 
74  TextField {
75  id: field1
76  objectName: "startValueField"
77  implicitWidth: units.gu(9)
78  verticalAlignment: Text.AlignVCenter
79  inputMethodHints: Qt.ImhFormattedNumbersOnly
80  validator: DoubleValidator {
81  notation: DoubleValidator.StandardNotation
82  }
83  onTextChanged: {
84  if (text === "") widgetData.eraseStartValue();
85  else widgetData.startValue = text;
86  }
87  }
88 
89  Label {
90  text: widgetData.startPostfixLabel
91  verticalAlignment: Text.AlignVCenter
92  }
93 
94  Item {
95  Layout.fillWidth: true
96  }
97 
98  Label {
99  text: widgetData.centralLabel
100  verticalAlignment: Text.AlignVCenter
101  }
102 
103  Item {
104  Layout.fillWidth: true
105  }
106 
107  Label {
108  text: widgetData.endPrefixLabel
109  verticalAlignment: Text.AlignVCenter
110  }
111 
112  TextField {
113  id: field2
114  objectName: "endValueField"
115  implicitWidth: units.gu(9)
116  verticalAlignment: Text.AlignVCenter
117  inputMethodHints: Qt.ImhFormattedNumbersOnly
118  validator: DoubleValidator {
119  notation: DoubleValidator.StandardNotation
120  }
121  onTextChanged: {
122  if (text === "") widgetData.eraseEndValue();
123  else widgetData.endValue = text;
124  }
125  }
126 
127  Label {
128  text: widgetData.endPostfixLabel
129  verticalAlignment: Text.AlignVCenter
130  }
131 
132  Item {
133  Layout.fillWidth: true
134  }
135  }
136 }