src/eric7/WebBrowser/Bookmarks/BookmarksMenu.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
18 18
19 19
20 class BookmarksMenu(EricModelMenu): 20 class BookmarksMenu(EricModelMenu):
21 """ 21 """
22 Class implementing the bookmarks menu base class. 22 Class implementing the bookmarks menu base class.
23 23
24 @signal openUrl(QUrl, str) emitted to open a URL with the given title in 24 @signal openUrl(QUrl, str) emitted to open a URL with the given title in
25 the current tab 25 the current tab
26 @signal newTab(QUrl, str) emitted to open a URL with the given title in a 26 @signal newTab(QUrl, str) emitted to open a URL with the given title in a
27 new tab 27 new tab
28 @signal newWindow(QUrl, str) emitted to open a URL with the given title in 28 @signal newWindow(QUrl, str) emitted to open a URL with the given title in
29 a new window 29 a new window
30 """ 30 """
31
31 openUrl = pyqtSignal(QUrl, str) 32 openUrl = pyqtSignal(QUrl, str)
32 newTab = pyqtSignal(QUrl, str) 33 newTab = pyqtSignal(QUrl, str)
33 newWindow = pyqtSignal(QUrl, str) 34 newWindow = pyqtSignal(QUrl, str)
34 35
35 def __init__(self, parent=None): 36 def __init__(self, parent=None):
36 """ 37 """
37 Constructor 38 Constructor
38 39
39 @param parent reference to the parent widget (QWidget) 40 @param parent reference to the parent widget (QWidget)
40 """ 41 """
41 EricModelMenu.__init__(self, parent) 42 EricModelMenu.__init__(self, parent)
42 43
43 self.activated.connect(self.__activated) 44 self.activated.connect(self.__activated)
44 self.setStatusBarTextRole(BookmarksModel.UrlStringRole) 45 self.setStatusBarTextRole(BookmarksModel.UrlStringRole)
45 self.setSeparatorRole(BookmarksModel.SeparatorRole) 46 self.setSeparatorRole(BookmarksModel.SeparatorRole)
46 47
47 self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) 48 self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
48 self.customContextMenuRequested.connect(self.__contextMenuRequested) 49 self.customContextMenuRequested.connect(self.__contextMenuRequested)
49 50
50 def createBaseMenu(self): 51 def createBaseMenu(self):
51 """ 52 """
52 Public method to get the menu that is used to populate sub menu's. 53 Public method to get the menu that is used to populate sub menu's.
53 54
54 @return reference to the menu (BookmarksMenu) 55 @return reference to the menu (BookmarksMenu)
55 """ 56 """
56 menu = BookmarksMenu(self) 57 menu = BookmarksMenu(self)
57 menu.openUrl.connect(self.openUrl) 58 menu.openUrl.connect(self.openUrl)
58 menu.newTab.connect(self.newTab) 59 menu.newTab.connect(self.newTab)
59 menu.newWindow.connect(self.newWindow) 60 menu.newWindow.connect(self.newWindow)
60 return menu 61 return menu
61 62
62 def __updateVisitCount(self, idx): 63 def __updateVisitCount(self, idx):
63 """ 64 """
64 Private method to update the visit count of a bookmark. 65 Private method to update the visit count of a bookmark.
65 66
66 @param idx index of the bookmark item (QModelIndex) 67 @param idx index of the bookmark item (QModelIndex)
67 """ 68 """
68 from WebBrowser.WebBrowserWindow import WebBrowserWindow 69 from WebBrowser.WebBrowserWindow import WebBrowserWindow
69 70
70 bookmarkNode = self.model().node(idx) 71 bookmarkNode = self.model().node(idx)
71 manager = WebBrowserWindow.bookmarksManager() 72 manager = WebBrowserWindow.bookmarksManager()
72 manager.incVisitCount(bookmarkNode) 73 manager.incVisitCount(bookmarkNode)
73 74
74 def __activated(self, idx): 75 def __activated(self, idx):
75 """ 76 """
76 Private slot handling the activated signal. 77 Private slot handling the activated signal.
77 78
78 @param idx index of the activated item (QModelIndex) 79 @param idx index of the activated item (QModelIndex)
79 """ 80 """
80 if self._keyboardModifiers & Qt.KeyboardModifier.ControlModifier: 81 if self._keyboardModifiers & Qt.KeyboardModifier.ControlModifier:
81 self.newTab.emit( 82 self.newTab.emit(
82 idx.data(BookmarksModel.UrlRole), 83 idx.data(BookmarksModel.UrlRole), idx.data(Qt.ItemDataRole.DisplayRole)
83 idx.data(Qt.ItemDataRole.DisplayRole)) 84 )
84 elif self._keyboardModifiers & Qt.KeyboardModifier.ShiftModifier: 85 elif self._keyboardModifiers & Qt.KeyboardModifier.ShiftModifier:
85 self.newWindow.emit( 86 self.newWindow.emit(
86 idx.data(BookmarksModel.UrlRole), 87 idx.data(BookmarksModel.UrlRole), idx.data(Qt.ItemDataRole.DisplayRole)
87 idx.data(Qt.ItemDataRole.DisplayRole)) 88 )
88 else: 89 else:
89 self.openUrl.emit( 90 self.openUrl.emit(
90 idx.data(BookmarksModel.UrlRole), 91 idx.data(BookmarksModel.UrlRole), idx.data(Qt.ItemDataRole.DisplayRole)
91 idx.data(Qt.ItemDataRole.DisplayRole)) 92 )
92 self.__updateVisitCount(idx) 93 self.__updateVisitCount(idx)
93 94
94 def postPopulated(self): 95 def postPopulated(self):
95 """ 96 """
96 Public method to add any actions after the tree. 97 Public method to add any actions after the tree.
97 """ 98 """
98 if self.isEmpty(): 99 if self.isEmpty():
99 return 100 return
100 101
101 parent = self.rootIndex() 102 parent = self.rootIndex()
102 103
103 hasBookmarks = False 104 hasBookmarks = False
104 105
105 for i in range(parent.model().rowCount(parent)): 106 for i in range(parent.model().rowCount(parent)):
106 child = parent.model().index(i, 0, parent) 107 child = parent.model().index(i, 0, parent)
107 108
108 if child.data(BookmarksModel.TypeRole) == BookmarkNode.Bookmark: 109 if child.data(BookmarksModel.TypeRole) == BookmarkNode.Bookmark:
109 hasBookmarks = True 110 hasBookmarks = True
110 break 111 break
111 112
112 if not hasBookmarks: 113 if not hasBookmarks:
113 return 114 return
114 115
115 self.addSeparator() 116 self.addSeparator()
116 act = self.addAction(self.tr("Open all in Tabs")) 117 act = self.addAction(self.tr("Open all in Tabs"))
117 act.triggered.connect(lambda: self.openAll(act)) 118 act.triggered.connect(lambda: self.openAll(act))
118 119
119 def openAll(self, act): 120 def openAll(self, act):
120 """ 121 """
121 Public slot to open all the menu's items. 122 Public slot to open all the menu's items.
122 123
123 @param act reference to the action object 124 @param act reference to the action object
124 @type QAction 125 @type QAction
125 """ 126 """
126 menu = act.parent() 127 menu = act.parent()
127 if menu is None: 128 if menu is None:
128 return 129 return
129 130
130 parent = menu.rootIndex() 131 parent = menu.rootIndex()
131 if not parent.isValid(): 132 if not parent.isValid():
132 return 133 return
133 134
134 for i in range(parent.model().rowCount(parent)): 135 for i in range(parent.model().rowCount(parent)):
135 child = parent.model().index(i, 0, parent) 136 child = parent.model().index(i, 0, parent)
136 137
137 if child.data(BookmarksModel.TypeRole) != BookmarkNode.Bookmark: 138 if child.data(BookmarksModel.TypeRole) != BookmarkNode.Bookmark:
138 continue 139 continue
139 140
140 if i == 0: 141 if i == 0:
141 self.openUrl.emit( 142 self.openUrl.emit(
142 child.data(BookmarksModel.UrlRole), 143 child.data(BookmarksModel.UrlRole),
143 child.data(Qt.ItemDataRole.DisplayRole)) 144 child.data(Qt.ItemDataRole.DisplayRole),
145 )
144 else: 146 else:
145 self.newTab.emit( 147 self.newTab.emit(
146 child.data(BookmarksModel.UrlRole), 148 child.data(BookmarksModel.UrlRole),
147 child.data(Qt.ItemDataRole.DisplayRole)) 149 child.data(Qt.ItemDataRole.DisplayRole),
150 )
148 self.__updateVisitCount(child) 151 self.__updateVisitCount(child)
149 152
150 def __contextMenuRequested(self, pos): 153 def __contextMenuRequested(self, pos):
151 """ 154 """
152 Private slot to handle the context menu request. 155 Private slot to handle the context menu request.
153 156
154 @param pos position the context menu shall be shown (QPoint) 157 @param pos position the context menu shall be shown (QPoint)
155 """ 158 """
156 act = self.actionAt(pos) 159 act = self.actionAt(pos)
157 160
158 if ( 161 if (
159 act is not None and 162 act is not None
160 act not in self.menuActions and 163 and act not in self.menuActions
161 self.index(act).isValid() 164 and self.index(act).isValid()
162 ): 165 ):
163 menu = QMenu() 166 menu = QMenu()
164 v = act.data() 167 v = act.data()
165 168
166 act2 = menu.addAction(self.tr("Open")) 169 act2 = menu.addAction(self.tr("Open"))
167 act2.setData(v) 170 act2.setData(v)
168 act2.triggered.connect( 171 act2.triggered.connect(lambda: self.__openBookmark(act2))
169 lambda: self.__openBookmark(act2))
170 act2 = menu.addAction(self.tr("Open in New Tab\tCtrl+LMB")) 172 act2 = menu.addAction(self.tr("Open in New Tab\tCtrl+LMB"))
171 act2.setData(v) 173 act2.setData(v)
172 act2.triggered.connect( 174 act2.triggered.connect(lambda: self.__openBookmarkInNewTab(act2))
173 lambda: self.__openBookmarkInNewTab(act2))
174 act2 = menu.addAction(self.tr("Open in New Window")) 175 act2 = menu.addAction(self.tr("Open in New Window"))
175 act2.setData(v) 176 act2.setData(v)
176 act2.triggered.connect( 177 act2.triggered.connect(lambda: self.__openBookmarkInNewWindow(act2))
177 lambda: self.__openBookmarkInNewWindow(act2))
178 act2 = menu.addAction(self.tr("Open in New Private Window")) 178 act2 = menu.addAction(self.tr("Open in New Private Window"))
179 act2.setData(v) 179 act2.setData(v)
180 act2.triggered.connect( 180 act2.triggered.connect(lambda: self.__openBookmarkInPrivateWindow(act2))
181 lambda: self.__openBookmarkInPrivateWindow(act2))
182 menu.addSeparator() 181 menu.addSeparator()
183 182
184 act2 = menu.addAction(self.tr("Remove")) 183 act2 = menu.addAction(self.tr("Remove"))
185 act2.setData(v) 184 act2.setData(v)
186 act2.triggered.connect(lambda: self.__removeBookmark(act2)) 185 act2.triggered.connect(lambda: self.__removeBookmark(act2))
187 menu.addSeparator() 186 menu.addSeparator()
188 187
189 act2 = menu.addAction(self.tr("Properties...")) 188 act2 = menu.addAction(self.tr("Properties..."))
190 act2.setData(v) 189 act2.setData(v)
191 act2.triggered.connect(lambda: self.__edit(act2)) 190 act2.triggered.connect(lambda: self.__edit(act2))
192 191
193 execAct = menu.exec(QCursor.pos()) 192 execAct = menu.exec(QCursor.pos())
194 if execAct is not None: 193 if execAct is not None:
195 self.close() 194 self.close()
196 parent = self.parent() 195 parent = self.parent()
197 while parent is not None and isinstance(parent, QMenu): 196 while parent is not None and isinstance(parent, QMenu):
198 parent.close() 197 parent.close()
199 parent = parent.parent() 198 parent = parent.parent()
200 199
201 def __openBookmark(self, act): 200 def __openBookmark(self, act):
202 """ 201 """
203 Private slot to open a bookmark in the current browser tab. 202 Private slot to open a bookmark in the current browser tab.
204 203
205 @param act reference to the triggering action 204 @param act reference to the triggering action
206 @type QAction 205 @type QAction
207 """ 206 """
208 idx = self.index(act) 207 idx = self.index(act)
209 208
210 self.openUrl.emit( 209 self.openUrl.emit(
211 idx.data(BookmarksModel.UrlRole), 210 idx.data(BookmarksModel.UrlRole), idx.data(Qt.ItemDataRole.DisplayRole)
212 idx.data(Qt.ItemDataRole.DisplayRole)) 211 )
213 self.__updateVisitCount(idx) 212 self.__updateVisitCount(idx)
214 213
215 def __openBookmarkInNewTab(self, act): 214 def __openBookmarkInNewTab(self, act):
216 """ 215 """
217 Private slot to open a bookmark in a new browser tab. 216 Private slot to open a bookmark in a new browser tab.
218 217
219 @param act reference to the triggering action 218 @param act reference to the triggering action
220 @type QAction 219 @type QAction
221 """ 220 """
222 idx = self.index(act) 221 idx = self.index(act)
223 222
224 self.newTab.emit( 223 self.newTab.emit(
225 idx.data(BookmarksModel.UrlRole), 224 idx.data(BookmarksModel.UrlRole), idx.data(Qt.ItemDataRole.DisplayRole)
226 idx.data(Qt.ItemDataRole.DisplayRole)) 225 )
227 self.__updateVisitCount(idx) 226 self.__updateVisitCount(idx)
228 227
229 def __openBookmarkInNewWindow(self, act): 228 def __openBookmarkInNewWindow(self, act):
230 """ 229 """
231 Private slot to open a bookmark in a new window. 230 Private slot to open a bookmark in a new window.
232 231
233 @param act reference to the triggering action 232 @param act reference to the triggering action
234 @type QAction 233 @type QAction
235 """ 234 """
236 idx = self.index(act) 235 idx = self.index(act)
237 url = idx.data(BookmarksModel.UrlRole) 236 url = idx.data(BookmarksModel.UrlRole)
238 237
239 from WebBrowser.WebBrowserWindow import WebBrowserWindow 238 from WebBrowser.WebBrowserWindow import WebBrowserWindow
239
240 WebBrowserWindow.mainWindow().newWindow(url) 240 WebBrowserWindow.mainWindow().newWindow(url)
241 self.__updateVisitCount(idx) 241 self.__updateVisitCount(idx)
242 242
243 def __openBookmarkInPrivateWindow(self, act): 243 def __openBookmarkInPrivateWindow(self, act):
244 """ 244 """
245 Private slot to open a bookmark in a new private window. 245 Private slot to open a bookmark in a new private window.
246 246
247 @param act reference to the triggering action 247 @param act reference to the triggering action
248 @type QAction 248 @type QAction
249 """ 249 """
250 idx = self.index(act) 250 idx = self.index(act)
251 url = idx.data(BookmarksModel.UrlRole) 251 url = idx.data(BookmarksModel.UrlRole)
252 252
253 from WebBrowser.WebBrowserWindow import WebBrowserWindow 253 from WebBrowser.WebBrowserWindow import WebBrowserWindow
254
254 WebBrowserWindow.mainWindow().newPrivateWindow(url) 255 WebBrowserWindow.mainWindow().newPrivateWindow(url)
255 self.__updateVisitCount(idx) 256 self.__updateVisitCount(idx)
256 257
257 def __removeBookmark(self, act): 258 def __removeBookmark(self, act):
258 """ 259 """
259 Private slot to remove a bookmark. 260 Private slot to remove a bookmark.
260 261
261 @param act reference to the triggering action 262 @param act reference to the triggering action
262 @type QAction 263 @type QAction
263 """ 264 """
264 idx = self.index(act) 265 idx = self.index(act)
265 self.removeEntry(idx) 266 self.removeEntry(idx)
266 267
267 def __edit(self, act): 268 def __edit(self, act):
268 """ 269 """
269 Private slot to edit a bookmarks properties. 270 Private slot to edit a bookmarks properties.
270 271
271 @param act reference to the triggering action 272 @param act reference to the triggering action
272 @type QAction 273 @type QAction
273 """ 274 """
274 from .BookmarkPropertiesDialog import BookmarkPropertiesDialog 275 from .BookmarkPropertiesDialog import BookmarkPropertiesDialog
275 276
276 idx = self.index(act) 277 idx = self.index(act)
277 node = self.model().node(idx) 278 node = self.model().node(idx)
278 dlg = BookmarkPropertiesDialog(node) 279 dlg = BookmarkPropertiesDialog(node)
279 dlg.exec() 280 dlg.exec()
280 281
282
281 ############################################################################## 283 ##############################################################################
282 284
283 285
284 class BookmarksMenuBarMenu(BookmarksMenu): 286 class BookmarksMenuBarMenu(BookmarksMenu):
285 """ 287 """
286 Class implementing a dynamically populated menu for bookmarks. 288 Class implementing a dynamically populated menu for bookmarks.
287 289
288 @signal openUrl(QUrl, str) emitted to open a URL with the given title in 290 @signal openUrl(QUrl, str) emitted to open a URL with the given title in
289 the current tab 291 the current tab
290 """ 292 """
293
291 openUrl = pyqtSignal(QUrl, str) 294 openUrl = pyqtSignal(QUrl, str)
292 295
293 def __init__(self, parent=None): 296 def __init__(self, parent=None):
294 """ 297 """
295 Constructor 298 Constructor
296 299
297 @param parent reference to the parent widget (QWidget) 300 @param parent reference to the parent widget (QWidget)
298 """ 301 """
299 BookmarksMenu.__init__(self, parent) 302 BookmarksMenu.__init__(self, parent)
300 303
301 self.__initialActions = [] 304 self.__initialActions = []
302 305
303 def prePopulated(self): 306 def prePopulated(self):
304 """ 307 """
305 Public method to add any actions before the tree. 308 Public method to add any actions before the tree.
306 309
307 @return flag indicating if any actions were added (boolean) 310 @return flag indicating if any actions were added (boolean)
308 """ 311 """
309 from WebBrowser.WebBrowserWindow import WebBrowserWindow 312 from WebBrowser.WebBrowserWindow import WebBrowserWindow
310 313
311 manager = WebBrowserWindow.bookmarksManager() 314 manager = WebBrowserWindow.bookmarksManager()
312 self.setModel(manager.bookmarksModel()) 315 self.setModel(manager.bookmarksModel())
313 self.setRootIndex(manager.bookmarksModel().nodeIndex(manager.menu())) 316 self.setRootIndex(manager.bookmarksModel().nodeIndex(manager.menu()))
314 317
315 # initial actions 318 # initial actions
316 for act in self.__initialActions: 319 for act in self.__initialActions:
317 if act == "--SEPARATOR--": 320 if act == "--SEPARATOR--":
318 self.addSeparator() 321 self.addSeparator()
319 else: 322 else:
320 self.addAction(act) 323 self.addAction(act)
321 if len(self.__initialActions) != 0: 324 if len(self.__initialActions) != 0:
322 self.addSeparator() 325 self.addSeparator()
323 326
324 self.createMenu( 327 self.createMenu(manager.bookmarksModel().nodeIndex(manager.toolbar()), 1, self)
325 manager.bookmarksModel().nodeIndex(manager.toolbar()),
326 1, self)
327 return True 328 return True
328 329
329 def postPopulated(self): 330 def postPopulated(self):
330 """ 331 """
331 Public method to add any actions after the tree. 332 Public method to add any actions after the tree.
332 """ 333 """
333 if self.isEmpty(): 334 if self.isEmpty():
334 return 335 return
335 336
336 parent = self.rootIndex() 337 parent = self.rootIndex()
337 338
338 hasBookmarks = False 339 hasBookmarks = False
339 340
340 for i in range(parent.model().rowCount(parent)): 341 for i in range(parent.model().rowCount(parent)):
341 child = parent.model().index(i, 0, parent) 342 child = parent.model().index(i, 0, parent)
342 343
343 if child.data(BookmarksModel.TypeRole) == BookmarkNode.Bookmark: 344 if child.data(BookmarksModel.TypeRole) == BookmarkNode.Bookmark:
344 hasBookmarks = True 345 hasBookmarks = True
345 break 346 break
346 347
347 if not hasBookmarks: 348 if not hasBookmarks:
348 return 349 return
349 350
350 self.addSeparator() 351 self.addSeparator()
351 act_1 = self.addAction(self.tr("Default Home Page")) 352 act_1 = self.addAction(self.tr("Default Home Page"))
352 act_1.setData("eric:home") 353 act_1.setData("eric:home")
353 act_1.triggered.connect( 354 act_1.triggered.connect(lambda: self.__defaultBookmarkTriggered(act_1))
354 lambda: self.__defaultBookmarkTriggered(act_1))
355 act_2 = self.addAction(self.tr("Speed Dial")) 355 act_2 = self.addAction(self.tr("Speed Dial"))
356 act_2.setData("eric:speeddial") 356 act_2.setData("eric:speeddial")
357 act_2.triggered.connect( 357 act_2.triggered.connect(lambda: self.__defaultBookmarkTriggered(act_2))
358 lambda: self.__defaultBookmarkTriggered(act_2))
359 self.addSeparator() 358 self.addSeparator()
360 act_3 = self.addAction(self.tr("Open all in Tabs")) 359 act_3 = self.addAction(self.tr("Open all in Tabs"))
361 act_3.triggered.connect(lambda: self.openAll(act_3)) 360 act_3.triggered.connect(lambda: self.openAll(act_3))
362 361
363 def setInitialActions(self, actions): 362 def setInitialActions(self, actions):
364 """ 363 """
365 Public method to set the list of actions that should appear first in 364 Public method to set the list of actions that should appear first in
366 the menu. 365 the menu.
367 366
368 @param actions list of initial actions (list of QAction) 367 @param actions list of initial actions (list of QAction)
369 """ 368 """
370 self.__initialActions = actions[:] 369 self.__initialActions = actions[:]
371 for act in self.__initialActions: 370 for act in self.__initialActions:
372 self.addAction(act) 371 self.addAction(act)
373 372
374 def __defaultBookmarkTriggered(self, act): 373 def __defaultBookmarkTriggered(self, act):
375 """ 374 """
376 Private slot handling the default bookmark menu entries. 375 Private slot handling the default bookmark menu entries.
377 376
378 @param act reference to the action object 377 @param act reference to the action object
379 @type QAction 378 @type QAction
380 """ 379 """
381 urlStr = act.data() 380 urlStr = act.data()
382 if urlStr.startswith("eric:"): 381 if urlStr.startswith("eric:"):

eric ide

mercurial