KWidgetsAddons

kpasswordlineedit.cpp
1/*
2 SPDX-FileCopyrightText: 2017 Montel Laurent <montel@kde.org>
3 SPDX-FileCopyrightText: 2015 Elvis Angelaccio <elvis.angelaccio@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "kpasswordlineedit.h"
9
10#include <QAction>
11#include <QContextMenuEvent>
12#include <QHBoxLayout>
13#include <QIcon>
14#include <QMenu>
15
16class KPasswordLineEditPrivate
17{
18public:
19 KPasswordLineEditPrivate(KPasswordLineEdit *qq)
20 : q(qq)
21 {
22 }
23 void initialize();
24 void echoModeToggled();
25 void showToggleEchoModeAction(const QString &text);
26 void slotCustomContextMenuRequested(const QPoint &pos);
27
28 QIcon passwordIcon;
29 QIcon visibleIcon;
30
31 QLineEdit *passwordLineEdit = nullptr;
32 QAction *toggleEchoModeAction = nullptr;
33 bool isToggleEchoModeAvailable = true;
34 KPassword::RevealMode revealPasswordMode = KPassword::RevealMode::OnlyNew;
35 KPasswordLineEdit *const q;
36};
37
38void KPasswordLineEditPrivate::initialize()
39{
40 QIcon visibilityIcon = QIcon::fromTheme(QStringLiteral("visibility"), QIcon(QStringLiteral(":/icons/visibility.svg")));
41 toggleEchoModeAction = passwordLineEdit->addAction(visibilityIcon, QLineEdit::TrailingPosition);
42 toggleEchoModeAction->setObjectName(QStringLiteral("visibilityAction"));
43 toggleEchoModeAction->setVisible(false);
44 toggleEchoModeAction->setToolTip(QObject::tr("Change the visibility of the password", "@info:tooltip"));
45 q->connect(toggleEchoModeAction, &QAction::triggered, q, [this]() {
46 echoModeToggled();
47 });
48 q->connect(passwordLineEdit, &QLineEdit::textChanged, q, [this](const QString &str) {
49 showToggleEchoModeAction(str);
50 });
51}
52
53void KPasswordLineEditPrivate::showToggleEchoModeAction(const QString &text)
54{
55 if (revealPasswordMode != KPassword::RevealMode::Never) {
56 toggleEchoModeAction->setVisible(isToggleEchoModeAvailable && (passwordLineEdit->echoMode() == QLineEdit::Normal || !text.isEmpty()));
57 } else {
58 toggleEchoModeAction->setVisible(false);
59 }
60}
61
62void KPasswordLineEditPrivate::echoModeToggled()
63{
64 if (passwordLineEdit->echoMode() == QLineEdit::Password) {
65 passwordLineEdit->setEchoMode(QLineEdit::Normal);
66 if (passwordIcon.isNull()) {
67 passwordIcon = QIcon::fromTheme(QStringLiteral("hint"), QIcon(QStringLiteral(":/icons/hint.svg")));
68 }
69 toggleEchoModeAction->setIcon(passwordIcon);
70 } else if (passwordLineEdit->echoMode() == QLineEdit::Normal) {
71 if (visibleIcon.isNull()) {
72 visibleIcon = QIcon::fromTheme(QStringLiteral("visibility"), QIcon(QStringLiteral(":/icons/visibility.svg")));
73 }
74 passwordLineEdit->setEchoMode(QLineEdit::Password);
75 toggleEchoModeAction->setIcon(visibleIcon);
76 }
77 Q_EMIT q->echoModeChanged(passwordLineEdit->echoMode());
78}
79
80void KPasswordLineEditPrivate::slotCustomContextMenuRequested(const QPoint &pos)
81{
82 auto popup = std::unique_ptr<QMenu>(passwordLineEdit->createStandardContextMenu());
83 if (popup) {
84 if (toggleEchoModeAction->isVisible()) {
85 popup->addSeparator();
86
87 toggleEchoModeAction->setText(passwordLineEdit->echoMode() == QLineEdit::Password ? QObject::tr("Show Password", "@action:inmenu")
88 : QObject::tr("Hide Password", "@action:inmenu"));
89 popup->addAction(toggleEchoModeAction);
90 }
91 popup->exec(pos);
92 }
93}
94
96 : QWidget(parent)
97 , d(new KPasswordLineEditPrivate(this))
98{
99 QHBoxLayout *mainLayout = new QHBoxLayout(this);
100 mainLayout->setObjectName(QStringLiteral("mainlayout"));
101 mainLayout->setContentsMargins(0, 0, 0, 0);
102 d->passwordLineEdit = new QLineEdit(this);
103 d->passwordLineEdit->setObjectName(QStringLiteral("passwordlineedit"));
104 d->passwordLineEdit->setEchoMode(QLineEdit::Password);
105 d->passwordLineEdit->setContextMenuPolicy(Qt::CustomContextMenu);
106 connect(d->passwordLineEdit, &QWidget::customContextMenuRequested, this, [this](const QPoint &pos) {
107 d->slotCustomContextMenuRequested(pos);
108 });
109 connect(d->passwordLineEdit, &QLineEdit::textChanged, this, &KPasswordLineEdit::passwordChanged);
110 setFocusProxy(d->passwordLineEdit);
111 setFocusPolicy(d->passwordLineEdit->focusPolicy());
112 mainLayout->addWidget(d->passwordLineEdit);
113 d->initialize();
114}
115
117
119{
120 if (d->passwordLineEdit->text() != password) {
121 if (d->revealPasswordMode == KPassword::RevealMode::OnlyNew) {
122 d->isToggleEchoModeAvailable = password.isEmpty();
123 }
124 d->passwordLineEdit->setText(password);
125 }
126}
127
128QString KPasswordLineEdit::password() const
129{
130 return d->passwordLineEdit->text();
131}
132
134{
135 d->passwordLineEdit->clear();
136}
137
139{
140 d->passwordLineEdit->setClearButtonEnabled(clear);
141}
142
144{
145 return d->passwordLineEdit->isClearButtonEnabled();
146}
147
149{
150 d->passwordLineEdit->setEchoMode(mode);
151}
152
153QLineEdit::EchoMode KPasswordLineEdit::echoMode() const
154{
155 return d->passwordLineEdit->echoMode();
156}
157
159{
160 d->passwordLineEdit->setReadOnly(readOnly);
161}
162
164{
165 return d->passwordLineEdit->isReadOnly();
166}
167
169{
170 return d->passwordLineEdit;
171}
172
173#if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(6, 0)
175{
176 d->revealPasswordMode = reveal ? KPassword::RevealMode::OnlyNew : KPassword::RevealMode::Never;
177 d->showToggleEchoModeAction(password());
178}
179
181{
182 return d->revealPasswordMode != KPassword::RevealMode::Never;
183}
184#endif
185
186void KPasswordLineEdit::setRevealPasswordMode(KPassword::RevealMode mode)
187{
188 d->revealPasswordMode = mode;
189 d->showToggleEchoModeAction(password());
190}
191
192KPassword::RevealMode KPasswordLineEdit::revealPasswordMode() const
193{
194 return d->revealPasswordMode;
195}
196
198{
199 return d->toggleEchoModeAction;
200}
201
202#include "moc_kpasswordlineedit.cpp"
A lineedit which allows to display password.
bool isRevealPasswordAvailable() const
Whether the visibility trailing action in the line edit is visible.
KPassword::RevealMode revealPasswordMode() const
Return when the reveal password button is visible.
bool isReadOnly() const
Return whether the line edit is read only.
void setRevealPasswordMode(KPassword::RevealMode revealPasswordMode)
Set when the reveal password button will be visible.
void setReadOnly(bool readOnly)
Set whether the line edit is read only.
QLineEdit * lineEdit() const
Returns the lineedit widget.
void setEchoMode(QLineEdit::EchoMode mode)
Change echo mode (QLineEdit::Password by default)
void setPassword(const QString &password)
Assign password.
bool isClearButtonEnabled() const
Inform if we show or not clear button.
void clear()
Clear text.
QAction * toggleEchoModeAction() const
void echoModeChanged(QLineEdit::EchoMode echoMode)
When we click on visibility icon echo mode is switched between Normal echo mode and Password echo mod...
void setRevealPasswordAvailable(bool reveal)
Whether to show the visibility trailing action in the line edit.
~KPasswordLineEdit() override
Destructs the lineedit password widget.
void setClearButtonEnabled(bool clear)
Show/hide clear button (false by default)
KPasswordLineEdit(QWidget *parent=nullptr)
Constructs a lineedit password widget.
void setIcon(const QIcon &icon)
void setText(const QString &text)
void setToolTip(const QString &tip)
void triggered(bool checked)
void setVisible(bool)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QIcon fromTheme(const QString &name)
bool isNull() const const
void setContentsMargins(const QMargins &margins)
QAction * addAction(const QIcon &icon, ActionPosition position)
QMenu * createStandardContextMenu()
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
void setObjectName(QAnyStringView name)
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isEmpty() const const
CustomContextMenu
void customContextMenuRequested(const QPoint &pos)
void setFocusPolicy(Qt::FocusPolicy policy)
void setFocusProxy(QWidget *w)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:45:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.