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

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

      1

      2  import re
      3

      4  from PyQt4 import QtGui, QtCore
      5  from PyQt4.QtCore import Qt
      6

      7  from customeditor import CustomEditor, editingFinished
      8  from camelot.view.art import Icon
      9  import camelot.types
     10

     11 -class VirtualAddressEditor(CustomEditor):
     12

     13 -    def __init__(self, parent=None, editable=True, **kwargs):
     14          CustomEditor.__init__(self, parent)
     15          self.layout = QtGui.QHBoxLayout()
     16          self.layout.setMargin(0)
     17          self.combo = QtGui.QComboBox()
     18          self.combo.addItems(camelot.types.VirtualAddress.virtual_address_types)
     19          self.combo.setEnabled(editable)
     20          self.layout.addWidget(self.combo)
     21          self.editor = QtGui.QLineEdit()
     22          self.editor.setEnabled(editable)
     23          self.layout.addWidget(self.editor)
     24          self.setFocusProxy(self.editor)
     25          self.editable = editable
     26          nullIcon = Icon('tango/16x16/apps/internet-mail.png').getQIcon()
     27          self.label = QtGui.QToolButton()
     28          self.label.setIcon(nullIcon)
     29          self.label.setAutoFillBackground(False)
     30          self.label.setAutoRaise(True)
     31          self.label.setEnabled(False)
     32          self.label.setToolButtonStyle(Qt.ToolButtonIconOnly)
     33

     34          self.layout.addWidget(self.label)
     35          self.connect(self.editor,

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

     37                       self.editingFinished)
     38          self.connect(self.editor,

     39                       QtCore.SIGNAL('textEdited(const QString&)'),

     40                       self.editorValueChanged)
     41          self.connect(self.combo,

     42                       QtCore.SIGNAL('currentIndexChanged(int)'),

     43                       lambda:self.comboIndexChanged())
     44          self.setLayout(self.layout)
     45          self.setAutoFillBackground(True)
     46          self.checkValue(self.editor.text())
     47

     48 -    def comboIndexChanged(self):
     49          self.checkValue(self.editor.text())
     50          self.editingFinished()
     51

     52 -    def set_value(self, value):
     53          value = CustomEditor.set_value(self, value)
     54          if value:
     55              self.editor.setText(value[1])
     56              idx = camelot.types.VirtualAddress.virtual_address_types.index(value[0])
     57              self.combo.setCurrentIndex(idx)
     58              icon = Icon('tango/16x16/devices/printer.png').getQIcon()
     59  # These icons don't exist any more in the new tango icon set

     60  #            if str(self.combo.currentText()) == 'phone':

     61  #                icon = Icon('tango/16x16/devices/phone.png').getQIcon()

     62              if str(self.combo.currentText()) == 'fax':
     63                  icon = Icon('tango/16x16/devices/printer.png').getQIcon()
     64  #            if str(self.combo.currentText()) == 'mobile':

     65  #                icon = Icon('tango/16x16/devices/mobile.png').getQIcon()

     66  #            if str(self.combo.currentText()) == 'im':

     67  #                icon = Icon('tango/16x16/places/instant-messaging.png').getQIcon()

     68  #            if str(self.combo.currentText()) == 'pager':

     69  #                icon = Icon('tango/16x16/devices/pager.png').getQIcon()

     70              if str(self.combo.currentText()) == 'email':
     71                  icon = Icon('tango/16x16/apps/internet-mail.png').getQIcon()
     72                  #self.label.setFocusPolicy(Qt.StrongFocus)

     73                  self.label.setAutoRaise(True)
     74                  #self.label.setAutoFillBackground(True)

     75                  self.label.setIcon(icon)
     76                  self.label.setEnabled(self.editable)
     77                  self.connect(self.label,

     78                               QtCore.SIGNAL('clicked()'),

     79                               lambda:self.mailClick(self.editor.text()))
     80              else:
     81                  self.label.setIcon(icon)
     82                  #self.label.setAutoFillBackground(False)

     83                  self.label.setAutoRaise(True)
     84                  self.label.setEnabled(self.editable)
     85                  self.label.setToolButtonStyle(Qt.ToolButtonIconOnly)
     86

     87  #      self.update()

     88  #      self.label.update()

     89  #      self.layout.update()

     90

     91

     92              self.checkValue(value[1])
     93

     94 -    def get_value(self):
     95          value = (unicode(self.combo.currentText()), unicode(self.editor.text()))
     96          return CustomEditor.get_value(self) or value
     97

     98 -    def set_enabled(self, editable=True):
     99          self.combo.setEnabled(editable)
    100          self.editor.setEnabled(editable)
    101          if not editable:
    102              self.label.setEnabled(False)
    103          else:
    104              if self.combo.currentText() == 'email':
    105                  self.label.setEnabled(True)
    106

    107

    108 -    def checkValue(self, text):
    109          if self.combo.currentText() == 'email':
    110              email = unicode(text)
    111              mailCheck = re.compile('^\S+@\S+\.\S+$')
    112              if not mailCheck.match(email):
    113                  palette = self.editor.palette()
    114                  palette.setColor(QtGui.QPalette.Active,

    115                                   QtGui.QPalette.Base,

    116                                   QtGui.QColor(255, 0, 0))
    117                  self.editor.setPalette(palette)
    118              else:
    119                  palette = self.editor.palette()
    120                  palette.setColor(QtGui.QPalette.Active,

    121                                   QtGui.QPalette.Base,

    122                                   QtGui.QColor(255, 255, 255))
    123                  self.editor.setPalette(palette)
    124

    125          elif self.combo.currentText() == 'phone' \

    126           or self.combo.currentText() == 'pager' \

    127           or self.combo.currentText() == 'fax' \

    128           or self.combo.currentText() == 'mobile':
    129

    130              number = unicode(text)
    131              numberCheck = re.compile('^[0-9 ]+$')
    132

    133              if not numberCheck.match(number):
    134                  palette = self.editor.palette()
    135                  palette.setColor(QtGui.QPalette.Active,

    136                                   QtGui.QPalette.Base,

    137                                   QtGui.QColor(255, 0, 0))
    138                  self.editor.setPalette(palette)
    139              else:
    140                  palette = self.editor.palette()
    141                  palette.setColor(QtGui.QPalette.Active,

    142                                   QtGui.QPalette.Base,

    143                                   QtGui.QColor(255, 255, 255))
    144                  self.editor.setPalette(palette)
    145

    146          else:
    147              Check = re.compile('^.+$')
    148              if not Check.match(unicode(text)):
    149                  palette = self.editor.palette()
    150                  palette.setColor(QtGui.QPalette.Active,

    151                                   QtGui.QPalette.Base,

    152                                    QtGui.QColor(255, 0, 0))
    153                  self.editor.setPalette(palette)
    154              else:
    155                  palette = self.editor.palette()
    156                  palette.setColor(QtGui.QPalette.Active,

    157                                    QtGui.QPalette.Base,

    158                                    QtGui.QColor(255, 255, 255))
    159                  self.editor.setPalette(palette)
    160

    161 -    def editorValueChanged(self, text):
    162          self.checkValue(text)
    163

    164 -    def mailClick(self, adress):
    165          url = QtCore.QUrl()
    166          url.setUrl('mailto:%s?subject=Subject'%str(adress))
    167          QtGui.QDesktopServices.openUrl(url)
    168

    169 -    def editingFinished(self):
    170          self.value = []
    171          self.value.append(str(self.combo.currentText()))
    172          self.value.append(str(self.editor.text()))
    173          self.set_value(self.value)
    174          self.label.setFocus()
    175          # emiting editingFinished without a value for the mechanism itself will lead to

    176          # integrity errors

    177          if self.value[1]:
    178              self.emit(editingFinished)
    179

    180 -    def set_background_color(self, background_color):
    181          if background_color:
    182              palette = self.editor.palette()
    183              palette.setColor(self.backgroundRole(), background_color)
    184              self.editor.setPalette(palette)
    185          else:
    186              return False
    187