UI/SearchWidget.py

changeset 1823
21d988eaf1bf
child 1830
f2fccb8c2ab4
equal deleted inserted replaced
1822:6ee4a434e64c 1823:21d988eaf1bf
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the search box for the shel, terminal and log viewer.
8 """
9
10 from PyQt4.QtCore import pyqtSlot, pyqtSignal, Qt
11 from PyQt4.QtGui import QWidget
12
13 from .Ui_SearchWidget import Ui_SearchWidget
14
15 import UI.PixmapCache
16
17
18 class SearchWidget(QWidget, Ui_SearchWidget):
19 """
20 Class implementing the search box for the shel, terminal and log viewer.
21
22 @signal searchNext(text, caseSensitive, wholeWord) emitted when the user pressed
23 the next button (string, boolean, boolean)
24 @signal searchPrevious(text, caseSensitive, wholeWord) emitted when the user pressed
25 the previous button (string, boolean, boolean)
26 """
27 searchNext = pyqtSignal(str, bool, bool)
28 searchPrevious = pyqtSignal(str, bool, bool)
29
30 def __init__(self, mainWindow, parent=None):
31 """
32 Constructor
33
34 @param parent reference to the parent widget (QWidget)
35 """
36 super().__init__(parent)
37 self.setupUi(self)
38
39 self.__mainWindow = mainWindow
40 self.__findBackwards = True
41
42 self.closeButton.setIcon(UI.PixmapCache.getIcon("close.png"))
43 self.findPrevButton.setIcon(UI.PixmapCache.getIcon("1leftarrow.png"))
44 self.findNextButton.setIcon(UI.PixmapCache.getIcon("1rightarrow.png"))
45
46 self.findHistory = []
47
48 self.findtextCombo.lineEdit().returnPressed.connect(self.__findByReturnPressed)
49
50 @pyqtSlot()
51 def on_closeButton_clicked(self):
52 """
53 Private slot to close the widget.
54 """
55 self.close()
56
57 def keyPressEvent(self, event):
58 """
59 Protected slot to handle key press events.
60
61 @param event reference to the key press event (QKeyEvent)
62 """
63 if event.key() == Qt.Key_Escape:
64 self.__mainWindow.setFocus(Qt.ActiveWindowFocusReason)
65 event.accept()
66 self.close()
67
68 @pyqtSlot()
69 def on_findNextButton_clicked(self):
70 """
71 Private slot to find the next occurrence.
72 """
73 txt = self.findtextCombo.currentText()
74 self.__findBackwards = False
75
76 # This moves any previous occurrence of this statement to the head
77 # of the list and updates the combobox
78 if txt in self.findHistory:
79 self.findHistory.remove(txt)
80 self.findHistory.insert(0, txt)
81 self.findtextCombo.clear()
82 self.findtextCombo.addItems(self.findHistory)
83
84 self.searchNext.emit(txt,
85 self.caseCheckBox.isChecked(),
86 self.wordCheckBox.isChecked())
87
88 @pyqtSlot()
89 def on_findPrevButton_clicked(self):
90 """
91 Private slot to find the previous occurrence.
92 """
93 txt = self.findtextCombo.currentText()
94 self.__findBackwards = True
95
96 # This moves any previous occurrence of this statement to the head
97 # of the list and updates the combobox
98 if txt in self.findHistory:
99 self.findHistory.remove(txt)
100 self.findHistory.insert(0, txt)
101 self.findtextCombo.clear()
102 self.findtextCombo.addItems(self.findHistory)
103
104 self.searchPrevious.emit(txt,
105 self.caseCheckBox.isChecked(),
106 self.wordCheckBox.isChecked())
107
108 @pyqtSlot(str)
109 def on_findtextCombo_editTextChanged(self, txt):
110 """
111 Private slot to enable/disable the find buttons.
112
113 @param txt text of the combobox (string)
114 """
115 self.__setSearchButtons(txt != "")
116
117 def __setSearchButtons(self, enabled):
118 """
119 Private slot to set the state of the search buttons.
120
121 @param enabled flag indicating the state (boolean)
122 """
123 self.findPrevButton.setEnabled(enabled)
124 self.findNextButton.setEnabled(enabled)
125
126 def __findByReturnPressed(self):
127 """
128 Private slot to handle the returnPressed signal of the findtext combobox.
129 """
130 if self.__findBackwards:
131 self.on_findPrevButton_clicked()
132 else:
133 self.on_findNextButton_clicked()
134
135 def showFind(self, txt=""):
136 """
137 Public method to display this widget.
138
139 @param txt text to be shown in the combo (string)
140 """
141 self.findtextCombo.clear()
142 self.findtextCombo.addItems(self.findHistory)
143 self.findtextCombo.setEditText(txt)
144 self.findtextCombo.setFocus()
145
146 self.__setSearchButtons(txt != "")
147
148 self.show()

eric ide

mercurial