|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a window for showing the QtHelp index. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl |
|
11 from PyQt5.QtWidgets import ( |
|
12 QWidget, QVBoxLayout, QTextBrowser, QApplication, QMenu |
|
13 ) |
|
14 |
|
15 |
|
16 class HelpSearchWidget(QWidget): |
|
17 """ |
|
18 Class implementing a window for showing the QtHelp index. |
|
19 |
|
20 @signal escapePressed() emitted when the ESC key was pressed |
|
21 @signal openUrl(QUrl, str) emitted to open a search result entry in the |
|
22 current tab |
|
23 @signal newTab(QUrl, str) emitted to open a search result entry in a |
|
24 new tab |
|
25 @signal newBackgroundTab(QUrl, str) emitted to open a search result entry |
|
26 in a new background tab |
|
27 @signal newWindow(QUrl, str) emitted to open a search result entry in a |
|
28 new window |
|
29 """ |
|
30 escapePressed = pyqtSignal() |
|
31 openUrl = pyqtSignal(QUrl) |
|
32 newTab = pyqtSignal(QUrl) |
|
33 newBackgroundTab = pyqtSignal(QUrl) |
|
34 newWindow = pyqtSignal(QUrl) |
|
35 |
|
36 def __init__(self, engine, parent=None): |
|
37 """ |
|
38 Constructor |
|
39 |
|
40 @param engine reference to the help search engine (QHelpSearchEngine) |
|
41 @param parent reference to the parent widget (QWidget) |
|
42 """ |
|
43 super().__init__(parent) |
|
44 |
|
45 self.__engine = engine |
|
46 |
|
47 self.__layout = QVBoxLayout(self) |
|
48 |
|
49 self.__result = self.__engine.resultWidget() |
|
50 self.__query = self.__engine.queryWidget() |
|
51 |
|
52 self.__layout.addWidget(self.__query) |
|
53 self.__layout.addWidget(self.__result) |
|
54 |
|
55 self.setFocusProxy(self.__query) |
|
56 |
|
57 self.__query.search.connect(self.__search) |
|
58 self.__result.requestShowLink.connect(self.__linkActivated) |
|
59 |
|
60 self.__engine.searchingStarted.connect(self.__searchingStarted) |
|
61 self.__engine.searchingFinished.connect(self.__searchingFinished) |
|
62 |
|
63 self.__browser = self.__result.findChildren(QTextBrowser)[0] |
|
64 |
|
65 def __search(self): |
|
66 """ |
|
67 Private slot to perform a search of the database. |
|
68 """ |
|
69 query = self.__query.query() |
|
70 self.__engine.search(query) |
|
71 |
|
72 def __searchingStarted(self): |
|
73 """ |
|
74 Private slot to handle the start of a search. |
|
75 """ |
|
76 QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor) |
|
77 |
|
78 def __searchingFinished(self, hits): |
|
79 """ |
|
80 Private slot to handle the end of the search. |
|
81 |
|
82 @param hits number of hits (integer) (unused) |
|
83 """ |
|
84 QApplication.restoreOverrideCursor() |
|
85 |
|
86 @pyqtSlot(QUrl) |
|
87 def __linkActivated(self, url): |
|
88 """ |
|
89 Private slot handling the activation of an entry. |
|
90 |
|
91 @param url URL of the activated entry |
|
92 @type QUrl |
|
93 """ |
|
94 if not url.isEmpty() and url.isValid(): |
|
95 buttons = QApplication.mouseButtons() |
|
96 modifiers = QApplication.keyboardModifiers() |
|
97 |
|
98 if buttons & Qt.MouseButton.MidButton: |
|
99 self.newTab.emit(url) |
|
100 else: |
|
101 if ( |
|
102 modifiers & ( |
|
103 Qt.KeyboardModifier.ControlModifier | |
|
104 Qt.KeyboardModifier.ShiftModifier |
|
105 ) == ( |
|
106 Qt.KeyboardModifier.ControlModifier | |
|
107 Qt.KeyboardModifier.ShiftModifier |
|
108 ) |
|
109 ): |
|
110 self.newBackgroundTab.emit(url) |
|
111 elif modifiers & Qt.KeyboardModifier.ControlModifier: |
|
112 self.newTab.emit(url) |
|
113 elif modifiers & Qt.KeyboardModifier.ShiftModifier: |
|
114 self.newWindow.emit(url) |
|
115 else: |
|
116 self.openUrl.emit(url) |
|
117 |
|
118 def keyPressEvent(self, evt): |
|
119 """ |
|
120 Protected method handling key press events. |
|
121 |
|
122 @param evt reference to the key press event (QKeyEvent) |
|
123 """ |
|
124 if evt.key() == Qt.Key.Key_Escape: |
|
125 self.escapePressed.emit() |
|
126 else: |
|
127 evt.ignore() |
|
128 |
|
129 def contextMenuEvent(self, evt): |
|
130 """ |
|
131 Protected method handling context menu events. |
|
132 |
|
133 @param evt reference to the context menu event (QContextMenuEvent) |
|
134 """ |
|
135 point = evt.globalPos() |
|
136 |
|
137 if self.__browser: |
|
138 point = self.__browser.mapFromGlobal(point) |
|
139 if not self.__browser.rect().contains(point, True): |
|
140 return |
|
141 link = QUrl(self.__browser.anchorAt(point)) |
|
142 else: |
|
143 point = self.__result.mapFromGlobal(point) |
|
144 link = self.__result.linkAt(point) |
|
145 |
|
146 if link.isEmpty() or not link.isValid(): |
|
147 return |
|
148 |
|
149 menu = QMenu() |
|
150 curTab = menu.addAction(self.tr("Open Link")) |
|
151 newTab = menu.addAction(self.tr("Open Link in New Tab")) |
|
152 newBackgroundTab = menu.addAction( |
|
153 self.tr("Open Link in Background Tab")) |
|
154 newWindow = menu.addAction(self.tr("Open Link in New Window")) |
|
155 menu.move(evt.globalPos()) |
|
156 act = menu.exec() |
|
157 if act == curTab: |
|
158 self.openUrl.emit(link) |
|
159 elif act == newTab: |
|
160 self.newTab.emit(link) |
|
161 elif act == newBackgroundTab: |
|
162 self.newBackgroundTab.emit(link) |
|
163 elif act == newWindow: |
|
164 self.newWindow.emit(link) |