src/eric7/WebBrowser/WebBrowserTabBar.py

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

eric ide

mercurial