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