7 Module implementing a horizontal search widget for QTextEdit. |
7 Module implementing a horizontal search widget for QTextEdit. |
8 """ |
8 """ |
9 |
9 |
10 import enum |
10 import enum |
11 |
11 |
12 from PyQt6.QtCore import pyqtSlot, Qt, QMetaObject, QSize |
12 from PyQt6.QtCore import pyqtSlot, pyqtSignal, Qt, QMetaObject, QSize |
13 from PyQt6.QtGui import QPalette, QBrush, QColor, QTextDocument, QTextCursor |
13 from PyQt6.QtGui import QPalette, QBrush, QColor, QTextDocument, QTextCursor |
14 from PyQt6.QtWidgets import ( |
14 from PyQt6.QtWidgets import ( |
15 QWidget, QVBoxLayout, QHBoxLayout, QLabel, QComboBox, QCheckBox, |
15 QWidget, QVBoxLayout, QHBoxLayout, QLabel, QComboBox, QCheckBox, |
16 QToolButton, QSizePolicy |
16 QToolButton, QSizePolicy |
17 ) |
17 ) |
30 |
30 |
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 |
|
36 @signal closePressed() emitted to indicate the closing of the widget via |
|
37 the close button |
35 """ |
38 """ |
|
39 closePressed = pyqtSignal() |
|
40 |
36 def __init__(self, parent=None, widthForHeight=True, enableClose=False): |
41 def __init__(self, parent=None, widthForHeight=True, enableClose=False): |
37 """ |
42 """ |
38 Constructor |
43 Constructor |
39 |
44 |
40 @param parent reference to the parent widget |
45 @param parent reference to the parent widget |
49 super().__init__(parent) |
54 super().__init__(parent) |
50 self.__setupUi(widthForHeight, enableClose) |
55 self.__setupUi(widthForHeight, enableClose) |
51 |
56 |
52 self.__textedit = None |
57 self.__textedit = None |
53 self.__texteditType = EricTextEditType.UNKNOWN |
58 self.__texteditType = EricTextEditType.UNKNOWN |
54 self.__findBackwards = True |
59 self.__findBackwards = False |
55 |
60 |
56 self.__defaultBaseColor = ( |
61 self.__defaultBaseColor = ( |
57 self.findtextCombo.lineEdit().palette().color( |
62 self.findtextCombo.lineEdit().palette().color( |
58 QPalette.ColorRole.Base) |
63 QPalette.ColorRole.Base) |
59 ) |
64 ) |
235 |
240 |
236 self.__textedit = None |
241 self.__textedit = None |
237 self.__texteditType = EricTextEditType.UNKNOWN |
242 self.__texteditType = EricTextEditType.UNKNOWN |
238 |
243 |
239 @pyqtSlot() |
244 @pyqtSlot() |
|
245 def activate(self): |
|
246 """ |
|
247 Public slot to activate the widget. |
|
248 """ |
|
249 self.show() |
|
250 self.findtextCombo.setFocus( |
|
251 Qt.FocusReason.ActiveWindowFocusReason) |
|
252 self.findtextCombo.lineEdit().selectAll() |
|
253 |
|
254 @pyqtSlot() |
|
255 def deactivate(self): |
|
256 """ |
|
257 Public slot to deactivate the widget. |
|
258 """ |
|
259 if self.__textedit: |
|
260 self.__textedit.setFocus(Qt.FocusReason.ActiveWindowFocusReason) |
|
261 if self.closeButton is not None: |
|
262 self.hide() |
|
263 self.closePressed.emit() |
|
264 |
|
265 @pyqtSlot() |
240 def __closeButtonClicked(self): |
266 def __closeButtonClicked(self): |
241 """ |
267 """ |
242 Private slot to close the widget. |
268 Private slot to close the widget. |
243 |
269 |
244 Note: The widget is just hidden. |
270 Note: The widget is just hidden. |
245 """ |
271 """ |
246 self.__textedit.setFocus(Qt.FocusReason.ActiveWindowFocusReason) |
272 self.deactivate() |
247 self.hide() |
|
248 |
273 |
249 def keyPressEvent(self, event): |
274 def keyPressEvent(self, event): |
250 """ |
275 """ |
251 Protected slot to handle key press events. |
276 Protected slot to handle key press events. |
252 |
277 |
256 if self.__textedit: |
281 if self.__textedit: |
257 key = event.key() |
282 key = event.key() |
258 modifiers = event.modifiers() |
283 modifiers = event.modifiers() |
259 |
284 |
260 if key == Qt.Key.Key_Escape: |
285 if key == Qt.Key.Key_Escape: |
261 self.__textedit.setFocus( |
286 self.deactivate() |
262 Qt.FocusReason.ActiveWindowFocusReason) |
|
263 if self.closeButton is not None: |
|
264 self.hide() |
|
265 event.accept() |
287 event.accept() |
266 |
288 |
267 elif key == Qt.Key.Key_F3: |
289 elif key == Qt.Key.Key_F3: |
268 if modifiers == Qt.KeyboardModifier.NoModifier: |
290 if modifiers == Qt.KeyboardModifier.NoModifier: |
269 # search forward |
291 # search forward |
315 def on_findNextButton_clicked(self): |
337 def on_findNextButton_clicked(self): |
316 """ |
338 """ |
317 Private slot to find the next occurrence. |
339 Private slot to find the next occurrence. |
318 """ |
340 """ |
319 self.__find(False) |
341 self.__find(False) |
|
342 |
|
343 @pyqtSlot() |
|
344 def findPrev(self): |
|
345 """ |
|
346 Public slot to find the previous occurrence of the current search term. |
|
347 """ |
|
348 self.on_findPrevButton_clicked() |
|
349 |
|
350 @pyqtSlot() |
|
351 def findNext(self): |
|
352 """ |
|
353 Public slot to find the next occurrence of the current search term. |
|
354 """ |
|
355 self.on_findNextButton_clicked() |
320 |
356 |
321 def __find(self, backwards): |
357 def __find(self, backwards): |
322 """ |
358 """ |
323 Private method to search the associated text edit. |
359 Private method to search the associated text edit. |
324 |
360 |