QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
SecretLineEdit Class Reference

A QLineEdit for a password that can be shown: whatever is copied out of it is marked as a secret and reported for autoclear. More...

#include <secretlineedit.h>

Inheritance diagram for SecretLineEdit:
Collaboration diagram for SecretLineEdit:

Signals

void copied (const QString &text)
 Selected text was copied out of the line edit.

Public Member Functions

 SecretLineEdit (QWidget *parent=nullptr)
 Constructs the line edit.
auto createSecretContextMenu () -> QMenu *
 The standard context menu, with Copy and Cut going through the secret copy.

Protected Member Functions

void keyPressEvent (QKeyEvent *event) override
 Ctrl+C copies with the hints, Ctrl+X as well before it deletes; after any other key the primary selection gets the marked selection, not QLineEdit's plain-text copy.
void mouseReleaseEvent (QMouseEvent *event) override
 Fills the primary selection with the marked selection on a left-button release, instead of QLineEdit's plain-text copy.
void contextMenuEvent (QContextMenuEvent *event) override
 Shows createSecretContextMenu().

Private Member Functions

void copySelection (QClipboard::Mode mode)
 Puts the selection on the clipboard or in the primary selection as secret-marked MIME data and emits copied(). Does nothing in Password echo mode or without a selection, as QLineEdit::copy() itself.
void cutSelection ()
 Copies the selection through copySelection() and then deletes it. Does nothing when read-only or without a selection, as QLineEdit::cut().
void withoutPlainSelectionCopy (const std::function< void()> &handler)
 Runs a base-class event handler so that it cannot fill the primary selection with plain text: in a read-only field in Normal echo mode the handler runs under Password mode, which makes QLineEdit skip the copy without moving cursor or selection. Editable fields run the handler as is and rely on publishSelection() replacing the plain copy at once.
void publishSelection ()
 Fills the X11 primary selection with the marked selection, on platforms that have one; called after a handled key or a left-button release, where QLineEdit would have copied plain text.

Detailed Description

A QLineEdit for a password that can be shown: whatever is copied out of it is marked as a secret and reported for autoclear.

QLineEdit copies nothing in Password echo mode, but once a "show" button switches it to Normal it copies plain text, without the hints, on four paths: Ctrl+C, the context menu's Copy, and the X11 primary selection after a mouse selection or a key. Unlike QTextEdit it has no createMimeDataFromSelection() to hook. Here Ctrl+C and Copy put buildClipboardMimeData() on the clipboard instead, and the key and mouse handlers keep QLineEdit from filling the primary selection at all (it runs them in Password echo mode) and fill it with the marked data instead, so plain text never gets there, not even for a moment. Each copy emits copied() so the text is cleared with the rest when the autoclear timer runs. Dragging is off, as QLineEdit has it by default.

In an editable field (the password editor) Ctrl+X, Cut, and the keys that delete to the end of the line or the whole line copy the same way before they delete. Password mode would turn off undo, make Ctrl+arrow jump to the ends and change the input method hints there, so the base handlers run as they are and the marked selection replaces QLineEdit's plain one at once, in the same event: X11 and Wayland hand selection data over only when another program asks for it, which reaches QtPass through its event loop, after the replacement.

Definition at line 44 of file secretlineedit.h.

Constructor & Destructor Documentation

◆ SecretLineEdit()

SecretLineEdit::SecretLineEdit ( QWidget * parent = nullptr)
explicit

Constructs the line edit.

Parameters
parentOwner.

Definition at line 17 of file secretlineedit.cpp.

Here is the caller graph for this function:

Member Function Documentation

◆ contextMenuEvent()

void SecretLineEdit::contextMenuEvent ( QContextMenuEvent * event)
overrideprotected

Shows createSecretContextMenu().

Parameters
eventThe context menu event.

Definition at line 75 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copied

void SecretLineEdit::copied ( const QString & text)
signal

Selected text was copied out of the line edit.

Parameters
textThe text, as put on the clipboard or in the selection.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ copySelection()

void SecretLineEdit::copySelection ( QClipboard::Mode mode)
private

Puts the selection on the clipboard or in the primary selection as secret-marked MIME data and emits copied(). Does nothing in Password echo mode or without a selection, as QLineEdit::copy() itself.

Parameters
modeClipboard for Ctrl+C and Copy, Selection for the X11 primary selection.

Definition at line 81 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createSecretContextMenu()

auto SecretLineEdit::createSecretContextMenu ( ) -> QMenu *
nodiscard

The standard context menu, with Copy and Cut going through the secret copy.

Returns
A new menu; the caller owns it.

Definition at line 19 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cutSelection()

void SecretLineEdit::cutSelection ( )
private

Copies the selection through copySelection() and then deletes it. Does nothing when read-only or without a selection, as QLineEdit::cut().

Definition at line 91 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ keyPressEvent()

void SecretLineEdit::keyPressEvent ( QKeyEvent * event)
overrideprotected

Ctrl+C copies with the hints, Ctrl+X as well before it deletes; after any other key the primary selection gets the marked selection, not QLineEdit's plain-text copy.

Parameters
eventThe key event.

Definition at line 36 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mouseReleaseEvent()

void SecretLineEdit::mouseReleaseEvent ( QMouseEvent * event)
overrideprotected

Fills the primary selection with the marked selection on a left-button release, instead of QLineEdit's plain-text copy.

Parameters
eventThe mouse event.

Definition at line 67 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ publishSelection()

void SecretLineEdit::publishSelection ( )
private

Fills the X11 primary selection with the marked selection, on platforms that have one; called after a handled key or a left-button release, where QLineEdit would have copied plain text.

Definition at line 119 of file secretlineedit.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ withoutPlainSelectionCopy()

void SecretLineEdit::withoutPlainSelectionCopy ( const std::function< void()> & handler)
private

Runs a base-class event handler so that it cannot fill the primary selection with plain text: in a read-only field in Normal echo mode the handler runs under Password mode, which makes QLineEdit skip the copy without moving cursor or selection. Editable fields run the handler as is and rely on publishSelection() replacing the plain copy at once.

Parameters
handlerThe base handler call, e.g. QLineEdit::keyPressEvent.

Definition at line 100 of file secretlineedit.cpp.

Here is the caller graph for this function:

The documentation for this class was generated from the following files: