Helpviewer/Bookmarks/BookmarksMenu.py

changeset 0
de9c2efb9d02
child 7
c679fb30c8f3
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 the bookmarks menu.
8 """
9
10 from PyQt4.QtCore import *
11 from PyQt4.QtGui import *
12
13 from E4Gui.E4ModelMenu import E4ModelMenu
14
15 import Helpviewer.HelpWindow
16
17 from BookmarksModel import BookmarksModel
18 from BookmarkNode import BookmarkNode
19
20 class BookmarksMenu(E4ModelMenu):
21 """
22 Class implementing the bookmarks menu base class.
23
24 @signal openUrl(const QUrl&, const QString&) emitted to open a URL in the current
25 tab
26 @signal newUrl(const QUrl&, const QString&) emitted to open a URL in a new tab
27 """
28 def __init__(self, parent = None):
29 """
30 Constructor
31
32 @param parent reference to the parent widget (QWidget)
33 """
34 E4ModelMenu.__init__(self, parent)
35
36 self.connect(self, SIGNAL("activated(const QModelIndex&)"), self.__activated)
37 self.setStatusBarTextRole(BookmarksModel.UrlStringRole)
38 self.setSeparatorRole(BookmarksModel.SeparatorRole)
39
40 self.setContextMenuPolicy(Qt.CustomContextMenu)
41 self.connect(self, SIGNAL("customContextMenuRequested(const QPoint &)"),
42 self.__contextMenuRequested)
43
44 def createBaseMenu(self):
45 """
46 Public method to get the menu that is used to populate sub menu's.
47
48 @return reference to the menu (BookmarksMenu)
49 """
50 menu = BookmarksMenu(self)
51 self.connect(menu, SIGNAL("openUrl(const QUrl&, const QString&)"),
52 self, SIGNAL("openUrl(const QUrl&, const QString&)"))
53 self.connect(menu, SIGNAL("newUrl(const QUrl&, const QString&)"),
54 self, SIGNAL("newUrl(const QUrl&, const QString&)"))
55 return menu
56
57 def __activated(self, idx):
58 """
59 Private slot handling the activated signal.
60
61 @param idx index of the activated item (QModelIndex)
62 """
63 if self._keyboardModifiers & Qt.ControlModifier:
64 self.emit(SIGNAL("newUrl(const QUrl&, const QString&)"),
65 idx.data(BookmarksModel.UrlRole).toUrl(),
66 idx.data(Qt.DisplayRole).toString())
67 else:
68 self.emit(SIGNAL("openUrl(const QUrl&, const QString&)"),
69 idx.data(BookmarksModel.UrlRole).toUrl(),
70 idx.data(Qt.DisplayRole).toString())
71 self.resetFlags()
72
73 def postPopulated(self):
74 """
75 Public method to add any actions after the tree.
76 """
77 if self.isEmpty():
78 return
79
80 parent = self.rootIndex()
81
82 hasBookmarks = False
83
84 for i in range(parent.model().rowCount(parent)):
85 child = parent.model().index(i, 0, parent)
86
87 if child.data(BookmarksModel.TypeRole).toInt()[0] == BookmarkNode.Bookmark:
88 hasBookmarks = True
89 break
90
91 if not hasBookmarks:
92 return
93
94 self.addSeparator()
95 act = self.addAction(self.trUtf8("Open all in Tabs"))
96 self.connect(act, SIGNAL("triggered()"), self.__openAll)
97
98 def __openAll(self):
99 """
100 Private slot to open all the menu's items.
101 """
102 menu = self.sender().parent()
103 if menu is None:
104 return
105
106 parent = menu.rootIndex()
107 if not parent.isValid():
108 return
109
110 for i in range(parent.model().rowCount(parent)):
111 child = parent.model().index(i, 0, parent)
112
113 if child.data(BookmarksModel.TypeRole).toInt()[0] != BookmarkNode.Bookmark:
114 continue
115
116 if i == 0:
117 self.emit(SIGNAL("openUrl(const QUrl&, const QString&)"),
118 child.data(BookmarksModel.UrlRole).toUrl(),
119 child.data(Qt.DisplayRole).toString())
120 else:
121 self.emit(SIGNAL("newUrl(const QUrl&, const QString&)"),
122 child.data(BookmarksModel.UrlRole).toUrl(),
123 child.data(Qt.DisplayRole).toString())
124
125 def __contextMenuRequested(self, pos):
126 """
127 Private slot to handle the context menu request.
128
129 @param pos position the context menu shall be shown (QPoint)
130 """
131 act = self.actionAt(pos)
132
133 if act is not None and act.menu() is None and self.index(act).isValid():
134 menu = QMenu()
135 v = act.data()
136
137 menuAction = menu.addAction(self.trUtf8("&Open"), self.__openBookmark)
138 menuAction.setData(v)
139
140 menuAction = menu.addAction(self.trUtf8("Open in New &Tab\tCtrl+LMB"),
141 self.__openBookmarkInNewTab)
142 menuAction.setData(v)
143
144 menu.addSeparator()
145
146 menuAction = menu.addAction(self.trUtf8("&Remove"), self.__removeBookmark)
147 menuAction.setData(v)
148
149 execAct = menu.exec_(QCursor.pos())
150 if execAct is not None:
151 self.close()
152 parent = self.parent()
153 while parent is not None and isinstance(parent, QMenu):
154 parent.close()
155 parent = parent.parent()
156
157 def __openBookmark(self):
158 """
159 Private slot to open a bookmark in the current browser tab.
160 """
161 idx = self.index(self.sender())
162
163 self.emit(SIGNAL("openUrl(const QUrl&, const QString&)"),
164 idx.data(BookmarksModel.UrlRole).toUrl(),
165 idx.data(Qt.DisplayRole).toString())
166
167 def __openBookmarkInNewTab(self):
168 """
169 Private slot to open a bookmark in a new browser tab.
170 """
171 idx = self.index(self.sender())
172
173 self.emit(SIGNAL("newUrl(const QUrl&, const QString&)"),
174 idx.data(BookmarksModel.UrlRole).toUrl(),
175 idx.data(Qt.DisplayRole).toString())
176
177 def __removeBookmark(self):
178 """
179 Private slot to remove a bookmark.
180 """
181 idx = self.index(self.sender())
182 self.removeEntry(idx)
183
184 ##########################################################################################
185
186 class BookmarksMenuBarMenu(BookmarksMenu):
187 """
188 Class implementing a dynamically populated menu for bookmarks.
189 """
190 def __init__(self, parent = None):
191 """
192 Constructor
193
194 @param parent reference to the parent widget (QWidget)
195 """
196 BookmarksMenu.__init__(self, parent)
197
198 self.__bookmarksManager = None
199 self.__initialActions = []
200
201 def prePopulated(self):
202 """
203 Public method to add any actions before the tree.
204
205 @return flag indicating if any actions were added (boolean)
206 """
207 self.__bookmarksManager = Helpviewer.HelpWindow.HelpWindow.bookmarksManager()
208 self.setModel(self.__bookmarksManager.bookmarksModel())
209 self.setRootIndex(self.__bookmarksManager.bookmarksModel()\
210 .nodeIndex(self.__bookmarksManager.menu()))
211
212 # initial actions
213 for act in self.__initialActions:
214 self.addAction(act)
215 if len(self.__initialActions) != 0:
216 self.addSeparator()
217
218 self.createMenu(
219 self.__bookmarksManager.bookmarksModel()\
220 .nodeIndex(self.__bookmarksManager.toolbar()),
221 1, self)
222 return True
223
224 def setInitialActions(self, actions):
225 """
226 Public method to set the list of actions that should appear first in the menu.
227
228 @param actions list of initial actions (list of QAction)
229 """
230 self.__initialActions = actions[:]
231 for act in self.__initialActions:
232 self.addAction(act)

eric ide

mercurial