eric7/EricWidgets/EricTextEditSearchWidget.py

branch
eric7
changeset 8690
25f68ec4181a
parent 8553
10d31e5ce9e5
child 8702
131ef7267fd4
--- a/eric7/EricWidgets/EricTextEditSearchWidget.py	Fri Oct 15 20:09:38 2021 +0200
+++ b/eric7/EricWidgets/EricTextEditSearchWidget.py	Sat Oct 16 20:36:12 2021 +0200
@@ -33,7 +33,7 @@
     """
     Class implementing a horizontal search widget for QTextEdit.
     """
-    def __init__(self, parent=None, widthForHeight=True):
+    def __init__(self, parent=None, widthForHeight=True, enableClose=False):
         """
         Constructor
         
@@ -43,9 +43,11 @@
             If this parameter is False, some widgets are shown in a third
             line.
         @type bool
+        @param enableClose flag indicating to show a close button
+        @type bool
         """
         super().__init__(parent)
-        self.__setupUi(widthForHeight)
+        self.__setupUi(widthForHeight, enableClose)
         
         self.__textedit = None
         self.__texteditType = EricTextEditType.UNKNOWN
@@ -71,12 +73,14 @@
         
         self.setFocusProxy(self.findtextCombo)
     
-    def __setupUi(self, widthForHeight):
+    def __setupUi(self, widthForHeight, enableClose):
         """
         Private method to generate the UI.
         
         @param widthForHeight flag indicating to prefer width for height
         @type bool
+        @param enableClose flag indicating to show a close button
+        @type bool
         """
         self.setObjectName("EricTextEditSearchWidget")
         
@@ -88,6 +92,14 @@
         self.horizontalLayout1 = QHBoxLayout()
         self.horizontalLayout1.setObjectName("horizontalLayout1")
         
+        if enableClose:
+            self.closeButton = QToolButton(self)
+            self.closeButton.setIcon(UI.PixmapCache.getIcon("close"))
+            self.closeButton.clicked.connect(self.__closeButtonClicked)
+            self.horizontalLayout1.addWidget(self.closeButton)
+        else:
+            self.closeButton = None
+        
         self.label = QLabel(self)
         self.label.setObjectName("label")
         self.label.setText(self.tr("Find:"))
@@ -193,34 +205,82 @@
     
     def attachTextEdit(self, textedit, editType=EricTextEditType.QTEXTEDIT):
         """
-        Public method to attach a QTextEdit widget.
+        Public method to attach a QTextEdit or QWebEngineView widget.
         
         @param textedit reference to the edit widget to be attached
-        @type QTextEdit, QWebEngineView or QWebView
+        @type QTextEdit, QTextBrowser or QWebEngineView
         @param editType type of the attached edit widget
         @type EricTextEditType
         """
+        if self.__textedit is not None:
+            self.detachTextEdit()
+        
         self.__textedit = textedit
         self.__texteditType = editType
         
-        self.wordCheckBox.setVisible(editType == "QTextEdit")
+        self.wordCheckBox.setVisible(editType in (
+            EricTextEditType.QTEXTEDIT, EricTextEditType.QTEXTBROWSER
+        ))
+        if editType == EricTextEditType.QWEBENGINEVIEW:
+            self.__textedit.page().findTextFinished.connect(
+                self.__findTextFinished)
+    
+    def detachTextEdit(self):
+        """
+        Public method to detach the current text edit.
+        """
+        if self.__texteditType == EricTextEditType.QWEBENGINEVIEW:
+            self.__textedit.page().findTextFinished.disconnect(
+                self.__findTextFinished)
+        
+        self.__textedit = None
+        self.__texteditType = EricTextEditType.UNKNOWN
+    
+    @pyqtSlot()
+    def __closeButtonClicked(self):
+        """
+        Private slot to close the widget.
+        
+        Note: The widget is just hidden.
+        """
+        self.__textedit.setFocus(Qt.FocusReason.ActiveWindowFocusReason)
+        self.hide()
     
     def keyPressEvent(self, event):
         """
         Protected slot to handle key press events.
         
-        @param event reference to the key press event (QKeyEvent)
+        @param event reference to the key press event
+        @type QKeyEvent
         """
-        if self.__textedit and event.key() == Qt.Key.Key_Escape:
-            self.__textedit.setFocus(Qt.FocusReason.ActiveWindowFocusReason)
-            event.accept()
+        if self.__textedit:
+            key = event.key()
+            modifiers = event.modifiers()
+            
+            if key == Qt.Key.Key_Escape:
+                self.__textedit.setFocus(
+                    Qt.FocusReason.ActiveWindowFocusReason)
+                if self.closeButton is not None:
+                    self.hide()
+                event.accept()
+            
+            elif key == Qt.Key.Key_F3:
+                if modifiers == Qt.KeyboardModifier.NoModifier:
+                    # search forward
+                    self.on_findNextButton_clicked()
+                    event.accept()
+                elif modifiers == Qt.KeyboardModifier.ShiftModifier:
+                    # search backward
+                    self.on_findPrevButton_clicked()
+                    event.accept()
     
     @pyqtSlot(str)
     def on_findtextCombo_editTextChanged(self, txt):
         """
         Private slot to enable/disable the find buttons.
         
-        @param txt text of the combobox (string)
+        @param txt text of the combobox
+        @type str
         """
         self.__setSearchButtons(txt != "")
         
@@ -231,7 +291,8 @@
         """
         Private slot to set the state of the search buttons.
         
-        @param enabled flag indicating the state (boolean)
+        @param enabled flag indicating the state
+        @type bool
         """
         self.findPrevButton.setEnabled(enabled)
         self.findNextButton.setEnabled(enabled)
@@ -261,7 +322,8 @@
         """
         Private method to search the associated text edit.
         
-        @param backwards flag indicating a backwards search (boolean)
+        @param backwards flag indicating a backwards search
+        @type bool
         """
         if not self.__textedit:
             return
@@ -364,7 +426,8 @@
         Private slot to change the findtext combo background to indicate
         errors.
         
-        @param error flag indicating an error condition (boolean)
+        @param error flag indicating an error condition
+        @type bool
         """
         le = self.findtextCombo.lineEdit()
         p = le.palette()
@@ -376,3 +439,26 @@
             p.setBrush(QPalette.ColorRole.Text, self.__defaultTextColor)
         le.setPalette(p)
         le.update()
+    
+    def __findTextFinished(self, result):
+        """
+        Private slot handling the findTextFinished signal of the web page.
+        
+        @param result reference to the QWebEngineFindTextResult object of the
+            last search
+        @type QWebEngineFindTextResult
+        """
+        self.infoLabel.setText(self.tr("Match {0} of {1}").format(
+            result.activeMatch(), result.numberOfMatches())
+        )
+        self.infoLabel.show()
+    
+    def showInfo(self, info):
+        """
+        Public method to show some information in the info label.
+        
+        @param info informational text to be shown
+        @type str
+        """
+        self.infoLabel.setText(info)
+        self.infoLabel.show()

eric ide

mercurial