eric7/WebBrowser/QtHelp/HelpTocWidget.py

branch
eric7
changeset 8312
800c432b34c8
parent 8218
7c09585bd960
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
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 TOC.
8 """
9
10 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl
11 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QMenu, QApplication
12
13
14 class HelpTocWidget(QWidget):
15 """
16 Class implementing a window for showing the QtHelp TOC.
17
18 @signal escapePressed() emitted when the ESC key was pressed
19 @signal openUrl(QUrl, str) emitted to open an entry in the current tab
20 @signal newTab(QUrl, str) emitted to open an entry in a new tab
21 @signal newBackgroundTab(QUrl, str) emitted to open an entry in a
22 new background tab
23 @signal newWindow(QUrl, str) emitted to open an entry in a new window
24 """
25 escapePressed = pyqtSignal()
26 openUrl = pyqtSignal(QUrl)
27 newTab = pyqtSignal(QUrl)
28 newBackgroundTab = pyqtSignal(QUrl)
29 newWindow = pyqtSignal(QUrl)
30
31 def __init__(self, engine, parent=None):
32 """
33 Constructor
34
35 @param engine reference to the help engine (QHelpEngine)
36 @param parent reference to the parent widget (QWidget)
37 """
38 super().__init__(parent)
39
40 self.__engine = engine
41 self.__expandDepth = -2
42
43 self.__tocWidget = self.__engine.contentWidget()
44 self.__tocWidget.setContextMenuPolicy(
45 Qt.ContextMenuPolicy.CustomContextMenu)
46 self.__tocWidget.setSortingEnabled(True)
47
48 self.__layout = QVBoxLayout(self)
49 self.__layout.addWidget(self.__tocWidget)
50
51 self.__tocWidget.customContextMenuRequested.connect(
52 self.__showContextMenu)
53 self.__tocWidget.linkActivated.connect(self.__linkActivated)
54
55 model = self.__tocWidget.model()
56 model.contentsCreated.connect(self.__contentsCreated)
57
58 @pyqtSlot(QUrl)
59 def __linkActivated(self, url):
60 """
61 Private slot handling the activation of an entry.
62
63 @param url URL of the activated entry
64 @type QUrl
65 """
66 if not url.isEmpty() and url.isValid():
67 buttons = QApplication.mouseButtons()
68 modifiers = QApplication.keyboardModifiers()
69
70 if buttons & Qt.MouseButton.MidButton:
71 self.newTab.emit(url)
72 else:
73 if (
74 modifiers & (
75 Qt.KeyboardModifier.ControlModifier |
76 Qt.KeyboardModifier.ShiftModifier
77 ) == (
78 Qt.KeyboardModifier.ControlModifier |
79 Qt.KeyboardModifier.ShiftModifier
80 )
81 ):
82 self.newBackgroundTab.emit(url)
83 elif modifiers & Qt.KeyboardModifier.ControlModifier:
84 self.newTab.emit(url)
85 elif modifiers & Qt.KeyboardModifier.ShiftModifier:
86 self.newWindow.emit(url)
87 else:
88 self.openUrl.emit(url)
89
90 def __contentsCreated(self):
91 """
92 Private slot to be run after the contents was generated.
93 """
94 self.__tocWidget.sortByColumn(0, Qt.SortOrder.AscendingOrder)
95 self.__expandTOC()
96
97 def __expandTOC(self):
98 """
99 Private slot to expand the table of contents.
100 """
101 if self.__expandDepth > -2:
102 self.expandToDepth(self.__expandDepth)
103 self.__expandDepth = -2
104
105 def expandToDepth(self, depth):
106 """
107 Public slot to expand the table of contents to a specific depth.
108
109 @param depth depth to expand to (integer)
110 """
111 self.__expandDepth = depth
112 if depth == -1:
113 self.__tocWidget.expandAll()
114 else:
115 self.__tocWidget.expandToDepth(depth)
116
117 def focusInEvent(self, evt):
118 """
119 Protected method handling focus in events.
120
121 @param evt reference to the focus event object (QFocusEvent)
122 """
123 if evt.reason() != Qt.FocusReason.MouseFocusReason:
124 self.__tocWidget.setFocus()
125
126 def keyPressEvent(self, evt):
127 """
128 Protected method handling key press events.
129
130 @param evt reference to the key press event (QKeyEvent)
131 """
132 if evt.key() == Qt.Key.Key_Escape:
133 self.escapePressed.emit()
134
135 def syncToContent(self, url):
136 """
137 Public method to sync the TOC to the displayed page.
138
139 @param url URL of the displayed page (QUrl)
140 @return flag indicating a successful synchronization (boolean)
141 """
142 idx = self.__tocWidget.indexOf(url)
143 if not idx.isValid():
144 return False
145 self.__tocWidget.setCurrentIndex(idx)
146 return True
147
148 def __showContextMenu(self, pos):
149 """
150 Private slot showing the context menu.
151
152 @param pos position to show the menu at (QPoint)
153 """
154 if not self.__tocWidget.indexAt(pos).isValid():
155 return
156
157 model = self.__tocWidget.model()
158 itm = model.contentItemAt(self.__tocWidget.currentIndex())
159 link = itm.url()
160 if link.isEmpty() or not link.isValid():
161 return
162
163 menu = QMenu()
164 curTab = menu.addAction(self.tr("Open Link"))
165 newTab = menu.addAction(self.tr("Open Link in New Tab"))
166 newBackgroundTab = menu.addAction(
167 self.tr("Open Link in Background Tab"))
168 newWindow = menu.addAction(self.tr("Open Link in New Window"))
169 menu.move(self.__tocWidget.mapToGlobal(pos))
170
171 act = menu.exec()
172 if act == curTab:
173 self.openUrl.emit(link)
174 elif act == newTab:
175 self.newTab.emit(link)
176 elif act == newBackgroundTab:
177 self.newBackgroundTab.emit(link)
178 elif act == newWindow:
179 self.newWindow.emit(link)

eric ide

mercurial