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#ifndef KPASSWORDLINEEDIT_H
9#define KPASSWORDLINEEDIT_H
10
11#include <KPassword>
12#include <QLineEdit>
13#include <QWidget>
14#include <kwidgetsaddons_export.h>
15#include <memory>
16class QAction;
17
18/**
19 * @class KPasswordLineEdit kpasswordlineedit.h KPasswordLineEdit
20 *
21 * A lineedit which allows to display password
22 *
23 * \section usage Usage Example
24 *
25 * Get password
26 *
27 * \code
28 * KPasswordLineEdit *passwordLineEdit = new KPasswordLineEdit(parent);
29 * QString password = passwordLineEdit->password();
30 * \endcode
31 *
32 * @author Laurent Montel <montel@kde.org>
33 * @since 5.37
34 */
35class KWIDGETSADDONS_EXPORT KPasswordLineEdit : public QWidget
36{
37 Q_OBJECT
38 Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
39 Q_PROPERTY(bool clearButtonEnabled READ isClearButtonEnabled WRITE setClearButtonEnabled)
40 Q_PROPERTY(QLineEdit::EchoMode echoMode READ echoMode WRITE setEchoMode NOTIFY echoModeChanged)
41public:
42 /**
43 * Constructs a lineedit password widget.
44 * @since 5.37
45 *
46 * @param parent Passed to lower level constructor.
47 */
48 explicit KPasswordLineEdit(QWidget *parent = nullptr);
49 /**
50 * Destructs the lineedit password widget.
51 */
52 ~KPasswordLineEdit() override;
53
54 /**
55 * Assign password
56 */
57 void setPassword(const QString &password);
58
59 /**
60 * Returns the password entered.
61 */
62 QString password() const;
63
64 /**
65 * Clear text
66 */
67 void clear();
68
69 /**
70 * Show/hide clear button (false by default)
71 */
72 void setClearButtonEnabled(bool clear);
73
74 /**
75 * Inform if we show or not clear button
76 */
77 bool isClearButtonEnabled() const;
78
79 /**
80 * Change echo mode (QLineEdit::Password by default)
81 */
82 void setEchoMode(QLineEdit::EchoMode mode);
83
84 /**
85 * Return echo mode
86 */
87 QLineEdit::EchoMode echoMode() const;
88
89 /**
90 * Set whether the line edit is read only.
91 * @since 6.0
92 */
93 void setReadOnly(bool readOnly);
94
95 /**
96 * Return whether the line edit is read only.
97 * @since 6.0
98 */
99 bool isReadOnly() const;
100
101 /**
102 * Return when the reveal password button is visible.
103 * @since 6.0
104 */
105 KPassword::RevealMode revealPasswordMode() const;
106
107 /**
108 * Set when the reveal password button will be visible.
109 *
110 * The default is RevealPasswordMode::OnlyNew and the reveal password button will
111 * only be visible when entering a new password.
112 *
113 * This can be used to honor the lineedit_reveal_password kiosk key, for example:
114 *
115 * @code{.cpp}
116 * if (KAuthorized::authorize(QStringLiteral("lineedit_reveal_password"))) {
117 * passwordLineEdit.setRevealPasswordMode(KPasswordLineEdit::RevealPasswordMode::OnlyNew);
118 * } else {
119 * passwordLineEdit.setRevealPasswordMode(KPasswordLineEdit::RevealPasswordMode::Never);
120 * }
121 * @endcode
122 * @since 6.0
123 */
124 void setRevealPasswordMode(KPassword::RevealMode revealPasswordMode);
125
126#if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(6, 0)
127 /**
128 * Whether to show the visibility trailing action in the line edit.
129 * Default is true. This can be used to honor the lineedit_reveal_password
130 * kiosk key, for example:
131 * \code
132 * passwordLineEdit.setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
133 * \endcode
134 */
135 [[deprecated("Use setRevealPasswordMode")]] void setRevealPasswordAvailable(bool reveal);
136
137 /**
138 * Whether the visibility trailing action in the line edit is visible.
139 */
140 [[deprecated("Use revealPasswordMode instead.")]] bool isRevealPasswordAvailable() const;
141#endif
142
143 /**
144 * @internal
145 * Returns the QAction
146 */
147 QAction *toggleEchoModeAction() const;
148
149 /**
150 * Returns the lineedit widget.
151 */
152 QLineEdit *lineEdit() const;
153
154Q_SIGNALS:
155 /**
156 * When we click on visibility icon echo mode is switched between Normal echo mode and Password echo mode
157 */
158 void echoModeChanged(QLineEdit::EchoMode echoMode);
159 void passwordChanged(const QString &password);
160
161private:
162 std::unique_ptr<class KPasswordLineEditPrivate> const d;
163};
164
165#endif
166

source code of kwidgetsaddons/src/kpasswordlineedit.h