Line data Source code
1 : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief FieldLabel: the in-place renamable, removable name label of a
6 : * `key: value` field in PasswordDialog.
7 : */
8 : #ifndef FIELDLABEL_H_
9 : #define FIELDLABEL_H_
10 :
11 : #include <QLabel>
12 : #include <QPointer>
13 :
14 : class QLineEdit;
15 :
16 : /**
17 : * @class FieldLabel
18 : * @ingroup secrets
19 : * @brief The name of a `key: value` field in PasswordDialog, editable in
20 : * place: double-click or the context menu renames it, the context
21 : * menu can also remove the field (#132).
22 : *
23 : * The label only reports what the user asked for; PasswordDialog owns the
24 : * line edit the field belongs to and decides whether a new name is usable.
25 : */
26 : class FieldLabel : public QLabel {
27 59 : Q_OBJECT
28 :
29 : public:
30 : /**
31 : * @brief Label for the field called @p name.
32 : * @param name the field's key, as shown and as written back
33 : * @param parent owning widget
34 : */
35 : explicit FieldLabel(const QString &name, QWidget *parent = nullptr);
36 :
37 : /**
38 : * @brief The field's key, whole; the label shows it through
39 : * Util::shownName().
40 : * @return The key.
41 : */
42 : [[nodiscard]] auto name() const -> QString { return m_name; }
43 : /**
44 : * @brief Name the field @p name and show it.
45 : * @param name The field's new key.
46 : */
47 : void setName(const QString &name);
48 : /**
49 : * @brief Takes an open editor with it: the editor is a sibling, not a
50 : * child, so it would otherwise stay behind when the field is removed
51 : * while its name is being edited.
52 : */
53 : ~FieldLabel() override;
54 :
55 : public slots:
56 : /**
57 : * @brief Open the in-place editor over the label.
58 : */
59 : void startEdit();
60 :
61 : signals:
62 : /**
63 : * @brief The user typed a different name and confirmed it.
64 : * @param from the name before, still shown until the receiver accepts
65 : * @param to the new name, trimmed, never empty, without a colon or a line
66 : * break
67 : */
68 : void renamed(const QString &from, const QString &to);
69 : /**
70 : * @brief The user chose "Remove field".
71 : */
72 : void removeRequested();
73 : /**
74 : * @brief Text was copied out of the name editor, marked as a secret.
75 : * @param text The text copied.
76 : */
77 : void copied(const QString &text);
78 :
79 : protected:
80 : /**
81 : * @brief A left double-click starts editing the name.
82 : * @param event the mouse event
83 : */
84 : void mouseDoubleClickEvent(QMouseEvent *event) override;
85 : /**
86 : * @brief Menu with Rename field and Remove field.
87 : * @param event the context menu event
88 : */
89 : void contextMenuEvent(QContextMenuEvent *event) override;
90 : /**
91 : * @brief Escape in the editor cancels the edit.
92 : * @param watched the editor
93 : * @param event the event to inspect
94 : * @return true when the event was consumed
95 : */
96 : auto eventFilter(QObject *watched, QEvent *event) -> bool override;
97 :
98 : private:
99 : /**
100 : * @brief Close the editor, putting the label back in its place.
101 : * @param commit emit renamed() for a changed, non-empty name
102 : */
103 : void finishEdit(bool commit);
104 :
105 : /**
106 : * @brief The line edit open over the label while a rename is in progress;
107 : * null otherwise. A sibling in the parent's form layout rather than
108 : * a child, so the destructor deletes it explicitly.
109 : */
110 : QPointer<QLineEdit> m_editor;
111 : /** @brief The field's key, whole (the text shown may be cut). */
112 : QString m_name;
113 : };
114 :
115 : #endif // FIELDLABEL_H_
|