Package IDAscope :: Package idascope :: Package widgets :: Module BoundsEditor
[hide private]
[frames] | no frames]

Source Code for Module IDAscope.idascope.widgets.BoundsEditor

  1  #!/usr/bin/python 
  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   
43 -class BoundsEditor(QtGui.QWidget):
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 # The default size is a bit too big and probably doesn't need to grow. 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 # The default size is a bit too big and probably doesn't need to grow. 98 sh = self._label_hi.sizeHint() 99 sh.setWidth(sh.width() / 2) 100 self._label_hi.setMaximumSize(sh)
101
102 - def update_low_on_enter(self):
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
125 - def update_high_on_enter(self):
126 try: 127 try: 128 high = eval(unicode(self._label_hi.text()).strip()) 129 except: 130 high = self.high 131 self._label_hi.setText(self.format % self.high) 132 133 if not self.is_float: 134 high = int(high) 135 136 if high < self.low: 137 high = self.low + self._step_size() 138 self._label_hi.setText(self.format % high) 139 140 self.slider.setHigh(self._convert_to_slider(high)) 141 self.high = high 142 143 self.boundsChanged.emit() 144 except: 145 pass
146
147 - def update_object_on_scroll(self, pos):
148 low = self._convert_from_slider(self.slider.low()) 149 high = self._convert_from_slider(self.slider.high()) 150 151 if self.is_float: 152 self.low = low 153 self.high = high 154 else: 155 self.low = int(low) 156 self.high = int(high) 157 158 # update the sliders to the int values or the sliders 159 # will jiggle 160 self.slider.setLow(self._convert_to_slider(low)) 161 self.slider.setHigh(self._convert_to_slider(high)) 162 163 self._label_hi.setText(self.format % self.high) 164 self._label_lo.setText(self.format % self.low)
165
166 - def update_editor(self):
167 return
168
169 - def _check_max_and_min(self):
170 # check if max & min have been defined: 171 if self.max is None: 172 self.max = self.high 173 if self.min is None: 174 self.min = self.low
175
176 - def _step_size(self):
177 slider_delta = self.slider.maximum() - self.slider.minimum() 178 range_delta = self.max - self.min 179 return float(range_delta) / slider_delta
180
181 - def _convert_from_slider(self, slider_val):
182 self._check_max_and_min() 183 return self.min + slider_val * self._step_size()
184
185 - def _convert_to_slider(self, value):
186 self._check_max_and_min() 187 return self.slider.minimum() + (value - self.min) / self._step_size()
188
189 - def _low_changed(self, low):
190 if self._label_lo is not None: 191 self._label_lo.setText(self.format % low) 192 self.slider.setLow(self._convert_to_slider(low))
193
194 - def _high_changed(self, high):
195 if self._label_hi is not None: 196 self._label_hi.setText(self.format % high) 197 self.slider.setHigh(self._convert_to_slider(self.high))
198
199 - def mouseReleaseEvent(self, event):
200 event.accept() 201 self.boundsChanged.emit()
202