--- a/WebBrowser/QtHelp/HelpIndexWidget.py Mon Oct 17 18:57:24 2016 +0200 +++ b/WebBrowser/QtHelp/HelpIndexWidget.py Mon Oct 17 19:51:51 2016 +0200 @@ -9,7 +9,7 @@ from __future__ import unicode_literals -from PyQt5.QtCore import pyqtSignal, Qt, QUrl, QEvent +from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QEvent from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QMenu, \ QDialog, QApplication @@ -56,47 +56,60 @@ self.__layout.addWidget(self.__searchEdit) self.__index = self.__engine.indexWidget() - self.__index.installEventFilter(self) self.__index.setContextMenuPolicy(Qt.CustomContextMenu) self.__engine.indexModel().indexCreationStarted.connect( self.__disableSearchEdit) self.__engine.indexModel().indexCreated.connect( self.__enableSearchEdit) - self.__index.activated.connect(self.__activated) + self.__index.linkActivated.connect(self.__linkActivated) + self.__index.linksActivated.connect(self.__linksActivated) self.__index.customContextMenuRequested.connect( self.__showContextMenu) self.__searchEdit.returnPressed.connect( self.__index.activateCurrentItem) self.__layout.addWidget(self.__index) - - self.__index.viewport().installEventFilter(self) - def __activated(self, idx, midButton=False): + @pyqtSlot(QUrl, str) + def __linkActivated(self, url, keyword, modifiers=None): """ Private slot to handle the activation of a keyword entry. - @param idx index of the activated entry - @type QModelIndex - @param midButton flag indicating a middle mouse button release - @type bool + @param url URL of the selected entry + @type QUrl + @param keyword keyword for the URL + @type str + @keyparam modifiers keyboard modifiers + @type Qt.KeyboardModifiers or None """ - model = self.__index.model() - if model is not None: + if modifiers is None: modifiers = QApplication.keyboardModifiers() - keyword = model.data(idx, Qt.DisplayRole) - links = model.linksForKeyword(keyword) - if len(links) == 1: - link = QUrl(links[list(links.keys())[0]]) + if not url.isEmpty() and url.isValid(): + if modifiers & (Qt.ControlModifier | Qt.ShiftModifier) == \ + (Qt.ControlModifier | Qt.ShiftModifier): + self.newBackgroundTab.emit(url) + elif modifiers & Qt.ControlModifier: + self.newTab.emit(url) + elif modifiers & Qt.ShiftModifier: + self.newWindow.emit(url) else: - link = self.__selectLink(links, keyword) - if not link.isEmpty() and link.isValid(): - if modifiers & Qt.ControlModifier or midButton: - self.newTab.emit(link) - elif modifiers & Qt.ShiftModifier: - self.newWindow.emit(link) - else: - self.openUrl.emit(link) + self.openUrl.emit(url) + + def __linksActivated(self, links, keyword): + """ + Private slot to handle the activation of an entry with multiple links. + + @param links dictionary containing the links + @type dict of key:str and value:QUrl + @param keyword keyword for the entry + @type str + """ + modifiers = QApplication.keyboardModifiers() + if len(links) == 1: + url = QUrl(links[list(links.keys())[0]]) + else: + url = self.__selectLink(links, keyword) + self.__linkActivated(url, keyword, modifiers) def __selectLink(self, links, keyword): """ @@ -174,11 +187,6 @@ self.__index.setCurrentIndex(idx) elif event.key() == Qt.Key_Escape: self.escapePressed.emit() - elif self.__index and watched == self.__index.viewport() and \ - event.type() == QEvent.MouseButtonRelease: - idx = self.__index.indexAt(event.pos()) - if idx.isValid(): - self.__activated(idx, midButton=event.button() == Qt.MidButton) return QWidget.eventFilter(self, watched, event) @@ -208,11 +216,12 @@ else: link = self.__selectLink(links, keyword) - if act == curTab: - self.openUrl.emit(link) - elif act == newTab: - self.newTab.emit(link) - elif act == newBackgroundTab: - self.newBackgroundTab.emit(link) - elif act == newWindow: - self.newWindow.emit(link) + if not link.isEmpty() and link.isValid(): + if act == curTab: + self.openUrl.emit(link) + elif act == newTab: + self.newTab.emit(link) + elif act == newBackgroundTab: + self.newBackgroundTab.emit(link) + elif act == newWindow: + self.newWindow.emit(link)