-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpasswordlineedit.cc
More file actions
206 lines (172 loc) · 5.68 KB
/
passwordlineedit.cc
File metadata and controls
206 lines (172 loc) · 5.68 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "passwordlineedit.hpp"
#include <QtWidgets>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
namespace {
bool checkCapsLockState()
{
#ifdef Q_OS_WIN
return (GetKeyState(VK_CAPITAL) & 0x0001) != 0;
#else
qWarning() << "Unsupported platform";
return false;
#endif
}
} // namespace
class PasswordLineEdit::PasswordLineEditPrivate
{
public:
explicit PasswordLineEditPrivate(PasswordLineEdit *q)
: q_ptr(q)
{
toolButton = new QToolButton(q_ptr);
toolButton->setIconSize({20, 20});
toolButton->setCursor(Qt::PointingHandCursor);
toolButton->setCheckable(true);
toolButton->setToolTip(q_ptr->tr("Show/hide password"));
toolTipLabel = new QLabel(q_ptr->window());
toolTipLabel->setWindowFlags(Qt::ToolTip);
toolTipLabel->setStyleSheet("QLabel{"
"background: #FFF3CD;"
"border: 1px solid #FFEaa8;"
"padding: 8px;"
"color: #856404;"
"font-size: 12px;"
"border-radius: 4px;"
"}");
toolTipLabel->hide();
toolTipTimer = new QTimer(q_ptr);
toolTipTimer->setSingleShot(true);
}
PasswordLineEdit *q_ptr;
QToolButton *toolButton;
QIcon visibleIcon;
QIcon hiddenIcon;
QLabel *toolTipLabel;
QTimer *toolTipTimer;
bool capsLockWarning = true;
};
PasswordLineEdit::PasswordLineEdit(QWidget *parent)
: QLineEdit(parent)
, d_ptr(new PasswordLineEditPrivate(this))
{
setupIcons();
setupUI();
setupConnections();
// 设置合理的密码验证
QRegularExpression regExp(R"([\x20-\x7E]+)"); // 所有可打印ASCII字符
setValidator(new QRegularExpressionValidator(regExp, this));
setAttribute(Qt::WA_InputMethodEnabled, false);
setEchoMode(QLineEdit::Password);
installEventFilter(this);
}
PasswordLineEdit::~PasswordLineEdit() = default;
bool PasswordLineEdit::capsLockWarningEnabled() const
{
return d_ptr->capsLockWarning;
}
void PasswordLineEdit::setCapsLockWarningEnabled(bool enabled)
{
d_ptr->capsLockWarning = enabled;
}
void PasswordLineEdit::setToggleIcons(const QIcon &visibleIcon, const QIcon &hiddenIcon)
{
d_ptr->visibleIcon = visibleIcon;
d_ptr->hiddenIcon = hiddenIcon;
d_ptr->toolButton->setIcon(hiddenIcon);
}
void PasswordLineEdit::setToolTipDuration(int milliseconds)
{
d_ptr->toolTipTimer->setInterval(milliseconds);
}
void PasswordLineEdit::togglePasswordVisibility()
{
d_ptr->toolButton->toggle();
}
bool PasswordLineEdit::eventFilter(QObject *watched, QEvent *event)
{
if (watched == this && d_ptr->capsLockWarning) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_CapsLock) {
// 延迟检查,确保状态已更新
QTimer::singleShot(0, this, &PasswordLineEdit::showCapsLockWarning);
}
}
}
return QLineEdit::eventFilter(watched, event);
}
void PasswordLineEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
if (d_ptr->capsLockWarning) {
showCapsLockWarning();
}
}
void PasswordLineEdit::focusOutEvent(QFocusEvent *event)
{
QLineEdit::focusOutEvent(event);
d_ptr->toolTipLabel->hide();
}
void PasswordLineEdit::onShowPassword(bool visible)
{
setEchoMode(visible ? QLineEdit::Normal : QLineEdit::Password);
d_ptr->toolButton->setIcon(visible ? d_ptr->visibleIcon : d_ptr->hiddenIcon);
d_ptr->toolButton->setToolTip(visible ? tr("Hide password") : tr("Show password"));
}
void PasswordLineEdit::hideToolTip()
{
d_ptr->toolTipLabel->hide();
}
void PasswordLineEdit::setupUI()
{
setPlaceholderText(tr("Enter password"));
setMinimumSize(200, 35);
// 使用布局确保按钮正确对齐
auto *layout = new QHBoxLayout(this);
layout->setContentsMargins(1, 1, 1, 1);
layout->setSpacing(0);
layout->addStretch();
layout->addWidget(d_ptr->toolButton);
}
void PasswordLineEdit::setupIcons()
{
// 尝试加载自定义图标,失败则使用系统图标
if (!d_ptr->visibleIcon.isNull() && !d_ptr->hiddenIcon.isNull()) {
return; // 图标已设置
}
// 使用系统图标作为备选
d_ptr->visibleIcon = QApplication::style()->standardIcon(QStyle::SP_FileDialogDetailedView);
d_ptr->hiddenIcon = QApplication::style()->standardIcon(QStyle::SP_FileDialogContentsView);
d_ptr->toolButton->setIcon(d_ptr->hiddenIcon);
}
void PasswordLineEdit::setupConnections()
{
connect(d_ptr->toolButton, &QToolButton::toggled, this, &PasswordLineEdit::onShowPassword);
connect(d_ptr->toolTipTimer, &QTimer::timeout, this, &PasswordLineEdit::hideToolTip);
}
void PasswordLineEdit::showCapsLockWarning()
{
if (!checkCapsLockState()) {
return;
}
d_ptr->toolTipLabel->setText(tr("Caps Lock is ON"));
d_ptr->toolTipLabel->adjustSize();
// 计算提示位置
QPoint globalPos = mapToGlobal(QPoint(0, 0));
int x = globalPos.x();
int y = globalPos.y() - d_ptr->toolTipLabel->height() - 5;
// 边界检查
QScreen *screen = window()->windowHandle()->screen();
QRect screenGeometry = screen->availableGeometry();
if (y < screenGeometry.top()) {
y = globalPos.y() + height() + 5;
}
if (x + d_ptr->toolTipLabel->width() > screenGeometry.right()) {
x = screenGeometry.right() - d_ptr->toolTipLabel->width();
}
d_ptr->toolTipLabel->move(x, y);
d_ptr->toolTipLabel->show();
d_ptr->toolTipTimer->start(3000); // 3秒后自动隐藏
}