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 __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import pyqtSignal, Qt, QUrl, QEvent |
12 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QEvent |
13 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QMenu, \ |
13 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QMenu, \ |
14 QDialog |
14 QDialog, QApplication |
15 |
15 |
16 |
16 |
17 class HelpIndexWidget(QWidget): |
17 class HelpIndexWidget(QWidget): |
18 """ |
18 """ |
19 Class implementing a window for showing the QtHelp index. |
19 Class implementing a window for showing the QtHelp index. |
52 self.__searchEdit.textChanged.connect(self.__filterIndices) |
52 self.__searchEdit.textChanged.connect(self.__filterIndices) |
53 self.__searchEdit.installEventFilter(self) |
53 self.__searchEdit.installEventFilter(self) |
54 self.__layout.addWidget(self.__searchEdit) |
54 self.__layout.addWidget(self.__searchEdit) |
55 |
55 |
56 self.__index = self.__engine.indexWidget() |
56 self.__index = self.__engine.indexWidget() |
57 self.__index.installEventFilter(self) |
57 self.__index.setContextMenuPolicy(Qt.CustomContextMenu) |
58 self.__engine.indexModel().indexCreationStarted.connect( |
58 self.__engine.indexModel().indexCreationStarted.connect( |
59 self.__disableSearchEdit) |
59 self.__disableSearchEdit) |
60 self.__engine.indexModel().indexCreated.connect( |
60 self.__engine.indexModel().indexCreated.connect( |
61 self.__enableSearchEdit) |
61 self.__enableSearchEdit) |
62 self.__index.activated.connect(self.__activated) |
62 self.__index.linkActivated.connect(self.__linkActivated) |
|
63 self.__index.linksActivated.connect(self.__linksActivated) |
|
64 self.__index.customContextMenuRequested.connect( |
|
65 self.__showContextMenu) |
63 self.__searchEdit.returnPressed.connect( |
66 self.__searchEdit.returnPressed.connect( |
64 self.__index.activateCurrentItem) |
67 self.__index.activateCurrentItem) |
65 self.__layout.addWidget(self.__index) |
68 self.__layout.addWidget(self.__index) |
66 |
69 |
67 self.__index.viewport().installEventFilter(self) |
70 @pyqtSlot(QUrl, str) |
68 |
71 def __linkActivated(self, url, keyword, modifiers=None): |
69 def __activated(self, idx): |
|
70 """ |
72 """ |
71 Private slot to handle the activation of a keyword entry. |
73 Private slot to handle the activation of a keyword entry. |
72 |
74 |
73 @param idx index of the activated entry (QModelIndex) |
75 @param url URL of the selected entry |
74 """ |
76 @type QUrl |
75 model = self.__index.model() |
77 @param keyword keyword for the URL |
76 if model is not None: |
78 @type str |
77 keyword = model.data(idx, Qt.DisplayRole) |
79 @keyparam modifiers keyboard modifiers |
78 links = model.linksForKeyword(keyword) |
80 @type Qt.KeyboardModifiers or None |
79 if len(links) == 1: |
81 """ |
80 self.linkActivated.emit(QUrl(links[list(links.keys())[0]])) |
82 if modifiers is None: |
|
83 modifiers = QApplication.keyboardModifiers() |
|
84 if not url.isEmpty() and url.isValid(): |
|
85 if modifiers & Qt.ControlModifier: |
|
86 self.__mw.newTab(url) |
81 else: |
87 else: |
82 self.linksActivated.emit(links, keyword) |
88 self.linkActivated.emit(url) |
|
89 |
|
90 def __linksActivated(self, links, keyword): |
|
91 """ |
|
92 Private slot to handle the activation of an entry with multiple links. |
|
93 |
|
94 @param links dictionary containing the links |
|
95 @type dict of key:str and value:QUrl |
|
96 @param keyword keyword for the entry |
|
97 @type str |
|
98 """ |
|
99 modifiers = QApplication.keyboardModifiers() |
|
100 if len(links) == 1: |
|
101 url = QUrl(links[list(links.keys())[0]]) |
|
102 else: |
|
103 url = self.__selectLink(links, keyword) |
|
104 self.__linkActivated(url, keyword, modifiers) |
|
105 |
|
106 def __selectLink(self, links, keyword): |
|
107 """ |
|
108 Private method to give the user a chance to select among the |
|
109 returned links. |
|
110 |
|
111 @param links dictionary of document title and URL to select from |
|
112 @type dictionary of str (key) and QUrl (value) |
|
113 @param keyword keyword for the link set |
|
114 @type str |
|
115 @return selected link |
|
116 @rtype QUrl |
|
117 """ |
|
118 link = QUrl() |
|
119 from .HelpTopicDialog import HelpTopicDialog |
|
120 dlg = HelpTopicDialog(self, keyword, links) |
|
121 if dlg.exec_() == QDialog.Accepted: |
|
122 link = dlg.link() |
|
123 return link |
83 |
124 |
84 def __filterIndices(self, filter): |
125 def __filterIndices(self, filter): |
85 """ |
126 """ |
86 Private slot to filter the indices according to the given filter. |
127 Private slot to filter the indices according to the given filter. |
87 |
128 |
136 idx.row() + 1, idx.column(), idx.parent()) |
177 idx.row() + 1, idx.column(), idx.parent()) |
137 if idx.isValid(): |
178 if idx.isValid(): |
138 self.__index.setCurrentIndex(idx) |
179 self.__index.setCurrentIndex(idx) |
139 elif event.key() == Qt.Key_Escape: |
180 elif event.key() == Qt.Key_Escape: |
140 self.escapePressed.emit() |
181 self.escapePressed.emit() |
141 elif self.__index and watched == self.__index and \ |
182 |
142 event.type() == QEvent.ContextMenu: |
183 return QWidget.eventFilter(self, watched, event) |
143 idx = self.__index.indexAt(event.pos()) |
184 |
144 if idx.isValid(): |
185 def __showContextMenu(self, pos): |
145 menu = QMenu() |
186 """ |
146 curTab = menu.addAction(self.tr("Open Link")) |
187 Private slot showing the context menu. |
147 newTab = menu.addAction(self.tr("Open Link in New Tab")) |
188 |
148 menu.move(self.__index.mapToGlobal(event.pos())) |
189 @param pos position to show the menu at (QPoint) |
|
190 """ |
|
191 idx = self.__index.indexAt(pos) |
|
192 if idx.isValid(): |
|
193 menu = QMenu() |
|
194 curTab = menu.addAction(self.tr("Open Link")) |
|
195 newTab = menu.addAction(self.tr("Open Link in New Tab")) |
|
196 menu.move(self.__index.mapToGlobal(pos)) |
|
197 |
|
198 act = menu.exec_() |
|
199 model = self.__index.model() |
|
200 if model is not None: |
|
201 keyword = model.data(idx, Qt.DisplayRole) |
|
202 links = model.linksForKeyword(keyword) |
|
203 if len(links) == 1: |
|
204 link = QUrl(links[list(links.keys())[0]]) |
|
205 else: |
|
206 link = self.__selectLink(links, keyword) |
149 |
207 |
150 act = menu.exec_() |
208 if not link.isEmpty() and link.isValid(): |
151 if act == curTab: |
209 if act == curTab: |
152 self.__activated(idx) |
210 self.linkActivated.emit(link) |
153 elif act == newTab: |
211 elif act == newTab: |
154 model = self.__index.model() |
212 self.__mw.newTab(link) |
155 if model is not None: |
|
156 keyword = model.data(idx, Qt.DisplayRole) |
|
157 links = model.linksForKeyword(keyword) |
|
158 if len(links) == 1: |
|
159 self.__mw.newTab(list(links.values())[0]) |
|
160 elif len(links) > 1: |
|
161 from .HelpTopicDialog import HelpTopicDialog |
|
162 dlg = HelpTopicDialog(self, keyword, links) |
|
163 if dlg.exec_() == QDialog.Accepted: |
|
164 self.__mw.newTab(dlg.link()) |
|
165 elif self.__index and watched == self.__index.viewport() and \ |
|
166 event.type() == QEvent.MouseButtonRelease: |
|
167 idx = self.__index.indexAt(event.pos()) |
|
168 if idx.isValid() and event.button() == Qt.MidButton: |
|
169 model = self.__index.model() |
|
170 if model is not None: |
|
171 keyword = model.data(idx, Qt.DisplayRole) |
|
172 links = model.linksForKeyword(keyword) |
|
173 if len(links) == 1: |
|
174 self.__mw.newTab(list(links.values())[0]) |
|
175 elif len(links) > 1: |
|
176 from .HelpTopicDialog import HelpTopicDialog |
|
177 dlg = HelpTopicDialog(self, keyword, links) |
|
178 if dlg.exec_() == QDialog.Accepted: |
|
179 self.__mw.newTab(dlg.link()) |
|
180 |
|
181 return QWidget.eventFilter(self, watched, event) |
|