From 24dc07897beec4eadcdc3daaaec6575d4a7a2052 Mon Sep 17 00:00:00 2001 From: Carlo Teubner Date: Fri, 7 Jun 2024 10:02:03 +0100 Subject: [PATCH] Search entry: respect shortcut config on Copy key If the system Copy key sequence (i.e. Ctrl+C or Cmd+C) is pressed while inside the search entry without any text being selected, previously we would copy the currently selected entry's password. This made sense when keyboard shortcuts were fixed. Now that they are configurable, change it to re-route the event to the main window, which can then take the appropriate action (i.e. Ctrl+C might be bound to some other action). --- src/gui/SearchWidget.cpp | 19 +++++++++++++++---- src/gui/SearchWidget.h | 1 - tests/gui/TestGui.cpp | 10 +++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/gui/SearchWidget.cpp b/src/gui/SearchWidget.cpp index 5f247ee101..36a3483653 100644 --- a/src/gui/SearchWidget.cpp +++ b/src/gui/SearchWidget.cpp @@ -16,6 +16,7 @@ */ #include "SearchWidget.h" +#include "gui/MainWindow.h" #include "ui_SearchHelpWidget.h" #include "ui_SearchWidget.h" @@ -94,10 +95,21 @@ bool SearchWidget::eventFilter(QObject* obj, QEvent* event) emit escapePressed(); return true; } else if (keyEvent->matches(QKeySequence::Copy)) { - // If Control+C is pressed in the search edit when no text - // is selected, copy the password of the current entry. + // If the system Copy shortcut (typically Ctrl+C or Cmd+C) is pressed + // in the search edit when no text is selected, route the event to the + // main window. With the default shorcut configuration, this will copy + // the password of the current entry to the clipboard. if (!m_ui->searchEdit->hasSelectedText()) { - emit copyPressed(); + // Prevent infinite recursion, in case the main window ends up + // sending this event back to us. This hasn't actually been observed + // in practice and is just a precaution. + static bool sendingCopyShortcutEvent = false; + if (sendingCopyShortcutEvent) { + return true; + } + sendingCopyShortcutEvent = true; + QCoreApplication::sendEvent(getMainWindow(), event); + sendingCopyShortcutEvent = false; return true; } } else if (keyEvent->matches(QKeySequence::MoveToNextLine)) { @@ -135,7 +147,6 @@ void SearchWidget::connectSignals(SignalMultiplexer& mx) mx.connect(this, SIGNAL(saveSearch(QString)), SLOT(saveSearch(QString))); mx.connect(this, SIGNAL(caseSensitiveChanged(bool)), SLOT(setSearchCaseSensitive(bool))); mx.connect(this, SIGNAL(limitGroupChanged(bool)), SLOT(setSearchLimitGroup(bool))); - mx.connect(this, SIGNAL(copyPressed()), SLOT(copyPassword())); mx.connect(this, SIGNAL(downPressed()), SLOT(focusOnEntries())); mx.connect(SIGNAL(requestSearch(QString)), m_ui->searchEdit, SLOT(setText(QString))); mx.connect(SIGNAL(clearSearch()), this, SLOT(clearSearch())); diff --git a/src/gui/SearchWidget.h b/src/gui/SearchWidget.h index 55edad5833..8d2c63394d 100644 --- a/src/gui/SearchWidget.h +++ b/src/gui/SearchWidget.h @@ -57,7 +57,6 @@ class SearchWidget : public QWidget void caseSensitiveChanged(bool state); void limitGroupChanged(bool state); void escapePressed(); - void copyPressed(); void downPressed(); void enterPressed(); void lostFocus(); diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index 03cb5e347d..8ee6c56580 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -1129,15 +1129,15 @@ void TestGui::testSearch() searchedEntry->setPassword("password"); QClipboard* clipboard = QApplication::clipboard(); - // Attempt password copy with selected test (should fail) + // Copy to clipboard: should copy search text (not password) QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier); - QVERIFY(clipboard->text() != searchedEntry->password()); + QCOMPARE(clipboard->text(), QString("someTHING")); // Deselect text and confirm password copies QTest::mouseClick(searchTextEdit, Qt::LeftButton); QTRY_VERIFY(searchTextEdit->selectedText().isEmpty()); QTRY_VERIFY(searchTextEdit->hasFocus()); QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier); - QCOMPARE(searchedEntry->password(), clipboard->text()); + QCOMPARE(clipboard->text(), searchedEntry->password()); // Ensure Down focuses on entry view when search text is selected QTest::keyClick(searchTextEdit, Qt::Key_A, Qt::ControlModifier); QTest::keyClick(searchTextEdit, Qt::Key_Down); @@ -1145,11 +1145,11 @@ void TestGui::testSearch() QCOMPARE(entryView->currentEntry(), searchedEntry); // Test that password copies with entry focused QTest::keyClick(entryView, Qt::Key_C, Qt::ControlModifier); - QCOMPARE(searchedEntry->password(), clipboard->text()); + QCOMPARE(clipboard->text(), searchedEntry->password()); // Refocus back to search edit QTest::mouseClick(searchTextEdit, Qt::LeftButton); QTRY_VERIFY(searchTextEdit->hasFocus()); - // Test that password does not copy + // Select search text and test that password does not copy searchTextEdit->selectAll(); QTest::keyClick(searchTextEdit, Qt::Key_C, Qt::ControlModifier); QTRY_COMPARE(clipboard->text(), QString("someTHING"));