E5Gui/E5TextEditSearchWidget.py

changeset 4039
661167a99aef
child 4586
9221c0c5c66f
equal deleted inserted replaced
4038:175f13f4eb9d 4039:661167a99aef
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2015 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a horizontal search widget for QTextEdit.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot, Qt
13 from PyQt5.QtGui import QTextDocument
14 from PyQt5.QtWidgets import QWidget
15
16 from E5Gui import E5MessageBox
17
18 from .Ui_E5TextEditSearchWidget import Ui_E5TextEditSearchWidget
19
20 import UI.PixmapCache
21
22
23 class E5TextEditSearchWidget(QWidget, Ui_E5TextEditSearchWidget):
24 """
25 Class implementing a horizontal search widget for QTextEdit.
26 """
27 def __init__(self, parent=None):
28 """
29 Constructor
30
31 @param parent reference to the parent widget (QWidget)
32 """
33 super(E5TextEditSearchWidget, self).__init__(parent)
34 self.setupUi(self)
35
36 self.__textedit = None
37 self.__findBackwards = True
38
39 self.findPrevButton.setIcon(UI.PixmapCache.getIcon("1leftarrow.png"))
40 self.findNextButton.setIcon(UI.PixmapCache.getIcon("1rightarrow.png"))
41
42 self.findHistory = []
43
44 self.findtextCombo.setCompleter(None)
45 self.findtextCombo.lineEdit().returnPressed.connect(
46 self.__findByReturnPressed)
47
48 self.__setSearchButtons(False)
49
50 def attachTextEdit(self, textedit):
51 """
52 Public method to attach a QTextEdit widget.
53
54 @param textedit reference to the QTextEdit to be attached (QTextEdit)
55 """
56 self.__textedit = textedit
57
58 def keyPressEvent(self, event):
59 """
60 Protected slot to handle key press events.
61
62 @param event reference to the key press event (QKeyEvent)
63 """
64 if self.__textedit and event.key() == Qt.Key_Escape:
65 self.__textedit.setFocus(Qt.ActiveWindowFocusReason)
66 event.accept()
67
68 @pyqtSlot(str)
69 def on_findtextCombo_editTextChanged(self, txt):
70 """
71 Private slot to enable/disable the find buttons.
72
73 @param txt text of the combobox (string)
74 """
75 self.__setSearchButtons(txt != "")
76
77 def __setSearchButtons(self, enabled):
78 """
79 Private slot to set the state of the search buttons.
80
81 @param enabled flag indicating the state (boolean)
82 """
83 self.findPrevButton.setEnabled(enabled)
84 self.findNextButton.setEnabled(enabled)
85
86 def __findByReturnPressed(self):
87 """
88 Private slot to handle the returnPressed signal of the findtext
89 combobox.
90 """
91 self.__find(self.__findBackwards)
92
93 @pyqtSlot()
94 def on_findPrevButton_clicked(self):
95 """
96 Private slot to find the previous occurrence.
97 """
98 self.__find(True)
99
100 @pyqtSlot()
101 def on_findNextButton_clicked(self):
102 """
103 Private slot to find the next occurrence.
104 """
105 self.__find(False)
106
107 def __find(self, backwards):
108 """
109 Private method to search the associated text edit.
110
111 @param backwards flag indicating a backwards search (boolean)
112 """
113 if not self.__textedit:
114 return
115
116 txt = self.findtextCombo.currentText()
117 self.__findBackwards = backwards
118
119 # This moves any previous occurrence of this statement to the head
120 # of the list and updates the combobox
121 if txt in self.findHistory:
122 self.findHistory.remove(txt)
123 self.findHistory.insert(0, txt)
124 self.findtextCombo.clear()
125 self.findtextCombo.addItems(self.findHistory)
126
127 if backwards:
128 flags = QTextDocument.FindFlags(QTextDocument.FindBackward)
129 else:
130 flags = QTextDocument.FindFlags()
131 if self.caseCheckBox.isChecked():
132 flags |= QTextDocument.FindCaseSensitively
133 if self.wordCheckBox.isChecked():
134 flags |= QTextDocument.FindWholeWords
135 ok = self.__textedit.find(txt, flags)
136
137 if not ok:
138 E5MessageBox.information(
139 self,
140 self.tr("Find"),
141 self.tr("""'{0}' was not found.""").format(txt))

eric ide

mercurial