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

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

      1  #!/usr/bin/python
      2  # -*- coding: utf-8 -*-
      3
      4  """ pyforms.gui.Controls.ControlEventTimeline.TimelinePopupWindow
      5
      6  """
      7
      8  from PyQt4 import uic
      9  from PyQt4 import QtCore, QtGui
     10  import pyforms.Utils.tools as tools
     11  # from pyforms.gui.BaseWidget import BaseWidget
     12  # from pyforms.gui.Controls.ControlButton import ControlButton
     13  # from pyforms.gui.Controls.ControlCombo import ControlCombo
     14
     15  __author__ = ["Ricardo Ribeiro", "Hugo Cachitas"]
     16  __credits__ = ["Ricardo Ribeiro", "Hugo Cachitas"]
     17  __license__ = "MIT"
     18  __version__ = "0.0"
     19  __maintainer__ = "Ricardo Ribeiro"
     20  __email__ = "ricardojvr@gmail.com"
     21  __status__ = "Development"
     22
     23
     24 -class TimelinePopupWindow(QtGui.QDialog):
     25      """
     26      Opens a dialog where the user can specify the behavior annotated
     27      in the selected line, as well as some related options.
     28
     29      The parent timeline widget must be given, as well as the track
     30      identifier to edit.
     31      """
     32
     33 -    def __init__(self, parent, track_id):
     34          super(TimelinePopupWindow, self).__init__(parent=parent)
     35          self._parent = parent
     36          control_path = tools.getFileInSameDirectory(
     37              __file__, "TimelinePopupWindow.ui")
     38          self._ui = uic.loadUi(control_path)
     39          self._ui.setWindowTitle("Track {:d} properties".format(track_id + 1))
     40
     41          # Dialog variables
     42          self.behaviors = []
     43          self.behavior = None
     44          self.color = self._parent.color
     45          self.current_track = track_id
     46
     47          self._default_comboBox_text = "Add a new behavior"
     48          self.__get_existing_tracklabels()
     49
     50          # Set default color display
     51          self.__preview_color()
     52
     53          # SIGNALS
     54          self._ui.comboBox.currentIndexChanged.connect(
     55              self.__on_comboBox_change)
     56          self._ui.pushButton_add.clicked.connect(self.__add_behavior)
     57          self._ui.pushButton_remove.clicked.connect(self.__remove_behavior)
     58          self._ui.pushButton_color.clicked.connect(self.__pick_color)
     59
     60 -    def __on_comboBox_change(self):
     61          """Handles comboBox index change."""
     62          cb = self._ui.comboBox
     63          self.behavior = cb.itemText(cb.currentIndex())
     64
     65 -    def __add_behavior(self):
     66          """Add a behavior to the already existing ones."""
     67          cb = self._ui.comboBox
     68          text, ok = QtGui.QInputDialog.getText(
     69              self, 'Add behavior', 'Description:', text='')
     70          if ok:
     71              self.behavior = str(text)
     72              self.behaviors.append(self.behavior)
     73              self._ui.comboBox.addItem(self.behavior)
     74              cb.setCurrentIndex(cb.findText(self.behavior))
     75
     76          # If adding the first item, we need to enable the comboBox and
     77          # remove the placeholder text
     78          if not cb.isEnabled():
     79              cb.removeItem(cb.findText(self._default_comboBox_text))
     80              cb.setEnabled(True)
     81
     82 -    def __remove_behavior(self):
     83          """Remove a behavior from the already existing ones."""
     84          cb = self._ui.comboBox
     85          i = cb.currentIndex()
     86          self.behaviors.remove(str(cb.itemText(i)))
     87          cb.removeItem(i)
     88
     89          # If there are no behaviors assigned, just fill the comboBox
     90          # with a placeholder
     91          if cb.count() < 1:
     92              cb.addItem(self._default_comboBox_text)
     93              cb.setEnabled(False)
     94
     95 -    def __pick_color(self):
     96          """Dialog to choose a color."""
     97          self.color = QtGui.QColorDialog.getColor(self.color)
     98          self.__preview_color()
     99
    100 -    def __preview_color(self):
    101          """
    102          Shows selected colors in two QLabel widgets.
    103
    104          The first shows true color, while the second presents the color
    105          with an opacity as seen in the timeline and with some dummy text
    106          to preview readability.
    107          """
    108          pixmap = QtGui.QPixmap(50, 25)
    109          color = QtGui.QColor(*self.color.getRgb())
    110
    111          # Preview color
    112          color.setAlpha(int(255 * 1.0))
    113          pixmap.fill(color)
    114          self._ui.label_color.setPixmap(pixmap)
    115
    116          # Preview color with transparency and some text
    117          color.setAlpha(int(255 * 0.5))
    118          pixmap.fill(color)
    119          painter = QtGui.QPainter(pixmap)
    120          painter.setFont(QtGui.QFont('Decorative', 8))
    121          painter.drawText(pixmap.rect(), QtCore.Qt.AlignCenter, "Text")
    122          painter.end()
    123          self._ui.label_color_alpha.setPixmap(pixmap)
    124
    125 -    def __get_existing_tracklabels(self):
    126          """
    127          Gets existing track labels already assigned.
    128
    129          Scans the timeline track labels and sets the comboBox value
    130          accordingly.
    131          """
    132          d = self._parent._tracks_info
    133          cb = self._ui.comboBox
    134
    135          # Loop across timeline labels
    136          for key, value in d.items():
    137              # If there is already an assigned label, append it to the
    138              # behaviors list and to the comboBox (if not a duplicate)
    139              if value[0] != self._parent._defaulttracklabel:
    140                  self.behavior = value[0]
    141                  if self.behavior not in self.behaviors:
    142                      cb.addItem(self.behavior)
    143                      self.behaviors.append(self.behavior)
    144
    145                  # Set comboBox value to the one of the selected track
    146                  if key == self.current_track:
    147
    148                      cb.setCurrentIndex(cb.findText(self.behavior))
    149
    150          # If there are no behaviors assigned yet, just fill the comboBox
    151          # with a placeholder
    152          if cb.count() < 1:
    153              cb.addItem(self._default_comboBox_text)
    154              cb.setEnabled(False)
    155
    156
    157  ########################################################################
    158  ###
    159  # MAYBE IN THE FUTURE IMPLEMENT THIS USING THE BaseWidget
    160
    161  # class TimelinePopupWindow(BaseWidget):
    162  #     """
    163  #     Opens a dialog where the user can specify the behavior annotaded
    164  #     in the selected line, as well as some related options.
    165  #     """
    166
    167  #     def __init__(self, parent=None):
    168  #         super(TimelinePopupWindow, self).__init__('Adjust line options')
    169
    170  #         if parent is not None:
    171  #             self.parent = parent
    172
    173  #         self._behaviors = None
    174  #         self._behaviorname  = None
    175  #         self._linecolor = None
    176
    177  #         # Combobox
    178  #         self._combobox = ControlCombo()
    179
    180  #         # Buttons
    181  #         self._btn_add = ControlButton()
    182  #         self._btn_remove = ControlButton()
    183  #         self._btn_import = ControlButton()
    184  #         self._btn_export = ControlButton()
    185  #         self._btn_color = ControlButton()
    186  #         self._btn_ok = ControlButton()
    187  #         self._btn_cancel = ControlButton()
    188
    189  #         self._formset = ['_combobox',
    190  #                          ('_btn_add', '_btn_remove', '_btn_import', '_btn_export'),
    191  #                          ('_btn_ok', '_btn_cancel')]
    192
    193  #         def __choose_color(self):
    194  #             pass
    195  #             QtGui.QColorDialog.getColor()
    196