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