|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2016 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.QtWebKitWidgets import QWebPage |
|
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 newUrl(QUrl, str) emitted to open a URL in a new tab |
|
28 """ |
|
29 openUrl = pyqtSignal(QUrl, str) |
|
30 newUrl = pyqtSignal(QUrl, str) |
|
31 |
|
32 def __init__(self, mainWindow, model, parent=None): |
|
33 """ |
|
34 Constructor |
|
35 |
|
36 @param mainWindow reference to the main window (HelpWindow) |
|
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.CustomContextMenu) |
|
54 self.customContextMenuRequested.connect(self.__contextMenuRequested) |
|
55 self.activated.connect(self.__bookmarkActivated) |
|
56 |
|
57 self.setHidden(True) |
|
58 self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) |
|
59 |
|
60 self._build() |
|
61 |
|
62 def __rebuild(self): |
|
63 """ |
|
64 Private slot to rebuild the toolbar. |
|
65 """ |
|
66 self.__bookmarksModel = \ |
|
67 self.__mw.bookmarksManager().bookmarksModel() |
|
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 menuAction = menu.addAction( |
|
87 self.tr("&Open"), self.__openBookmark) |
|
88 menuAction.setData(v) |
|
89 menuAction = menu.addAction( |
|
90 self.tr("Open in New &Tab\tCtrl+LMB"), |
|
91 self.__openBookmarkInNewTab) |
|
92 menuAction.setData(v) |
|
93 menu.addSeparator() |
|
94 |
|
95 menuAction = menu.addAction( |
|
96 self.tr("&Remove"), self.__removeBookmark) |
|
97 menuAction.setData(v) |
|
98 menu.addSeparator() |
|
99 |
|
100 menuAction = menu.addAction( |
|
101 self.tr("&Properties..."), self.__edit) |
|
102 menuAction.setData(v) |
|
103 menu.addSeparator() |
|
104 |
|
105 menu.addAction(self.tr("Add &Bookmark..."), self.__newBookmark) |
|
106 menu.addAction(self.tr("Add &Folder..."), self.__newFolder) |
|
107 |
|
108 menu.exec_(QCursor.pos()) |
|
109 |
|
110 def __bookmarkActivated(self, idx): |
|
111 """ |
|
112 Private slot handling the activation of a bookmark. |
|
113 |
|
114 @param idx index of the activated bookmark (QModelIndex) |
|
115 """ |
|
116 assert idx.isValid() |
|
117 |
|
118 if self._mouseButton == Qt.XButton1: |
|
119 self.__mw.currentBrowser().pageAction(QWebPage.Back).trigger() |
|
120 elif self._mouseButton == Qt.XButton2: |
|
121 self.__mw.currentBrowser().pageAction(QWebPage.Forward).trigger() |
|
122 elif self._mouseButton == Qt.LeftButton: |
|
123 if self._keyboardModifiers & Qt.ControlModifier: |
|
124 self.newUrl.emit( |
|
125 idx.data(BookmarksModel.UrlRole), |
|
126 idx.data(Qt.DisplayRole)) |
|
127 else: |
|
128 self.openUrl.emit( |
|
129 idx.data(BookmarksModel.UrlRole), |
|
130 idx.data(Qt.DisplayRole)) |
|
131 |
|
132 def __openToolBarBookmark(self): |
|
133 """ |
|
134 Private slot to open a bookmark in the current browser tab. |
|
135 """ |
|
136 idx = self.index(self.sender()) |
|
137 |
|
138 if self._keyboardModifiers & Qt.ControlModifier: |
|
139 self.newUrl.emit( |
|
140 idx.data(BookmarksModel.UrlRole), |
|
141 idx.data(Qt.DisplayRole)) |
|
142 else: |
|
143 self.openUrl.emit( |
|
144 idx.data(BookmarksModel.UrlRole), |
|
145 idx.data(Qt.DisplayRole)) |
|
146 self.resetFlags() |
|
147 |
|
148 def __openBookmark(self): |
|
149 """ |
|
150 Private slot to open a bookmark in the current browser tab. |
|
151 """ |
|
152 idx = self.index(self.sender()) |
|
153 |
|
154 self.openUrl.emit( |
|
155 idx.data(BookmarksModel.UrlRole), |
|
156 idx.data(Qt.DisplayRole)) |
|
157 |
|
158 def __openBookmarkInNewTab(self): |
|
159 """ |
|
160 Private slot to open a bookmark in a new browser tab. |
|
161 """ |
|
162 idx = self.index(self.sender()) |
|
163 |
|
164 self.newUrl.emit( |
|
165 idx.data(BookmarksModel.UrlRole), |
|
166 idx.data(Qt.DisplayRole)) |
|
167 |
|
168 def __removeBookmark(self): |
|
169 """ |
|
170 Private slot to remove a bookmark. |
|
171 """ |
|
172 idx = self.index(self.sender()) |
|
173 |
|
174 self.__bookmarksModel.removeRow(idx.row(), self.rootIndex()) |
|
175 |
|
176 def __newBookmark(self): |
|
177 """ |
|
178 Private slot to add a new bookmark. |
|
179 """ |
|
180 from .AddBookmarkDialog import AddBookmarkDialog |
|
181 dlg = AddBookmarkDialog() |
|
182 dlg.setCurrentIndex(self.rootIndex()) |
|
183 dlg.exec_() |
|
184 |
|
185 def __newFolder(self): |
|
186 """ |
|
187 Private slot to add a new bookmarks folder. |
|
188 """ |
|
189 from .AddBookmarkDialog import AddBookmarkDialog |
|
190 dlg = AddBookmarkDialog() |
|
191 dlg.setCurrentIndex(self.rootIndex()) |
|
192 dlg.setFolder(True) |
|
193 dlg.exec_() |
|
194 |
|
195 def _createMenu(self): |
|
196 """ |
|
197 Protected method to create the menu for a tool bar action. |
|
198 |
|
199 @return menu for a tool bar action (E5ModelMenu) |
|
200 """ |
|
201 from .BookmarksMenu import BookmarksMenu |
|
202 menu = BookmarksMenu(self) |
|
203 menu.openUrl.connect(self.openUrl) |
|
204 menu.newUrl.connect(self.newUrl) |
|
205 return menu |
|
206 |
|
207 def __edit(self): |
|
208 """ |
|
209 Private slot to edit a bookmarks properties. |
|
210 """ |
|
211 from .BookmarkPropertiesDialog import BookmarkPropertiesDialog |
|
212 idx = self.index(self.sender()) |
|
213 node = self.__bookmarksModel.node(idx) |
|
214 dlg = BookmarkPropertiesDialog(node) |
|
215 dlg.exec_() |