|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a specialized tab bar for the web browser. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import Qt, QPoint, QTimer, QEvent |
|
13 from PyQt5.QtWidgets import QFrame, QLabel |
|
14 |
|
15 from E5Gui.E5TabWidget import E5WheelTabBar |
|
16 from E5Gui.E5PassivePopup import E5PassivePopup |
|
17 |
|
18 import Preferences |
|
19 |
|
20 |
|
21 class WebBrowserTabBar(E5WheelTabBar): |
|
22 """ |
|
23 Class implementing the tab bar of the web browser. |
|
24 """ |
|
25 def __init__(self, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent reference to the parent widget (WebBrowserTabWidget) |
|
30 """ |
|
31 super(WebBrowserTabBar, self).__init__(parent) |
|
32 |
|
33 self.__tabWidget = parent |
|
34 |
|
35 self.__previewPopup = None |
|
36 |
|
37 self.setMouseTracking(True) |
|
38 |
|
39 def __showTabPreview(self, index): |
|
40 """ |
|
41 Private slot to show the tab preview. |
|
42 |
|
43 @param index index of tab to show a preview for |
|
44 @type int |
|
45 """ |
|
46 indexedBrowser = self.__tabWidget.browserAt(index) |
|
47 currentBrowser = self.__tabWidget.currentBrowser() |
|
48 |
|
49 if indexedBrowser is None or currentBrowser is None: |
|
50 return |
|
51 |
|
52 # no previews during load |
|
53 if indexedBrowser.progress() != 0: |
|
54 return |
|
55 |
|
56 preview = indexedBrowser.getPreview() |
|
57 if not preview.isNull(): |
|
58 w = self.tabSizeHint(index).width() |
|
59 h = int(w * currentBrowser.height() / currentBrowser.width()) |
|
60 |
|
61 self.__previewPopup = E5PassivePopup(self) |
|
62 self.__previewPopup.setFrameShape(QFrame.StyledPanel) |
|
63 self.__previewPopup.setFrameShadow(QFrame.Plain) |
|
64 self.__previewPopup.setFixedSize(w, h) |
|
65 self.__previewPopup.setCustomData("index", index) |
|
66 |
|
67 label = QLabel() |
|
68 label.setPixmap(preview.scaled(w, h)) |
|
69 |
|
70 self.__previewPopup.setView(label) |
|
71 self.__previewPopup.layout().setAlignment(Qt.AlignTop) |
|
72 self.__previewPopup.layout().setContentsMargins(0, 0, 0, 0) |
|
73 |
|
74 tr = self.tabRect(index) |
|
75 pos = QPoint(tr.x(), tr.y() + tr.height()) |
|
76 |
|
77 self.__previewPopup.show(self.mapToGlobal(pos)) |
|
78 |
|
79 def __hidePreview(self): |
|
80 """ |
|
81 Private method to hide the preview. |
|
82 """ |
|
83 if self.__previewPopup is not None: |
|
84 self.__previewPopup.hide() |
|
85 self.__previewPopup = None |
|
86 |
|
87 def mouseMoveEvent(self, evt): |
|
88 """ |
|
89 Protected method to handle mouse move events. |
|
90 |
|
91 @param evt reference to the mouse move event (QMouseEvent) |
|
92 """ |
|
93 if self.count() == 1: |
|
94 return |
|
95 |
|
96 super(WebBrowserTabBar, self).mouseMoveEvent(evt) |
|
97 |
|
98 if Preferences.getWebBrowser("ShowPreview"): |
|
99 # Find the tab under the mouse |
|
100 i = 0 |
|
101 tabIndex = -1 |
|
102 while i < self.count() and tabIndex == -1: |
|
103 if self.tabRect(i).contains(evt.pos()): |
|
104 tabIndex = i |
|
105 i += 1 |
|
106 |
|
107 # If found and not the current tab then show tab preview |
|
108 if tabIndex != -1 and \ |
|
109 tabIndex != self.currentIndex() and \ |
|
110 evt.buttons() == Qt.NoButton: |
|
111 if self.__previewPopup is None or \ |
|
112 (self.__previewPopup is not None and |
|
113 self.__previewPopup.getCustomData("index") != tabIndex): |
|
114 QTimer.singleShot( |
|
115 0, lambda: self.__showTabPreview(tabIndex)) |
|
116 |
|
117 # If current tab or not found then hide previous tab preview |
|
118 if tabIndex == self.currentIndex() or \ |
|
119 tabIndex == -1: |
|
120 self.__hidePreview() |
|
121 |
|
122 def leaveEvent(self, evt): |
|
123 """ |
|
124 Protected method to handle leave events. |
|
125 |
|
126 @param evt reference to the leave event (QEvent) |
|
127 """ |
|
128 if Preferences.getWebBrowser("ShowPreview"): |
|
129 # If leave tabwidget then hide previous tab preview |
|
130 self.__hidePreview() |
|
131 |
|
132 super(WebBrowserTabBar, self).leaveEvent(evt) |
|
133 |
|
134 def mousePressEvent(self, evt): |
|
135 """ |
|
136 Protected method to handle mouse press events. |
|
137 |
|
138 @param evt reference to the mouse press event (QMouseEvent) |
|
139 """ |
|
140 if Preferences.getWebBrowser("ShowPreview"): |
|
141 self.__hidePreview() |
|
142 |
|
143 super(WebBrowserTabBar, self).mousePressEvent(evt) |
|
144 |
|
145 def event(self, evt): |
|
146 """ |
|
147 Public method to handle event. |
|
148 |
|
149 This event handler just handles the tooltip event and passes the |
|
150 handling of all others to the superclass. |
|
151 |
|
152 @param evt reference to the event to be handled (QEvent) |
|
153 @return flag indicating, if the event was handled (boolean) |
|
154 """ |
|
155 if evt.type() == QEvent.ToolTip and \ |
|
156 Preferences.getWebBrowser("ShowPreview"): |
|
157 # suppress tool tips if we are showing previews |
|
158 evt.setAccepted(True) |
|
159 return True |
|
160 |
|
161 return super(WebBrowserTabBar, self).event(evt) |
|
162 |
|
163 def tabRemoved(self, index): |
|
164 """ |
|
165 Public slot to handle the removal of a tab. |
|
166 |
|
167 @param index index of the removed tab (integer) |
|
168 """ |
|
169 if Preferences.getWebBrowser("ShowPreview"): |
|
170 self.__hidePreview() |