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).
This commit is contained in:
Carlo Teubner 2024-06-07 10:02:03 +01:00 committed by Jonathan White
parent def56f745c
commit 24dc07897b
3 changed files with 20 additions and 10 deletions

View File

@ -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()));

View File

@ -57,7 +57,6 @@ signals:
void caseSensitiveChanged(bool state);
void limitGroupChanged(bool state);
void escapePressed();
void copyPressed();
void downPressed();
void enterPressed();
void lostFocus();

View File

@ -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"));