|
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 HelpTabBar(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 (HelpTabWidget) |
|
30 """ |
|
31 E5WheelTabBar.__init__(self, 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 w = self.tabSizeHint(index).width() |
|
57 h = int(w * currentBrowser.height() / currentBrowser.width()) |
|
58 |
|
59 self.__previewPopup = E5PassivePopup(self) |
|
60 self.__previewPopup.setFrameShape(QFrame.StyledPanel) |
|
61 self.__previewPopup.setFrameShadow(QFrame.Plain) |
|
62 self.__previewPopup.setFixedSize(w, h) |
|
63 self.__previewPopup.setCustomData("index", index) |
|
64 |
|
65 from .HelpSnap import renderTabPreview |
|
66 label = QLabel() |
|
67 label.setPixmap(renderTabPreview(indexedBrowser.page(), w, h)) |
|
68 |
|
69 self.__previewPopup.setView(label) |
|
70 self.__previewPopup.layout().setAlignment(Qt.AlignTop) |
|
71 self.__previewPopup.layout().setContentsMargins(0, 0, 0, 0) |
|
72 |
|
73 tr = self.tabRect(index) |
|
74 pos = QPoint(tr.x(), tr.y() + tr.height()) |
|
75 |
|
76 self.__previewPopup.show(self.mapToGlobal(pos)) |
|
77 |
|
78 def __hidePreview(self): |
|
79 """ |
|
80 Private method to hide the preview. |
|
81 """ |
|
82 if self.__previewPopup is not None: |
|
83 self.__previewPopup.hide() |
|
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 E5WheelTabBar.mouseMoveEvent(self, evt) |
|
96 |
|
97 if Preferences.getHelp("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 tabIndex != -1 and \ |
|
108 tabIndex != self.currentIndex() and \ |
|
109 evt.buttons() == Qt.NoButton: |
|
110 if self.__previewPopup is None or \ |
|
111 (self.__previewPopup is not None and |
|
112 self.__previewPopup.getCustomData("index") != tabIndex): |
|
113 QTimer.singleShot( |
|
114 0, lambda: self.__showTabPreview(tabIndex)) |
|
115 |
|
116 # If current tab or not found then hide previous tab preview |
|
117 if tabIndex == self.currentIndex() or \ |
|
118 tabIndex == -1: |
|
119 self.__hidePreview() |
|
120 |
|
121 def leaveEvent(self, evt): |
|
122 """ |
|
123 Protected method to handle leave events. |
|
124 |
|
125 @param evt reference to the leave event (QEvent) |
|
126 """ |
|
127 if Preferences.getHelp("ShowPreview"): |
|
128 # If leave tabwidget then hide previous tab preview |
|
129 self.__hidePreview() |
|
130 |
|
131 E5WheelTabBar.leaveEvent(self, evt) |
|
132 |
|
133 def mousePressEvent(self, evt): |
|
134 """ |
|
135 Protected method to handle mouse press events. |
|
136 |
|
137 @param evt reference to the mouse press event (QMouseEvent) |
|
138 """ |
|
139 if Preferences.getHelp("ShowPreview"): |
|
140 self.__hidePreview() |
|
141 |
|
142 E5WheelTabBar.mousePressEvent(self, evt) |
|
143 |
|
144 def event(self, evt): |
|
145 """ |
|
146 Public method to handle event. |
|
147 |
|
148 This event handler just handles the tooltip event and passes the |
|
149 handling of all others to the superclass. |
|
150 |
|
151 @param evt reference to the event to be handled (QEvent) |
|
152 @return flag indicating, if the event was handled (boolean) |
|
153 """ |
|
154 if evt.type() == QEvent.ToolTip and \ |
|
155 Preferences.getHelp("ShowPreview"): |
|
156 # suppress tool tips if we are showing previews |
|
157 evt.setAccepted(True) |
|
158 return True |
|
159 |
|
160 return E5WheelTabBar.event(self, evt) |
|
161 |
|
162 def tabRemoved(self, index): |
|
163 """ |
|
164 Public slot to handle the removal of a tab. |
|
165 |
|
166 @param index index of the removed tab (integer) |
|
167 """ |
|
168 if Preferences.getHelp("ShowPreview"): |
|
169 self.__hidePreview() |