eric6/WebBrowser/Bookmarks/BookmarksToolBar.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7196
ab0a91b82b37
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a tool bar showing bookmarks.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSignal, Qt, QUrl, QCoreApplication
13 from PyQt5.QtGui import QCursor
14 from PyQt5.QtWidgets import QMenu
15 from PyQt5.QtWebEngineWidgets import QWebEnginePage
16
17 from E5Gui.E5ModelToolBar import E5ModelToolBar
18
19 from .BookmarksModel import BookmarksModel
20
21
22 class BookmarksToolBar(E5ModelToolBar):
23 """
24 Class implementing a tool bar showing bookmarks.
25
26 @signal openUrl(QUrl, str) emitted to open a URL in the current tab
27 @signal newTab(QUrl, str) emitted to open a URL in a new tab
28 @signal newWindow(QUrl, str) emitted to open a URL in a new window
29 """
30 openUrl = pyqtSignal(QUrl, str)
31 newTab = pyqtSignal(QUrl, str)
32 newWindow = pyqtSignal(QUrl, str)
33
34 def __init__(self, mainWindow, model, parent=None):
35 """
36 Constructor
37
38 @param mainWindow reference to the main window (HelpWindow)
39 @param model reference to the bookmarks model (BookmarksModel)
40 @param parent reference to the parent widget (QWidget)
41 """
42 E5ModelToolBar.__init__(
43 self, QCoreApplication.translate("BookmarksToolBar", "Bookmarks"),
44 parent)
45
46 self.__mw = mainWindow
47 self.__bookmarksModel = model
48
49 self.__mw.bookmarksManager().bookmarksReloaded.connect(self.__rebuild)
50
51 self.setModel(model)
52 self.setRootIndex(model.nodeIndex(
53 self.__mw.bookmarksManager().toolbar()))
54
55 self.setContextMenuPolicy(Qt.CustomContextMenu)
56 self.customContextMenuRequested.connect(self.__contextMenuRequested)
57 self.activated.connect(self.__bookmarkActivated)
58
59 self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
60
61 self._build()
62
63 def __rebuild(self):
64 """
65 Private slot to rebuild the toolbar.
66 """
67 self.__bookmarksModel = \
68 self.__mw.bookmarksManager().bookmarksModel()
69 self.setModel(self.__bookmarksModel)
70 self.setRootIndex(self.__bookmarksModel.nodeIndex(
71 self.__mw.bookmarksManager().toolbar()))
72 self._build()
73
74 def __contextMenuRequested(self, pos):
75 """
76 Private slot to handle the context menu request.
77
78 @param pos position the context menu shall be shown (QPoint)
79 """
80 act = self.actionAt(pos)
81 menu = QMenu()
82
83 if act is not None:
84 v = act.data()
85
86 if act.menu() is None:
87 act2 = menu.addAction(self.tr("Open"))
88 act2.setData(v)
89 act2.triggered.connect(
90 lambda: self.__openBookmark(act2))
91 act2 = menu.addAction(self.tr("Open in New Tab\tCtrl+LMB"))
92 act2.setData(v)
93 act2.triggered.connect(
94 lambda: self.__openBookmarkInNewTab(act2))
95 act2 = menu.addAction(self.tr("Open in New Window"))
96 act2.setData(v)
97 act2.triggered.connect(
98 lambda: self.__openBookmarkInNewWindow(act2))
99 act2 = menu.addAction(self.tr("Open in New Private Window"))
100 act2.setData(v)
101 act2.triggered.connect(
102 lambda: self.__openBookmarkInPrivateWindow(act2))
103 menu.addSeparator()
104
105 act2 = menu.addAction(self.tr("Remove"))
106 act2.setData(v)
107 act2.triggered.connect(lambda: self.__removeBookmark(act2))
108 menu.addSeparator()
109
110 act2 = menu.addAction(self.tr("Properties..."))
111 act2.setData(v)
112 act2.triggered.connect(lambda: self.__edit(act2))
113 menu.addSeparator()
114
115 menu.addAction(self.tr("Add Bookmark..."), self.__newBookmark)
116 menu.addAction(self.tr("Add Folder..."), self.__newFolder)
117
118 menu.exec_(QCursor.pos())
119
120 def __updateVisitCount(self, idx):
121 """
122 Private method to update the visit count of a bookmark.
123
124 @param idx index of the bookmark item (QModelIndex)
125 """
126 from WebBrowser.WebBrowserWindow import WebBrowserWindow
127
128 bookmarkNode = self.model().node(idx)
129 manager = WebBrowserWindow.bookmarksManager()
130 manager.incVisitCount(bookmarkNode)
131
132 def __bookmarkActivated(self, idx):
133 """
134 Private slot handling the activation of a bookmark.
135
136 @param idx index of the activated bookmark (QModelIndex)
137 """
138 assert idx.isValid()
139
140 if self._mouseButton == Qt.XButton1:
141 self.__mw.currentBrowser().triggerPageAction(QWebEnginePage.Back)
142 elif self._mouseButton == Qt.XButton2:
143 self.__mw.currentBrowser().triggerPageAction(
144 QWebEnginePage.Forward)
145 elif self._mouseButton == Qt.LeftButton:
146 if self._keyboardModifiers & Qt.ControlModifier:
147 self.newTab.emit(
148 idx.data(BookmarksModel.UrlRole),
149 idx.data(Qt.DisplayRole))
150 elif self._keyboardModifiers & Qt.ShiftModifier:
151 self.newWindow.emit(
152 idx.data(BookmarksModel.UrlRole),
153 idx.data(Qt.DisplayRole))
154 else:
155 self.openUrl.emit(
156 idx.data(BookmarksModel.UrlRole),
157 idx.data(Qt.DisplayRole))
158 self.__updateVisitCount(idx)
159
160 def __openBookmark(self, act):
161 """
162 Private slot to open a bookmark in the current browser tab.
163
164 @param act reference to the triggering action
165 @type QAction
166 """
167 idx = self.index(act)
168
169 self.openUrl.emit(
170 idx.data(BookmarksModel.UrlRole),
171 idx.data(Qt.DisplayRole))
172 self.__updateVisitCount(idx)
173
174 def __openBookmarkInNewTab(self, act):
175 """
176 Private slot to open a bookmark in a new browser tab.
177
178 @param act reference to the triggering action
179 @type QAction
180 """
181 idx = self.index(act)
182
183 self.newTab.emit(
184 idx.data(BookmarksModel.UrlRole),
185 idx.data(Qt.DisplayRole))
186 self.__updateVisitCount(idx)
187
188 def __openBookmarkInNewWindow(self, act):
189 """
190 Private slot to open a bookmark in a new window.
191
192 @param act reference to the triggering action
193 @type QAction
194 """
195 idx = self.index(act)
196
197 self.newWindow.emit(
198 idx.data(BookmarksModel.UrlRole),
199 idx.data(Qt.DisplayRole))
200 self.__updateVisitCount(idx)
201
202 def __openBookmarkInPrivateWindow(self, act):
203 """
204 Private slot to open a bookmark in a new private window.
205
206 @param act reference to the triggering action
207 @type QAction
208 """
209 idx = self.index(act)
210 url = idx.data(BookmarksModel.UrlRole)
211
212 from WebBrowser.WebBrowserWindow import WebBrowserWindow
213 WebBrowserWindow.mainWindow().newPrivateWindow(url)
214 self.__updateVisitCount(idx)
215
216 def __removeBookmark(self, act):
217 """
218 Private slot to remove a bookmark.
219
220 @param act reference to the triggering action
221 @type QAction
222 """
223 idx = self.index(act)
224
225 self.__bookmarksModel.removeRow(idx.row(), self.rootIndex())
226
227 def __newBookmark(self):
228 """
229 Private slot to add a new bookmark.
230 """
231 from .AddBookmarkDialog import AddBookmarkDialog
232 dlg = AddBookmarkDialog()
233 dlg.setCurrentIndex(self.rootIndex())
234 dlg.exec_()
235
236 def __newFolder(self):
237 """
238 Private slot to add a new bookmarks folder.
239 """
240 from .AddBookmarkDialog import AddBookmarkDialog
241 dlg = AddBookmarkDialog()
242 dlg.setCurrentIndex(self.rootIndex())
243 dlg.setFolder(True)
244 dlg.exec_()
245
246 def _createMenu(self):
247 """
248 Protected method to create the menu for a tool bar action.
249
250 @return menu for a tool bar action (E5ModelMenu)
251 """
252 from .BookmarksMenu import BookmarksMenu
253 menu = BookmarksMenu(self)
254 menu.openUrl.connect(self.openUrl)
255 menu.newTab.connect(self.newTab)
256 menu.newWindow.connect(self.newWindow)
257 return menu
258
259 def __edit(self, act):
260 """
261 Private slot to edit a bookmarks properties.
262
263 @param act reference to the triggering action
264 @type QAction
265 """
266 from .BookmarkPropertiesDialog import BookmarkPropertiesDialog
267 idx = self.index(act)
268 node = self.__bookmarksModel.node(idx)
269 dlg = BookmarkPropertiesDialog(node)
270 dlg.exec_()

eric ide

mercurial