src/eric7/WebBrowser/QtHelp/HelpTocWidget.py

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

eric ide

mercurial