|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to manage bookmarks. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSignal, Qt, QUrl, QModelIndex |
|
13 from PyQt5.QtGui import QFontMetrics, QCursor |
|
14 from PyQt5.QtWidgets import QDialog, QMenu, QApplication |
|
15 |
|
16 from E5Gui.E5TreeSortFilterProxyModel import E5TreeSortFilterProxyModel |
|
17 |
|
18 from .Ui_BookmarksDialog import Ui_BookmarksDialog |
|
19 |
|
20 |
|
21 class BookmarksDialog(QDialog, Ui_BookmarksDialog): |
|
22 """ |
|
23 Class implementing a dialog to manage bookmarks. |
|
24 |
|
25 @signal openUrl(QUrl, str) emitted to open a URL in the current tab |
|
26 @signal newTab(QUrl, str) emitted to open a URL in a new tab |
|
27 @signal newBackgroundTab(QUrl, str) emitted to open a URL in a new |
|
28 background tab |
|
29 @signal newWindow(QUrl, str) emitted to open a URL in a new window |
|
30 """ |
|
31 openUrl = pyqtSignal(QUrl, str) |
|
32 newTab = pyqtSignal(QUrl, str) |
|
33 newBackgroundTab = pyqtSignal(QUrl, str) |
|
34 newWindow = pyqtSignal(QUrl, str) |
|
35 |
|
36 def __init__(self, parent=None, manager=None): |
|
37 """ |
|
38 Constructor |
|
39 |
|
40 @param parent reference to the parent widget (QWidget |
|
41 @param manager reference to the bookmarks manager object |
|
42 (BookmarksManager) |
|
43 """ |
|
44 super(BookmarksDialog, self).__init__(parent) |
|
45 self.setupUi(self) |
|
46 self.setWindowFlags(Qt.Window) |
|
47 |
|
48 self.__bookmarksManager = manager |
|
49 if self.__bookmarksManager is None: |
|
50 import WebBrowser.WebBrowserWindow |
|
51 self.__bookmarksManager = WebBrowser.WebBrowserWindow\ |
|
52 .WebBrowserWindow.bookmarksManager() |
|
53 |
|
54 self.__bookmarksModel = self.__bookmarksManager.bookmarksModel() |
|
55 self.__proxyModel = E5TreeSortFilterProxyModel(self) |
|
56 self.__proxyModel.setFilterKeyColumn(-1) |
|
57 self.__proxyModel.setSourceModel(self.__bookmarksModel) |
|
58 |
|
59 self.searchEdit.textChanged.connect( |
|
60 self.__proxyModel.setFilterFixedString) |
|
61 |
|
62 self.bookmarksTree.setModel(self.__proxyModel) |
|
63 self.bookmarksTree.setExpanded(self.__proxyModel.index(0, 0), True) |
|
64 fm = QFontMetrics(self.font()) |
|
65 header = fm.width("m") * 40 |
|
66 self.bookmarksTree.header().resizeSection(0, header) |
|
67 self.bookmarksTree.header().setStretchLastSection(True) |
|
68 self.bookmarksTree.setContextMenuPolicy(Qt.CustomContextMenu) |
|
69 |
|
70 self.bookmarksTree.activated.connect(self.__activated) |
|
71 self.bookmarksTree.customContextMenuRequested.connect( |
|
72 self.__customContextMenuRequested) |
|
73 |
|
74 self.removeButton.clicked.connect( |
|
75 self.bookmarksTree.removeSelected) |
|
76 self.addFolderButton.clicked.connect(self.__newFolder) |
|
77 |
|
78 self.__expandNodes(self.__bookmarksManager.bookmarks()) |
|
79 |
|
80 def closeEvent(self, evt): |
|
81 """ |
|
82 Protected method to handle the closing of the dialog. |
|
83 |
|
84 @param evt reference to the event object (QCloseEvent) (ignored) |
|
85 """ |
|
86 self.__shutdown() |
|
87 |
|
88 def reject(self): |
|
89 """ |
|
90 Public method called when the dialog is rejected. |
|
91 """ |
|
92 self.__shutdown() |
|
93 super(BookmarksDialog, self).reject() |
|
94 |
|
95 def __shutdown(self): |
|
96 """ |
|
97 Private method to perform shutdown actions for the dialog. |
|
98 """ |
|
99 if self.__saveExpandedNodes(self.bookmarksTree.rootIndex()): |
|
100 self.__bookmarksManager.changeExpanded() |
|
101 |
|
102 def __saveExpandedNodes(self, parent): |
|
103 """ |
|
104 Private method to save the child nodes of an expanded node. |
|
105 |
|
106 @param parent index of the parent node (QModelIndex) |
|
107 @return flag indicating a change (boolean) |
|
108 """ |
|
109 changed = False |
|
110 for row in range(self.__proxyModel.rowCount(parent)): |
|
111 child = self.__proxyModel.index(row, 0, parent) |
|
112 sourceIndex = self.__proxyModel.mapToSource(child) |
|
113 childNode = self.__bookmarksModel.node(sourceIndex) |
|
114 wasExpanded = childNode.expanded |
|
115 if self.bookmarksTree.isExpanded(child): |
|
116 childNode.expanded = True |
|
117 changed |= self.__saveExpandedNodes(child) |
|
118 else: |
|
119 childNode.expanded = False |
|
120 changed |= (wasExpanded != childNode.expanded) |
|
121 |
|
122 return changed |
|
123 |
|
124 def __expandNodes(self, node): |
|
125 """ |
|
126 Private method to expand all child nodes of a node. |
|
127 |
|
128 @param node reference to the bookmark node to expand (BookmarkNode) |
|
129 """ |
|
130 for childNode in node.children(): |
|
131 if childNode.expanded: |
|
132 idx = self.__bookmarksModel.nodeIndex(childNode) |
|
133 idx = self.__proxyModel.mapFromSource(idx) |
|
134 self.bookmarksTree.setExpanded(idx, True) |
|
135 self.__expandNodes(childNode) |
|
136 |
|
137 def __customContextMenuRequested(self, pos): |
|
138 """ |
|
139 Private slot to handle the context menu request for the bookmarks tree. |
|
140 |
|
141 @param pos position the context menu was requested (QPoint) |
|
142 """ |
|
143 from .BookmarkNode import BookmarkNode |
|
144 |
|
145 menu = QMenu() |
|
146 idx = self.bookmarksTree.indexAt(pos) |
|
147 idx = idx.sibling(idx.row(), 0) |
|
148 sourceIndex = self.__proxyModel.mapToSource(idx) |
|
149 node = self.__bookmarksModel.node(sourceIndex) |
|
150 if idx.isValid() and node.type() != BookmarkNode.Folder: |
|
151 menu.addAction( |
|
152 self.tr("&Open"), self.__openBookmarkInCurrentTab) |
|
153 menu.addAction( |
|
154 self.tr("Open in New &Tab"), self.__openBookmarkInNewTab) |
|
155 menu.addAction( |
|
156 self.tr("Open in New &Background Tab"), |
|
157 self.__openBookmarkInNewBackgroundTab) |
|
158 menu.addAction( |
|
159 self.tr("Open in New &Window"), self.__openBookmarkInNewWindow) |
|
160 menu.addAction( |
|
161 self.tr("Open in New Pri&vate Window"), |
|
162 self.__openBookmarkInPrivateWindow) |
|
163 menu.addSeparator() |
|
164 act = menu.addAction(self.tr("Edit &Name"), self.__editName) |
|
165 act.setEnabled(idx.flags() & Qt.ItemIsEditable) |
|
166 if idx.isValid() and node.type() != BookmarkNode.Folder: |
|
167 menu.addAction(self.tr("Edit &Address"), self.__editAddress) |
|
168 menu.addSeparator() |
|
169 act = menu.addAction( |
|
170 self.tr("&Delete"), self.bookmarksTree.removeSelected) |
|
171 act.setEnabled(idx.flags() & Qt.ItemIsDragEnabled) |
|
172 menu.addSeparator() |
|
173 act = menu.addAction(self.tr("&Properties..."), self.__edit) |
|
174 act.setEnabled(idx.flags() & Qt.ItemIsEditable) |
|
175 menu.exec_(QCursor.pos()) |
|
176 |
|
177 def __activated(self, idx): |
|
178 """ |
|
179 Private slot to handle the activation of an entry. |
|
180 |
|
181 @param idx reference to the entry index (QModelIndex) |
|
182 """ |
|
183 if QApplication.keyboardModifiers() & Qt.ControlModifier: |
|
184 self.__openBookmarkInNewTab() |
|
185 elif QApplication.keyboardModifiers() & Qt.ShiftModifier: |
|
186 self.__openBookmarkInNewWindow() |
|
187 else: |
|
188 self.__openBookmarkInCurrentTab() |
|
189 |
|
190 def __openBookmarkInCurrentTab(self): |
|
191 """ |
|
192 Private slot to open a bookmark in the current browser tab. |
|
193 """ |
|
194 self.__openBookmark() |
|
195 |
|
196 def __openBookmarkInNewTab(self): |
|
197 """ |
|
198 Private slot to open a bookmark in a new browser tab. |
|
199 """ |
|
200 self.__openBookmark(newTab=True) |
|
201 |
|
202 def __openBookmarkInNewBackgroundTab(self): |
|
203 """ |
|
204 Private slot to open a bookmark in a new browser tab. |
|
205 """ |
|
206 self.__openBookmark(newTab=True, background=True) |
|
207 |
|
208 def __openBookmarkInNewWindow(self): |
|
209 """ |
|
210 Private slot to open a bookmark in a new browser window. |
|
211 """ |
|
212 self.__openBookmark(newWindow=True) |
|
213 |
|
214 def __openBookmarkInPrivateWindow(self): |
|
215 """ |
|
216 Private slot to open a bookmark in a new private browser window. |
|
217 """ |
|
218 self.__openBookmark(newWindow=True, privateWindow=True) |
|
219 |
|
220 def __openBookmark(self, newTab=False, newWindow=False, |
|
221 privateWindow=False, background=False): |
|
222 """ |
|
223 Private method to open a bookmark. |
|
224 |
|
225 @param newTab flag indicating to open the bookmark in a new tab |
|
226 @type bool |
|
227 @param newWindow flag indicating to open the bookmark in a new window |
|
228 @type bool |
|
229 @param privateWindow flag indicating to open the bookmark in a new |
|
230 private window |
|
231 @type bool |
|
232 @param background flag indicating to open the bookmark in a new |
|
233 background tab |
|
234 @type bool |
|
235 """ |
|
236 from .BookmarkNode import BookmarkNode |
|
237 from .BookmarksModel import BookmarksModel |
|
238 |
|
239 idx = self.bookmarksTree.currentIndex() |
|
240 sourceIndex = self.__proxyModel.mapToSource(idx) |
|
241 node = self.__bookmarksModel.node(sourceIndex) |
|
242 if not idx.parent().isValid() or \ |
|
243 node is None or \ |
|
244 node.type() == BookmarkNode.Folder: |
|
245 return |
|
246 |
|
247 if newWindow: |
|
248 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
249 url = idx.sibling(idx.row(), 1).data(BookmarksModel.UrlRole) |
|
250 if privateWindow: |
|
251 WebBrowserWindow.mainWindow().newPrivateWindow(url) |
|
252 else: |
|
253 WebBrowserWindow.mainWindow().newWindow(url) |
|
254 else: |
|
255 if newTab: |
|
256 if background: |
|
257 self.newBackgroundTab.emit( |
|
258 idx.sibling(idx.row(), 1).data(BookmarksModel.UrlRole), |
|
259 idx.sibling(idx.row(), 0).data(Qt.DisplayRole)) |
|
260 else: |
|
261 self.newTab.emit( |
|
262 idx.sibling(idx.row(), 1).data(BookmarksModel.UrlRole), |
|
263 idx.sibling(idx.row(), 0).data(Qt.DisplayRole)) |
|
264 else: |
|
265 self.openUrl.emit( |
|
266 idx.sibling(idx.row(), 1).data(BookmarksModel.UrlRole), |
|
267 idx.sibling(idx.row(), 0).data(Qt.DisplayRole)) |
|
268 self.__bookmarksManager.incVisitCount(node) |
|
269 |
|
270 def __editName(self): |
|
271 """ |
|
272 Private slot to edit the name part of a bookmark. |
|
273 """ |
|
274 idx = self.bookmarksTree.currentIndex() |
|
275 idx = idx.sibling(idx.row(), 0) |
|
276 self.bookmarksTree.edit(idx) |
|
277 |
|
278 def __editAddress(self): |
|
279 """ |
|
280 Private slot to edit the address part of a bookmark. |
|
281 """ |
|
282 idx = self.bookmarksTree.currentIndex() |
|
283 idx = idx.sibling(idx.row(), 1) |
|
284 self.bookmarksTree.edit(idx) |
|
285 |
|
286 def __edit(self): |
|
287 """ |
|
288 Private slot to edit a bookmarks properties. |
|
289 """ |
|
290 from .BookmarkPropertiesDialog import BookmarkPropertiesDialog |
|
291 |
|
292 idx = self.bookmarksTree.currentIndex() |
|
293 sourceIndex = self.__proxyModel.mapToSource(idx) |
|
294 node = self.__bookmarksModel.node(sourceIndex) |
|
295 dlg = BookmarkPropertiesDialog(node) |
|
296 dlg.exec_() |
|
297 |
|
298 def __newFolder(self): |
|
299 """ |
|
300 Private slot to add a new bookmarks folder. |
|
301 """ |
|
302 from .BookmarkNode import BookmarkNode |
|
303 |
|
304 currentIndex = self.bookmarksTree.currentIndex() |
|
305 idx = QModelIndex(currentIndex) |
|
306 sourceIndex = self.__proxyModel.mapToSource(idx) |
|
307 sourceNode = self.__bookmarksModel.node(sourceIndex) |
|
308 row = -1 # append new folder as the last item per default |
|
309 |
|
310 if sourceNode is not None and \ |
|
311 sourceNode.type() != BookmarkNode.Folder: |
|
312 # If the selected item is not a folder, add a new folder to the |
|
313 # parent folder, but directly below the selected item. |
|
314 idx = idx.parent() |
|
315 row = currentIndex.row() + 1 |
|
316 |
|
317 if not idx.isValid(): |
|
318 # Select bookmarks menu as default. |
|
319 idx = self.__proxyModel.index(1, 0) |
|
320 |
|
321 idx = self.__proxyModel.mapToSource(idx) |
|
322 parent = self.__bookmarksModel.node(idx) |
|
323 node = BookmarkNode(BookmarkNode.Folder) |
|
324 node.title = self.tr("New Folder") |
|
325 self.__bookmarksManager.addBookmark(parent, node, row) |