|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the search bar for the web browser. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 from Ui_SearchWidget import Ui_SearchWidget |
|
14 |
|
15 import UI.PixmapCache |
|
16 |
|
17 class SearchWidget(QWidget, Ui_SearchWidget): |
|
18 """ |
|
19 Class implementing the search bar for the web browser. |
|
20 """ |
|
21 def __init__(self, mainWindow, parent = None): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param mainWindow reference to the main window (QMainWindow) |
|
26 @param parent parent widget of this dialog (QWidget) |
|
27 """ |
|
28 QWidget.__init__(self, parent) |
|
29 self.setupUi(self) |
|
30 |
|
31 self.__mainWindow = mainWindow |
|
32 |
|
33 self.wrapCheckBox.setChecked(True) |
|
34 self.closeButton.setIcon(UI.PixmapCache.getIcon("close.png")) |
|
35 self.findPrevButton.setIcon(UI.PixmapCache.getIcon("1leftarrow.png")) |
|
36 self.findNextButton.setIcon(UI.PixmapCache.getIcon("1rightarrow.png")) |
|
37 |
|
38 self.__defaultBaseColor = \ |
|
39 self.findtextCombo.lineEdit().palette().color(QPalette.Base) |
|
40 self.__defaultTextColor = \ |
|
41 self.findtextCombo.lineEdit().palette().color(QPalette.Text) |
|
42 |
|
43 self.findHistory = [] |
|
44 self.havefound = False |
|
45 self.__findBackwards = False |
|
46 |
|
47 self.connect(self.findtextCombo.lineEdit(), SIGNAL("returnPressed()"), |
|
48 self.__findByReturnPressed) |
|
49 |
|
50 def on_findtextCombo_editTextChanged(self, txt): |
|
51 """ |
|
52 Private slot to enable/disable the find buttons. |
|
53 |
|
54 @param txt text of the combobox (string) |
|
55 """ |
|
56 self.findPrevButton.setEnabled(txt) |
|
57 self.findNextButton.setEnabled(txt) |
|
58 |
|
59 def __findNextPrev(self): |
|
60 """ |
|
61 Private slot to find the next occurrence of text. |
|
62 """ |
|
63 self.infoLabel.clear() |
|
64 self.__setFindtextComboBackground(False) |
|
65 |
|
66 if not self.havefound or not self.findtextCombo.currentText(): |
|
67 self.showFind() |
|
68 return |
|
69 |
|
70 if not self.__mainWindow.currentBrowser().findNextPrev( |
|
71 self.findtextCombo.currentText(), |
|
72 self.caseCheckBox.isChecked(), |
|
73 self.__findBackwards, |
|
74 self.wrapCheckBox.isChecked()): |
|
75 self.infoLabel.setText(self.trUtf8("Expression was not found.")) |
|
76 self.__setFindtextComboBackground(True) |
|
77 |
|
78 @pyqtSlot() |
|
79 def on_findNextButton_clicked(self): |
|
80 """ |
|
81 Private slot to find the next occurrence. |
|
82 """ |
|
83 txt = self.findtextCombo.currentText() |
|
84 |
|
85 # This moves any previous occurrence of this statement to the head |
|
86 # of the list and updates the combobox |
|
87 if txt in self.findHistory: |
|
88 self.findHistory.remove(txt) |
|
89 self.findHistory.insert(0, txt) |
|
90 self.findtextCombo.clear() |
|
91 self.findtextCombo.addItems(self.findHistory) |
|
92 |
|
93 self.__findBackwards = False |
|
94 self.__findNextPrev() |
|
95 |
|
96 def findNext(self): |
|
97 """ |
|
98 Public slot to find the next occurrence. |
|
99 """ |
|
100 self.on_findNextButton_clicked() |
|
101 |
|
102 @pyqtSlot() |
|
103 def on_findPrevButton_clicked(self): |
|
104 """ |
|
105 Private slot to find the previous occurrence. |
|
106 """ |
|
107 txt = self.findtextCombo.currentText() |
|
108 |
|
109 # This moves any previous occurrence of this statement to the head |
|
110 # of the list and updates the combobox |
|
111 if txt in self.findHistory: |
|
112 self.findHistory.remove(txt) |
|
113 self.findHistory.insert(0, txt) |
|
114 self.findtextCombo.clear() |
|
115 self.findtextCombo.addItems(self.findHistory) |
|
116 |
|
117 self.__findBackwards = True |
|
118 self.__findNextPrev() |
|
119 |
|
120 def findPrevious(self): |
|
121 """ |
|
122 Public slot to find the previous occurrence. |
|
123 """ |
|
124 self.on_findPrevButton_clicked() |
|
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): |
|
136 """ |
|
137 Public method to display this dialog. |
|
138 """ |
|
139 self.havefound = True |
|
140 self.__findBackwards = False |
|
141 |
|
142 self.findtextCombo.clear() |
|
143 self.findtextCombo.addItems(self.findHistory) |
|
144 self.findtextCombo.setEditText('') |
|
145 self.findtextCombo.setFocus() |
|
146 |
|
147 self.caseCheckBox.setChecked(False) |
|
148 |
|
149 if self.__mainWindow.currentBrowser().hasSelection(): |
|
150 self.findtextCombo.setEditText( |
|
151 self.__mainWindow.currentBrowser().selectedText()) |
|
152 |
|
153 self.__setFindtextComboBackground(False) |
|
154 self.show() |
|
155 |
|
156 @pyqtSlot() |
|
157 def on_closeButton_clicked(self): |
|
158 """ |
|
159 Private slot to close the widget. |
|
160 """ |
|
161 self.close() |
|
162 |
|
163 def keyPressEvent(self, event): |
|
164 """ |
|
165 Protected slot to handle key press events. |
|
166 |
|
167 @param event reference to the key press event (QKeyEvent) |
|
168 """ |
|
169 if event.key() == Qt.Key_Escape: |
|
170 cb = self.__mainWindow.currentBrowser() |
|
171 if cb: |
|
172 cb.setFocus(Qt.ActiveWindowFocusReason) |
|
173 event.accept() |
|
174 self.close() |
|
175 |
|
176 def __setFindtextComboBackground(self, error): |
|
177 """ |
|
178 Private slot to change the findtext combo background to indicate errors. |
|
179 |
|
180 @param error flag indicating an error condition (boolean) |
|
181 """ |
|
182 le = self.findtextCombo.lineEdit() |
|
183 p = le.palette() |
|
184 if error: |
|
185 p.setBrush(QPalette.Base, QBrush(QColor("#FF6666"))) |
|
186 p.setBrush(QPalette.Text, QBrush(QColor("#FFFFFF"))) |
|
187 else: |
|
188 p.setBrush(QPalette.Base, self.__defaultBaseColor) |
|
189 p.setBrush(QPalette.Text, self.__defaultTextColor) |
|
190 le.setPalette(p) |
|
191 le.update() |