From 7ceca8ff3c0ce971a3cfa38719b45330c73e55ad Mon Sep 17 00:00:00 2001 From: Tobias Schwackenhofer Date: Fri, 19 Apr 2019 20:04:23 +0200 Subject: [PATCH] Enable copy & paste from AutoType selection dialog This change adds a right-click context menu to the AutoType dialog, which allows the user to copy either the username or password. The dialog then automatically closes. --- src/autotype/AutoTypeSelectDialog.cpp | 1 + src/gui/entry/AutoTypeMatchView.cpp | 29 +++++++++++++++++++++++++-- src/gui/entry/AutoTypeMatchView.h | 3 +++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/autotype/AutoTypeSelectDialog.cpp b/src/autotype/AutoTypeSelectDialog.cpp index 69f181765..997858f0d 100644 --- a/src/autotype/AutoTypeSelectDialog.cpp +++ b/src/autotype/AutoTypeSelectDialog.cpp @@ -75,6 +75,7 @@ AutoTypeSelectDialog::AutoTypeSelectDialog(QWidget* parent) connect(m_view, SIGNAL(clicked(QModelIndex)), SLOT(emitMatchActivated(QModelIndex))); connect(m_view->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(matchRemoved())); connect(m_view, SIGNAL(rejected()), SLOT(reject())); + connect(m_view, SIGNAL(matchTextCopied()), SLOT(reject())); // clang-format on QSortFilterProxyModel* proxy = qobject_cast(m_view->model()); diff --git a/src/gui/entry/AutoTypeMatchView.cpp b/src/gui/entry/AutoTypeMatchView.cpp index 9c4e81d92..180473fd3 100644 --- a/src/gui/entry/AutoTypeMatchView.cpp +++ b/src/gui/entry/AutoTypeMatchView.cpp @@ -18,8 +18,11 @@ #include "AutoTypeMatchView.h" +#include "core/Entry.h" +#include "gui/Clipboard.h" #include "gui/SortFilterHideProxyModel.h" +#include #include #include @@ -42,13 +45,35 @@ AutoTypeMatchView::AutoTypeMatchView(QWidget* parent) setSelectionMode(QAbstractItemView::SingleSelection); header()->setDefaultSectionSize(150); + setContextMenuPolicy(Qt::ActionsContextMenu); + auto* copyUserNameAction = new QAction(tr("Copy &username"), this); + auto* copyPasswordAction = new QAction(tr("Copy &password"), this); + addAction(copyUserNameAction); + addAction(copyPasswordAction); + + connect(copyUserNameAction, SIGNAL(triggered()), this, SLOT(userNameCopied())); + connect(copyPasswordAction, SIGNAL(triggered()), this, SLOT(passwordCopied())); + connect(this, SIGNAL(doubleClicked(QModelIndex)), SLOT(emitMatchActivated(QModelIndex))); // clang-format off - connect( - selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SIGNAL(matchSelectionChanged())); + connect(selectionModel(), + SIGNAL(selectionChanged(QItemSelection,QItemSelection)), + SIGNAL(matchSelectionChanged())); // clang-format on } +void AutoTypeMatchView::userNameCopied() +{ + clipboard()->setText(currentMatch().entry->username()); + emit matchTextCopied(); +} + +void AutoTypeMatchView::passwordCopied() +{ + clipboard()->setText(currentMatch().entry->password()); + emit matchTextCopied(); +} + void AutoTypeMatchView::keyPressEvent(QKeyEvent* event) { if ((event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) && currentIndex().isValid()) { diff --git a/src/gui/entry/AutoTypeMatchView.h b/src/gui/entry/AutoTypeMatchView.h index b9f3f3aa5..69d0795d9 100644 --- a/src/gui/entry/AutoTypeMatchView.h +++ b/src/gui/entry/AutoTypeMatchView.h @@ -42,12 +42,15 @@ public: signals: void matchActivated(AutoTypeMatch match); void matchSelectionChanged(); + void matchTextCopied(); protected: void keyPressEvent(QKeyEvent* event) override; private slots: void emitMatchActivated(const QModelIndex& index); + void userNameCopied(); + void passwordCopied(); private: AutoTypeMatchModel* const m_model;