E5Gui/E5PasswordMeter.py

changeset 1127
b1802ebe0066
child 1509
c0b5e693b0eb
equal deleted inserted replaced
1123:4ed80ed7e433 1127:b1802ebe0066
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2011 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 PyQt4.QtGui import QProgressBar
11
12 from Utilities.PasswordChecker import PasswordChecker
13
14
15 class E5PasswordMeter(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 self.setStyleSheet(self.__styleSheetTemplate.format(self.__noIndicator))
48
49 def checkPasswordStrength(self, password):
50 """
51 Public slot to check the password strength and update the
52 progress bar accordingly.
53
54 @param password password to be checked (string)
55 """
56 strength = PasswordChecker().checkPassword(password)
57 self.setStyleSheet(self.__styleSheetTemplate.format(
58 self.__indicatorColors[strength]))
59 super().setValue((strength + 1) * self.__increment)
60
61 def setValue(self, value):
62 """
63 Public method to set the value.
64
65 Overwritten to do nothing.
66 """
67 pass
68
69 def setMaximum(self, value):
70 """
71 Public method to set the maximum value.
72
73 Overwritten to do nothing.
74 """
75 pass
76
77 def setMinimum(self, value):
78 """
79 Public method to set the minimal value.
80
81 Overwritten to do nothing.
82 """
83 pass
84
85 if __name__ == "__main__":
86 import sys
87 from PyQt4.QtGui import QApplication
88
89 app = QApplication(sys.argv)
90 meter = E5PasswordMeter()
91 meter.show()
92 meter.checkPasswordStrength("Blah2+")
93 app.exec_()

eric ide

mercurial