QtPass Developer documentation
QtPass test coverage
Current view: top level - src/fieldlabel.cpp (source / functions) Coverage Total Hit
Test: QtPass Lines: 100.0 % 81 81
Test Date: 2026-12-03 23:34:20 Functions: 100.0 % 8 8
Branches: 87.2 % 39 34

             Branch data     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 Implements FieldLabel; startEdit() and finishEdit() swap the label
       6                 :             :  *        and its editor in and out of the parent's QFormLayout cell.
       7                 :             :  */
       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                 :             : 
      18                 :          55 : FieldLabel::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                 :          55 :   setTextFormat(Qt::PlainText);
      23                 :          55 :   setName(name);
      24                 :          55 :   setToolTip(tr("Double-click to rename this field"));
      25                 :          55 : }
      26                 :             : 
      27                 :          65 : void FieldLabel::setName(const QString &name) {
      28                 :          65 :   m_name = name;
      29                 :             :   // Bounded: the label sizes the dialog, and every & in it is a mnemonic.
      30                 :          65 :   setText(Util::shownName(name));
      31                 :          65 : }
      32                 :             : 
      33                 :           2 : void FieldLabel::mouseDoubleClickEvent(QMouseEvent *event) {
      34         [ +  + ]:           2 :   if (event->button() == Qt::LeftButton) {
      35                 :           1 :     startEdit();
      36                 :           1 :     return;
      37                 :             :   }
      38                 :           1 :   QLabel::mouseDoubleClickEvent(event);
      39                 :             : }
      40                 :             : 
      41                 :           2 : void FieldLabel::contextMenuEvent(QContextMenuEvent *event) {
      42                 :           2 :   QMenu menu(this);
      43                 :           2 :   menu.addAction(tr("Rename field…"), this, &FieldLabel::startEdit);
      44                 :           4 :   menu.addAction(tr("Remove field"), this, &FieldLabel::removeRequested);
      45                 :           2 :   menu.exec(event->globalPos());
      46                 :           2 : }
      47                 :             : 
      48                 :         165 : FieldLabel::~FieldLabel() { delete m_editor.data(); }
      49                 :             : 
      50                 :          18 : void FieldLabel::startEdit() {
      51         [ +  + ]:          18 :   if (m_editor != nullptr) {
      52                 :           1 :     m_editor->setFocus();
      53                 :           1 :     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                 :          17 :   auto *editor = new SecretLineEdit(m_name, parentWidget());
      59                 :          17 :   connect(editor, &SecretLineEdit::copied, this, &FieldLabel::copied);
      60                 :             :   m_editor = editor;
      61                 :          34 :   m_editor->setObjectName(QStringLiteral("fieldNameEditor"));
      62                 :          17 :   m_editor->installEventFilter(this);
      63                 :          17 :   connect(m_editor, &QLineEdit::editingFinished, this,
      64                 :          46 :           [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                 :          17 :   auto *form = qobject_cast<QFormLayout *>(parentWidget()->layout());
      68                 :          17 :   int row = -1;
      69                 :          17 :   QFormLayout::ItemRole role{};
      70         [ +  + ]:          17 :   if (form != nullptr) {
      71                 :           1 :     form->getWidgetPosition(this, &row, &role);
      72                 :             :   }
      73         [ +  + ]:          17 :   if (row >= 0) {
      74                 :           1 :     form->removeWidget(this);
      75                 :           1 :     hide();
      76                 :           1 :     form->setWidget(row, role, m_editor);
      77                 :             :   } else {
      78                 :          16 :     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         [ +  + ]:          17 :   if (QWidget *value = buddy()) {
      84                 :          16 :     QWidget *before = value->previousInFocusChain();
      85   [ +  -  +  - ]:          32 :     if (before != nullptr && before != m_editor) {
      86                 :          16 :       QWidget::setTabOrder(before, m_editor);
      87                 :             :     }
      88                 :          16 :     QWidget::setTabOrder(m_editor, value);
      89                 :             :   }
      90                 :          17 :   m_editor->show();
      91                 :          17 :   m_editor->selectAll();
      92                 :          17 :   m_editor->setFocus();
      93                 :             : }
      94                 :             : 
      95                 :         283 : auto 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   [ +  +  -  + ]:         302 :   if (event->type() != QEvent::KeyPress || watched != m_editor) {
     100                 :         264 :     return QLabel::eventFilter(watched, event);
     101                 :             :   }
     102      [ +  +  + ]:          19 :   switch (static_cast<QKeyEvent *>(event)->key()) {
     103                 :           2 :   case Qt::Key_Escape:
     104                 :           2 :     finishEdit(false);
     105                 :           2 :     return true;
     106                 :           9 :   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                 :           9 :     finishEdit(true);
     111                 :           9 :     return true;
     112                 :           8 :   default:
     113                 :           8 :     return QLabel::eventFilter(watched, event);
     114                 :             :   }
     115                 :             : }
     116                 :             : 
     117                 :          23 : void FieldLabel::finishEdit(bool commit) {
     118         [ +  + ]:          23 :   if (m_editor == nullptr) {
     119                 :           9 :     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                 :          42 :       SecretLineEdit::withoutLineBreaks(editor->text().remove(u':')).trimmed();
     129                 :          14 :   auto *form = qobject_cast<QFormLayout *>(parentWidget()->layout());
     130                 :          14 :   int row = -1;
     131                 :          14 :   QFormLayout::ItemRole role{};
     132         [ +  + ]:          14 :   if (form != nullptr) {
     133                 :           1 :     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      [ +  +  + ]:          14 :   if (editor->hasFocus()) {
     139      [ +  +  + ]:           9 :     if (QWidget *value = buddy()) {
     140                 :           8 :       value->setFocus(Qt::OtherFocusReason);
     141                 :             :     }
     142                 :             :   }
     143                 :          14 :   editor->hide();
     144         [ +  + ]:          14 :   if (row >= 0) {
     145                 :           1 :     form->removeWidget(editor);
     146                 :           1 :     form->setWidget(row, role, this);
     147                 :           1 :     show();
     148                 :             :   }
     149                 :          14 :   editor->deleteLater();
     150   [ +  +  +  -  :          14 :   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                 :          12 :     emit renamed(from, name);
     155                 :             :   }
     156                 :             : }
        

Generated by: LCOV version 2.0-1