11#include <QApplication>
12#include <QContextMenuEvent>
30class SingleLineValidator :
public QValidator {
32 using QValidator::QValidator;
41 auto validate(QString &input,
int &pos)
const -> State
override {
42 if (std::none_of(input.cbegin(), input.cend(),
46 const QStringView head = QStringView(input).left(pos);
47 pos -=
static_cast<int>(std::count_if(head.cbegin(), head.cend(),
56 const char16_t c = ch.unicode();
57 return c == u
'\n' || c == u
'\r' || c == u
'\u0085' || c == u
'\u2028' ||
69 const QValidator *filter = validator();
70 setValidator(
nullptr);
79 setValidator(
new SingleLineValidator(
this));
83 : QLineEdit(text, parent) {
85 setValidator(
new SingleLineValidator(
this));
89 QMenu *menu = createStandardContextMenu();
90 for (QAction *action : menu->actions()) {
93 if (action->objectName() == QLatin1String(
"edit-copy")) {
94 disconnect(action, &QAction::triggered,
this,
nullptr);
95 connect(action, &QAction::triggered,
this,
97 }
else if (action->objectName() == QLatin1String(
"edit-cut")) {
98 disconnect(action, &QAction::triggered,
this,
nullptr);
99 connect(action, &QAction::triggered,
this, [
this] {
cutSelection(); });
100 }
else if (action->objectName() == QLatin1String(
"edit-paste")) {
101 disconnect(action, &QAction::triggered,
this,
nullptr);
102 connect(action, &QAction::triggered,
this,
110 if (event->matches(QKeySequence::Copy)) {
115 if (event->matches(QKeySequence::Cut)) {
120 if (event->matches(QKeySequence::Paste)) {
123 const bool selection =
124 event->key() == Qt::Key_Insert &&
125 event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) &&
126 QApplication::clipboard()->supportsSelection();
127 pasteOneLine(selection ? QClipboard::Selection : QClipboard::Clipboard);
133 const bool toEnd =
event->matches(QKeySequence::DeleteEndOfLine);
134 if (toEnd || event->matches(QKeySequence::DeleteCompleteLine)) {
136 const int from = toEnd ? cursorPosition() : 0;
137 setSelection(from,
static_cast<int>(text().size()) - from);
146 if (event->isAccepted()) {
153 if (event->button() == Qt::MiddleButton && !isReadOnly() &&
154 QApplication::clipboard()->supportsSelection()) {
161 [
this, event] { QLineEdit::mouseReleaseEvent(event); });
162 if (event->button() == Qt::LeftButton) {
169 menu->setAttribute(Qt::WA_DeleteOnClose);
170 menu->popup(event->globalPos());
175 if (echoMode() != QLineEdit::Normal || !hasSelectedText()) {
178 const QString text = selectedText();
185 if (isReadOnly() || !hasSelectedText()) {
193 const std::function<
void()> &handler) {
202 if (echoMode() != QLineEdit::Normal || !isReadOnly()) {
206 setEchoMode(QLineEdit::Password);
208 setEchoMode(QLineEdit::Normal);
212 if (QApplication::clipboard()->supportsSelection()) {
222 if (text.isEmpty() && !hasSelectedText()) {
238 if (hasSelectedText() || text().isEmpty()) {
241 const int pos = cursorPosition();
242 setCursorPosition(pos > 0 ? pos - 1 : pos + 1);
243 setCursorPosition(pos);
void mouseReleaseEvent(QMouseEvent *event) override
Fills the primary selection with the marked selection on a left-button release, instead of QLineEdit'...
void copied(const QString &text)
Selected text was copied out of the line edit.
void cutSelection()
Copies the selection through copySelection() and then deletes it. Does nothing when read-only or with...
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 re...
static auto withoutLineBreaks(QString text) -> QString
text with every line break (see isLineBreak()) removed: what a one-line field keeps of a paste.
auto createSecretContextMenu() -> QMenu *
The standard context menu, with Copy and Cut going through the secret copy.
void publishSelection()
Fills the X11 primary selection with the marked selection, on platforms that have one; called after a...
SecretLineEdit(QWidget *parent=nullptr)
Constructs the line edit.
void contextMenuEvent(QContextMenuEvent *event) override
Shows createSecretContextMenu().
static constexpr int kMaxLength
Most characters the line edit holds (1 MiB of them): its maxLength in place of QLineEdit's 32767.
void setStoredText(const QString &text)
Shows a value read from the entry exactly as it is stored.
static auto isLineBreak(QChar ch) -> bool
Whether ch breaks a line: LF, CR, NEL (U+0085), or the Unicode line and paragraph separators (U+2028,...
void markUndoBoundary()
Ends the current undo step, so that the next edit starts its own, as QLineEdit::paste() does around a...
void pasteOneLine(QClipboard::Mode mode)
Pastes the clipboard or the primary selection with its line breaks removed, as one undoable edit....
void copySelection(QClipboard::Mode mode)
Puts the selection on the clipboard or in the primary selection as secret-marked MIME data and emits ...
void keyPressEvent(QKeyEvent *event) override
Ctrl+C copies with the hints, Ctrl+X as well before it deletes; after any other key the primary selec...
auto buildClipboardMimeData(const QString &text) -> QMimeData *
Build clipboard MIME data with platform-specific security hints.
ClipboardManager, which puts secrets on the clipboard and removes them again, and the MIME data that ...
QLineEdit whose copies, cuts and primary-selection fills carry the secret clipboard hints and are rep...