|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2016 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 newUrl(QUrl, str) emitted to open a URL in a new tab |
|
29 """ |
|
30 openUrl = pyqtSignal(QUrl, str) |
|
31 newUrl = pyqtSignal(QUrl, str) |
|
32 |
|
33 def __init__(self, parent=None, manager=None): |
|
34 """ |
|
35 Constructor |
|
36 |
|
37 @param parent reference to the parent widget (QWidget |
|
38 @param manager reference to the history manager object (HistoryManager) |
|
39 """ |
|
40 super(HistoryDialog, self).__init__(parent) |
|
41 self.setupUi(self) |
|
42 self.setWindowFlags(Qt.Window) |
|
43 |
|
44 self.__historyManager = manager |
|
45 if self.__historyManager is None: |
|
46 import WebBrowser.WebBrowserWindow |
|
47 self.__historyManager = \ |
|
48 WebBrowser.WebBrowserWindow.WebBrowserWindow.historyManager() |
|
49 |
|
50 self.__model = self.__historyManager.historyTreeModel() |
|
51 self.__proxyModel = E5TreeSortFilterProxyModel(self) |
|
52 self.__proxyModel.setSortRole(HistoryModel.DateTimeRole) |
|
53 self.__proxyModel.setFilterKeyColumn(-1) |
|
54 self.__proxyModel.setSourceModel(self.__model) |
|
55 self.historyTree.setModel(self.__proxyModel) |
|
56 self.historyTree.expandAll() |
|
57 fm = QFontMetrics(self.font()) |
|
58 header = fm.width("m") * 40 |
|
59 self.historyTree.header().resizeSection(0, header) |
|
60 self.historyTree.header().setStretchLastSection(True) |
|
61 self.historyTree.setContextMenuPolicy(Qt.CustomContextMenu) |
|
62 |
|
63 self.historyTree.activated.connect(self.__activated) |
|
64 self.historyTree.customContextMenuRequested.connect( |
|
65 self.__customContextMenuRequested) |
|
66 |
|
67 self.searchEdit.textChanged.connect( |
|
68 self.__proxyModel.setFilterFixedString) |
|
69 self.removeButton.clicked.connect(self.historyTree.removeSelected) |
|
70 self.removeAllButton.clicked.connect(self.__historyManager.clear) |
|
71 |
|
72 self.__proxyModel.modelReset.connect(self.__modelReset) |
|
73 |
|
74 def __modelReset(self): |
|
75 """ |
|
76 Private slot handling a reset of the tree view's model. |
|
77 """ |
|
78 self.historyTree.expandAll() |
|
79 |
|
80 def __customContextMenuRequested(self, pos): |
|
81 """ |
|
82 Private slot to handle the context menu request for the bookmarks tree. |
|
83 |
|
84 @param pos position the context menu was requested (QPoint) |
|
85 """ |
|
86 menu = QMenu() |
|
87 idx = self.historyTree.indexAt(pos) |
|
88 idx = idx.sibling(idx.row(), 0) |
|
89 if idx.isValid() and not self.historyTree.model().hasChildren(idx): |
|
90 menu.addAction( |
|
91 self.tr("&Open"), self.__openHistoryInCurrentTab) |
|
92 menu.addAction( |
|
93 self.tr("Open in New &Tab"), self.__openHistoryInNewTab) |
|
94 menu.addSeparator() |
|
95 menu.addAction(self.tr("&Copy"), self.__copyHistory) |
|
96 menu.addAction(self.tr("&Remove"), self.historyTree.removeSelected) |
|
97 menu.exec_(QCursor.pos()) |
|
98 |
|
99 def __activated(self, idx): |
|
100 """ |
|
101 Private slot to handle the activation of an entry. |
|
102 |
|
103 @param idx reference to the entry index (QModelIndex) |
|
104 """ |
|
105 self.__openHistory( |
|
106 QApplication.keyboardModifiers() & Qt.ControlModifier) |
|
107 |
|
108 def __openHistoryInCurrentTab(self): |
|
109 """ |
|
110 Private slot to open a history entry in the current browser tab. |
|
111 """ |
|
112 self.__openHistory(False) |
|
113 |
|
114 def __openHistoryInNewTab(self): |
|
115 """ |
|
116 Private slot to open a history entry in a new browser tab. |
|
117 """ |
|
118 self.__openHistory(True) |
|
119 |
|
120 def __openHistory(self, newTab): |
|
121 """ |
|
122 Private method to open a history entry. |
|
123 |
|
124 @param newTab flag indicating to open the history entry in a new tab |
|
125 (boolean) |
|
126 """ |
|
127 idx = self.historyTree.currentIndex() |
|
128 if newTab: |
|
129 self.newUrl.emit( |
|
130 idx.data(HistoryModel.UrlRole), |
|
131 idx.data(HistoryModel.TitleRole)) |
|
132 else: |
|
133 self.openUrl.emit( |
|
134 idx.data(HistoryModel.UrlRole), |
|
135 idx.data(HistoryModel.TitleRole)) |
|
136 |
|
137 def __copyHistory(self): |
|
138 """ |
|
139 Private slot to copy a history entry's URL to the clipboard. |
|
140 """ |
|
141 idx = self.historyTree.currentIndex() |
|
142 if not idx.parent().isValid(): |
|
143 return |
|
144 |
|
145 url = idx.data(HistoryModel.UrlStringRole) |
|
146 |
|
147 clipboard = QApplication.clipboard() |
|
148 clipboard.setText(url) |