eric7/WebBrowser/QtHelp/HelpIndexWidget.py

branch
eric7
changeset 8421
cd4eee7f1d28
parent 8318
962bce857696
child 8564
c48137b0d7ba
equal deleted inserted replaced
8420:ff89f8bac0a5 8421:cd4eee7f1d28
6 """ 6 """
7 Module implementing a window for showing the QtHelp index. 7 Module implementing a window for showing the QtHelp index.
8 """ 8 """
9 9
10 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QEvent 10 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QEvent
11 from PyQt6.QtHelp import QHelpLink
11 from PyQt6.QtWidgets import ( 12 from PyQt6.QtWidgets import (
12 QWidget, QVBoxLayout, QLabel, QLineEdit, QMenu, QDialog, QApplication 13 QWidget, QVBoxLayout, QLabel, QLineEdit, QMenu, QDialog, QApplication
13 ) 14 )
14 15
15 16
32 33
33 def __init__(self, engine, parent=None): 34 def __init__(self, engine, parent=None):
34 """ 35 """
35 Constructor 36 Constructor
36 37
37 @param engine reference to the help engine (QHelpEngine) 38 @param engine reference to the help engine
38 @param parent reference to the parent widget (QWidget) 39 @type QHelpEngine
40 @param parent reference to the parent widget
41 @type QWidget
39 """ 42 """
40 super().__init__(parent) 43 super().__init__(parent)
41 44
42 self.__engine = engine 45 self.__engine = engine
43 46
60 63
61 self.__engine.indexModel().indexCreationStarted.connect( 64 self.__engine.indexModel().indexCreationStarted.connect(
62 self.__disableSearchEdit) 65 self.__disableSearchEdit)
63 self.__engine.indexModel().indexCreated.connect( 66 self.__engine.indexModel().indexCreated.connect(
64 self.__enableSearchEdit) 67 self.__enableSearchEdit)
65 # TODO: change code to use documentActivated and documentsActivated 68 self.__index.documentActivated.connect(self.__documentActivated)
66 self.__index.linkActivated.connect(self.__linkActivated) 69 self.__index.documentsActivated.connect(self.__documentsActivated)
67 self.__index.linksActivated.connect(self.__linksActivated)
68 self.__index.customContextMenuRequested.connect( 70 self.__index.customContextMenuRequested.connect(
69 self.__showContextMenu) 71 self.__showContextMenu)
70 self.__searchEdit.returnPressed.connect( 72 self.__searchEdit.returnPressed.connect(
71 self.__index.activateCurrentItem) 73 self.__index.activateCurrentItem)
72 self.__layout.addWidget(self.__index) 74 self.__layout.addWidget(self.__index)
73 75
74 @pyqtSlot(QUrl, str) 76 @pyqtSlot(QUrl, str)
75 def __linkActivated(self, url, keyword, modifiers=None): 77 def __documentActivated(self, document, keyword, modifiers=None):
76 """ 78 """
77 Private slot to handle the activation of a keyword entry. 79 Private slot to handle the activation of a keyword entry.
78 80
79 @param url URL of the selected entry 81 @param document reference to a data structure containing the
80 @type QUrl 82 document info
83 @type QHelpLink
81 @param keyword keyword for the URL 84 @param keyword keyword for the URL
82 @type str 85 @type str
83 @param modifiers keyboard modifiers 86 @param modifiers keyboard modifiers
84 @type Qt.KeyboardModifiers or None 87 @type Qt.KeyboardModifiers or None
85 """ 88 """
86 if modifiers is None: 89 if modifiers is None:
87 modifiers = QApplication.keyboardModifiers() 90 modifiers = QApplication.keyboardModifiers()
88 if not url.isEmpty() and url.isValid(): 91 if not document.url.isEmpty() and document.url.isValid():
89 if ( 92 if modifiers & (
90 modifiers & ( 93 Qt.KeyboardModifier.ControlModifier |
91 Qt.KeyboardModifier.ControlModifier | 94 Qt.KeyboardModifier.ControlModifier
92 Qt.KeyboardModifier.ShiftModifier
93 ) == (
94 Qt.KeyboardModifier.ControlModifier |
95 Qt.KeyboardModifier.ShiftModifier
96 )
97 ): 95 ):
98 self.newBackgroundTab.emit(url) 96 self.newBackgroundTab.emit(document.url)
99 elif modifiers & Qt.KeyboardModifier.ControlModifier: 97 elif modifiers & Qt.KeyboardModifier.ControlModifier:
100 self.newTab.emit(url) 98 self.newTab.emit(document.url)
101 elif modifiers & Qt.KeyboardModifier.ShiftModifier: 99 elif modifiers & Qt.KeyboardModifier.ShiftModifier:
102 self.newWindow.emit(url) 100 self.newWindow.emit(document.url)
103 else: 101 else:
104 self.openUrl.emit(url) 102 self.openUrl.emit(document.url)
105 103
106 def __linksActivated(self, links, keyword): 104 def __documentsActivated(self, documents, keyword):
107 """ 105 """
108 Private slot to handle the activation of an entry with multiple links. 106 Private slot to handle the activation of an entry with multiple help
109 107 documents.
110 @param links dictionary containing the links 108
111 @type dict of key:str and value:QUrl 109 @param documents list of help document link data structures
110 @type list of QHelpLink
112 @param keyword keyword for the entry 111 @param keyword keyword for the entry
113 @type str 112 @type str
114 """ 113 """
115 modifiers = QApplication.keyboardModifiers() 114 modifiers = QApplication.keyboardModifiers()
116 url = ( 115 document = (
117 QUrl(links[list(links.keys())[0]]) 116 documents[0]
118 if len(links) == 1 else 117 if len(documents) == 1 else
119 self.__selectLink(links, keyword) 118 self.__selectDocument(documents, keyword)
120 ) 119 )
121 self.__linkActivated(url, keyword, modifiers) 120 self.__documentActivated(document, keyword, modifiers)
122 121
123 def __selectLink(self, links, keyword): 122 def __selectDocument(self, documents, keyword):
124 """ 123 """
125 Private method to give the user a chance to select among the 124 Private method to give the user a chance to select among the
126 returned links. 125 given documents.
127 126
128 @param links dictionary of document title and URL to select from 127 @param documents list of help document link data structures
129 @type dictionary of str (key) and QUrl (value) 128 @type list of QHelpLink
130 @param keyword keyword for the link set 129 @param keyword keyword for the documents
131 @type str 130 @type str
132 @return selected link 131 @return selected document
133 @rtype QUrl 132 @rtype QHelpLink
134 """ 133 """
135 link = QUrl() 134 document = QHelpLink()
136 from .HelpTopicDialog import HelpTopicDialog 135 from .HelpTopicDialog import HelpTopicDialog
137 dlg = HelpTopicDialog(self, keyword, links) 136 dlg = HelpTopicDialog(self, keyword, documents)
138 if dlg.exec() == QDialog.DialogCode.Accepted: 137 if dlg.exec() == QDialog.DialogCode.Accepted:
139 link = dlg.link() 138 document = dlg.document()
140 return link 139 return document
141 140
142 def __filterIndices(self, indexFilter): 141 def __filterIndices(self, indexFilter):
143 """ 142 """
144 Private slot to filter the indexes according to the given filter. 143 Private slot to filter the indexes according to the given filter.
145 144
146 @param indexFilter filter to be used (string) 145 @param indexFilter filter to be used
146 @type str
147 """ 147 """
148 if '*' in indexFilter: 148 if '*' in indexFilter:
149 self.__index.filterIndices(indexFilter, indexFilter) 149 self.__index.filterIndices(indexFilter, indexFilter)
150 else: 150 else:
151 self.__index.filterIndices(indexFilter) 151 self.__index.filterIndices(indexFilter)
165 165
166 def focusInEvent(self, evt): 166 def focusInEvent(self, evt):
167 """ 167 """
168 Protected method handling focus in events. 168 Protected method handling focus in events.
169 169
170 @param evt reference to the focus event object (QFocusEvent) 170 @param evt reference to the focus event object
171 @type QFocusEvent
171 """ 172 """
172 if evt.reason() != Qt.FocusReason.MouseFocusReason: 173 if evt.reason() != Qt.FocusReason.MouseFocusReason:
173 self.__searchEdit.selectAll() 174 self.__searchEdit.selectAll()
174 self.__searchEdit.setFocus() 175 self.__searchEdit.setFocus()
175 176
176 def eventFilter(self, watched, event): 177 def eventFilter(self, watched, event):
177 """ 178 """
178 Public method called to filter the event queue. 179 Public method called to filter the event queue.
179 180
180 @param watched the QObject being watched (QObject) 181 @param watched the QObject being watched
181 @param event the event that occurred (QEvent) 182 @type QObject
182 @return flag indicating whether the event was handled (boolean) 183 @param event the event that occurred
184 @type QEvent
185 @return flag indicating whether the event was handled
186 @rtype bool
183 """ 187 """
184 if ( 188 if (
185 self.__searchEdit and watched == self.__searchEdit and 189 self.__searchEdit and watched == self.__searchEdit and
186 event.type() == QEvent.Type.KeyPress 190 event.type() == QEvent.Type.KeyPress
187 ): 191 ):
203 207
204 def __showContextMenu(self, pos): 208 def __showContextMenu(self, pos):
205 """ 209 """
206 Private slot showing the context menu. 210 Private slot showing the context menu.
207 211
208 @param pos position to show the menu at (QPoint) 212 @param pos position to show the menu at
213 @type QPoint
209 """ 214 """
210 idx = self.__index.indexAt(pos) 215 idx = self.__index.indexAt(pos)
211 if idx.isValid(): 216 if idx.isValid():
212 menu = QMenu() 217 menu = QMenu()
213 curTab = menu.addAction(self.tr("Open Link")) 218 curTab = menu.addAction(self.tr("Open Link"))

eric ide

mercurial