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