eric6/WebBrowser/QtHelp/HelpSearchWidget.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
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 escapePressed() emitted when the ESC key was pressed
22 @signal openUrl(QUrl, str) emitted to open a search result entry in the
23 current tab
24 @signal newTab(QUrl, str) emitted to open a search result entry in a
25 new tab
26 @signal newBackgroundTab(QUrl, str) emitted to open a search result entry
27 in a new background tab
28 @signal newWindow(QUrl, str) emitted to open a search result entry in a
29 new window
30 """
31 escapePressed = pyqtSignal()
32 openUrl = pyqtSignal(QUrl)
33 newTab = pyqtSignal(QUrl)
34 newBackgroundTab = pyqtSignal(QUrl)
35 newWindow = pyqtSignal(QUrl)
36
37 def __init__(self, engine, parent=None):
38 """
39 Constructor
40
41 @param engine reference to the help search engine (QHelpSearchEngine)
42 @param parent reference to the parent widget (QWidget)
43 """
44 super(HelpSearchWidget, self).__init__(parent)
45
46 self.__engine = engine
47
48 self.__layout = QVBoxLayout(self)
49
50 self.__result = self.__engine.resultWidget()
51 self.__query = self.__engine.queryWidget()
52
53 self.__layout.addWidget(self.__query)
54 self.__layout.addWidget(self.__result)
55
56 self.setFocusProxy(self.__query)
57
58 self.__query.search.connect(self.__search)
59 self.__result.requestShowLink.connect(self.__linkActivated)
60
61 self.__engine.searchingStarted.connect(self.__searchingStarted)
62 self.__engine.searchingFinished.connect(self.__searchingFinished)
63
64 self.__browser = self.__result.findChildren(QTextBrowser)[0]
65
66 def __search(self):
67 """
68 Private slot to perform a search of the database.
69 """
70 query = self.__query.query()
71 self.__engine.search(query)
72
73 def __searchingStarted(self):
74 """
75 Private slot to handle the start of a search.
76 """
77 QApplication.setOverrideCursor(Qt.WaitCursor)
78
79 def __searchingFinished(self, hits):
80 """
81 Private slot to handle the end of the search.
82
83 @param hits number of hits (integer) (unused)
84 """
85 QApplication.restoreOverrideCursor()
86
87 @pyqtSlot(QUrl)
88 def __linkActivated(self, url):
89 """
90 Private slot handling the activation of an entry.
91
92 @param url URL of the activated entry
93 @type QUrl
94 """
95 if not url.isEmpty() and url.isValid():
96 buttons = QApplication.mouseButtons()
97 modifiers = QApplication.keyboardModifiers()
98
99 if buttons & Qt.MidButton:
100 self.newTab.emit(url)
101 else:
102 if modifiers & (Qt.ControlModifier | Qt.ShiftModifier) == \
103 (Qt.ControlModifier | Qt.ShiftModifier):
104 self.newBackgroundTab.emit(url)
105 elif modifiers & Qt.ControlModifier:
106 self.newTab.emit(url)
107 elif modifiers & Qt.ShiftModifier:
108 self.newWindow.emit(url)
109 else:
110 self.openUrl.emit(url)
111
112 def keyPressEvent(self, evt):
113 """
114 Protected method handling key press events.
115
116 @param evt reference to the key press event (QKeyEvent)
117 """
118 if evt.key() == Qt.Key_Escape:
119 self.escapePressed.emit()
120 else:
121 evt.ignore()
122
123 def contextMenuEvent(self, evt):
124 """
125 Protected method handling context menu events.
126
127 @param evt reference to the context menu event (QContextMenuEvent)
128 """
129 point = evt.globalPos()
130
131 if self.__browser:
132 point = self.__browser.mapFromGlobal(point)
133 if not self.__browser.rect().contains(point, True):
134 return
135 link = QUrl(self.__browser.anchorAt(point))
136 else:
137 point = self.__result.mapFromGlobal(point)
138 link = self.__result.linkAt(point)
139
140 if link.isEmpty() or not link.isValid():
141 return
142
143 menu = QMenu()
144 curTab = menu.addAction(self.tr("Open Link"))
145 newTab = menu.addAction(self.tr("Open Link in New Tab"))
146 newBackgroundTab = menu.addAction(
147 self.tr("Open Link in Background Tab"))
148 newWindow = menu.addAction(self.tr("Open Link in New Window"))
149 menu.move(evt.globalPos())
150 act = menu.exec_()
151 if act == curTab:
152 self.openUrl.emit(link)
153 elif act == newTab:
154 self.newTab.emit(link)
155 elif act == newBackgroundTab:
156 self.newBackgroundTab.emit(link)
157 elif act == newWindow:
158 self.newWindow.emit(link)

eric ide

mercurial