src/eric7/UI/SearchWidget.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the search box for the shell and log viewer.
8 """
9
10 from PyQt6.QtCore import pyqtSlot, pyqtSignal, Qt
11 from PyQt6.QtWidgets import QWidget, QSpacerItem, QSizePolicy
12
13 import UI.PixmapCache
14
15
16 class SearchWidget(QWidget):
17 """
18 Class implementing the search box for the shell and log viewer.
19
20 @signal searchNext(text, caseSensitive, wholeWord, regexp) emitted when the
21 user pressed the next button (string, boolean, boolean)
22 @signal searchPrevious(text, caseSensitive, wholeWord, regexp) emitted when
23 the user pressed the previous button (string, boolean, boolean)
24 """
25 searchNext = pyqtSignal(str, bool, bool, bool)
26 searchPrevious = pyqtSignal(str, bool, bool, bool)
27
28 def __init__(self, mainWindow, parent=None, spacer=True, showLine=False):
29 """
30 Constructor
31
32 @param mainWindow reference to the main window
33 @type QWidget
34 @param parent reference to the parent widget
35 @type QWidget
36 @param spacer flag indicating to add a vertical spacer to the
37 main layout
38 @type bool
39 @param showLine flag indicating to show all widget in one row
40 @type bool
41 """
42 super().__init__(parent)
43
44 if showLine:
45 from .Ui_SearchWidgetLine import Ui_SearchWidgetLine
46 self.__ui = Ui_SearchWidgetLine()
47 else:
48 from .Ui_SearchWidget import Ui_SearchWidget
49 self.__ui = Ui_SearchWidget()
50 self.__ui.setupUi(self)
51
52 if not showLine:
53 if spacer:
54 spacerItem = QSpacerItem(
55 20, 1,
56 QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding
57 )
58 self.__ui.verticalLayout.addItem(spacerItem)
59 else:
60 # change the size policy of the search combo if the spacer is
61 # not wanted, i.e. it is below the to be searched widget
62 sizePolicy = self.__ui.findtextCombo.sizePolicy()
63 sizePolicy.setHorizontalPolicy(QSizePolicy.Policy.Expanding)
64 self.__ui.findtextCombo.setSizePolicy(sizePolicy)
65
66 self.__mainWindow = mainWindow
67 self.__findBackwards = True
68
69 self.__ui.closeButton.setIcon(
70 UI.PixmapCache.getIcon("close"))
71 self.__ui.findPrevButton.setIcon(
72 UI.PixmapCache.getIcon("1leftarrow"))
73 self.__ui.findNextButton.setIcon(
74 UI.PixmapCache.getIcon("1rightarrow"))
75
76 self.findHistory = []
77
78 self.__ui.findtextCombo.setCompleter(None)
79 self.__ui.findtextCombo.lineEdit().returnPressed.connect(
80 self.__findByReturnPressed)
81
82 msh = self.minimumSizeHint()
83 self.resize(max(self.width(), msh.width()), msh.height())
84
85 @pyqtSlot()
86 def on_closeButton_clicked(self):
87 """
88 Private slot to close the widget.
89 """
90 self.close()
91
92 def keyPressEvent(self, event):
93 """
94 Protected slot to handle key press events.
95
96 @param event reference to the key press event (QKeyEvent)
97 """
98 if event.key() == Qt.Key.Key_Escape:
99 self.__mainWindow.setFocus(Qt.FocusReason.ActiveWindowFocusReason)
100 event.accept()
101 self.close()
102
103 @pyqtSlot()
104 def on_findNextButton_clicked(self):
105 """
106 Private slot to find the next occurrence.
107 """
108 txt = self.__ui.findtextCombo.currentText()
109 if not txt and not self.isVisible():
110 self.showFind()
111 return
112
113 self.__findBackwards = False
114
115 # This moves any previous occurrence of this statement to the head
116 # of the list and updates the combobox
117 if txt in self.findHistory:
118 self.findHistory.remove(txt)
119 self.findHistory.insert(0, txt)
120 self.__ui.findtextCombo.clear()
121 self.__ui.findtextCombo.addItems(self.findHistory)
122
123 self.searchNext.emit(
124 txt,
125 self.__ui.caseCheckBox.isChecked(),
126 self.__ui.wordCheckBox.isChecked(),
127 self.__ui.regexpCheckBox.isChecked(),
128 )
129
130 @pyqtSlot()
131 def on_findPrevButton_clicked(self):
132 """
133 Private slot to find the previous occurrence.
134 """
135 txt = self.__ui.findtextCombo.currentText()
136 if not txt and not self.isVisible():
137 self.showFind()
138 return
139
140 self.__findBackwards = True
141
142 # This moves any previous occurrence of this statement to the head
143 # of the list and updates the combobox
144 if txt in self.findHistory:
145 self.findHistory.remove(txt)
146 self.findHistory.insert(0, txt)
147 self.__ui.findtextCombo.clear()
148 self.__ui.findtextCombo.addItems(self.findHistory)
149
150 self.searchPrevious.emit(
151 txt,
152 self.__ui.caseCheckBox.isChecked(),
153 self.__ui.wordCheckBox.isChecked(),
154 self.__ui.regexpCheckBox.isChecked(),
155 )
156
157 @pyqtSlot(str)
158 def on_findtextCombo_editTextChanged(self, txt):
159 """
160 Private slot to enable/disable the find buttons.
161
162 @param txt text of the combobox (string)
163 """
164 self.__setSearchButtons(txt != "")
165
166 def __setSearchButtons(self, enabled):
167 """
168 Private slot to set the state of the search buttons.
169
170 @param enabled flag indicating the state (boolean)
171 """
172 self.__ui.findPrevButton.setEnabled(enabled)
173 self.__ui.findNextButton.setEnabled(enabled)
174
175 def __findByReturnPressed(self):
176 """
177 Private slot to handle the returnPressed signal of the findtext
178 combobox.
179 """
180 if self.__findBackwards:
181 self.on_findPrevButton_clicked()
182 else:
183 self.on_findNextButton_clicked()
184
185 def showFind(self, txt=""):
186 """
187 Public method to display this widget.
188
189 @param txt text to be shown in the combo (string)
190 """
191 self.__ui.findtextCombo.clear()
192 self.__ui.findtextCombo.addItems(self.findHistory)
193 self.__ui.findtextCombo.setEditText(txt)
194 self.__ui.findtextCombo.setFocus()
195
196 self.__setSearchButtons(txt != "")
197
198 self.show()
199
200 def searchStringFound(self, found):
201 """
202 Public slot to indicate that the search string was found.
203
204 @param found flag indicating success (boolean)
205 """
206 if found:
207 self.__ui.statusLabel.clear()
208 else:
209 txt = self.__ui.findtextCombo.currentText()
210 self.__ui.statusLabel.setText(
211 self.tr("'{0}' was not found.").format(txt))

eric ide

mercurial