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, QApplication |
14 QDialog, QApplication |
15 |
15 |
16 |
16 |
17 class HelpIndexWidget(QWidget): |
17 class HelpIndexWidget(QWidget): |
54 self.__searchEdit.textChanged.connect(self.__filterIndices) |
54 self.__searchEdit.textChanged.connect(self.__filterIndices) |
55 self.__searchEdit.installEventFilter(self) |
55 self.__searchEdit.installEventFilter(self) |
56 self.__layout.addWidget(self.__searchEdit) |
56 self.__layout.addWidget(self.__searchEdit) |
57 |
57 |
58 self.__index = self.__engine.indexWidget() |
58 self.__index = self.__engine.indexWidget() |
59 self.__index.installEventFilter(self) |
|
60 self.__index.setContextMenuPolicy(Qt.CustomContextMenu) |
59 self.__index.setContextMenuPolicy(Qt.CustomContextMenu) |
61 |
60 |
62 self.__engine.indexModel().indexCreationStarted.connect( |
61 self.__engine.indexModel().indexCreationStarted.connect( |
63 self.__disableSearchEdit) |
62 self.__disableSearchEdit) |
64 self.__engine.indexModel().indexCreated.connect( |
63 self.__engine.indexModel().indexCreated.connect( |
65 self.__enableSearchEdit) |
64 self.__enableSearchEdit) |
66 self.__index.activated.connect(self.__activated) |
65 self.__index.linkActivated.connect(self.__linkActivated) |
|
66 self.__index.linksActivated.connect(self.__linksActivated) |
67 self.__index.customContextMenuRequested.connect( |
67 self.__index.customContextMenuRequested.connect( |
68 self.__showContextMenu) |
68 self.__showContextMenu) |
69 self.__searchEdit.returnPressed.connect( |
69 self.__searchEdit.returnPressed.connect( |
70 self.__index.activateCurrentItem) |
70 self.__index.activateCurrentItem) |
71 self.__layout.addWidget(self.__index) |
71 self.__layout.addWidget(self.__index) |
72 |
72 |
73 self.__index.viewport().installEventFilter(self) |
73 @pyqtSlot(QUrl, str) |
74 |
74 def __linkActivated(self, url, keyword, modifiers=None): |
75 def __activated(self, idx, midButton=False): |
|
76 """ |
75 """ |
77 Private slot to handle the activation of a keyword entry. |
76 Private slot to handle the activation of a keyword entry. |
78 |
77 |
79 @param idx index of the activated entry |
78 @param url URL of the selected entry |
80 @type QModelIndex |
79 @type QUrl |
81 @param midButton flag indicating a middle mouse button release |
80 @param keyword keyword for the URL |
82 @type bool |
81 @type str |
83 """ |
82 @keyparam modifiers keyboard modifiers |
84 model = self.__index.model() |
83 @type Qt.KeyboardModifiers or None |
85 if model is not None: |
84 """ |
|
85 if modifiers is None: |
86 modifiers = QApplication.keyboardModifiers() |
86 modifiers = QApplication.keyboardModifiers() |
87 keyword = model.data(idx, Qt.DisplayRole) |
87 if not url.isEmpty() and url.isValid(): |
88 links = model.linksForKeyword(keyword) |
88 if modifiers & (Qt.ControlModifier | Qt.ShiftModifier) == \ |
89 if len(links) == 1: |
89 (Qt.ControlModifier | Qt.ShiftModifier): |
90 link = QUrl(links[list(links.keys())[0]]) |
90 self.newBackgroundTab.emit(url) |
|
91 elif modifiers & Qt.ControlModifier: |
|
92 self.newTab.emit(url) |
|
93 elif modifiers & Qt.ShiftModifier: |
|
94 self.newWindow.emit(url) |
91 else: |
95 else: |
92 link = self.__selectLink(links, keyword) |
96 self.openUrl.emit(url) |
93 if not link.isEmpty() and link.isValid(): |
97 |
94 if modifiers & Qt.ControlModifier or midButton: |
98 def __linksActivated(self, links, keyword): |
95 self.newTab.emit(link) |
99 """ |
96 elif modifiers & Qt.ShiftModifier: |
100 Private slot to handle the activation of an entry with multiple links. |
97 self.newWindow.emit(link) |
101 |
98 else: |
102 @param links dictionary containing the links |
99 self.openUrl.emit(link) |
103 @type dict of key:str and value:QUrl |
|
104 @param keyword keyword for the entry |
|
105 @type str |
|
106 """ |
|
107 modifiers = QApplication.keyboardModifiers() |
|
108 if len(links) == 1: |
|
109 url = QUrl(links[list(links.keys())[0]]) |
|
110 else: |
|
111 url = self.__selectLink(links, keyword) |
|
112 self.__linkActivated(url, keyword, modifiers) |
100 |
113 |
101 def __selectLink(self, links, keyword): |
114 def __selectLink(self, links, keyword): |
102 """ |
115 """ |
103 Private method to give the user a chance to select among the |
116 Private method to give the user a chance to select among the |
104 returned links. |
117 returned links. |
172 idx.row() + 1, idx.column(), idx.parent()) |
185 idx.row() + 1, idx.column(), idx.parent()) |
173 if idx.isValid(): |
186 if idx.isValid(): |
174 self.__index.setCurrentIndex(idx) |
187 self.__index.setCurrentIndex(idx) |
175 elif event.key() == Qt.Key_Escape: |
188 elif event.key() == Qt.Key_Escape: |
176 self.escapePressed.emit() |
189 self.escapePressed.emit() |
177 elif self.__index and watched == self.__index.viewport() and \ |
|
178 event.type() == QEvent.MouseButtonRelease: |
|
179 idx = self.__index.indexAt(event.pos()) |
|
180 if idx.isValid(): |
|
181 self.__activated(idx, midButton=event.button() == Qt.MidButton) |
|
182 |
190 |
183 return QWidget.eventFilter(self, watched, event) |
191 return QWidget.eventFilter(self, watched, event) |
184 |
192 |
185 def __showContextMenu(self, pos): |
193 def __showContextMenu(self, pos): |
186 """ |
194 """ |