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