|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a window for showing the QtHelp index. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 class HelpSearchWidget(QWidget): |
|
14 """ |
|
15 Class implementing a window for showing the QtHelp index. |
|
16 |
|
17 @signal linkActivated(const QUrl&) emitted when a search result entry is activated |
|
18 @signal escapePressed() emitted when the ESC key was pressed |
|
19 """ |
|
20 def __init__(self, engine, mainWindow, parent = None): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param engine reference to the help search engine (QHelpSearchEngine) |
|
25 @param mainWindow reference to the main window object (QMainWindow) |
|
26 @param parent reference to the parent widget (QWidget) |
|
27 """ |
|
28 QWidget.__init__(self, parent) |
|
29 |
|
30 self.__engine = engine |
|
31 self.__mw = mainWindow |
|
32 |
|
33 self.__layout = QVBoxLayout(self) |
|
34 |
|
35 self.__result = self.__engine.resultWidget() |
|
36 self.__query = self.__engine.queryWidget() |
|
37 |
|
38 self.__layout.addWidget(self.__query) |
|
39 self.__layout.addWidget(self.__result) |
|
40 |
|
41 self.setFocusProxy(self.__query) |
|
42 |
|
43 self.connect(self.__query, SIGNAL("search()"), |
|
44 self.__search) |
|
45 self.connect(self.__result, SIGNAL("requestShowLink(const QUrl&)"), |
|
46 self, SIGNAL("linkActivated(const QUrl&)")) |
|
47 |
|
48 self.connect(self.__engine, SIGNAL("searchingStarted()"), |
|
49 self.__searchingStarted) |
|
50 self.connect(self.__engine, SIGNAL("searchingFinished(int)"), |
|
51 self.__searchingFinished) |
|
52 |
|
53 self.__browser = self.__result.findChildren(QTextBrowser)[0] |
|
54 if self.__browser: |
|
55 self.__browser.viewport().installEventFilter(self) |
|
56 |
|
57 def __search(self): |
|
58 """ |
|
59 Private slot to perform a search of the database. |
|
60 """ |
|
61 query = self.__query.query() |
|
62 self.__engine.search(query) |
|
63 |
|
64 def __searchingStarted(self): |
|
65 """ |
|
66 Private slot to handle the start of a search. |
|
67 """ |
|
68 QApplication.setOverrideCursor(Qt.WaitCursor) |
|
69 |
|
70 def __searchingFinished(self, hits): |
|
71 """ |
|
72 Private slot to handle the end of the search. |
|
73 |
|
74 @param hits number of hits (integer) (unused) |
|
75 """ |
|
76 QApplication.restoreOverrideCursor() |
|
77 |
|
78 def eventFilter(self, watched, event): |
|
79 """ |
|
80 Public method called to filter the event queue. |
|
81 |
|
82 @param watched the QObject being watched (QObject) |
|
83 @param event the event that occurred (QEvent) |
|
84 @return flag indicating whether the event was handled (boolean) |
|
85 """ |
|
86 if self.__browser and watched == self.__browser.viewport() and \ |
|
87 event.type() == QEvent.MouseButtonRelease: |
|
88 link = self.__result.linkAt(event.pos()) |
|
89 if not link.isEmpty() and link.isValid(): |
|
90 ctrl = event.modifiers() & Qt.ControlModifier |
|
91 if (event.button() == Qt.LeftButton and ctrl) or \ |
|
92 event.button() == Qt.MidButton: |
|
93 self.__mw.newTab(link) |
|
94 |
|
95 return QWidget.eventFilter(self, watched, event) |
|
96 |
|
97 def keyPressEvent(self, evt): |
|
98 """ |
|
99 Protected method handling key press events. |
|
100 |
|
101 @param evt reference to the key press event (QKeyEvent) |
|
102 """ |
|
103 if evt.key() == Qt.Key_Escape: |
|
104 self.emit(SIGNAL("escapePressed()")) |
|
105 else: |
|
106 evt.ignore() |
|
107 |
|
108 def contextMenuEvent(self, evt): |
|
109 """ |
|
110 Protected method handling context menu events. |
|
111 |
|
112 @param evt reference to the context menu event (QContextMenuEvent) |
|
113 """ |
|
114 point = evt.globalPos() |
|
115 |
|
116 if self.__browser: |
|
117 point = self.__browser.mapFromGlobal(point) |
|
118 if not self.__browser.rect().contains(point, True): |
|
119 return |
|
120 link = QUrl(self.__browser.anchorAt(point)) |
|
121 else: |
|
122 point = self.__result.mapFromGlobal(point) |
|
123 link = self.__result.linkAt(point) |
|
124 |
|
125 if link.isEmpty() or not link.isValid(): |
|
126 return |
|
127 |
|
128 menu = QMenu() |
|
129 curTab = menu.addAction(self.trUtf8("Open Link")) |
|
130 newTab = menu.addAction(self.trUtf8("Open Link in New Tab")) |
|
131 menu.move(evt.globalPos()) |
|
132 act = menu.exec_() |
|
133 if act == curTab: |
|
134 self.emit(SIGNAL("linkActivated(const QUrl&)"), link) |
|
135 elif act == newTab: |
|
136 self.__mw.newTab(link) |