eric6/Helpviewer/HelpTocWidget.py

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

eric ide

mercurial