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

# Source Code for Module Camelot.camelot.view.controls.decorated_line_edit

      1  #  ============================================================================
      2  #
      3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.
      4  #  www.conceptive.be / project-camelot@conceptive.be
      5  #
      6  #  This file is part of the Camelot Library.
      7  #
      8  #  This file may be used under the terms of the GNU General Public
      9  #  License version 2.0 as published by the Free Software Foundation
     10  #  and appearing in the file LICENSE.GPL included in the packaging of
     11  #  this file.  Please review the following information to ensure GNU
     12  #  General Public Licensing requirements will be met:
     13  #  http://www.trolltech.com/products/qt/opensource.html
     14  #
     15  #  If you are unsure which license is appropriate for your use, please
     16  #  review the following information:
     17  #  http://www.trolltech.com/products/qt/licensing.html or contact
     18  #  project-camelot@conceptive.be.
     19  #
     20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
     21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22  #
     23  #  For use of this library in commercial applications, please contact
     24  #  project-camelot@conceptive.be
     25  #
     26  #  ============================================================================
     27
     28  from PyQt4 import QtGui
     29
     30 -class DecoratedLineEdit(QtGui.QLineEdit):
     31      """
     32      A QLineEdit with additional decorations :
     33
     34       * a background text, visible when the line edit doesn't contain any text
     35       * a validity, which will trigger the background color
     36
     37      Use the user_input method to get the text that was entered by the user.
     38      """
     39
     40 -    def __init__(self, parent = None):
     41          QtGui.QLineEdit.__init__(self, parent)
     42          self._foreground_color = self.palette().color(self.foregroundRole())
     43          self._background_color = self.palette().color(self.backgroundRole())
     44          self._showing_background_text = False
     45          self._background_text = None
     46          self._valid = True
     47
     48 -    def set_valid(self, valid):
     49          """Set the validity of the current content of the line edit
     50          :param valid: True or False
     51          """
     52          if valid!=self._valid:
     53              self._valid = valid
     54              self._update_background_color()
     55
     56 -    def set_background_text(self, background_text):
     57          """Set the text to be displayed in the background when the line
     58          input does not contain any text
     59          :param background_text: the text to be shown, None if no text should be shown
     60          """
     61          self._hide_background_text()
     62          self._background_text = background_text
     63          if not self.hasFocus() and background_text!=None:
     64              self._show_background_text()
     65
     66 -    def _show_background_text(self):
     67          if not self._showing_background_text and self.text().isEmpty() and self._background_text!=None:
     68              self._showing_background_text = True
     69              self._update_foreground_color()
     70              self.setText(unicode(self._background_text))
     71
     72 -    def _hide_background_text(self):
     73          if self._showing_background_text and self._background_text!=None:
     74              self._showing_background_text = False
     75              self._update_foreground_color()
     76              self.setText('')
     77
     78 -    def _update_background_color(self):
     79          from camelot.view.art import ColorScheme
     80          palette = self.palette()
     81          if self._valid:
     82              palette.setColor(self.backgroundRole(), self._background_color)
     83          else:
     84              palette.setColor(self.backgroundRole(), ColorScheme.orange_2)
     85          self.setPalette(palette)
     86
     87 -    def _update_foreground_color(self):
     88          from camelot.view.art import ColorScheme
     89          palette = self.palette()
     90          if self._showing_background_text:
     91              palette.setColor(self.foregroundRole(), ColorScheme.aluminium_1)
     92          else:
     93              palette.setColor(self.foregroundRole(), self._foreground_color)
     94          self.setPalette(palette)
     95
     96 -    def focusInEvent(self, e):
     97          self._hide_background_text()
     98          QtGui.QLineEdit.focusInEvent(self, e)
     99
    100 -    def focusOutEvent(self, e):
    101          self._show_background_text()
    102          self._update_foreground_color()
    103          QtGui.QLineEdit.focusOutEvent(self, e)
    104
    105 -    def user_input(self):
    106          if self._showing_background_text:
    107              return u''
    108          return unicode(self.text())
    109
    110 -    def set_user_input(self, text):
    111          if text!=None:
    112              self._hide_background_text()
    113              self.setText(text)
    114          else:
    115              if not self.hasFocus() and not self._showing_background_text:
    116                  self.setText('')
    117                  self._show_background_text()
    118