|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the feature permission bar widget. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSignal, QPropertyAnimation, QByteArray, \ |
|
13 QEasingCurve, QPoint |
|
14 from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QPushButton |
|
15 from PyQt5.QtWebKitWidgets import QWebFrame, QWebPage |
|
16 |
|
17 import UI.PixmapCache |
|
18 |
|
19 class FeaturePermissionBar(QWidget): |
|
20 """ |
|
21 Class implementing the feature permission bar widget. |
|
22 """ |
|
23 featurePermissionProvided = pyqtSignal(QWebFrame, QWebPage.Feature, |
|
24 QWebPage.PermissionPolicy) |
|
25 |
|
26 DefaultHeight = 30 |
|
27 |
|
28 def __init__(self, view): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param view reference to the web view |
|
33 @type QWebView |
|
34 """ |
|
35 super(FeaturePermissionBar, self).__init__(view) |
|
36 |
|
37 self.__messageLabel = QLabel(self) |
|
38 |
|
39 self.__frame = None |
|
40 self.__feature = None |
|
41 |
|
42 self.__permissionFeatureTexts = { |
|
43 QWebPage.Notifications: |
|
44 self.tr("{0} wants to use desktop notifications."), |
|
45 QWebPage.Geolocation: |
|
46 self.tr("{0} wants to use your position.") |
|
47 } |
|
48 |
|
49 self.setAutoFillBackground(True) |
|
50 self.__layout = QHBoxLayout() |
|
51 self.setLayout(self.__layout) |
|
52 self.__layout.setContentsMargins(self.DefaultHeight, 0, 0, 0) |
|
53 self.__layout.addWidget(self.__messageLabel) |
|
54 self.__layout.addStretch() |
|
55 self.__allowButton = QPushButton(self.tr("Allow"), self) |
|
56 self.__denyButton = QPushButton(self.tr("Deny"), self) |
|
57 self.__discardButton = QPushButton(UI.PixmapCache.getIcon("close.png"), |
|
58 "", self) |
|
59 self.__allowButton.clicked.connect(self.__permissionGranted) |
|
60 self.__denyButton.clicked.connect(self.__permissionDenied) |
|
61 self.__discardButton.clicked.connect(self.__permissionUnknown) |
|
62 self.__layout.addWidget(self.__allowButton) |
|
63 self.__layout.addWidget(self.__denyButton) |
|
64 self.__layout.addWidget(self.__discardButton) |
|
65 self.setGeometry(0, -self.DefaultHeight, view.width(), |
|
66 self.DefaultHeight) |
|
67 |
|
68 def requestPermission(self, frame, feature): |
|
69 """ |
|
70 Public method to ask the user for a permission. |
|
71 |
|
72 @param frame frame sending the request |
|
73 @type QWebFrame |
|
74 @param feature requested feature |
|
75 @type QWebPage.Feature |
|
76 """ |
|
77 self.__frame = frame |
|
78 self.__feature = feature |
|
79 |
|
80 try: |
|
81 self.__messageLabel.setText( |
|
82 self.__permissionFeatureTexts[self.__feature].format( |
|
83 self.__frame.securityOrigin().host())) |
|
84 except KeyError: |
|
85 self.__messageLabel.setText( |
|
86 self.tr("{0} wants to use an unknown feature.").format( |
|
87 self.__frame.securityOrigin().host())) |
|
88 self.show() |
|
89 |
|
90 self.__animation = QPropertyAnimation(self) |
|
91 self.__animation.setTargetObject(self) |
|
92 self.__animation.setPropertyName(QByteArray(b"pos")) |
|
93 self.__animation.setDuration(300) |
|
94 self.__animation.setStartValue(self.pos()) |
|
95 self.__animation.setEndValue(QPoint(0, 0)) |
|
96 self.__animation.setEasingCurve(QEasingCurve.InOutQuad) |
|
97 self.__animation.start(QPropertyAnimation.DeleteWhenStopped) |
|
98 |
|
99 def __permissionDenied(self): |
|
100 """ |
|
101 Private slot handling the user pressing the deny button. |
|
102 """ |
|
103 self.featurePermissionProvided.emit(self.__frame, self.__feature, |
|
104 QWebPage.PermissionDeniedByUser) |
|
105 |
|
106 def __permissionGranted(self): |
|
107 """ |
|
108 Private slot handling the user pressing the allow button. |
|
109 """ |
|
110 self.featurePermissionProvided.emit(self.__frame, self.__feature, |
|
111 QWebPage.PermissionGrantedByUser) |
|
112 |
|
113 def __permissionUnknown(self): |
|
114 """ |
|
115 Private slot handling the user closing the dialog without. |
|
116 """ |
|
117 self.featurePermissionProvided.emit(self.__frame, self.__feature, |
|
118 QWebPage.PermissionUnknown) |