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