src/eric7/WebBrowser/SiteInfo/SiteInfoWidget.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9384
b1b8e2dc2280
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
7 Module implementing a widget to show some site information. 7 Module implementing a widget to show some site information.
8 """ 8 """
9 9
10 from PyQt6.QtCore import pyqtSlot, Qt, QPoint 10 from PyQt6.QtCore import pyqtSlot, Qt, QPoint
11 from PyQt6.QtWidgets import ( 11 from PyQt6.QtWidgets import (
12 QMenu, QGridLayout, QHBoxLayout, QLabel, QFrame, QSizePolicy, QPushButton, 12 QMenu,
13 QSpacerItem 13 QGridLayout,
14 QHBoxLayout,
15 QLabel,
16 QFrame,
17 QSizePolicy,
18 QPushButton,
19 QSpacerItem,
14 ) 20 )
15 21
16 import UI.PixmapCache 22 import UI.PixmapCache
17 23
18 from WebBrowser.WebBrowserWindow import WebBrowserWindow 24 from WebBrowser.WebBrowserWindow import WebBrowserWindow
20 26
21 class SiteInfoWidget(QMenu): 27 class SiteInfoWidget(QMenu):
22 """ 28 """
23 Class implementing a widget to show site related infos. 29 Class implementing a widget to show site related infos.
24 """ 30 """
31
25 def __init__(self, browser, parent=None): 32 def __init__(self, browser, parent=None):
26 """ 33 """
27 Constructor 34 Constructor
28 35
29 @param browser reference to the browser view 36 @param browser reference to the browser view
30 @type WebBrowserView 37 @type WebBrowserView
31 @param parent reference to the parent object 38 @param parent reference to the parent object
32 @type QWidget 39 @type QWidget
33 """ 40 """
34 super().__init__(parent) 41 super().__init__(parent)
35 42
36 self.__browser = browser 43 self.__browser = browser
37 url = browser.url() 44 url = browser.url()
38 45
39 self.setMinimumWidth(400) 46 self.setMinimumWidth(400)
40 47
41 layout = QGridLayout(self) 48 layout = QGridLayout(self)
42 rows = 0 49 rows = 0
43 50
44 titleLabel = QLabel(self) 51 titleLabel = QLabel(self)
45 titleLabel.setText(self.tr("<b>Site {0}</b>").format(url.host())) 52 titleLabel.setText(self.tr("<b>Site {0}</b>").format(url.host()))
46 layout.addWidget(titleLabel, rows, 0, 1, -1, 53 layout.addWidget(titleLabel, rows, 0, 1, -1, Qt.AlignmentFlag.AlignCenter)
47 Qt.AlignmentFlag.AlignCenter) 54 rows += 1
48 rows += 1 55
49
50 line = QFrame(self) 56 line = QFrame(self)
51 line.setLineWidth(1) 57 line.setLineWidth(1)
52 line.setFrameStyle(QFrame.Shape.HLine | QFrame.Shadow.Sunken) 58 line.setFrameStyle(QFrame.Shape.HLine | QFrame.Shadow.Sunken)
53 layout.addWidget(line, rows, 0, 1, -1) 59 layout.addWidget(line, rows, 0, 1, -1)
54 rows += 1 60 rows += 1
55 61
56 secureIcon = QLabel() 62 secureIcon = QLabel()
57 layout.addWidget(secureIcon, rows, 0, Qt.AlignmentFlag.AlignCenter) 63 layout.addWidget(secureIcon, rows, 0, Qt.AlignmentFlag.AlignCenter)
58 secureLabel = QLabel() 64 secureLabel = QLabel()
59 secureLabel.setSizePolicy(QSizePolicy.Policy.Expanding, 65 secureLabel.setSizePolicy(
60 QSizePolicy.Policy.Preferred) 66 QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred
67 )
61 layout.addWidget(secureLabel, rows, 1) 68 layout.addWidget(secureLabel, rows, 1)
62 if url.scheme() in ["https"]: 69 if url.scheme() in ["https"]:
63 if WebBrowserWindow.networkManager().isInsecureHost(url.host()): 70 if WebBrowserWindow.networkManager().isInsecureHost(url.host()):
64 secureLabel.setText( 71 secureLabel.setText(
65 self.tr("Your connection to this site " 72 self.tr("Your connection to this site " "<b>may not be secure</b>.")
66 "<b>may not be secure</b>.")) 73 )
67 secureIcon.setPixmap( 74 secureIcon.setPixmap(UI.PixmapCache.getPixmap("securityMedium"))
68 UI.PixmapCache.getPixmap("securityMedium"))
69 else: 75 else:
70 secureLabel.setText( 76 secureLabel.setText(
71 self.tr("Your connection to this site is <b>secure</b>.")) 77 self.tr("Your connection to this site is <b>secure</b>.")
72 secureIcon.setPixmap( 78 )
73 UI.PixmapCache.getPixmap("securityHigh")) 79 secureIcon.setPixmap(UI.PixmapCache.getPixmap("securityHigh"))
74 else: 80 else:
75 secureLabel.setText( 81 secureLabel.setText(
76 self.tr("Your connection to this site is <b>not secure</b>.")) 82 self.tr("Your connection to this site is <b>not secure</b>.")
77 secureIcon.setPixmap( 83 )
78 UI.PixmapCache.getPixmap("securityLow")) 84 secureIcon.setPixmap(UI.PixmapCache.getPixmap("securityLow"))
79 rows += 1 85 rows += 1
80 86
81 visits = WebBrowserWindow.historyManager().siteVisitsCount( 87 visits = WebBrowserWindow.historyManager().siteVisitsCount(
82 url.scheme(), url.host()) 88 url.scheme(), url.host()
89 )
83 historyIcon = QLabel() 90 historyIcon = QLabel()
84 layout.addWidget(historyIcon, rows, 0, Qt.AlignmentFlag.AlignCenter) 91 layout.addWidget(historyIcon, rows, 0, Qt.AlignmentFlag.AlignCenter)
85 historyLabel = QLabel() 92 historyLabel = QLabel()
86 historyLabel.setSizePolicy(QSizePolicy.Policy.Expanding, 93 historyLabel.setSizePolicy(
87 QSizePolicy.Policy.Preferred) 94 QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred
95 )
88 layout.addWidget(historyLabel, rows, 1) 96 layout.addWidget(historyLabel, rows, 1)
89 if visits > 3: 97 if visits > 3:
90 historyLabel.setText( 98 historyLabel.setText(
91 self.tr("This is your <b>{0}.</b> visit of this site.") 99 self.tr("This is your <b>{0}.</b> visit of this site.").format(visits)
92 .format(visits)) 100 )
93 historyIcon.setPixmap( 101 historyIcon.setPixmap(UI.PixmapCache.getPixmap("flagGreen"))
94 UI.PixmapCache.getPixmap("flagGreen"))
95 elif visits == 0: 102 elif visits == 0:
96 historyLabel.setText( 103 historyLabel.setText(
97 self.tr("You have <b>never</b> visited this site before.") 104 self.tr("You have <b>never</b> visited this site before.").format(
98 .format(visits)) 105 visits
99 historyIcon.setPixmap( 106 )
100 UI.PixmapCache.getPixmap("flagBlack")) 107 )
108 historyIcon.setPixmap(UI.PixmapCache.getPixmap("flagBlack"))
101 else: 109 else:
102 historyIcon.setPixmap( 110 historyIcon.setPixmap(UI.PixmapCache.getPixmap("flagYellow"))
103 UI.PixmapCache.getPixmap("flagYellow"))
104 if visits == 1: 111 if visits == 1:
105 visitStr = self.tr("first") 112 visitStr = self.tr("first")
106 elif visits == 2: 113 elif visits == 2:
107 visitStr = self.tr("second") 114 visitStr = self.tr("second")
108 else: 115 else:
109 visitStr = self.tr("third") 116 visitStr = self.tr("third")
110 historyLabel.setText( 117 historyLabel.setText(
111 self.tr("This is your <b>{0}</b> visit of this site.") 118 self.tr("This is your <b>{0}</b> visit of this site.").format(visitStr)
112 .format(visitStr)) 119 )
113 rows += 1 120 rows += 1
114 121
115 line = QFrame(self) 122 line = QFrame(self)
116 line.setLineWidth(1) 123 line.setLineWidth(1)
117 line.setFrameStyle(QFrame.Shape.HLine | QFrame.Shadow.Sunken) 124 line.setFrameStyle(QFrame.Shape.HLine | QFrame.Shadow.Sunken)
118 layout.addWidget(line, rows, 0, 1, -1) 125 layout.addWidget(line, rows, 0, 1, -1)
119 rows += 1 126 rows += 1
120 127
121 page = self.__browser.page() 128 page = self.__browser.page()
122 scheme = page.registerProtocolHandlerRequestScheme() 129 scheme = page.registerProtocolHandlerRequestScheme()
123 registeredUrl = ( 130 registeredUrl = WebBrowserWindow.protocolHandlerManager().protocolHandler(
124 WebBrowserWindow.protocolHandlerManager().protocolHandler(scheme) 131 scheme
125 ) 132 )
126 if ( 133 if bool(scheme) and registeredUrl != page.registerProtocolHandlerRequestUrl():
127 bool(scheme) and
128 registeredUrl != page.registerProtocolHandlerRequestUrl()
129 ):
130 horizontalLayout = QHBoxLayout() 134 horizontalLayout = QHBoxLayout()
131 protocolHandlerLabel = QLabel( 135 protocolHandlerLabel = QLabel(
132 self.tr("Register as <b>{0}</b> links handler.") 136 self.tr("Register as <b>{0}</b> links handler.").format(scheme), self
133 .format(scheme), self) 137 )
134 protocolHandlerLabel.setSizePolicy(QSizePolicy.Policy.Expanding, 138 protocolHandlerLabel.setSizePolicy(
135 QSizePolicy.Policy.Preferred) 139 QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred
136 140 )
141
137 horizontalLayout.addWidget(protocolHandlerLabel) 142 horizontalLayout.addWidget(protocolHandlerLabel)
138 protocolHandlerButton = QPushButton(self.tr("Register"), self) 143 protocolHandlerButton = QPushButton(self.tr("Register"), self)
139 horizontalLayout.addWidget(protocolHandlerButton) 144 horizontalLayout.addWidget(protocolHandlerButton)
140 protocolHandlerButton.clicked.connect( 145 protocolHandlerButton.clicked.connect(self.__registerProtocolHandler)
141 self.__registerProtocolHandler)
142 layout.addLayout(horizontalLayout, rows, 0, 1, -1) 146 layout.addLayout(horizontalLayout, rows, 0, 1, -1)
143 rows += 1 147 rows += 1
144 148
145 protocolHandlerLine = QFrame(self) 149 protocolHandlerLine = QFrame(self)
146 protocolHandlerLine.setLineWidth(1) 150 protocolHandlerLine.setLineWidth(1)
147 protocolHandlerLine.setFrameStyle( 151 protocolHandlerLine.setFrameStyle(QFrame.Shape.HLine | QFrame.Shadow.Sunken)
148 QFrame.Shape.HLine | QFrame.Shadow.Sunken)
149 layout.addWidget(protocolHandlerLine, rows, 0, 1, -1) 152 layout.addWidget(protocolHandlerLine, rows, 0, 1, -1)
150 rows += 1 153 rows += 1
151 154
152 horizontalLayout = QHBoxLayout() 155 horizontalLayout = QHBoxLayout()
153 spacerItem = QSpacerItem( 156 spacerItem = QSpacerItem(
154 40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) 157 40, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum
158 )
155 horizontalLayout.addItem(spacerItem) 159 horizontalLayout.addItem(spacerItem)
156 moreButton = QPushButton(self.tr("More..."), self) 160 moreButton = QPushButton(self.tr("More..."), self)
157 horizontalLayout.addWidget(moreButton) 161 horizontalLayout.addWidget(moreButton)
158 moreButton.clicked.connect(self.__showSiteInfo) 162 moreButton.clicked.connect(self.__showSiteInfo)
159 layout.addLayout(horizontalLayout, rows, 0, 1, -1) 163 layout.addLayout(horizontalLayout, rows, 0, 1, -1)
160 164
161 layout.activate() 165 layout.activate()
162 166
163 def showAt(self, pos): 167 def showAt(self, pos):
164 """ 168 """
165 Public method to show the widget. 169 Public method to show the widget.
166 170
167 @param pos position to show at 171 @param pos position to show at
168 @type QPoint 172 @type QPoint
169 """ 173 """
170 self.adjustSize() 174 self.adjustSize()
171 xpos = pos.x() - self.width() // 2 175 xpos = pos.x() - self.width() // 2
172 if xpos < 0: 176 if xpos < 0:
173 xpos = 10 177 xpos = 10
174 p = QPoint(xpos, pos.y() + 10) 178 p = QPoint(xpos, pos.y() + 10)
175 self.move(p) 179 self.move(p)
176 self.show() 180 self.show()
177 181
178 def accept(self): 182 def accept(self):
179 """ 183 """
180 Public method to accept the widget. 184 Public method to accept the widget.
181 """ 185 """
182 self.close() 186 self.close()
183 187
184 @pyqtSlot() 188 @pyqtSlot()
185 def __showSiteInfo(self): 189 def __showSiteInfo(self):
186 """ 190 """
187 Private slot to show the site info dialog. 191 Private slot to show the site info dialog.
188 """ 192 """
189 from .SiteInfoDialog import SiteInfoDialog 193 from .SiteInfoDialog import SiteInfoDialog
190 siteinfoDialog = SiteInfoDialog( 194
191 self.__browser, self.__browser.mainWindow()) 195 siteinfoDialog = SiteInfoDialog(self.__browser, self.__browser.mainWindow())
192 siteinfoDialog.show() 196 siteinfoDialog.show()
193 197
194 @pyqtSlot() 198 @pyqtSlot()
195 def __registerProtocolHandler(self): 199 def __registerProtocolHandler(self):
196 """ 200 """
197 Private slot to register a protocol handler. 201 Private slot to register a protocol handler.
198 """ 202 """
199 self.close() 203 self.close()
200 page = self.__browser.page() 204 page = self.__browser.page()
201 WebBrowserWindow.protocolHandlerManager().addProtocolHandler( 205 WebBrowserWindow.protocolHandlerManager().addProtocolHandler(
202 page.registerProtocolHandlerRequestScheme(), 206 page.registerProtocolHandlerRequestScheme(),
203 page.registerProtocolHandlerRequestUrl()) 207 page.registerProtocolHandlerRequestUrl(),
208 )

eric ide

mercurial