eric7/EricWidgets/EricTextEditSearchWidget.py

branch
eric7
changeset 8690
25f68ec4181a
parent 8553
10d31e5ce9e5
child 8702
131ef7267fd4
equal deleted inserted replaced
8689:da40117c42d6 8690:25f68ec4181a
31 31
32 class EricTextEditSearchWidget(QWidget): 32 class EricTextEditSearchWidget(QWidget):
33 """ 33 """
34 Class implementing a horizontal search widget for QTextEdit. 34 Class implementing a horizontal search widget for QTextEdit.
35 """ 35 """
36 def __init__(self, parent=None, widthForHeight=True): 36 def __init__(self, parent=None, widthForHeight=True, enableClose=False):
37 """ 37 """
38 Constructor 38 Constructor
39 39
40 @param parent reference to the parent widget 40 @param parent reference to the parent widget
41 @type QWidget 41 @type QWidget
42 @param widthForHeight flag indicating to prefer width for height. 42 @param widthForHeight flag indicating to prefer width for height.
43 If this parameter is False, some widgets are shown in a third 43 If this parameter is False, some widgets are shown in a third
44 line. 44 line.
45 @type bool 45 @type bool
46 @param enableClose flag indicating to show a close button
47 @type bool
46 """ 48 """
47 super().__init__(parent) 49 super().__init__(parent)
48 self.__setupUi(widthForHeight) 50 self.__setupUi(widthForHeight, enableClose)
49 51
50 self.__textedit = None 52 self.__textedit = None
51 self.__texteditType = EricTextEditType.UNKNOWN 53 self.__texteditType = EricTextEditType.UNKNOWN
52 self.__findBackwards = True 54 self.__findBackwards = True
53 55
69 self.__setSearchButtons(False) 71 self.__setSearchButtons(False)
70 self.infoLabel.hide() 72 self.infoLabel.hide()
71 73
72 self.setFocusProxy(self.findtextCombo) 74 self.setFocusProxy(self.findtextCombo)
73 75
74 def __setupUi(self, widthForHeight): 76 def __setupUi(self, widthForHeight, enableClose):
75 """ 77 """
76 Private method to generate the UI. 78 Private method to generate the UI.
77 79
78 @param widthForHeight flag indicating to prefer width for height 80 @param widthForHeight flag indicating to prefer width for height
81 @type bool
82 @param enableClose flag indicating to show a close button
79 @type bool 83 @type bool
80 """ 84 """
81 self.setObjectName("EricTextEditSearchWidget") 85 self.setObjectName("EricTextEditSearchWidget")
82 86
83 self.verticalLayout = QVBoxLayout(self) 87 self.verticalLayout = QVBoxLayout(self)
85 self.verticalLayout.setContentsMargins(0, 0, 0, 0) 89 self.verticalLayout.setContentsMargins(0, 0, 0, 0)
86 90
87 # row 1 of widgets 91 # row 1 of widgets
88 self.horizontalLayout1 = QHBoxLayout() 92 self.horizontalLayout1 = QHBoxLayout()
89 self.horizontalLayout1.setObjectName("horizontalLayout1") 93 self.horizontalLayout1.setObjectName("horizontalLayout1")
94
95 if enableClose:
96 self.closeButton = QToolButton(self)
97 self.closeButton.setIcon(UI.PixmapCache.getIcon("close"))
98 self.closeButton.clicked.connect(self.__closeButtonClicked)
99 self.horizontalLayout1.addWidget(self.closeButton)
100 else:
101 self.closeButton = None
90 102
91 self.label = QLabel(self) 103 self.label = QLabel(self)
92 self.label.setObjectName("label") 104 self.label.setObjectName("label")
93 self.label.setText(self.tr("Find:")) 105 self.label.setText(self.tr("Find:"))
94 self.horizontalLayout1.addWidget(self.label) 106 self.horizontalLayout1.addWidget(self.label)
191 203
192 self.__widthForHeight = widthForHeight 204 self.__widthForHeight = widthForHeight
193 205
194 def attachTextEdit(self, textedit, editType=EricTextEditType.QTEXTEDIT): 206 def attachTextEdit(self, textedit, editType=EricTextEditType.QTEXTEDIT):
195 """ 207 """
196 Public method to attach a QTextEdit widget. 208 Public method to attach a QTextEdit or QWebEngineView widget.
197 209
198 @param textedit reference to the edit widget to be attached 210 @param textedit reference to the edit widget to be attached
199 @type QTextEdit, QWebEngineView or QWebView 211 @type QTextEdit, QTextBrowser or QWebEngineView
200 @param editType type of the attached edit widget 212 @param editType type of the attached edit widget
201 @type EricTextEditType 213 @type EricTextEditType
202 """ 214 """
215 if self.__textedit is not None:
216 self.detachTextEdit()
217
203 self.__textedit = textedit 218 self.__textedit = textedit
204 self.__texteditType = editType 219 self.__texteditType = editType
205 220
206 self.wordCheckBox.setVisible(editType == "QTextEdit") 221 self.wordCheckBox.setVisible(editType in (
222 EricTextEditType.QTEXTEDIT, EricTextEditType.QTEXTBROWSER
223 ))
224 if editType == EricTextEditType.QWEBENGINEVIEW:
225 self.__textedit.page().findTextFinished.connect(
226 self.__findTextFinished)
227
228 def detachTextEdit(self):
229 """
230 Public method to detach the current text edit.
231 """
232 if self.__texteditType == EricTextEditType.QWEBENGINEVIEW:
233 self.__textedit.page().findTextFinished.disconnect(
234 self.__findTextFinished)
235
236 self.__textedit = None
237 self.__texteditType = EricTextEditType.UNKNOWN
238
239 @pyqtSlot()
240 def __closeButtonClicked(self):
241 """
242 Private slot to close the widget.
243
244 Note: The widget is just hidden.
245 """
246 self.__textedit.setFocus(Qt.FocusReason.ActiveWindowFocusReason)
247 self.hide()
207 248
208 def keyPressEvent(self, event): 249 def keyPressEvent(self, event):
209 """ 250 """
210 Protected slot to handle key press events. 251 Protected slot to handle key press events.
211 252
212 @param event reference to the key press event (QKeyEvent) 253 @param event reference to the key press event
213 """ 254 @type QKeyEvent
214 if self.__textedit and event.key() == Qt.Key.Key_Escape: 255 """
215 self.__textedit.setFocus(Qt.FocusReason.ActiveWindowFocusReason) 256 if self.__textedit:
216 event.accept() 257 key = event.key()
258 modifiers = event.modifiers()
259
260 if key == Qt.Key.Key_Escape:
261 self.__textedit.setFocus(
262 Qt.FocusReason.ActiveWindowFocusReason)
263 if self.closeButton is not None:
264 self.hide()
265 event.accept()
266
267 elif key == Qt.Key.Key_F3:
268 if modifiers == Qt.KeyboardModifier.NoModifier:
269 # search forward
270 self.on_findNextButton_clicked()
271 event.accept()
272 elif modifiers == Qt.KeyboardModifier.ShiftModifier:
273 # search backward
274 self.on_findPrevButton_clicked()
275 event.accept()
217 276
218 @pyqtSlot(str) 277 @pyqtSlot(str)
219 def on_findtextCombo_editTextChanged(self, txt): 278 def on_findtextCombo_editTextChanged(self, txt):
220 """ 279 """
221 Private slot to enable/disable the find buttons. 280 Private slot to enable/disable the find buttons.
222 281
223 @param txt text of the combobox (string) 282 @param txt text of the combobox
283 @type str
224 """ 284 """
225 self.__setSearchButtons(txt != "") 285 self.__setSearchButtons(txt != "")
226 286
227 self.infoLabel.hide() 287 self.infoLabel.hide()
228 self.__setFindtextComboBackground(False) 288 self.__setFindtextComboBackground(False)
229 289
230 def __setSearchButtons(self, enabled): 290 def __setSearchButtons(self, enabled):
231 """ 291 """
232 Private slot to set the state of the search buttons. 292 Private slot to set the state of the search buttons.
233 293
234 @param enabled flag indicating the state (boolean) 294 @param enabled flag indicating the state
295 @type bool
235 """ 296 """
236 self.findPrevButton.setEnabled(enabled) 297 self.findPrevButton.setEnabled(enabled)
237 self.findNextButton.setEnabled(enabled) 298 self.findNextButton.setEnabled(enabled)
238 299
239 def __findByReturnPressed(self): 300 def __findByReturnPressed(self):
259 320
260 def __find(self, backwards): 321 def __find(self, backwards):
261 """ 322 """
262 Private method to search the associated text edit. 323 Private method to search the associated text edit.
263 324
264 @param backwards flag indicating a backwards search (boolean) 325 @param backwards flag indicating a backwards search
326 @type bool
265 """ 327 """
266 if not self.__textedit: 328 if not self.__textedit:
267 return 329 return
268 330
269 self.infoLabel.clear() 331 self.infoLabel.clear()
362 def __setFindtextComboBackground(self, error): 424 def __setFindtextComboBackground(self, error):
363 """ 425 """
364 Private slot to change the findtext combo background to indicate 426 Private slot to change the findtext combo background to indicate
365 errors. 427 errors.
366 428
367 @param error flag indicating an error condition (boolean) 429 @param error flag indicating an error condition
430 @type bool
368 """ 431 """
369 le = self.findtextCombo.lineEdit() 432 le = self.findtextCombo.lineEdit()
370 p = le.palette() 433 p = le.palette()
371 if error: 434 if error:
372 p.setBrush(QPalette.ColorRole.Base, QBrush(QColor("#FF6666"))) 435 p.setBrush(QPalette.ColorRole.Base, QBrush(QColor("#FF6666")))
374 else: 437 else:
375 p.setBrush(QPalette.ColorRole.Base, self.__defaultBaseColor) 438 p.setBrush(QPalette.ColorRole.Base, self.__defaultBaseColor)
376 p.setBrush(QPalette.ColorRole.Text, self.__defaultTextColor) 439 p.setBrush(QPalette.ColorRole.Text, self.__defaultTextColor)
377 le.setPalette(p) 440 le.setPalette(p)
378 le.update() 441 le.update()
442
443 def __findTextFinished(self, result):
444 """
445 Private slot handling the findTextFinished signal of the web page.
446
447 @param result reference to the QWebEngineFindTextResult object of the
448 last search
449 @type QWebEngineFindTextResult
450 """
451 self.infoLabel.setText(self.tr("Match {0} of {1}").format(
452 result.activeMatch(), result.numberOfMatches())
453 )
454 self.infoLabel.show()
455
456 def showInfo(self, info):
457 """
458 Public method to show some information in the info label.
459
460 @param info informational text to be shown
461 @type str
462 """
463 self.infoLabel.setText(info)
464 self.infoLabel.show()

eric ide

mercurial