|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a window for showing the QtHelp TOC. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl |
|
11 from PyQt6.QtGui import QGuiApplication, QClipboard |
|
12 from PyQt6.QtWidgets import QWidget, QVBoxLayout, QMenu, QApplication |
|
13 |
|
14 |
|
15 class HelpTocWidget(QWidget): |
|
16 """ |
|
17 Class implementing a window for showing the QtHelp TOC. |
|
18 |
|
19 @signal escapePressed() emitted when the ESC key was pressed |
|
20 @signal openUrl(QUrl, str) emitted to open an entry in the current tab |
|
21 @signal newTab(QUrl, str) emitted to open an entry in a new tab |
|
22 @signal newBackgroundTab(QUrl, str) emitted to open an entry in a |
|
23 new background tab |
|
24 @signal newWindow(QUrl, str) emitted to open an entry in a new window |
|
25 """ |
|
26 escapePressed = pyqtSignal() |
|
27 openUrl = pyqtSignal(QUrl) |
|
28 newTab = pyqtSignal(QUrl) |
|
29 newBackgroundTab = pyqtSignal(QUrl) |
|
30 newWindow = pyqtSignal(QUrl) |
|
31 |
|
32 def __init__(self, engine, internal=False, parent=None): |
|
33 """ |
|
34 Constructor |
|
35 |
|
36 @param engine reference to the help engine |
|
37 @type QHelpEngine |
|
38 @param internal flag indicating the internal help viewer |
|
39 @type bool |
|
40 @param parent reference to the parent widget |
|
41 @type QWidget |
|
42 """ |
|
43 super().__init__(parent) |
|
44 |
|
45 self.__engine = engine |
|
46 self.__expandDepth = -2 |
|
47 |
|
48 self.__internal = internal |
|
49 |
|
50 self.__tocWidget = self.__engine.contentWidget() |
|
51 self.__tocWidget.setContextMenuPolicy( |
|
52 Qt.ContextMenuPolicy.CustomContextMenu) |
|
53 self.__tocWidget.setSortingEnabled(True) |
|
54 |
|
55 self.__layout = QVBoxLayout(self) |
|
56 if internal: |
|
57 # no margins for the internal variant |
|
58 self.__layout.setContentsMargins(0, 0, 0, 0) |
|
59 self.__layout.addWidget(self.__tocWidget) |
|
60 |
|
61 self.__tocWidget.customContextMenuRequested.connect( |
|
62 self.__showContextMenu) |
|
63 self.__tocWidget.linkActivated.connect(self.__linkActivated) |
|
64 |
|
65 model = self.__tocWidget.model() |
|
66 model.contentsCreated.connect(self.__contentsCreated) |
|
67 |
|
68 @pyqtSlot(QUrl) |
|
69 def __linkActivated(self, url): |
|
70 """ |
|
71 Private slot handling the activation of an entry. |
|
72 |
|
73 @param url URL of the activated entry |
|
74 @type QUrl |
|
75 """ |
|
76 if not url.isEmpty() and url.isValid(): |
|
77 buttons = QApplication.mouseButtons() |
|
78 modifiers = QApplication.keyboardModifiers() |
|
79 |
|
80 if buttons & Qt.MouseButton.MiddleButton: |
|
81 self.newTab.emit(url) |
|
82 else: |
|
83 if ( |
|
84 modifiers & ( |
|
85 Qt.KeyboardModifier.ControlModifier | |
|
86 Qt.KeyboardModifier.ShiftModifier |
|
87 ) == ( |
|
88 Qt.KeyboardModifier.ControlModifier | |
|
89 Qt.KeyboardModifier.ShiftModifier |
|
90 ) |
|
91 ): |
|
92 self.newBackgroundTab.emit(url) |
|
93 elif modifiers & Qt.KeyboardModifier.ControlModifier: |
|
94 self.newTab.emit(url) |
|
95 elif ( |
|
96 modifiers & Qt.KeyboardModifier.ShiftModifier and |
|
97 not self.__internal |
|
98 ): |
|
99 self.newWindow.emit(url) |
|
100 else: |
|
101 self.openUrl.emit(url) |
|
102 |
|
103 def __contentsCreated(self): |
|
104 """ |
|
105 Private slot to be run after the contents was generated. |
|
106 """ |
|
107 self.__tocWidget.sortByColumn(0, Qt.SortOrder.AscendingOrder) |
|
108 self.__expandTOC() |
|
109 |
|
110 def __expandTOC(self): |
|
111 """ |
|
112 Private slot to expand the table of contents. |
|
113 """ |
|
114 if self.__expandDepth > -2: |
|
115 self.expandToDepth(self.__expandDepth) |
|
116 self.__expandDepth = -2 |
|
117 |
|
118 def expandToDepth(self, depth): |
|
119 """ |
|
120 Public slot to expand the table of contents to a specific depth. |
|
121 |
|
122 @param depth depth to expand to (integer) |
|
123 """ |
|
124 self.__expandDepth = depth |
|
125 if depth == -1: |
|
126 self.__tocWidget.expandAll() |
|
127 else: |
|
128 self.__tocWidget.expandToDepth(depth) |
|
129 |
|
130 def focusInEvent(self, evt): |
|
131 """ |
|
132 Protected method handling focus in events. |
|
133 |
|
134 @param evt reference to the focus event object (QFocusEvent) |
|
135 """ |
|
136 if evt.reason() != Qt.FocusReason.MouseFocusReason: |
|
137 self.__tocWidget.setFocus() |
|
138 |
|
139 def keyPressEvent(self, evt): |
|
140 """ |
|
141 Protected method handling key press events. |
|
142 |
|
143 @param evt reference to the key press event (QKeyEvent) |
|
144 """ |
|
145 if evt.key() == Qt.Key.Key_Escape: |
|
146 self.escapePressed.emit() |
|
147 |
|
148 def syncToContent(self, url): |
|
149 """ |
|
150 Public method to sync the TOC to the displayed page. |
|
151 |
|
152 @param url URL of the displayed page (QUrl) |
|
153 @return flag indicating a successful synchronization (boolean) |
|
154 """ |
|
155 idx = self.__tocWidget.indexOf(url) |
|
156 if not idx.isValid(): |
|
157 return False |
|
158 self.__tocWidget.setCurrentIndex(idx) |
|
159 return True |
|
160 |
|
161 def __showContextMenu(self, pos): |
|
162 """ |
|
163 Private slot showing the context menu. |
|
164 |
|
165 @param pos position to show the menu at (QPoint) |
|
166 """ |
|
167 if not self.__tocWidget.indexAt(pos).isValid(): |
|
168 return |
|
169 |
|
170 model = self.__tocWidget.model() |
|
171 itm = model.contentItemAt(self.__tocWidget.currentIndex()) |
|
172 link = itm.url() |
|
173 if link.isEmpty() or not link.isValid(): |
|
174 return |
|
175 |
|
176 menu = QMenu() |
|
177 curTab = menu.addAction(self.tr("Open Link")) |
|
178 if self.__internal: |
|
179 newTab = menu.addAction(self.tr("Open Link in New Page")) |
|
180 newBackgroundTab = menu.addAction( |
|
181 self.tr("Open Link in Background Page")) |
|
182 else: |
|
183 newTab = menu.addAction(self.tr("Open Link in New Tab")) |
|
184 newBackgroundTab = menu.addAction( |
|
185 self.tr("Open Link in Background Tab")) |
|
186 newWindow = menu.addAction(self.tr("Open Link in New Window")) |
|
187 menu.addSeparator() |
|
188 copyLink = menu.addAction(self.tr("Copy URL to Clipboard")) |
|
189 menu.move(self.__tocWidget.mapToGlobal(pos)) |
|
190 |
|
191 act = menu.exec() |
|
192 if act == curTab: |
|
193 self.openUrl.emit(link) |
|
194 elif act == newTab: |
|
195 self.newTab.emit(link) |
|
196 elif act == newBackgroundTab: |
|
197 self.newBackgroundTab.emit(link) |
|
198 elif not self.__internal and act == newWindow: |
|
199 self.newWindow.emit(link) |
|
200 elif act == copyLink: |
|
201 # copy the URL to both clipboard areas |
|
202 QGuiApplication.clipboard().setText( |
|
203 link.toString(), QClipboard.Mode.Clipboard) |
|
204 QGuiApplication.clipboard().setText( |
|
205 link.toString(), QClipboard.Mode.Selection) |