Added a line edit widget with an internal clear button to E5LineEdit.py. 5_2_x

Fri, 09 Nov 2012 16:34:35 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 09 Nov 2012 16:34:35 +0100
branch
5_2_x
changeset 2200
719cf8ba4743
parent 2198
602e28b8bd76
child 2201
2cb2e3bb4f3f

Added a line edit widget with an internal clear button to E5LineEdit.py.

E5Gui/E5LineEdit.py file | annotate | diff | comparison | revisions
diff -r 602e28b8bd76 -r 719cf8ba4743 E5Gui/E5LineEdit.py
--- a/E5Gui/E5LineEdit.py	Thu Nov 08 15:28:53 2012 +0100
+++ b/E5Gui/E5LineEdit.py	Fri Nov 09 16:34:35 2012 +0100
@@ -11,8 +11,12 @@
 from PyQt4.QtGui import QLineEdit, QStyleOptionFrameV2, QStyle, QPainter, QPalette, \
     QWidget, QHBoxLayout, QBoxLayout, QLayout, QApplication, QSpacerItem, QSizePolicy
 
+from E5Gui.E5LineEditButton import E5LineEditButton
 
-class SideWidget(QWidget):
+import UI.PixmapCache
+
+
+class E5LineEditSideWidget(QWidget):
     """
     Class implementing the side widgets for the line edit class.
     """
@@ -58,7 +62,7 @@
         
         self.__inactiveText = inactiveText
         
-        self.__leftWidget = SideWidget(self)
+        self.__leftWidget = E5LineEditSideWidget(self)
         self.__leftWidget.resize(0, 0)
         self.__leftLayout = QHBoxLayout(self.__leftWidget)
         self.__leftLayout.setContentsMargins(0, 0, 0, 0)
@@ -68,7 +72,7 @@
             self.__leftLayout.setDirection(QBoxLayout.LeftToRight)
         self.__leftLayout.setSizeConstraint(QLayout.SetFixedSize)
         
-        self.__rightWidget = SideWidget(self)
+        self.__rightWidget = E5LineEditSideWidget(self)
         self.__rightWidget.resize(0, 0)
         self.__rightLayout = QHBoxLayout(self.__rightWidget)
         self.__rightLayout.setContentsMargins(0, 0, 0, 0)
@@ -250,3 +254,38 @@
         """
         self.__inactiveText = inactiveText
         self.update()
+
+
+class E5ClearableLineEdit(E5LineEdit):
+    """
+    Class implementing a line edit widget showing some inactive text and a clear button,
+    if it has some contents.
+    """
+    def __init__(self, parent=None, inactiveText="", side=E5LineEdit.RightSide):
+        """
+        Constructor
+        
+        @param parent reference to the parent widget (QWidget)
+        @keyparam inactiveText text to be shown on inactivity (string)
+        @keyparam side side the clear button should be shown at (E5LineEdit.RightSide,
+            E5LineEdit.LeftSide)
+        """
+        assert side in [E5LineEdit.RightSide, E5LineEdit.LeftSide]
+        
+        super().__init__(parent, inactiveText)
+        
+        self.__clearButton = E5LineEditButton(self)
+        self.__clearButton.setIcon(UI.PixmapCache.getIcon("clearLeft.png"))
+        self.addWidget(self.__clearButton, side)
+        self.__clearButton.setVisible(False)
+        
+        self.__clearButton.clicked[()].connect(self.clear)
+        self.textChanged.connect(self.__textChanged)
+    
+    def __textChanged(self, txt):
+        """
+        Private slot to handle changes of the text.
+        
+        @param txt text (string)
+        """
+        self.__clearButton.setVisible(txt != "")

eric ide

mercurial