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 38 : 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 : * @brief Takes an open editor with it: the editor is a sibling, not a
38 : * child, so it would otherwise stay behind when the field is removed
39 : * while its name is being edited.
40 : */
41 : ~FieldLabel() override;
42 :
43 : public slots:
44 : /**
45 : * @brief Open the in-place editor over the label.
46 : */
47 : void startEdit();
48 :
49 : signals:
50 : /**
51 : * @brief The user typed a different name and confirmed it.
52 : * @param from the name before, still shown until the receiver accepts
53 : * @param to the new name, trimmed, never empty, without a colon
54 : */
55 : void renamed(const QString &from, const QString &to);
56 : /**
57 : * @brief The user chose "Remove field".
58 : */
59 : void removeRequested();
60 :
61 : protected:
62 : /**
63 : * @brief A left double-click starts editing the name.
64 : * @param event the mouse event
65 : */
66 : void mouseDoubleClickEvent(QMouseEvent *event) override;
67 : /**
68 : * @brief Menu with Rename field and Remove field.
69 : * @param event the context menu event
70 : */
71 : void contextMenuEvent(QContextMenuEvent *event) override;
72 : /**
73 : * @brief Escape in the editor cancels the edit.
74 : * @param watched the editor
75 : * @param event the event to inspect
76 : * @return true when the event was consumed
77 : */
78 : auto eventFilter(QObject *watched, QEvent *event) -> bool override;
79 :
80 : private:
81 : /**
82 : * @brief Close the editor, putting the label back in its place.
83 : * @param commit emit renamed() for a changed, non-empty name
84 : */
85 : void finishEdit(bool commit);
86 :
87 : /**
88 : * @brief The line edit open over the label while a rename is in progress;
89 : * null otherwise. A sibling in the parent's form layout rather than
90 : * a child, so the destructor deletes it explicitly.
91 : */
92 : QPointer<QLineEdit> m_editor;
93 : };
94 :
95 : #endif // FIELDLABEL_H_
|