32 """ |
30 """ |
33 super(E5TextEditSearchWidget, self).__init__(parent) |
31 super(E5TextEditSearchWidget, self).__init__(parent) |
34 self.setupUi(self) |
32 self.setupUi(self) |
35 |
33 |
36 self.__textedit = None |
34 self.__textedit = None |
|
35 self.__texteditType = "" |
37 self.__findBackwards = True |
36 self.__findBackwards = True |
38 |
37 |
39 self.findPrevButton.setIcon(UI.PixmapCache.getIcon("1leftarrow.png")) |
38 self.findPrevButton.setIcon(UI.PixmapCache.getIcon("1leftarrow.png")) |
40 self.findNextButton.setIcon(UI.PixmapCache.getIcon("1rightarrow.png")) |
39 self.findNextButton.setIcon(UI.PixmapCache.getIcon("1rightarrow.png")) |
|
40 |
|
41 self.__defaultBaseColor = \ |
|
42 self.findtextCombo.lineEdit().palette().color(QPalette.Base) |
|
43 self.__defaultTextColor = \ |
|
44 self.findtextCombo.lineEdit().palette().color(QPalette.Text) |
41 |
45 |
42 self.findHistory = [] |
46 self.findHistory = [] |
43 |
47 |
44 self.findtextCombo.setCompleter(None) |
48 self.findtextCombo.setCompleter(None) |
45 self.findtextCombo.lineEdit().returnPressed.connect( |
49 self.findtextCombo.lineEdit().returnPressed.connect( |
46 self.__findByReturnPressed) |
50 self.__findByReturnPressed) |
47 |
51 |
48 self.__setSearchButtons(False) |
52 self.__setSearchButtons(False) |
|
53 self.infoLabel.hide() |
49 |
54 |
50 self.setFocusProxy(self.findtextCombo) |
55 self.setFocusProxy(self.findtextCombo) |
51 |
56 |
52 def attachTextEdit(self, textedit): |
57 def attachTextEdit(self, textedit, editType="QTextEdit"): |
53 """ |
58 """ |
54 Public method to attach a QTextEdit widget. |
59 Public method to attach a QTextEdit widget. |
55 |
60 |
56 @param textedit reference to the QTextEdit to be attached (QTextEdit) |
61 @param textedit reference to the edit widget to be attached |
57 """ |
62 @type QTextEdit, QWebEngineView or QWebView |
|
63 @param editType type of the attached edit widget |
|
64 @type str (one of "QTextEdit", "QWebEngineView" or "QWebView") |
|
65 """ |
|
66 assert editType in ["QTextEdit", "QWebEngineView", "QWebView"] |
|
67 |
58 self.__textedit = textedit |
68 self.__textedit = textedit |
|
69 self.__texteditType = editType |
|
70 |
|
71 self.wordCheckBox.setVisible(editType == "QTextEdit") |
59 |
72 |
60 def keyPressEvent(self, event): |
73 def keyPressEvent(self, event): |
61 """ |
74 """ |
62 Protected slot to handle key press events. |
75 Protected slot to handle key press events. |
63 |
76 |
113 @param backwards flag indicating a backwards search (boolean) |
129 @param backwards flag indicating a backwards search (boolean) |
114 """ |
130 """ |
115 if not self.__textedit: |
131 if not self.__textedit: |
116 return |
132 return |
117 |
133 |
|
134 self.infoLabel.clear() |
|
135 self.infoLabel.hide() |
|
136 self.__setFindtextComboBackground(False) |
|
137 |
118 txt = self.findtextCombo.currentText() |
138 txt = self.findtextCombo.currentText() |
|
139 if not txt: |
|
140 return |
119 self.__findBackwards = backwards |
141 self.__findBackwards = backwards |
120 |
142 |
121 # This moves any previous occurrence of this statement to the head |
143 # This moves any previous occurrence of this statement to the head |
122 # of the list and updates the combobox |
144 # of the list and updates the combobox |
123 if txt in self.findHistory: |
145 if txt in self.findHistory: |
124 self.findHistory.remove(txt) |
146 self.findHistory.remove(txt) |
125 self.findHistory.insert(0, txt) |
147 self.findHistory.insert(0, txt) |
126 self.findtextCombo.clear() |
148 self.findtextCombo.clear() |
127 self.findtextCombo.addItems(self.findHistory) |
149 self.findtextCombo.addItems(self.findHistory) |
128 |
150 |
|
151 if self.__texteditType == "QTextEdit": |
|
152 ok = self.__findPrevNextQTextEdit(backwards) |
|
153 self.__findNextPrevCallback(ok) |
|
154 elif self.__texteditType == "QWebEngineView": |
|
155 self.__findPrevNextQWebEngineView(backwards) |
|
156 elif self.__texteditType == "QWebView": |
|
157 ok = self.__findPrevNextQWebView(backwards) |
|
158 self.__findNextPrevCallback(ok) |
|
159 |
|
160 def __findPrevNextQTextEdit(self, backwards): |
|
161 """ |
|
162 Private method to to search the associated edit widget of |
|
163 type QTextEdit. |
|
164 |
|
165 @param backwards flag indicating a backwards search |
|
166 @type bool |
|
167 @return flag indicating the search result |
|
168 @rtype bool |
|
169 """ |
129 if backwards: |
170 if backwards: |
130 flags = QTextDocument.FindFlags(QTextDocument.FindBackward) |
171 flags = QTextDocument.FindFlags(QTextDocument.FindBackward) |
131 else: |
172 else: |
132 flags = QTextDocument.FindFlags() |
173 flags = QTextDocument.FindFlags() |
133 if self.caseCheckBox.isChecked(): |
174 if self.caseCheckBox.isChecked(): |
134 flags |= QTextDocument.FindCaseSensitively |
175 flags |= QTextDocument.FindCaseSensitively |
135 if self.wordCheckBox.isChecked(): |
176 if self.wordCheckBox.isChecked(): |
136 flags |= QTextDocument.FindWholeWords |
177 flags |= QTextDocument.FindWholeWords |
137 ok = self.__textedit.find(txt, flags) |
178 |
138 |
179 ok = self.__textedit.find(self.findtextCombo.currentText(), flags) |
139 if not ok: |
180 if not ok: |
140 E5MessageBox.information( |
181 # wrap around once |
141 self, |
182 cursor = self.__textedit.textCursor() |
142 self.tr("Find"), |
183 if backwards: |
143 self.tr("""'{0}' was not found.""").format(txt)) |
184 moveOp = QTextCursor.End # move to end of document |
|
185 else: |
|
186 moveOp = QTextCursor.Start # move to start of document |
|
187 cursor.movePosition(moveOp) |
|
188 self.__textedit.setTextCursor(cursor) |
|
189 ok = self.__textedit.find(self.findtextCombo.currentText(), flags) |
|
190 |
|
191 return ok |
|
192 |
|
193 def __findPrevNextQWebView(self, backwards): |
|
194 """ |
|
195 Private method to to search the associated edit widget of |
|
196 type QWebView. |
|
197 |
|
198 @param backwards flag indicating a backwards search |
|
199 @type bool |
|
200 @return flag indicating the search result |
|
201 @rtype bool |
|
202 """ |
|
203 from PyQt5.QtWebKitWidgets import QWebPage |
|
204 |
|
205 findFlags = QWebPage.FindFlags(QWebPage.HighlightAllOccurrences) |
|
206 if self.caseCheckBox.isChecked(): |
|
207 findFlags |= QWebPage.FindCaseSensitively |
|
208 if backwards: |
|
209 findFlags |= QWebPage.FindBackward |
|
210 |
|
211 return self.findText(self.findtextCombo.currentText(), findFlags) |
|
212 |
|
213 def __findPrevNextQWebEngineView(self, backwards): |
|
214 """ |
|
215 Private method to to search the associated edit widget of |
|
216 type QWebEngineView. |
|
217 |
|
218 @param backwards flag indicating a backwards search |
|
219 @type bool |
|
220 """ |
|
221 from PyQt5.QtWebEngineWidgets import QWebEnginePage |
|
222 |
|
223 findFlags = QWebEnginePage.FindFlags() |
|
224 if self.caseCheckBox.isChecked(): |
|
225 findFlags |= QWebEnginePage.FindCaseSensitively |
|
226 if backwards: |
|
227 findFlags |= QWebEnginePage.FindBackward |
|
228 self.__textedit.findText(self.findtextCombo.currentText(), |
|
229 findFlags, self.__findNextPrevCallback) |
|
230 |
|
231 def __findNextPrevCallback(self, found): |
|
232 """ |
|
233 Private method to process the result of the last search. |
|
234 |
|
235 @param found flag indicating if the last search succeeded |
|
236 @type bool |
|
237 """ |
|
238 if not found: |
|
239 txt = self.findtextCombo.currentText() |
|
240 self.infoLabel.setText( |
|
241 self.tr("'{0}' was not found.").format(txt)) |
|
242 self.infoLabel.show() |
|
243 self.__setFindtextComboBackground(True) |
|
244 |
|
245 def __setFindtextComboBackground(self, error): |
|
246 """ |
|
247 Private slot to change the findtext combo background to indicate |
|
248 errors. |
|
249 |
|
250 @param error flag indicating an error condition (boolean) |
|
251 """ |
|
252 le = self.findtextCombo.lineEdit() |
|
253 p = le.palette() |
|
254 if error: |
|
255 p.setBrush(QPalette.Base, QBrush(QColor("#FF6666"))) |
|
256 p.setBrush(QPalette.Text, QBrush(QColor("#000000"))) |
|
257 else: |
|
258 p.setBrush(QPalette.Base, self.__defaultBaseColor) |
|
259 p.setBrush(QPalette.Text, self.__defaultTextColor) |
|
260 le.setPalette(p) |
|
261 le.update() |