|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a window for showing the QtHelp index. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSignal, Qt, QUrl, QEvent |
|
13 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QLineEdit, QMenu, \ |
|
14 QDialog |
|
15 |
|
16 |
|
17 class HelpIndexWidget(QWidget): |
|
18 """ |
|
19 Class implementing a window for showing the QtHelp index. |
|
20 |
|
21 @signal linkActivated(QUrl) emitted when an index entry is activated |
|
22 @signal linksActivated(links, keyword) emitted when an index entry |
|
23 referencing multiple targets is activated |
|
24 @signal escapePressed() emitted when the ESC key was pressed |
|
25 """ |
|
26 linkActivated = pyqtSignal(QUrl) |
|
27 linksActivated = pyqtSignal(dict, str) |
|
28 escapePressed = pyqtSignal() |
|
29 |
|
30 def __init__(self, engine, mainWindow, parent=None): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param engine reference to the help engine (QHelpEngine) |
|
35 @param mainWindow reference to the main window object (QMainWindow) |
|
36 @param parent reference to the parent widget (QWidget) |
|
37 """ |
|
38 super(HelpIndexWidget, self).__init__(parent) |
|
39 |
|
40 self.__engine = engine |
|
41 self.__mw = mainWindow |
|
42 |
|
43 self.__searchEdit = None |
|
44 self.__index = None |
|
45 |
|
46 self.__layout = QVBoxLayout(self) |
|
47 label = QLabel(self.tr("&Look for:")) |
|
48 self.__layout.addWidget(label) |
|
49 |
|
50 self.__searchEdit = QLineEdit() |
|
51 label.setBuddy(self.__searchEdit) |
|
52 self.__searchEdit.textChanged.connect(self.__filterIndices) |
|
53 self.__searchEdit.installEventFilter(self) |
|
54 self.__layout.addWidget(self.__searchEdit) |
|
55 |
|
56 self.__index = self.__engine.indexWidget() |
|
57 self.__index.installEventFilter(self) |
|
58 self.__engine.indexModel().indexCreationStarted.connect( |
|
59 self.__disableSearchEdit) |
|
60 self.__engine.indexModel().indexCreated.connect( |
|
61 self.__enableSearchEdit) |
|
62 self.__index.activated.connect(self.__activated) |
|
63 self.__searchEdit.returnPressed.connect( |
|
64 self.__index.activateCurrentItem) |
|
65 self.__layout.addWidget(self.__index) |
|
66 |
|
67 self.__index.viewport().installEventFilter(self) |
|
68 |
|
69 def __activated(self, idx): |
|
70 """ |
|
71 Private slot to handle the activation of a keyword entry. |
|
72 |
|
73 @param idx index of the activated entry (QModelIndex) |
|
74 """ |
|
75 model = self.__index.model() |
|
76 if model is not None: |
|
77 keyword = model.data(idx, Qt.DisplayRole) |
|
78 links = model.linksForKeyword(keyword) |
|
79 if len(links) == 1: |
|
80 self.linkActivated.emit(QUrl(links[list(links.keys())[0]])) |
|
81 else: |
|
82 self.linksActivated.emit(links, keyword) |
|
83 |
|
84 def __filterIndices(self, filter): |
|
85 """ |
|
86 Private slot to filter the indices according to the given filter. |
|
87 |
|
88 @param filter filter to be used (string) |
|
89 """ |
|
90 if '*' in filter: |
|
91 self.__index.filterIndices(filter, filter) |
|
92 else: |
|
93 self.__index.filterIndices(filter) |
|
94 |
|
95 def __enableSearchEdit(self): |
|
96 """ |
|
97 Private slot to enable the search edit. |
|
98 """ |
|
99 self.__searchEdit.setEnabled(True) |
|
100 self.__filterIndices(self.__searchEdit.text()) |
|
101 |
|
102 def __disableSearchEdit(self): |
|
103 """ |
|
104 Private slot to enable the search edit. |
|
105 """ |
|
106 self.__searchEdit.setEnabled(False) |
|
107 |
|
108 def focusInEvent(self, evt): |
|
109 """ |
|
110 Protected method handling focus in events. |
|
111 |
|
112 @param evt reference to the focus event object (QFocusEvent) |
|
113 """ |
|
114 if evt.reason() != Qt.MouseFocusReason: |
|
115 self.__searchEdit.selectAll() |
|
116 self.__searchEdit.setFocus() |
|
117 |
|
118 def eventFilter(self, watched, event): |
|
119 """ |
|
120 Public method called to filter the event queue. |
|
121 |
|
122 @param watched the QObject being watched (QObject) |
|
123 @param event the event that occurred (QEvent) |
|
124 @return flag indicating whether the event was handled (boolean) |
|
125 """ |
|
126 if self.__searchEdit and watched == self.__searchEdit and \ |
|
127 event.type() == QEvent.KeyPress: |
|
128 idx = self.__index.currentIndex() |
|
129 if event.key() == Qt.Key_Up: |
|
130 idx = self.__index.model().index( |
|
131 idx.row() - 1, idx.column(), idx.parent()) |
|
132 if idx.isValid(): |
|
133 self.__index.setCurrentIndex(idx) |
|
134 elif event.key() == Qt.Key_Down: |
|
135 idx = self.__index.model().index( |
|
136 idx.row() + 1, idx.column(), idx.parent()) |
|
137 if idx.isValid(): |
|
138 self.__index.setCurrentIndex(idx) |
|
139 elif event.key() == Qt.Key_Escape: |
|
140 self.escapePressed.emit() |
|
141 elif self.__index and watched == self.__index and \ |
|
142 event.type() == QEvent.ContextMenu: |
|
143 idx = self.__index.indexAt(event.pos()) |
|
144 if idx.isValid(): |
|
145 menu = QMenu() |
|
146 curTab = menu.addAction(self.tr("Open Link")) |
|
147 newTab = menu.addAction(self.tr("Open Link in New Tab")) |
|
148 menu.move(self.__index.mapToGlobal(event.pos())) |
|
149 |
|
150 act = menu.exec_() |
|
151 if act == curTab: |
|
152 self.__activated(idx) |
|
153 elif act == newTab: |
|
154 model = self.__index.model() |
|
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) |