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