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