eric7/WebBrowser/WebBrowserTabBar.py

branch
eric7
changeset 8312
800c432b34c8
parent 8269
87f521f359d5
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a specialized tab bar for the web browser.
8 """
9
10 from PyQt5.QtCore import Qt, QPoint, QTimer, QEvent
11 from PyQt5.QtWidgets import QLabel
12
13 from E5Gui.E5TabWidget import E5WheelTabBar
14 from E5Gui.E5PassivePopup import E5PassivePopup, E5PassivePopupStyle
15
16 import Preferences
17
18
19 class WebBrowserTabBar(E5WheelTabBar):
20 """
21 Class implementing the tab bar of the web browser.
22 """
23 def __init__(self, parent=None):
24 """
25 Constructor
26
27 @param parent reference to the parent widget (WebBrowserTabWidget)
28 """
29 super().__init__(parent)
30
31 self.__tabWidget = parent
32
33 self.__previewPopup = None
34
35 self.setMouseTracking(True)
36
37 def __showTabPreview(self, index):
38 """
39 Private slot to show the tab preview.
40
41 @param index index of tab to show a preview for
42 @type int
43 """
44 indexedBrowser = self.__tabWidget.browserAt(index)
45 currentBrowser = self.__tabWidget.currentBrowser()
46
47 if indexedBrowser is None or currentBrowser is None:
48 return
49
50 # no previews during load
51 if indexedBrowser.progress() != 0:
52 return
53
54 preview = indexedBrowser.getPreview()
55 if not preview.isNull():
56 w = self.tabSizeHint(index).width()
57 h = int(w * currentBrowser.height() / currentBrowser.width())
58
59 self.__previewPopup = E5PassivePopup(
60 style=E5PassivePopupStyle.STYLED, parent=self)
61 self.__previewPopup.setFixedSize(w, h)
62 self.__previewPopup.setCustomData("index", index)
63
64 label = QLabel()
65 label.setPixmap(preview.scaled(w, h))
66
67 self.__previewPopup.setView(label)
68 self.__previewPopup.layout().setAlignment(
69 Qt.AlignmentFlag.AlignTop)
70 self.__previewPopup.layout().setContentsMargins(0, 0, 0, 0)
71
72 tr = self.tabRect(index)
73 pos = QPoint(tr.x(), tr.y() + tr.height())
74
75 self.__previewPopup.show(self.mapToGlobal(pos))
76
77 def __hidePreview(self):
78 """
79 Private method to hide the preview.
80 """
81 if self.__previewPopup is not None:
82 self.__previewPopup.hide()
83 self.__previewPopup.deleteLater()
84 self.__previewPopup = None
85
86 def mouseMoveEvent(self, evt):
87 """
88 Protected method to handle mouse move events.
89
90 @param evt reference to the mouse move event (QMouseEvent)
91 """
92 if self.count() == 1:
93 return
94
95 super().mouseMoveEvent(evt)
96
97 if Preferences.getWebBrowser("ShowPreview"):
98 # Find the tab under the mouse
99 i = 0
100 tabIndex = -1
101 while i < self.count() and tabIndex == -1:
102 if self.tabRect(i).contains(evt.pos()):
103 tabIndex = i
104 i += 1
105
106 # If found and not the current tab then show tab preview
107 if (
108 tabIndex != -1 and
109 tabIndex != self.currentIndex() and
110 evt.buttons() == Qt.MouseButton.NoButton and
111 (self.__previewPopup is None or
112 (self.__previewPopup is not None and
113 self.__previewPopup.getCustomData("index") != tabIndex)
114 )
115 ):
116 QTimer.singleShot(0, lambda: self.__showTabPreview(tabIndex))
117
118 # If current tab or not found then hide previous tab preview
119 if tabIndex in (self.currentIndex(), -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().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().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 (
156 evt.type() == QEvent.Type.ToolTip and
157 Preferences.getWebBrowser("ShowPreview")
158 ):
159 # suppress tool tips if we are showing previews
160 evt.setAccepted(True)
161 return True
162
163 return super().event(evt)
164
165 def tabRemoved(self, index):
166 """
167 Public slot to handle the removal of a tab.
168
169 @param index index of the removed tab (integer)
170 """
171 if Preferences.getWebBrowser("ShowPreview"):
172 self.__hidePreview()

eric ide

mercurial