Package pyforms :: Package gui :: Package Controls :: Module ControlBoundingSlider |
[hide private]
[frames] | no frames]

# Source Code for Module pyforms.gui.Controls.ControlBoundingSlider

      1  #!/usr/bin/python
      2  # -*- coding: utf-8 -*-
      3
      4  __author__      = "Ricardo Ribeiro"
      5  __credits__     = ["Ricardo Ribeiro"]
      6  __license__     = "MIT"
      7  __version__     = "0.0"
      8  __maintainer__  = "Ricardo Ribeiro"
      9  __email__       = "ricardojvr@gmail.com"
     10  __status__      = "Development"
     11
     12
     13  import pyforms.Utils.tools as tools
     14  from PyQt4 import uic, QtGui,QtCore
     15  from pyforms.gui.Controls.ControlBase import ControlBase
     16
     17 -class GaugeWidgetVertical(ControlBase, QtGui.QWidget):
     18
     19 -    def __init__(self, *args, **kwargs):
     20          super(GaugeWidgetVertical, self).__init__(*args, **kwargs)
     21          self.setMouseTracking(True)
     22          self.setMinimumWidth(30)
     23
     24          self._lower = 0
     25          self._higher = 100
     26          self._minVal = 0
     27          self._maxVal = 75
     28          self._lastMouseY = None
     29          self._moving = False
     30          self._resizingBottom = False
     31          self._resizingTop = False
     32
     33
     34 -    def paintEvent(self,e):
     35          # call the base implementation to paint normal interface
     36          super(GaugeWidgetVertical, self).paintEvent(e)
     37
     38          draw = QtGui.QPainter()
     39          draw.begin(self)
     40
     41          w = self.width()-1
     42          diff = self._higher-self._lower
     43          self._step = float(self.height()) / float(diff)
     44          y_start = self.height()-(self._minVal-self._lower) * self._step
     45          y_end   = self.height()-(self._maxVal-self._lower) * self._step
     46          draw.setBrush(QtGui.QColor(100,100,255))
     47          draw.setPen(QtCore.Qt.NoPen )
     48          draw.drawRect(0, y_start, w, y_end-y_start)
     49
     50          draw.setBrush(QtCore.Qt.NoBrush)
     51          draw.setPen(QtGui.QColor(200,200,255) )
     52
     53          for i in range(self.height()/5,(self.height()-self.height()/5)+1, self.height()/5 ):
     54              draw.drawLine(0,i,w,i)
     55
     56          draw.setFont(QtGui.QFont('Decorative', 8))
     57          draw.setPen(QtGui.QColor(30,30,30) )
     58          draw.drawText(3,self.height()-3, "%d" % self._lower )
     59          draw.drawText(3,10, "%d" % self._higher )
     60
     61          draw.drawText(3, y_start+11, "%d" % self._minVal )
     62          draw.drawText(3, y_end-4, "%d" % self._maxVal )
     63
     64          draw.end()
     65
     66
     67
     68
     69 -    def mousePressEvent(self, event):
     70          self._moving = False
     71          self._resizingBottom = False
     72          self._resizingTop = False
     73
     74          y_start = self.height()-(self._minVal-self._lower) * self._step
     75          y_end   = self.height()-(self._maxVal-self._lower) * self._step
     76
     77          if event.buttons()==QtCore.Qt.LeftButton:
     78              if y_start>event.y()>y_end: self._moving = True
     79              elif event.y()<y_end:       self._resizingTop = True
     80              elif y_start<event.y():     self._resizingBottom = True
     81
     82 -    def mouseReleaseEvent(self, event):
     83          self._moving = False
     84          self._resizingBottom = False
     85          self._resizingTop = False
     86
     87 -    def mouseMoveEvent(self, event):
     88          super(GaugeWidgetVertical, self).mouseMoveEvent(event)
     89
     90          y_start = self.height()-(self._minVal-self._lower) * self._step
     91          y_end   = self.height()-(self._maxVal-self._lower) * self._step
     92
     93          if y_start<event.y() or event.y()<y_end:
     94              self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor ))
     95          else:
     96              self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor ))
     97
     98          if event.buttons()==QtCore.Qt.LeftButton:
     99              if self._lastMouseY!=None:
    100                  diff = self._lastMouseY - event.y()
    101                  if diff!=0:
    102                      if self._moving:
    103                          new_min = self._minVal + diff * 1/self._step
    104                          new_max = self._maxVal + diff * 1/self._step
    105                          if new_min<new_max:
    106                              self._minVal = new_min
    107                              self._maxVal = new_max
    108                      elif self._resizingTop:
    109                          new_max = self._maxVal + diff * 1/self._step
    110                          if self._minVal<new_max: self._maxVal = new_max
    111                      elif self._resizingBottom:
    112                          new_min = self._minVal + diff * 1/self._step
    113                          if new_min<self._maxVal: self._minVal = new_min
    114
    115
    116                      #if self._maxVal>self._higher: self._higher=self._maxVal
    117                      #if self._minVal<self._lower:  self._lower=self._minVal
    118
    119                      self.repaint()
    120
    121                      self.changed()
    122
    123              self._lastMouseY = event.y()
    124
    125 -    def mouseReleaseEvent(self, event):
    126          self._lastMouseY = None
    127
    128
    129
    130
    131
    132
    133
    134
    135 -class GaugeWidgetHorizontal(GaugeWidgetVertical):
    136
    137 -    def __init__(self, *args, **kwargs):
    138          super(GaugeWidgetHorizontal, self).__init__(*args, **kwargs)
    139          self.setMinimumHeight(30)
    140
    141
    142
    143 -    def paintEvent(self,e):
    144          # call the base implementation to paint normal interface
    145          QtGui.QWidget.paintEvent(self,e); draw = QtGui.QPainter();draw.begin(self)
    146
    147          h = self.height()-1
    148          diff = self._higher-self._lower
    149          self._step = float(self.width()) / float(diff)
    150          x_start = (self._minVal-self._lower) * self._step
    151          x_end   = (self._maxVal-self._lower) * self._step
    152          draw.setBrush(QtGui.QColor(100,100,255))
    153          draw.setPen(QtCore.Qt.NoPen )
    154          draw.drawRect(x_start, 0, x_end-x_start, h)
    155
    156          draw.setBrush(QtCore.Qt.NoBrush)
    157          draw.setPen(QtGui.QColor(200,200,255) )
    158
    159          for i in range(self.width()/5,(self.width()-self.width()/5)+1, self.width()/5 ): draw.drawLine(i,0,i,h)
    160
    161          draw.setFont(QtGui.QFont('Decorative', 8))
    162          draw.setPen(QtGui.QColor(30,30,30) )
    163
    164          boundtext = draw.boundingRect( QtCore.QRectF(), "%d" % self._higher )
    165          draw.drawText(self.width()-boundtext.width(),15, "%d" % self._higher )
    166          draw.drawText(0,15, "%d" % self._lower )
    167
    168          boundtext = draw.boundingRect( QtCore.QRectF(), "%d" % self._minVal )
    169          draw.drawText(x_start-2-boundtext.width(), 15, "%d" % self._minVal )
    170          draw.drawText(x_end + 2, 15, "%d" % self._maxVal )
    171
    172
    173          draw.end()
    174
    175
    176 -    def mousePressEvent(self, event):
    177          self._moving = False
    178          self._resizingBottom = False
    179          self._resizingTop = False
    180
    181          x_start = (self._minVal-self._lower) * self._step
    182          x_end   = (self._maxVal-self._lower) * self._step
    183
    184          if event.buttons()==QtCore.Qt.LeftButton:
    185              if x_start<event.x()<x_end: self._moving = True
    186              elif event.x()>x_end:       self._resizingTop = True
    187              elif x_start>event.x():     self._resizingBottom = True
    188
    189
    190 -    def mouseMoveEvent(self, event):
    191          super(GaugeWidgetVertical, self).mouseMoveEvent(event)
    192
    193          x_start = (self._minVal-self._lower) * self._step
    194          x_end   = (self._maxVal-self._lower) * self._step
    195
    196          if x_start>event.x() or event.x()>x_end: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeHorCursor ))
    197          else:  self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor ))
    198
    199          if event.buttons()==QtCore.Qt.LeftButton:
    200              if self._lastMouseY!=None:
    201                  diff = self._lastMouseY - event.x()
    202                  if diff!=0:
    203                      if self._moving:
    204                          new_min = self._minVal - diff * 1/self._step
    205                          new_max = self._maxVal - diff * 1/self._step
    206                          if new_min<new_max:
    207                              self._minVal = new_min
    208                              self._maxVal = new_max
    209                      elif self._resizingTop:
    210                          new_max = self._maxVal - diff * 1/self._step
    211                          if self._minVal<new_max: self._maxVal = new_max
    212                      elif self._resizingBottom:
    213                          new_min = self._minVal - diff * 1/self._step
    214                          if new_min<self._maxVal: self._minVal = new_min
    215
    216                      self.repaint()
    217                      self.changed()
    218
    219              self._lastMouseY = event.x()
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238 -class ControlBoundingSlider(ControlBase):
    239
    240      _min = 0
    241      _max = 100
    242
    243 -    def __init__(self, label = "", defaultValue = [20,40], min = 0, max = 100, horizontal=False, **kwargs):
    244          #self._min = min
    245          #self._max = max
    246          #self.value = defaultValue
    247          self._horizontal = horizontal
    248          ControlBase.__init__(self, label, defaultValue, **kwargs)
    249
    250 -    def initForm(self):
    251          if self._horizontal: self._form = GaugeWidgetHorizontal()
    252          else: self._form = GaugeWidgetVertical()
    253
    254 -    def _update(self, minval, maxval):
    255          self.value = minval, maxval
    256
    257
    258      @property
    259 -    def value(self):
    260          return self._form._minVal, self._form._maxVal
    261
    262      @value.setter
    263 -    def value(self, value):
    264          ControlBase.value.fset(self, value)
    265          self._form._minVal, self._form._maxVal = value[0], value[1]
    266
    267
    268      @property
    269 -    def min(self): return self._form._lower
    270
    271      @min.setter
    272 -    def min(self, value):
    273          self._form._lower = value
    274          self._form.repaint()
    275
    276      @property
    277 -    def max(self): return self._form._higher
    278
    279      @max.setter
    280 -    def max(self, value):
    281          self._form._higher = value
    282          self._form.repaint()
    283