1
2 """
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 Copyright (c) 2006, Enthought, Inc.
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
11
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 * Neither the name of Enthought, Inc. nor the names of its contributors may
18 be used to endorse or promote products derived from this software without
19 specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 The software contained in the traits/protocols/ directory is
33 the pyprotocols project (http://peak.telecommunity.com/PyProtocols.html),
34 it is originaly licensed under the terms of the Python Software
35 Foundation License, which is compatible with the above terms.
36 """
37
38 from PySide import QtGui, QtCore
39
40 from RangeSlider import RangeSlider
41
42
44 """
45 Custom widget consisting of a QLineEdit, a custom double slider and another QLineEdit.
46 """
47
48 boundsChanged = QtCore.Signal()
49
50 - def __init__(self, name, min, max, low, high, is_float=True):
51 super(BoundsEditor, self).__init__()
52 self.name = name
53 self.min = min
54 self.max = max
55 self.low = low
56 self.high = high
57 self.is_float = is_float
58
59 self.format = "%d"
60 if self.is_float:
61 self.format = "%2.2f"
62
63 panel = QtGui.QHBoxLayout(self)
64 panel.setContentsMargins(0, 0, 0, 0)
65
66 self._label_name = QtGui.QLabel(self.name)
67 panel.addWidget(self._label_name)
68
69 self._label_lo = QtGui.QLineEdit(self.format % self.low)
70 self._label_lo.setMinimumSize(45, 0)
71 self._label_lo.editingFinished.connect(self.update_low_on_enter)
72 self._label_lo.returnPressed.connect(self.update_low_on_enter)
73 panel.addWidget(self._label_lo)
74
75
76 sh = self._label_lo.sizeHint()
77 sh.setWidth(sh.width() / 2)
78 self._label_lo.setMaximumSize(sh)
79
80 self.slider = slider = RangeSlider(QtCore.Qt.Horizontal)
81 slider.setMinimum(0)
82 slider.setMaximum(10000)
83 slider.setPageStep(1000)
84 slider.setSingleStep(100)
85 slider.setLow(self._convert_to_slider(self.low))
86 slider.setHigh(self._convert_to_slider(self.high))
87
88 slider.sliderMoved.connect(self.update_object_on_scroll)
89 panel.addWidget(slider)
90
91 self._label_hi = QtGui.QLineEdit(self.format % self.high)
92 self._label_hi.setMinimumSize(45, 0)
93 self._label_hi.editingFinished.connect(self.update_high_on_enter)
94 self._label_hi.returnPressed.connect(self.update_high_on_enter)
95 panel.addWidget(self._label_hi)
96
97
98 sh = self._label_hi.sizeHint()
99 sh.setWidth(sh.width() / 2)
100 self._label_hi.setMaximumSize(sh)
101
103 try:
104 try:
105 low = eval(unicode(self._label_lo.text()).strip())
106 except Exception as exc:
107 low = self.low
108 self._label_lo.setText(self.format % self.low)
109 print exc
110
111 if not self.is_float:
112 low = int(low)
113
114 if low > self.high:
115 low = self.high - self._step_size()
116 self._label_lo.setText(self.format % low)
117
118 self.slider.setLow(self._convert_to_slider(low))
119 self.low = low
120
121 self.boundsChanged.emit()
122 except:
123 pass
124
146
165
168
170
171 if self.max is None:
172 self.max = self.high
173 if self.min is None:
174 self.min = self.low
175
177 slider_delta = self.slider.maximum() - self.slider.minimum()
178 range_delta = self.max - self.min
179 return float(range_delta) / slider_delta
180
184
188
193
198
202