File indexing completed on 2024-04-28 03:59:11

0001 /*
0002     SPDX-FileCopyrightText: 2017 Montel Laurent <montel@kde.org>
0003     SPDX-FileCopyrightText: 2015 Elvis Angelaccio <elvis.angelaccio@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef KPASSWORDLINEEDIT_H
0009 #define KPASSWORDLINEEDIT_H
0010 
0011 #include <KPassword>
0012 #include <QLineEdit>
0013 #include <QWidget>
0014 #include <kwidgetsaddons_export.h>
0015 #include <memory>
0016 class QAction;
0017 
0018 /**
0019  * @class KPasswordLineEdit kpasswordlineedit.h KPasswordLineEdit
0020  *
0021  * A lineedit which allows to display password
0022  *
0023  * \section usage Usage Example
0024  *
0025  * Get password
0026  *
0027  * \code
0028  * KPasswordLineEdit *passwordLineEdit = new KPasswordLineEdit(parent);
0029  * QString password = passwordLineEdit->password();
0030  * \endcode
0031  *
0032  * @author Laurent Montel <montel@kde.org>
0033  * @since 5.37
0034  */
0035 class KWIDGETSADDONS_EXPORT KPasswordLineEdit : public QWidget
0036 {
0037     Q_OBJECT
0038     Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
0039     Q_PROPERTY(bool clearButtonEnabled READ isClearButtonEnabled WRITE setClearButtonEnabled)
0040     Q_PROPERTY(QLineEdit::EchoMode echoMode READ echoMode WRITE setEchoMode NOTIFY echoModeChanged)
0041 public:
0042     /**
0043      * Constructs a lineedit password widget.
0044      * @since 5.37
0045      *
0046      * @param parent Passed to lower level constructor.
0047      */
0048     explicit KPasswordLineEdit(QWidget *parent = nullptr);
0049     /**
0050      * Destructs the lineedit password widget.
0051      */
0052     ~KPasswordLineEdit() override;
0053 
0054     /**
0055      * Assign password
0056      */
0057     void setPassword(const QString &password);
0058 
0059     /**
0060      * Returns the password entered.
0061      */
0062     QString password() const;
0063 
0064     /**
0065      * Clear text
0066      */
0067     void clear();
0068 
0069     /**
0070      * Show/hide clear button (false by default)
0071      */
0072     void setClearButtonEnabled(bool clear);
0073 
0074     /**
0075      * Inform if we show or not clear button
0076      */
0077     bool isClearButtonEnabled() const;
0078 
0079     /**
0080      * Change echo mode (QLineEdit::Password by default)
0081      */
0082     void setEchoMode(QLineEdit::EchoMode mode);
0083 
0084     /**
0085      * Return echo mode
0086      */
0087     QLineEdit::EchoMode echoMode() const;
0088 
0089     /**
0090      * Set whether the line edit is read only.
0091      * @since 6.0
0092      */
0093     void setReadOnly(bool readOnly);
0094 
0095     /**
0096      * Return whether the line edit is read only.
0097      * @since 6.0
0098      */
0099     bool isReadOnly() const;
0100 
0101     /**
0102      * Return when the reveal password button is visible.
0103      * @since 6.0
0104      */
0105     KPassword::RevealMode revealPasswordMode() const;
0106 
0107     /**
0108      * Set when the reveal password button will be visible.
0109      *
0110      * The default is RevealPasswordMode::OnlyNew and the reveal password button will
0111      * only be visible when entering a new password.
0112      *
0113      * This can be used to honor the lineedit_reveal_password kiosk key, for example:
0114      *
0115      * @code{.cpp}
0116      * if (KAuthorized::authorize(QStringLiteral("lineedit_reveal_password"))) {
0117      *     passwordLineEdit.setRevealPasswordMode(KPasswordLineEdit::RevealPasswordMode::OnlyNew);
0118      * } else {
0119      *     passwordLineEdit.setRevealPasswordMode(KPasswordLineEdit::RevealPasswordMode::Never);
0120      * }
0121      * @endcode
0122      * @since 6.0
0123      */
0124     void setRevealPasswordMode(KPassword::RevealMode revealPasswordMode);
0125 
0126 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(6, 0)
0127     /**
0128      * Whether to show the visibility trailing action in the line edit.
0129      * Default is true. This can be used to honor the lineedit_reveal_password
0130      * kiosk key, for example:
0131      * \code
0132      * passwordLineEdit.setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0133      * \endcode
0134      */
0135     [[deprecated("Use setRevealPasswordMode")]] void setRevealPasswordAvailable(bool reveal);
0136 
0137     /**
0138      * Whether the visibility trailing action in the line edit is visible.
0139      */
0140     [[deprecated("Use revealPasswordMode instead.")]] bool isRevealPasswordAvailable() const;
0141 #endif
0142 
0143     /**
0144      * @internal
0145      * Returns the QAction
0146      */
0147     QAction *toggleEchoModeAction() const;
0148 
0149     /**
0150      * Returns the lineedit widget.
0151      */
0152     QLineEdit *lineEdit() const;
0153 
0154 Q_SIGNALS:
0155     /**
0156      * When we click on visibility icon echo mode is switched between Normal echo mode and Password echo mode
0157      */
0158     void echoModeChanged(QLineEdit::EchoMode echoMode);
0159     void passwordChanged(const QString &password);
0160 
0161 private:
0162     std::unique_ptr<class KPasswordLineEditPrivate> const d;
0163 };
0164 
0165 #endif