Package Camelot :: Package camelot :: Package view :: Package controls :: Package editors :: Module codeeditor |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.editors.codeeditor

     1  from PyQt4 import QtGui, QtCore
     2  from PyQt4.QtCore import Qt
     3

     4  from customeditor import CustomEditor
     5  import re
     6

     7 -class PartEditor(QtGui.QLineEdit):
     8

     9 -    def __init__(self, mask):
    10          super(PartEditor, self).__init__()
    11          self.setInputMask(mask)
    12          self.setCursorPosition(0)
    13

    14 -class CodeEditor(CustomEditor):
    15

    16 -    def __init__(self, parent=None, parts=['99','AA'], editable=True, **kwargs):
    17          CustomEditor.__init__(self, parent)
    18          self.setFocusPolicy(Qt.StrongFocus)
    19          self.parts = parts
    20          self.part_editors = []
    21          layout = QtGui.QHBoxLayout()
    22          layout.setMargin(0)
    23          layout.setSpacing(0)
    24          layout.setAlignment(Qt.AlignLeft)
    25          for part in parts:
    26              part = re.sub('\W*', '', part)
    27              part_length = len(part)
    28              editor = PartEditor(part)
    29              if not editable:
    30                  editor.setEnabled(False)
    31              space_width = editor.fontMetrics().size(Qt.TextSingleLine, 'A').width()
    32              editor.setMaximumWidth(space_width*(part_length+1))
    33              self.part_editors.append(editor)
    34              layout.addWidget(editor)
    35              self.connect(editor,

    36                           QtCore.SIGNAL('editingFinished()'),

    37                           self.editingFinished)
    38          self.setLayout(layout)
    39

    40 -    def editingFinished(self):
    41          self.emit(QtCore.SIGNAL('editingFinished()'))
    42

    43 -    def focusOutEvent(self, event):
    44          self.emit(QtCore.SIGNAL('editingFinished()'))
    45

    46 -    def set_enabled(self, editable=True):
    47          for editor in self.part_editors:
    48              value = editor.text()
    49              editor.setEnabled(editable)
    50              editor.setText(value)
    51

    52 -    def set_value(self, value):
    53          value = CustomEditor.set_value(self, value)
    54          if value:
    55              old_value = self.get_value()
    56              if value!=old_value:
    57                  for part_editor, part in zip(self.part_editors, value):
    58                      part_editor.setText(unicode(part))
    59          else:
    60              for part_editor in self.part_editors:
    61                  part_editor.setText(u'')
    62

    63 -    def get_value(self):
    64          value = []
    65          for part in self.part_editors:
    66              value.append(unicode(part.text()))
    67          return CustomEditor.get_value(self) or value
    68

    69 -    def set_background_color(self, background_color):
    70          if background_color:
    71              for editor in self.part_editors:
    72                  palette = editor.palette()
    73                  palette.setColor(editor.backgroundRole(), background_color)
    74                  editor.setPalette(palette)
    75          else:
    76              return False
    77