|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to manage history. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSignal, Qt, QUrl |
|
11 from PyQt5.QtGui import QFontMetrics, QCursor |
|
12 from PyQt5.QtWidgets import QDialog, QMenu, QApplication |
|
13 |
|
14 from E5Gui.E5TreeSortFilterProxyModel import E5TreeSortFilterProxyModel |
|
15 |
|
16 from .HistoryModel import HistoryModel |
|
17 |
|
18 from .Ui_HistoryDialog import Ui_HistoryDialog |
|
19 |
|
20 |
|
21 class HistoryDialog(QDialog, Ui_HistoryDialog): |
|
22 """ |
|
23 Class implementing a dialog to manage history. |
|
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 @signal newPrivateWindow(QUrl, str) emitted to open a URL in a new |
|
31 private window |
|
32 """ |
|
33 openUrl = pyqtSignal(QUrl, str) |
|
34 newTab = pyqtSignal(QUrl, str) |
|
35 newBackgroundTab = pyqtSignal(QUrl, str) |
|
36 newWindow = pyqtSignal(QUrl, str) |
|
37 newPrivateWindow = pyqtSignal(QUrl, str) |
|
38 |
|
39 def __init__(self, parent=None, manager=None): |
|
40 """ |
|
41 Constructor |
|
42 |
|
43 @param parent reference to the parent widget (QWidget |
|
44 @param manager reference to the history manager object (HistoryManager) |
|
45 """ |
|
46 super().__init__(parent) |
|
47 self.setupUi(self) |
|
48 self.setWindowFlags(Qt.WindowType.Window) |
|
49 |
|
50 self.__historyManager = manager |
|
51 if self.__historyManager is None: |
|
52 import WebBrowser.WebBrowserWindow |
|
53 self.__historyManager = ( |
|
54 WebBrowser.WebBrowserWindow.WebBrowserWindow.historyManager() |
|
55 ) |
|
56 |
|
57 self.__model = self.__historyManager.historyTreeModel() |
|
58 self.__proxyModel = E5TreeSortFilterProxyModel(self) |
|
59 self.__proxyModel.setSortRole(HistoryModel.DateTimeRole) |
|
60 self.__proxyModel.setFilterKeyColumn(-1) |
|
61 self.__proxyModel.setSourceModel(self.__model) |
|
62 self.historyTree.setModel(self.__proxyModel) |
|
63 self.historyTree.expandAll() |
|
64 fm = QFontMetrics(self.font()) |
|
65 try: |
|
66 header = fm.horizontalAdvance("m") * 40 |
|
67 except AttributeError: |
|
68 header = fm.width("m") * 40 |
|
69 self.historyTree.header().resizeSection(0, header) |
|
70 self.historyTree.header().resizeSection(1, header) |
|
71 self.historyTree.header().setStretchLastSection(True) |
|
72 self.historyTree.setContextMenuPolicy( |
|
73 Qt.ContextMenuPolicy.CustomContextMenu) |
|
74 |
|
75 self.historyTree.activated.connect(self.__activated) |
|
76 self.historyTree.customContextMenuRequested.connect( |
|
77 self.__customContextMenuRequested) |
|
78 |
|
79 self.searchEdit.textChanged.connect( |
|
80 self.__proxyModel.setFilterFixedString) |
|
81 self.removeButton.clicked.connect(self.historyTree.removeSelected) |
|
82 self.removeAllButton.clicked.connect(self.__historyManager.clear) |
|
83 |
|
84 self.__proxyModel.modelReset.connect(self.__modelReset) |
|
85 |
|
86 def __modelReset(self): |
|
87 """ |
|
88 Private slot handling a reset of the tree view's model. |
|
89 """ |
|
90 self.historyTree.expandAll() |
|
91 |
|
92 def __customContextMenuRequested(self, pos): |
|
93 """ |
|
94 Private slot to handle the context menu request for the bookmarks tree. |
|
95 |
|
96 @param pos position the context menu was requested (QPoint) |
|
97 """ |
|
98 menu = QMenu() |
|
99 idx = self.historyTree.indexAt(pos) |
|
100 idx = idx.sibling(idx.row(), 0) |
|
101 if ( |
|
102 idx.isValid() and |
|
103 not self.historyTree.model().hasChildren(idx) and |
|
104 len(self.historyTree.selectionModel().selectedRows()) == 1 |
|
105 ): |
|
106 menu.addAction( |
|
107 self.tr("&Open"), |
|
108 self.__openHistoryInCurrentTab) |
|
109 menu.addAction( |
|
110 self.tr("Open in New &Tab"), |
|
111 self.__openHistoryInNewTab) |
|
112 menu.addAction( |
|
113 self.tr("Open in New &Background Tab"), |
|
114 self.__openHistoryInNewBackgroundTab) |
|
115 menu.addAction( |
|
116 self.tr("Open in New &Window"), |
|
117 self.__openHistoryInNewWindow) |
|
118 menu.addAction( |
|
119 self.tr("Open in New Pri&vate Window"), |
|
120 self.__openHistoryInPrivateWindow) |
|
121 menu.addSeparator() |
|
122 menu.addAction(self.tr("&Copy"), self.__copyHistory) |
|
123 menu.addAction(self.tr("&Remove"), self.historyTree.removeSelected) |
|
124 menu.exec(QCursor.pos()) |
|
125 |
|
126 def __activated(self, idx): |
|
127 """ |
|
128 Private slot to handle the activation of an entry. |
|
129 |
|
130 @param idx reference to the entry index (QModelIndex) |
|
131 """ |
|
132 if ( |
|
133 QApplication.keyboardModifiers() & |
|
134 Qt.KeyboardModifier.ControlModifier |
|
135 ): |
|
136 self.__openHistoryInNewTab() |
|
137 elif ( |
|
138 QApplication.keyboardModifiers() & |
|
139 Qt.KeyboardModifier.ShiftModifier |
|
140 ): |
|
141 self.__openHistoryInNewWindow() |
|
142 else: |
|
143 self.__openHistoryInCurrentTab() |
|
144 |
|
145 def __openHistoryInCurrentTab(self): |
|
146 """ |
|
147 Private slot to open a history entry in the current browser tab. |
|
148 """ |
|
149 self.__openHistory() |
|
150 |
|
151 def __openHistoryInNewTab(self): |
|
152 """ |
|
153 Private slot to open a history entry in a new browser tab. |
|
154 """ |
|
155 self.__openHistory(newTab=True) |
|
156 |
|
157 def __openHistoryInNewBackgroundTab(self): |
|
158 """ |
|
159 Private slot to open a history entry in a new background tab. |
|
160 """ |
|
161 self.__openHistory(newTab=True, background=True) |
|
162 |
|
163 def __openHistoryInNewWindow(self): |
|
164 """ |
|
165 Private slot to open a history entry in a new browser window. |
|
166 """ |
|
167 self.__openHistory(newWindow=True) |
|
168 |
|
169 def __openHistoryInPrivateWindow(self): |
|
170 """ |
|
171 Private slot to open a history entry in a new private browser window. |
|
172 """ |
|
173 self.__openHistory(newWindow=True, privateWindow=True) |
|
174 |
|
175 def __openHistory(self, newTab=False, background=False, |
|
176 newWindow=False, privateWindow=False): |
|
177 """ |
|
178 Private method to open a history entry. |
|
179 |
|
180 @param newTab flag indicating to open the feed message in a new tab |
|
181 @type bool |
|
182 @param background flag indicating to open the bookmark in a new |
|
183 background tab |
|
184 @type bool |
|
185 @param newWindow flag indicating to open the bookmark in a new window |
|
186 @type bool |
|
187 @param privateWindow flag indicating to open the bookmark in a new |
|
188 private window |
|
189 @type bool |
|
190 (boolean) |
|
191 """ |
|
192 idx = self.historyTree.currentIndex() |
|
193 if newTab: |
|
194 if background: |
|
195 self.newBackgroundTab.emit( |
|
196 idx.data(HistoryModel.UrlRole), |
|
197 idx.data(HistoryModel.TitleRole)) |
|
198 else: |
|
199 self.newTab.emit( |
|
200 idx.data(HistoryModel.UrlRole), |
|
201 idx.data(HistoryModel.TitleRole)) |
|
202 elif newWindow: |
|
203 if privateWindow: |
|
204 self.newPrivateWindow.emit( |
|
205 idx.data(HistoryModel.UrlRole), |
|
206 idx.data(HistoryModel.TitleRole)) |
|
207 else: |
|
208 self.newWindow.emit( |
|
209 idx.data(HistoryModel.UrlRole), |
|
210 idx.data(HistoryModel.TitleRole)) |
|
211 else: |
|
212 self.openUrl.emit( |
|
213 idx.data(HistoryModel.UrlRole), |
|
214 idx.data(HistoryModel.TitleRole)) |
|
215 |
|
216 def __copyHistory(self): |
|
217 """ |
|
218 Private slot to copy a history entry's URL to the clipboard. |
|
219 """ |
|
220 idx = self.historyTree.currentIndex() |
|
221 if not idx.parent().isValid(): |
|
222 return |
|
223 |
|
224 url = idx.data(HistoryModel.UrlStringRole) |
|
225 |
|
226 clipboard = QApplication.clipboard() |
|
227 clipboard.setText(url) |