eric7/WebBrowser/Bookmarks/BookmarksToolBar.py

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

eric ide

mercurial