|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2011 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a custom widget indicating the strength of a password. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtWidgets import QProgressBar |
|
11 |
|
12 from Utilities.PasswordChecker import PasswordChecker |
|
13 |
|
14 |
|
15 class EricPasswordMeter(QProgressBar): |
|
16 """ |
|
17 Class implementing a custom widget indicating the strength of a password. |
|
18 """ |
|
19 def __init__(self, parent=None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param parent reference to the parent widget (QWidget) |
|
24 """ |
|
25 super().__init__(parent) |
|
26 |
|
27 super().setTextVisible(False) |
|
28 super().setMaximum(100) |
|
29 self.__increment = 100 // (PasswordChecker.Complexity_VeryStrong + 1) |
|
30 |
|
31 self.__indicatorColors = [ |
|
32 "#ff0000", # red |
|
33 "#ff8800", # orange |
|
34 "#ffff00", # yellow |
|
35 "#ccff00", # yellow green |
|
36 "#00ff00", # green |
|
37 ] |
|
38 self.__noIndicator = "#ffffff" |
|
39 |
|
40 self.__styleSheetTemplate = ( |
|
41 "QProgressBar {{" |
|
42 " border: 2px solid black;" |
|
43 " border-radius: 5px;" |
|
44 " text-align: center; }}" |
|
45 "QProgressBar::chunk:horizontal {{" |
|
46 " background-color: {0}; }}" |
|
47 ) |
|
48 self.setStyleSheet( |
|
49 self.__styleSheetTemplate.format(self.__noIndicator)) |
|
50 |
|
51 def checkPasswordStrength(self, password): |
|
52 """ |
|
53 Public slot to check the password strength and update the |
|
54 progress bar accordingly. |
|
55 |
|
56 @param password password to be checked (string) |
|
57 """ |
|
58 strength = PasswordChecker().checkPassword(password) |
|
59 self.setStyleSheet(self.__styleSheetTemplate.format( |
|
60 self.__indicatorColors[strength])) |
|
61 super().setValue( |
|
62 (strength + 1) * self.__increment) |
|
63 |
|
64 def setValue(self, value): |
|
65 """ |
|
66 Public method to set the value. |
|
67 |
|
68 Overwritten to do nothing. |
|
69 |
|
70 @param value value (integer) |
|
71 """ |
|
72 pass |
|
73 |
|
74 def setMaximum(self, value): |
|
75 """ |
|
76 Public method to set the maximum value. |
|
77 |
|
78 Overwritten to do nothing. |
|
79 |
|
80 @param value maximum value (integer) |
|
81 """ |
|
82 pass |
|
83 |
|
84 def setMinimum(self, value): |
|
85 """ |
|
86 Public method to set the minimal value. |
|
87 |
|
88 Overwritten to do nothing. |
|
89 |
|
90 @param value minimum value (integer) |
|
91 """ |
|
92 pass |
|
93 |
|
94 if __name__ == "__main__": |
|
95 import sys |
|
96 from PyQt6.QtWidgets import QApplication |
|
97 |
|
98 app = QApplication(sys.argv) |
|
99 meter = EricPasswordMeter() |
|
100 meter.show() |
|
101 meter.checkPasswordStrength("Blah2+") |
|
102 app.exec() |