QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
fieldlabel.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#include "fieldlabel.h"
9#include "secretlineedit.h"
10
11#include "util.h"
12#include <QContextMenuEvent>
13#include <QFormLayout>
14#include <QKeyEvent>
15#include <QLineEdit>
16#include <QMenu>
17
18FieldLabel::FieldLabel(const QString &name, QWidget *parent) : QLabel(parent) {
19 // A field name comes from the entry, which a shared store's other writers
20 // control: as rich text, <img src="/dev/zero"> would have Qt read that
21 // file without end. Plain text before the name is set.
22 setTextFormat(Qt::PlainText);
24 setToolTip(tr("Double-click to rename this field"));
25}
26
27void FieldLabel::setName(const QString &name) {
28 m_name = name;
29 // Bounded: the label sizes the dialog, and every & in it is a mnemonic.
30 setText(Util::shownName(name));
31}
32
33void FieldLabel::mouseDoubleClickEvent(QMouseEvent *event) {
34 if (event->button() == Qt::LeftButton) {
35 startEdit();
36 return;
37 }
38 QLabel::mouseDoubleClickEvent(event);
39}
40
41void FieldLabel::contextMenuEvent(QContextMenuEvent *event) {
42 QMenu menu(this);
43 menu.addAction(tr("Rename field…"), this, &FieldLabel::startEdit);
44 menu.addAction(tr("Remove field"), this, &FieldLabel::removeRequested);
45 menu.exec(event->globalPos());
46}
47
49
51 if (m_editor != nullptr) {
52 m_editor->setFocus();
53 return;
54 }
55 // A SecretLineEdit: in the "every key: value line" mode the name can be the
56 // sensitive part, so a copy out of the editor is marked and tracked as any
57 // copy out of a field.
58 auto *editor = new SecretLineEdit(m_name, parentWidget());
59 connect(editor, &SecretLineEdit::copied, this, &FieldLabel::copied);
60 m_editor = editor;
61 m_editor->setObjectName(QStringLiteral("fieldNameEditor"));
62 m_editor->installEventFilter(this);
63 connect(m_editor, &QLineEdit::editingFinished, this,
64 [this] { finishEdit(true); });
65 // Take the label's cell in the form, so the editor gets the label column
66 // (which widens for it) instead of floating over the value next to it.
67 auto *form = qobject_cast<QFormLayout *>(parentWidget()->layout());
68 int row = -1;
69 QFormLayout::ItemRole role{};
70 if (form != nullptr) {
71 form->getWidgetPosition(this, &row, &role);
72 }
73 if (row >= 0) {
74 form->removeWidget(this);
75 hide();
76 form->setWidget(row, role, m_editor);
77 } else {
78 m_editor->setGeometry(geometry());
79 }
80 // In the tab chain just before this field's value. Created last, the editor
81 // was the last tab stop, and Tab out of it wrapped to the password, all
82 // selected: the next key replaced it.
83 if (QWidget *value = buddy()) {
84 QWidget *before = value->previousInFocusChain();
85 if (before != nullptr && before != m_editor) {
86 QWidget::setTabOrder(before, m_editor);
87 }
88 QWidget::setTabOrder(m_editor, value);
89 }
90 m_editor->show();
91 m_editor->selectAll();
92 m_editor->setFocus();
93}
94
95auto FieldLabel::eventFilter(QObject *watched, QEvent *event) -> bool {
96 // The event type first: the editor also sends events from its destructor,
97 // when it is no QLineEdit any more and may not be read as one, which
98 // comparing with m_editor does.
99 if (event->type() != QEvent::KeyPress || watched != m_editor) {
100 return QLabel::eventFilter(watched, event);
101 }
102 switch (static_cast<QKeyEvent *>(event)->key()) {
103 case Qt::Key_Escape:
104 finishEdit(false);
105 return true;
106 case Qt::Key_Return:
107 case Qt::Key_Enter:
108 // Enter confirms the name only; left alone it would also reach the
109 // dialog's default button and save and close the whole entry.
110 finishEdit(true);
111 return true;
112 default:
113 return QLabel::eventFilter(watched, event);
114 }
115}
116
117void FieldLabel::finishEdit(bool commit) {
118 if (m_editor == nullptr) {
119 return;
120 }
121 // editingFinished fires again when the editor loses focus on hide; take
122 // the pointer first so the second call finds nothing to do.
123 QLineEdit *editor = m_editor;
124 m_editor = nullptr;
125 // A colon would start the value early when the line is written back, and
126 // a line break, which QLineEdit keeps from a paste, would end the line.
127 const QString name =
128 SecretLineEdit::withoutLineBreaks(editor->text().remove(u':')).trimmed();
129 auto *form = qobject_cast<QFormLayout *>(parentWidget()->layout());
130 int row = -1;
131 QFormLayout::ItemRole role{};
132 if (form != nullptr) {
133 form->getWidgetPosition(editor, &row, &role);
134 }
135 // Hidden with the focus, the editor would hand it on as Tab does, which
136 // could land on the password with all of it selected. The focus goes to
137 // this field's value instead, without selecting it.
138 if (editor->hasFocus()) {
139 if (QWidget *value = buddy()) {
140 value->setFocus(Qt::OtherFocusReason);
141 }
142 }
143 editor->hide();
144 if (row >= 0) {
145 form->removeWidget(editor);
146 form->setWidget(row, role, this);
147 show();
148 }
149 editor->deleteLater();
150 if (commit && !name.isEmpty() && name != m_name) {
151 // A copy: a receiver that accepts the name calls setName(), and later
152 // receivers would read the new name as the old one.
153 const QString from = m_name;
154 emit renamed(from, name);
155 }
156}
FieldLabel(const QString &name, QWidget *parent=nullptr)
Label for the field called name.
void contextMenuEvent(QContextMenuEvent *event) override
Menu with Rename field and Remove field.
QPointer< QLineEdit > m_editor
The line edit open over the label while a rename is in progress; null otherwise. A sibling in the par...
Definition fieldlabel.h:110
void copied(const QString &text)
Text was copied out of the name editor, marked as a secret.
void setName(const QString &name)
Name the field name and show it.
void removeRequested()
The user chose "Remove field".
void finishEdit(bool commit)
Close the editor, putting the label back in its place.
~FieldLabel() override
Takes an open editor with it: the editor is a sibling, not a child, so it would otherwise stay behind...
void renamed(const QString &from, const QString &to)
The user typed a different name and confirmed it.
void mouseDoubleClickEvent(QMouseEvent *event) override
A left double-click starts editing the name.
void startEdit()
Open the in-place editor over the label.
auto eventFilter(QObject *watched, QEvent *event) -> bool override
Escape in the editor cancels the edit.
auto name() const -> QString
The field's key, whole; the label shows it through Util::shownName().
Definition fieldlabel.h:42
QString m_name
The field's key, whole (the text shown may be cut).
Definition fieldlabel.h:112
void copied(const QString &text)
Selected text was copied out of the line edit.
static auto withoutLineBreaks(QString text) -> QString
text with every line break (see isLineBreak()) removed: what a one-line field keeps of a paste.
SecretLineEdit(QWidget *parent=nullptr)
Constructs the line edit.
static auto shownName(const QString &name, qsizetype max=kMaxShownName) -> QString
A name or path from the store, as a label or list shows it: one line, at most max characters.
Definition util.cpp:1000
FieldLabel: the in-place renamable, removable name label of a key: value field in PasswordDialog.
QLineEdit whose copies, cuts and primary-selection fills carry the secret clipboard hints and are rep...
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...