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

eric ide

mercurial