WebBrowser/QtHelp/HelpSearchWidget.py

branch
QtWebEngine
changeset 4875
4ee26909ac0d
parent 4631
5c1a96925da4
child 5038
df7103c3f2a6
equal deleted inserted replaced
4870:72901685681d 4875:4ee26909ac0d
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2016 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, Qt, QEvent, 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 if self.__browser:
58 self.__browser.viewport().installEventFilter(self)
59
60 def __search(self):
61 """
62 Private slot to perform a search of the database.
63 """
64 query = self.__query.query()
65 self.__engine.search(query)
66
67 def __searchingStarted(self):
68 """
69 Private slot to handle the start of a search.
70 """
71 QApplication.setOverrideCursor(Qt.WaitCursor)
72
73 def __searchingFinished(self, hits):
74 """
75 Private slot to handle the end of the search.
76
77 @param hits number of hits (integer) (unused)
78 """
79 QApplication.restoreOverrideCursor()
80
81 def eventFilter(self, watched, event):
82 """
83 Public method called to filter the event queue.
84
85 @param watched the QObject being watched (QObject)
86 @param event the event that occurred (QEvent)
87 @return flag indicating whether the event was handled (boolean)
88 """
89 if self.__browser and watched == self.__browser.viewport() and \
90 event.type() == QEvent.MouseButtonRelease:
91 link = self.__result.linkAt(event.pos())
92 if not link.isEmpty() and link.isValid():
93 ctrl = event.modifiers() & Qt.ControlModifier
94 if (event.button() == Qt.LeftButton and ctrl) or \
95 event.button() == Qt.MidButton:
96 self.__mw.newTab(link)
97
98 return QWidget.eventFilter(self, watched, event)
99
100 def keyPressEvent(self, evt):
101 """
102 Protected method handling key press events.
103
104 @param evt reference to the key press event (QKeyEvent)
105 """
106 if evt.key() == Qt.Key_Escape:
107 self.escapePressed.emit()
108 else:
109 evt.ignore()
110
111 def contextMenuEvent(self, evt):
112 """
113 Protected method handling context menu events.
114
115 @param evt reference to the context menu event (QContextMenuEvent)
116 """
117 point = evt.globalPos()
118
119 if self.__browser:
120 point = self.__browser.mapFromGlobal(point)
121 if not self.__browser.rect().contains(point, True):
122 return
123 link = QUrl(self.__browser.anchorAt(point))
124 else:
125 point = self.__result.mapFromGlobal(point)
126 link = self.__result.linkAt(point)
127
128 if link.isEmpty() or not link.isValid():
129 return
130
131 menu = QMenu()
132 curTab = menu.addAction(self.tr("Open Link"))
133 newTab = menu.addAction(self.tr("Open Link in New Tab"))
134 menu.move(evt.globalPos())
135 act = menu.exec_()
136 if act == curTab:
137 self.linkActivated.emit(link)
138 elif act == newTab:
139 self.__mw.newTab(link)

eric ide

mercurial