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

# Source Code for Module pyforms.gui.Controls.ControlEventTimeline.TimelineDelta

      1  #!/usr/bin/python
      2  # -*- coding: utf-8 -*-
      3
      4  """ pyforms.gui.Controls.ControlEventTimeline.TimelineDelta
      5
      6  """
      7
      8  from PyQt4 import QtGui
      9
     10  __author__ = ["Ricardo Ribeiro", "Hugo Cachitas"]
     11  __credits__ = ["Ricardo Ribeiro", "Hugo Cachitas"]
     12  __license__ = "MIT"
     13  __version__ = "0.0"
     14  __maintainer__ = "Ricardo Ribeiro"
     15  __email__ = "ricardojvr@gmail.com"
     16  __status__ = "Development"
     17
     18
     19 -class TimelineDelta(object):
     20
     21 -    def __init__(self, begin, end=30, title=None, height=30, top=0, parent=None):
     22          self._top = top
     23          self._height = height
     24          self._parent = parent
     25          self._title = title
     26          self._lock = False
     27          if self.track not in parent._tracks_info.keys():
     28              self._defautcolor = parent._defautcolor
     29          else:
     30              self._defautcolor = parent._tracks_info[self.track][1]
     31          self._begin = begin
     32          self._end = end
     33
     34          if self.track >= self._parent.numberoftracks:
     35              self._parent.numberoftracks = self.track + 1
     36
     37      ##########################################################################
     38      #### HELPERS/FUNCTIONS ###################################################
     39      ##########################################################################
     40
     41 -    def collide(self, x, y):
     42          return self.begin <= x <= self.end and self._top <= y <= (self._top + self._height)
     43
     44 -    def canSlideBegin(self, x, y):
     45          return not self._lock and x == int(round(self.begin)) and self._top <= y <= (self._top + self._height)
     46
     47 -    def canSlideEnd(self, x, y):
     48          return not self._lock and int(round(self.end)) == x and self._top <= y <= (self._top + self._height)
     49
     50 -    def moveEnd(self, x):
     51          """Move the right edge of the event rectangle."""
     52          # Do nothing if locked
     53          if self._lock:
     54              return
     55
     56          #  Do nothing if trying to go over the pther edge
     57          if self._end <= self._begin - x and x < 0:
     58              return
     59
     60          # Increment accordingly
     61          self._end += x / self._parent._scale
     62
     63          # Minimum begin position is at 0
     64          if self._end > (self._parent.width() / self._parent._scale):
     65              self._end = (self._parent.width() / self._parent._scale)
     66
     67 -    def moveBegin(self, x):
     68          """Move the left edge of the event rectangle."""
     69          # Do nothing if locked
     70          if self._lock:
     71              return
     72
     73          #  Do nothing if trying to go over the other edge
     74          if self._begin >= self._end - x and x > 0:
     75              return
     76
     77          # Increment accordingly
     78          self._begin += x / self._parent._scale
     79
     80          # Minimum begin position is at 0
     81          if self._begin < 0:
     82              self._begin = 0
     83
     84 -    def move(self, x, y):
     85          if self._lock:
     86              return
     87          if (self.begin + x) >= 0 and (self.end + x) <= self._parent.width():
     88              self._begin += x / self._parent._scale
     89              self._end += x / self._parent._scale
     90          current_track = self.track
     91          new_track = (y - 20) // 34
     92          if current_track != new_track and new_track >= 0 and new_track <= self._parent.numberoftracks:
     93              self.track = new_track
     94              if new_track >= self._parent.numberoftracks:
     95                  self._parent.numberoftracks += 1
     96
     97 -    def showEditWindow(self):
     98          text, ok = QtGui.QInputDialog.getText(
     99              self._parent, 'Edit event',  'Comment:', text=self._title)
    100          if ok:
    101              self._title = str(text)
    102
    103 -    def draw(self, painter, showvalues=False):
    104          start, end = self.begin, self.end
    105          if self._lock:
    106              transparency = 0.1
    107          else:
    108              transparency = 0.5
    109          painter.setPen(QtGui.QColor(0, 0, 0))
    110          painter.setOpacity(transparency)
    111          painter.drawRoundedRect(
    112              start, self._top, end - start, self._height, 3, 3)
    113          painter.setOpacity(1.0)
    114
    115          painter.drawText(start + 3, self._top + 19, self._title)
    116          if showvalues:
    117              painter.drawText(
    118                  start, self._top + 44, "[%d;%d] delta:%d" % (self._begin, self._end, self._end - self._begin))
    119
    120      ##########################################################################
    121      #### PROPERTIES ##########################################################
    122      ##########################################################################
    123
    124      @property
    125 -    def lock(self): return self._lock
    126
    127      @lock.setter
    128 -    def lock(self, value): self._lock = value
    129
    130      @property
    131 -    def begin(self): return self._begin * self._parent._scale
    132
    133      @begin.setter
    134 -    def begin(self, value):
    135          if self._lock:
    136              return
    137          self._begin = value / self._parent._scale
    138          if self._begin < 0:
    139              self._begin = 0
    140
    141      @property
    142 -    def end(self): return self._end * self._parent._scale
    143
    144      @end.setter
    145 -    def end(self, value):
    146          if self._lock:
    147              return
    148          self._end = value / self._parent._scale
    149          if self._end > (self._parent.width() / self._parent._scale):
    150              self._end = (self._parent.width() / self._parent._scale)
    151
    152      @property
    153 -    def track(self):
    154          return (self._top - 20) // 34
    155
    156      @track.setter
    157 -    def track(self, value):
    158          # FIXME This was preventing assigning a track when importing ingo locked
    159          # Is it needed?
    160          # if self._lock: return
    161          self._top = value * 34 + 20
    162
    163      @property
    164 -    def color(self): return self._defautcolor
    165
    166      @color.setter
    167 -    def color(self, value):
    168          if type(value) == str:
    169              self._defautcolor = QtGui.QColor(value)
    170          else:
    171              self._defautcolor = value
    172
    173      # @property
    174      # def row(self):
    175      #     return [    int(round(self._begin)),
    176      #                 int(round(self._end)),
    177      #                 self._title,
    178      #                 self.track,
    179      #                 self._defautcolor.name(),
    180      #                 self._lock ]
    181
    182      # @row.setter
    183      # def row(self, value):
    184      #     self._begin = int(value[0])
    185      #     self._end = int(value[1])
    186      #     self._title = value[2]
    187      #     self.track = int(value[3])
    188      #     self._defautcolor = QtGui.QColor(value[4])
    189      #     self._lock = value[5]=='True'
    190
    191      #     if self.track>=self._parent.numberoftracks: self._parent.numberoftracks = self.track+1
    192
    193      # TODO redefinition of the property above to write to file in the new format
    194      # This is a bad idea, better to access these individually
    195      @property
    196 -    def properties(self):
    197          return [self._lock,
    198                  int(round(self._begin)),
    199                  int(round(self._end)),
    200                  self._title,
    201                  self._defautcolor.name(),
    202                  self.track]
    203
    204      @properties.setter
    205 -    def properties(self, value):
    206          self._lock = value[0] == 'True'
    207          self._begin = int(value[1])
    208          self._end = int(value[2])
    209          self._title = value[3]
    210          self._defautcolor = QtGui.QColor(value[4])
    211          self.track = int(value[5])
    212
    213          if self.track >= self._parent.numberoftracks:
    214              self._parent.numberoftracks = self.track + 1
    215