1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "PasswordEdit.h"
#include "core/Config.h"
#include <QFontDatabase>
const QColor PasswordEdit::CorrectSoFarColor = QColor(255, 205, 15);
const QColor PasswordEdit::ErrorColor = QColor(255, 125, 125);
PasswordEdit::PasswordEdit(QWidget* parent)
: QLineEdit(parent)
, m_basePasswordEdit(nullptr)
{
setEchoMode(QLineEdit::Password);
updateStylesheet();
// set font to system monospace font and increase letter spacing
QFont passwordFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
passwordFont.setLetterSpacing(QFont::PercentageSpacing, 110);
setFont(passwordFont);
}
void PasswordEdit::enableVerifyMode(PasswordEdit* basePasswordEdit)
{
m_basePasswordEdit = basePasswordEdit;
updateStylesheet();
connect(m_basePasswordEdit, SIGNAL(textChanged(QString)), SLOT(autocompletePassword(QString)));
connect(m_basePasswordEdit, SIGNAL(textChanged(QString)), SLOT(updateStylesheet()));
connect(this, SIGNAL(textChanged(QString)), SLOT(updateStylesheet()));
connect(m_basePasswordEdit, SIGNAL(showPasswordChanged(bool)), SLOT(setShowPassword(bool)));
}
void PasswordEdit::setShowPassword(bool show)
{
setEchoMode(show ? QLineEdit::Normal : QLineEdit::Password);
// if I have a parent, I'm the child
if (m_basePasswordEdit){
if (config()->get("security/passwordsrepeat").toBool()) {
setEnabled(!show);
setReadOnly(show);
setText(m_basePasswordEdit->text());
}
else {
// This fix a bug when the QLineEdit is disabled while switching config
if (isEnabled() == false) {
setEnabled(true);
setReadOnly(false);
}
}
}
updateStylesheet();
Q_EMIT showPasswordChanged(show);
}
bool PasswordEdit::passwordsEqual() const
{
return text() == m_basePasswordEdit->text();
}
void PasswordEdit::updateStylesheet()
{
QString stylesheet("QLineEdit { ");
if (m_basePasswordEdit && !passwordsEqual()) {
stylesheet.append("background: %1; ");
if (m_basePasswordEdit->text().startsWith(text())) {
stylesheet = stylesheet.arg(CorrectSoFarColor.name());
}
else {
stylesheet = stylesheet.arg(ErrorColor.name());
}
}
stylesheet.append("}");
setStyleSheet(stylesheet);
}
void PasswordEdit::autocompletePassword(QString password)
{
if (config()->get("security/passwordsrepeat").toBool() && echoMode() == QLineEdit::Normal) {
setText(password);
}
}
|