src/eric7/WebBrowser/WebBrowserTabBar.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a specialized tab bar for the web browser.
8 """
9
10 from PyQt6.QtCore import Qt, QPoint, QTimer, QEvent
11 from PyQt6.QtWidgets import QLabel
12
13 from EricWidgets.EricTabWidget import EricWheelTabBar
14 from EricWidgets.EricPassivePopup import (
15 EricPassivePopup, EricPassivePopupStyle
16 )
17
18 import Preferences
19
20
21 class WebBrowserTabBar(EricWheelTabBar):
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().__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 = EricPassivePopup(
62 style=EricPassivePopupStyle.STYLED, parent=self)
63 self.__previewPopup.setFixedSize(w, h)
64 self.__previewPopup.setCustomData("index", index)
65
66 label = QLabel()
67 label.setPixmap(preview.scaled(w, h))
68
69 self.__previewPopup.setView(label)
70 self.__previewPopup.layout().setAlignment(
71 Qt.AlignmentFlag.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.deleteLater()
86 self.__previewPopup = None
87
88 def mouseMoveEvent(self, evt):
89 """
90 Protected method to handle mouse move events.
91
92 @param evt reference to the mouse move event (QMouseEvent)
93 """
94 if self.count() == 1:
95 return
96
97 super().mouseMoveEvent(evt)
98
99 if Preferences.getWebBrowser("ShowPreview"):
100 # Find the tab under the mouse
101 i = 0
102 tabIndex = -1
103 while i < self.count() and tabIndex == -1:
104 if self.tabRect(i).contains(evt.position().toPoint()):
105 tabIndex = i
106 i += 1
107
108 # If found and not the current tab then show tab preview
109 if (
110 tabIndex != -1 and
111 tabIndex != self.currentIndex() and
112 evt.buttons() == Qt.MouseButton.NoButton and
113 (self.__previewPopup is None or
114 (self.__previewPopup is not None and
115 self.__previewPopup.getCustomData("index") != tabIndex)
116 )
117 ):
118 QTimer.singleShot(0, lambda: self.__showTabPreview(tabIndex))
119
120 # If current tab or not found then hide previous tab preview
121 if tabIndex in (self.currentIndex(), -1):
122 self.__hidePreview()
123
124 def leaveEvent(self, evt):
125 """
126 Protected method to handle leave events.
127
128 @param evt reference to the leave event (QEvent)
129 """
130 if Preferences.getWebBrowser("ShowPreview"):
131 # If leave tabwidget then hide previous tab preview
132 self.__hidePreview()
133
134 super().leaveEvent(evt)
135
136 def mousePressEvent(self, evt):
137 """
138 Protected method to handle mouse press events.
139
140 @param evt reference to the mouse press event (QMouseEvent)
141 """
142 if Preferences.getWebBrowser("ShowPreview"):
143 self.__hidePreview()
144
145 super().mousePressEvent(evt)
146
147 def event(self, evt):
148 """
149 Public method to handle event.
150
151 This event handler just handles the tooltip event and passes the
152 handling of all others to the superclass.
153
154 @param evt reference to the event to be handled (QEvent)
155 @return flag indicating, if the event was handled (boolean)
156 """
157 if (
158 evt.type() == QEvent.Type.ToolTip and
159 Preferences.getWebBrowser("ShowPreview")
160 ):
161 # suppress tool tips if we are showing previews
162 evt.setAccepted(True)
163 return True
164
165 return super().event(evt)
166
167 def tabRemoved(self, index):
168 """
169 Public slot to handle the removal of a tab.
170
171 @param index index of the removed tab (integer)
172 """
173 if Preferences.getWebBrowser("ShowPreview"):
174 self.__hidePreview()

eric ide

mercurial