Helpviewer/HelpTocWidget.py

changeset 0
de9c2efb9d02
child 13
1af94a91f439
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
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 TOC.
8 """
9
10 from PyQt4.QtCore import *
11 from PyQt4.QtGui import *
12
13 class HelpTocWidget(QWidget):
14 """
15 Class implementing a window for showing the QtHelp TOC.
16
17 @signal linkActivated(const QUrl&) emitted when a TOC entry is activated
18 @signal escapePressed() emitted when the ESC key was pressed
19 """
20 def __init__(self, engine, mainWindow, parent = None):
21 """
22 Constructor
23
24 @param engine reference to the help engine (QHelpEngine)
25 @param mainWindow reference to the main window object (QMainWindow)
26 @param parent reference to the parent widget (QWidget)
27 """
28 QWidget.__init__(self, parent)
29
30 self.__engine = engine
31 self.__mw = mainWindow
32 self.__expandDepth = -2
33
34 self.__tocWidget = self.__engine.contentWidget()
35 self.__tocWidget.viewport().installEventFilter(self)
36 self.__tocWidget.setContextMenuPolicy(Qt.CustomContextMenu)
37
38 self.__layout = QVBoxLayout(self)
39 self.__layout.addWidget(self.__tocWidget)
40
41 self.connect(self.__tocWidget, SIGNAL("customContextMenuRequested(QPoint)"),
42 self.__showContextMenu)
43 self.connect(self.__tocWidget, SIGNAL("linkActivated(const QUrl&)"),
44 self, SIGNAL("linkActivated(const QUrl&)"))
45
46 model = self.__tocWidget.model()
47 self.connect(model, SIGNAL("contentsCreated()"), self.__expandTOC)
48
49 def __expandTOC(self):
50 """
51 Private slot to expand the table of contents.
52 """
53 if self.__expandDepth > -2:
54 self.expandToDepth(self.__expandDepth)
55 self.__expandDepth = -2
56
57 def expandToDepth(self, depth):
58 """
59 Public slot to expand the table of contents to a specific depth.
60
61 @param depth depth to expand to (integer)
62 """
63 self.__expandDepth = depth
64 if depth == -1:
65 self.__tocWidget.expandAll()
66 else:
67 self.__tocWidget.expandToDepth(depth)
68
69 def focusInEvent(self, evt):
70 """
71 Protected method handling focus in events.
72
73 @param evt reference to the focus event object (QFocusEvent)
74 """
75 if evt.reason() != Qt.MouseFocusReason:
76 self.__tocWidget.setFocus()
77
78 def keyPressEvent(self, evt):
79 """
80 Protected method handling key press events.
81
82 @param evt reference to the key press event (QKeyEvent)
83 """
84 if evt.key() == Qt.Key_Escape:
85 self.emit(SIGNAL("escapePressed()"))
86
87 def eventFilter(self, watched, event):
88 """
89 Public method called to filter the event queue.
90
91 @param watched the QObject being watched (QObject)
92 @param event the event that occurred (QEvent)
93 @return flag indicating whether the event was handled (boolean)
94 """
95 if self.__tocWidget and watched == self.__tocWidget.viewport() and \
96 event.type() == QEvent.MouseButtonRelease:
97 if self.__tocWidget.indexAt(event.pos()).isValid() and \
98 event.button() == Qt.LeftButton:
99 self.itemClicked(self.__tocWidget.currentIndex())
100 elif self.__tocWidget.indexAt(event.pos()).isValid() and \
101 event.button() == Qt.MidButton:
102 model = self.__tocWidget.model()
103 itm = model.contentItemAt(self.__tocWidget.currentIndex())
104 self.__mw.newTab(itm.url())
105
106 return QWidget.eventFilter(self, watched, event)
107
108 def itemClicked(self, index):
109 """
110 Public slot handling a click of a TOC entry.
111
112 @param index index of the TOC clicked (QModelIndex)
113 """
114 if not index.isValid():
115 return
116
117 model = self.__tocWidget.model()
118 itm = model.contentItemAt(index)
119 if itm:
120 self.emit(SIGNAL("linkActivated(const QUrl&)"), itm.url())
121
122 def syncToContent(self, url):
123 """
124 Public method to sync the TOC to the displayed page.
125
126 @param url URL of the displayed page (QUrl)
127 @return flag indicating a successful synchronization (boolean)
128 """
129 idx = self.__tocWidget.indexOf(url)
130 if not idx.isValid():
131 return False
132 self.__tocWidget.setCurrentIndex(idx)
133 return True
134
135 def __showContextMenu(self, pos):
136 """
137 Private slot showing the context menu.
138
139 @param pos position to show the menu at (QPoint)
140 """
141 if not self.__tocWidget.indexAt(pos).isValid():
142 return
143
144 menu = QMenu()
145 curTab = menu.addAction(self.trUtf8("Open Link"))
146 newTab = menu.addAction(self.trUtf8("Open Link in New Tab"))
147 menu.move(self.__tocWidget.mapToGlobal(pos))
148
149 model = self.__tocWidget.model()
150 itm = model.contentItemAt(self.__tocWidget.currentIndex())
151
152 act = menu.exec_()
153 if act == curTab:
154 self.emit(SIGNAL("linkActivated(const QUrl&)"), itm.url())
155 elif act == newTab:
156 self.__mw.newTab(itm.url())

eric ide

mercurial