|
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.QtWebKitWidgets import QWebPage |
|
16 |
|
17 from E5Gui.E5ModelToolBar import E5ModelToolBar |
|
18 |
|
19 import Helpviewer.HelpWindow |
|
20 |
|
21 from .BookmarksModel import BookmarksModel |
|
22 |
|
23 |
|
24 class BookmarksToolBar(E5ModelToolBar): |
|
25 """ |
|
26 Class implementing a tool bar showing bookmarks. |
|
27 |
|
28 @signal openUrl(QUrl, str) emitted to open a URL in the current tab |
|
29 @signal newUrl(QUrl, str) emitted to open a URL in a new tab |
|
30 """ |
|
31 openUrl = pyqtSignal(QUrl, str) |
|
32 newUrl = 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 Helpviewer.HelpWindow.HelpWindow.bookmarksManager()\ |
|
50 .bookmarksReloaded.connect(self.__rebuild) |
|
51 |
|
52 self.setModel(model) |
|
53 self.setRootIndex(model.nodeIndex( |
|
54 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().toolbar())) |
|
55 |
|
56 self.setContextMenuPolicy(Qt.CustomContextMenu) |
|
57 self.customContextMenuRequested.connect(self.__contextMenuRequested) |
|
58 self.activated.connect(self.__bookmarkActivated) |
|
59 |
|
60 self.setHidden(True) |
|
61 self.setToolButtonStyle(Qt.ToolButtonTextBesideIcon) |
|
62 |
|
63 self._build() |
|
64 |
|
65 def __rebuild(self): |
|
66 """ |
|
67 Private slot to rebuild the toolbar. |
|
68 """ |
|
69 self.__bookmarksModel = \ |
|
70 Helpviewer.HelpWindow.HelpWindow.bookmarksManager()\ |
|
71 .bookmarksModel() |
|
72 self.setModel(self.__bookmarksModel) |
|
73 self.setRootIndex(self.__bookmarksModel.nodeIndex( |
|
74 Helpviewer.HelpWindow.HelpWindow.bookmarksManager().toolbar())) |
|
75 self._build() |
|
76 |
|
77 def __contextMenuRequested(self, pos): |
|
78 """ |
|
79 Private slot to handle the context menu request. |
|
80 |
|
81 @param pos position the context menu shall be shown (QPoint) |
|
82 """ |
|
83 act = self.actionAt(pos) |
|
84 menu = QMenu() |
|
85 |
|
86 if act is not None: |
|
87 v = act.data() |
|
88 |
|
89 if act.menu() is None: |
|
90 act2 = menu.addAction(self.tr("Open")) |
|
91 act2.setData(v) |
|
92 act2.triggered.connect( |
|
93 lambda: self.__openBookmark(act2)) |
|
94 act2 = menu.addAction(self.tr("Open in New Tab\tCtrl+LMB")) |
|
95 act2.setData(v) |
|
96 act2.triggered.connect( |
|
97 lambda: self.__openBookmarkInNewTab(act2)) |
|
98 menu.addSeparator() |
|
99 |
|
100 act2 = menu.addAction(self.tr("Remove")) |
|
101 act2.setData(v) |
|
102 act2.triggered.connect(lambda: self.__removeBookmark(act2)) |
|
103 menu.addSeparator() |
|
104 |
|
105 act2 = menu.addAction(self.tr("Properties...")) |
|
106 act2.setData(v) |
|
107 act2.triggered.connect(lambda: self.__edit(act2)) |
|
108 menu.addSeparator() |
|
109 |
|
110 menu.addAction(self.tr("Add &Bookmark..."), self.__newBookmark) |
|
111 menu.addAction(self.tr("Add &Folder..."), self.__newFolder) |
|
112 |
|
113 menu.exec_(QCursor.pos()) |
|
114 |
|
115 def __bookmarkActivated(self, idx): |
|
116 """ |
|
117 Private slot handling the activation of a bookmark. |
|
118 |
|
119 @param idx index of the activated bookmark (QModelIndex) |
|
120 """ |
|
121 assert idx.isValid() |
|
122 |
|
123 if self._mouseButton == Qt.XButton1: |
|
124 self.__mw.currentBrowser().pageAction(QWebPage.Back).trigger() |
|
125 elif self._mouseButton == Qt.XButton2: |
|
126 self.__mw.currentBrowser().pageAction(QWebPage.Forward).trigger() |
|
127 elif self._mouseButton == Qt.LeftButton: |
|
128 if self._keyboardModifiers & Qt.ControlModifier: |
|
129 self.newUrl.emit( |
|
130 idx.data(BookmarksModel.UrlRole), |
|
131 idx.data(Qt.DisplayRole)) |
|
132 else: |
|
133 self.openUrl.emit( |
|
134 idx.data(BookmarksModel.UrlRole), |
|
135 idx.data(Qt.DisplayRole)) |
|
136 |
|
137 def __openBookmark(self, act): |
|
138 """ |
|
139 Private slot to open a bookmark in the current browser tab. |
|
140 |
|
141 @param act reference to the triggering action |
|
142 @type QAction |
|
143 """ |
|
144 idx = self.index(act) |
|
145 |
|
146 self.openUrl.emit( |
|
147 idx.data(BookmarksModel.UrlRole), |
|
148 idx.data(Qt.DisplayRole)) |
|
149 |
|
150 def __openBookmarkInNewTab(self, act): |
|
151 """ |
|
152 Private slot to open a bookmark in a new browser tab. |
|
153 |
|
154 @param act reference to the triggering action |
|
155 @type QAction |
|
156 """ |
|
157 idx = self.index(act) |
|
158 |
|
159 self.newUrl.emit( |
|
160 idx.data(BookmarksModel.UrlRole), |
|
161 idx.data(Qt.DisplayRole)) |
|
162 |
|
163 def __removeBookmark(self, act): |
|
164 """ |
|
165 Private slot to remove a bookmark. |
|
166 |
|
167 @param act reference to the triggering action |
|
168 @type QAction |
|
169 """ |
|
170 idx = self.index(act) |
|
171 |
|
172 self.__bookmarksModel.removeRow(idx.row(), self.rootIndex()) |
|
173 |
|
174 def __newBookmark(self): |
|
175 """ |
|
176 Private slot to add a new bookmark. |
|
177 """ |
|
178 from .AddBookmarkDialog import AddBookmarkDialog |
|
179 dlg = AddBookmarkDialog() |
|
180 dlg.setCurrentIndex(self.rootIndex()) |
|
181 dlg.exec_() |
|
182 |
|
183 def __newFolder(self): |
|
184 """ |
|
185 Private slot to add a new bookmarks folder. |
|
186 """ |
|
187 from .AddBookmarkDialog import AddBookmarkDialog |
|
188 dlg = AddBookmarkDialog() |
|
189 dlg.setCurrentIndex(self.rootIndex()) |
|
190 dlg.setFolder(True) |
|
191 dlg.exec_() |
|
192 |
|
193 def _createMenu(self): |
|
194 """ |
|
195 Protected method to create the menu for a tool bar action. |
|
196 |
|
197 @return menu for a tool bar action (E5ModelMenu) |
|
198 """ |
|
199 from .BookmarksMenu import BookmarksMenu |
|
200 menu = BookmarksMenu(self) |
|
201 menu.openUrl.connect(self.openUrl) |
|
202 menu.newUrl.connect(self.newUrl) |
|
203 return menu |
|
204 |
|
205 def __edit(self, act): |
|
206 """ |
|
207 Private slot to edit a bookmarks properties. |
|
208 |
|
209 @param act reference to the triggering action |
|
210 @type QAction |
|
211 """ |
|
212 from .BookmarkPropertiesDialog import BookmarkPropertiesDialog |
|
213 idx = self.index(act) |
|
214 node = self.__bookmarksModel.node(idx) |
|
215 dlg = BookmarkPropertiesDialog(node) |
|
216 dlg.exec_() |