Line data Source code
1 : // SPDX-FileCopyrightText: 2020 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief Eye-icon button that toggles a QLineEdit between password and
6 : * plain-text echo mode.
7 : */
8 : #ifndef SRC_QPUSHBUTTONSHOWPASSWORD_H_
9 : #define SRC_QPUSHBUTTONSHOWPASSWORD_H_
10 :
11 : #include <QLineEdit>
12 : #include <QPushButton>
13 :
14 : class QWidget;
15 : /**
16 : * @class QPushButtonShowPassword
17 : * @ingroup secrets
18 : * @brief QPushButton that toggles visibility of an associated QLineEdit.
19 : *
20 : * The button is icon-only and bound to one QLineEdit for its lifetime. Each
21 : * click flips that line edit between `QLineEdit::Password` and
22 : * `QLineEdit::Normal` echo mode and swaps the icon, tooltip and accessible
23 : * name between "Show password" and "Hide password" to match. It is driven
24 : * entirely by its own `QPushButton::clicked` signal; the owner only has to
25 : * create it next to the line edit (`PasswordDisplayPanel` does this for the
26 : * masked password row of a decrypted entry).
27 : */
28 : class QPushButtonShowPassword : public QPushButton {
29 50 : Q_OBJECT
30 :
31 : public:
32 : /**
33 : * @brief Construct a show-password button linked to the given QLineEdit.
34 : * @param line QLineEdit whose contents are shown or hidden by this button.
35 : * @param parent Optional parent widget.
36 : */
37 : explicit QPushButtonShowPassword(QLineEdit *line, QWidget *parent = nullptr);
38 :
39 : signals:
40 : /**
41 : * @brief Emitted on button activation with the current text of the line edit.
42 : * @param text Current text of the associated QLineEdit.
43 : */
44 : void clicked(const QString &text);
45 :
46 : private slots:
47 : /**
48 : * @brief Connected to `QPushButton::clicked(bool)`; flips the echo mode of
49 : * @ref line and updates icon, tooltip and accessible name accordingly.
50 : * @param checked Unused; the state is read from the line edit.
51 : */
52 : void buttonClicked(bool checked);
53 :
54 : private:
55 : /** @brief `view-visible` icon shown while the password is hidden. */
56 : QIcon iconEdit;
57 : /** @brief `view-hidden` icon shown while the password is revealed. */
58 : QIcon iconEditPushed;
59 : /** @brief The line edit being toggled; owned by the caller, must outlive
60 : * this button. */
61 : QLineEdit *line;
62 : };
63 :
64 : #endif // SRC_QPUSHBUTTONSHOWPASSWORD_H_
|