WebBrowser/QtHelp/HelpTocWidget.py

changeset 5038
df7103c3f2a6
parent 4875
4ee26909ac0d
child 5252
321c2003745d
equal deleted inserted replaced
5037:b2b37d7c0791 5038:df7103c3f2a6
15 15
16 class HelpTocWidget(QWidget): 16 class HelpTocWidget(QWidget):
17 """ 17 """
18 Class implementing a window for showing the QtHelp TOC. 18 Class implementing a window for showing the QtHelp TOC.
19 19
20 @signal linkActivated(QUrl) emitted when a TOC entry is activated
21 @signal escapePressed() emitted when the ESC key was pressed 20 @signal escapePressed() emitted when the ESC key was pressed
21 @signal openUrl(QUrl, str) emitted to open an entry in the current tab
22 @signal newTab(QUrl, str) emitted to open an entry in a new tab
23 @signal newBackgroundTab(QUrl, str) emitted to open an entry in a
24 new background tab
25 @signal newWindow(QUrl, str) emitted to open an entry in a new window
22 """ 26 """
23 linkActivated = pyqtSignal(QUrl)
24 escapePressed = pyqtSignal() 27 escapePressed = pyqtSignal()
28 openUrl = pyqtSignal(QUrl)
29 newTab = pyqtSignal(QUrl)
30 newBackgroundTab = pyqtSignal(QUrl)
31 newWindow = pyqtSignal(QUrl)
25 32
26 def __init__(self, engine, mainWindow, parent=None): 33 def __init__(self, engine, parent=None):
27 """ 34 """
28 Constructor 35 Constructor
29 36
30 @param engine reference to the help engine (QHelpEngine) 37 @param engine reference to the help engine (QHelpEngine)
31 @param mainWindow reference to the main window object (QMainWindow)
32 @param parent reference to the parent widget (QWidget) 38 @param parent reference to the parent widget (QWidget)
33 """ 39 """
34 super(HelpTocWidget, self).__init__(parent) 40 super(HelpTocWidget, self).__init__(parent)
35 41
36 self.__engine = engine 42 self.__engine = engine
37 self.__mw = mainWindow
38 self.__expandDepth = -2 43 self.__expandDepth = -2
39 44
40 self.__tocWidget = self.__engine.contentWidget() 45 self.__tocWidget = self.__engine.contentWidget()
41 self.__tocWidget.viewport().installEventFilter(self) 46 self.__tocWidget.viewport().installEventFilter(self)
42 self.__tocWidget.setContextMenuPolicy(Qt.CustomContextMenu) 47 self.__tocWidget.setContextMenuPolicy(Qt.CustomContextMenu)
45 self.__layout = QVBoxLayout(self) 50 self.__layout = QVBoxLayout(self)
46 self.__layout.addWidget(self.__tocWidget) 51 self.__layout.addWidget(self.__tocWidget)
47 52
48 self.__tocWidget.customContextMenuRequested.connect( 53 self.__tocWidget.customContextMenuRequested.connect(
49 self.__showContextMenu) 54 self.__showContextMenu)
50 self.__tocWidget.linkActivated.connect(self.linkActivated) 55 self.__tocWidget.linkActivated.connect(self.openUrl)
51 56
52 model = self.__tocWidget.model() 57 model = self.__tocWidget.model()
53 model.contentsCreated.connect(self.__contentsCreated) 58 model.contentsCreated.connect(self.__contentsCreated)
54 59
55 def __contentsCreated(self): 60 def __contentsCreated(self):
105 @param event the event that occurred (QEvent) 110 @param event the event that occurred (QEvent)
106 @return flag indicating whether the event was handled (boolean) 111 @return flag indicating whether the event was handled (boolean)
107 """ 112 """
108 if self.__tocWidget and watched == self.__tocWidget.viewport() and \ 113 if self.__tocWidget and watched == self.__tocWidget.viewport() and \
109 event.type() == QEvent.MouseButtonRelease: 114 event.type() == QEvent.MouseButtonRelease:
110 if self.__tocWidget.indexAt(event.pos()).isValid() and \ 115 if self.__tocWidget.indexAt(event.pos()).isValid():
111 event.button() == Qt.LeftButton:
112 self.itemClicked(self.__tocWidget.currentIndex())
113 elif self.__tocWidget.indexAt(event.pos()).isValid() and \
114 event.button() == Qt.MidButton:
115 model = self.__tocWidget.model() 116 model = self.__tocWidget.model()
116 itm = model.contentItemAt(self.__tocWidget.currentIndex()) 117 itm = model.contentItemAt(self.__tocWidget.currentIndex())
117 self.__mw.newTab(itm.url()) 118 if itm:
119 link = itm.url()
120 if not link.isEmpty() and link.isValid():
121 if event.button() == Qt.LeftButton:
122 if event.modifiers() & Qt.ControlModifier:
123 self.newTab.emit(link)
124 elif event.modifiers() & Qt.ShiftModifier:
125 self.newWindow.emit(link)
126 else:
127 self.openUrl.emit(link)
128 elif event.button() == Qt.MidButton:
129 self.newTab.emit(link)
118 130
119 return QWidget.eventFilter(self, watched, event) 131 return QWidget.eventFilter(self, watched, event)
120
121 def itemClicked(self, index):
122 """
123 Public slot handling a click of a TOC entry.
124
125 @param index index of the TOC clicked (QModelIndex)
126 """
127 if not index.isValid():
128 return
129
130 model = self.__tocWidget.model()
131 itm = model.contentItemAt(index)
132 if itm:
133 self.linkActivated.emit(itm.url())
134 132
135 def syncToContent(self, url): 133 def syncToContent(self, url):
136 """ 134 """
137 Public method to sync the TOC to the displayed page. 135 Public method to sync the TOC to the displayed page.
138 136
152 @param pos position to show the menu at (QPoint) 150 @param pos position to show the menu at (QPoint)
153 """ 151 """
154 if not self.__tocWidget.indexAt(pos).isValid(): 152 if not self.__tocWidget.indexAt(pos).isValid():
155 return 153 return
156 154
155 model = self.__tocWidget.model()
156 itm = model.contentItemAt(self.__tocWidget.currentIndex())
157 link = itm.url()
158 if link.isEmpty() or not link.isValid():
159 return
160
157 menu = QMenu() 161 menu = QMenu()
158 curTab = menu.addAction(self.tr("Open Link")) 162 curTab = menu.addAction(self.tr("Open Link"))
159 newTab = menu.addAction(self.tr("Open Link in New Tab")) 163 newTab = menu.addAction(self.tr("Open Link in New Tab"))
164 newBackgroundTab = menu.addAction(
165 self.tr("Open Link in Background Tab"))
166 newWindow = menu.addAction(self.tr("Open Link in New Window"))
160 menu.move(self.__tocWidget.mapToGlobal(pos)) 167 menu.move(self.__tocWidget.mapToGlobal(pos))
161
162 model = self.__tocWidget.model()
163 itm = model.contentItemAt(self.__tocWidget.currentIndex())
164 168
165 act = menu.exec_() 169 act = menu.exec_()
166 if act == curTab: 170 if act == curTab:
167 self.linkActivated.emit(itm.url()) 171 self.openUrl.emit(link)
168 elif act == newTab: 172 elif act == newTab:
169 self.__mw.newTab(itm.url()) 173 self.newTab.emit(link)
174 elif act == newBackgroundTab:
175 self.newBackgroundTab.emit(link)
176 elif act == newWindow:
177 self.newWindow.emit(link)

eric ide

mercurial