From 3366d9f192ef88cccc2d901c666e15061db20b4e Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sun, 3 Jan 2021 12:51:54 +0100 Subject: Create CopyEventFilter. Add Ctrl+C to the plugin list. --- src/copyeventfilter.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/copyeventfilter.cpp (limited to 'src/copyeventfilter.cpp') diff --git a/src/copyeventfilter.cpp b/src/copyeventfilter.cpp new file mode 100644 index 00000000..bbde13e6 --- /dev/null +++ b/src/copyeventfilter.cpp @@ -0,0 +1,51 @@ +#include "copyeventfilter.h" + +#include +#include +#include + +CopyEventFilter::CopyEventFilter(QAbstractItemView* view, int role) : + CopyEventFilter(view, [=](auto& index) { return index.data(role).toString(); }) +{ + +} + +CopyEventFilter::CopyEventFilter( + QAbstractItemView* view, std::function format) : + QObject(view), m_view(view), m_format(format) +{ + +} + +bool CopyEventFilter::copySelection() const +{ + if (!m_view->selectionModel()->hasSelection()) { + return true; + } + + // sort to reflect the visual order + QModelIndexList selectedRows = m_view->selectionModel()->selectedRows(); + std::sort(selectedRows.begin(), selectedRows.end(), [=](const auto& lidx, const auto& ridx) { + return m_view->visualRect(lidx).top() < m_view->visualRect(ridx).top(); + }); + + QStringList rows; + for (auto& idx : selectedRows) { + rows.append(m_format(idx)); + } + + QGuiApplication::clipboard()->setText(rows.join("\n")); + return true; +} + +bool CopyEventFilter::eventFilter(QObject* sender, QEvent* event) +{ + if (sender == m_view && event->type() == QEvent::KeyPress) { + QKeyEvent* keyEvent = static_cast(event); + if (keyEvent->modifiers() == Qt::ControlModifier + && keyEvent->key() == Qt::Key_C) { + return copySelection(); + } + } + return QObject::eventFilter(sender, event); +} -- cgit v1.3.1 From 4ac3ee42910739695bcdf065651555d2d6a700f7 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 6 Jan 2021 10:05:35 +0100 Subject: Do not return value from copySelection(). --- src/copyeventfilter.cpp | 8 ++++---- src/copyeventfilter.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/copyeventfilter.cpp') diff --git a/src/copyeventfilter.cpp b/src/copyeventfilter.cpp index bbde13e6..223561b0 100644 --- a/src/copyeventfilter.cpp +++ b/src/copyeventfilter.cpp @@ -17,10 +17,10 @@ CopyEventFilter::CopyEventFilter( } -bool CopyEventFilter::copySelection() const +void CopyEventFilter::copySelection() const { if (!m_view->selectionModel()->hasSelection()) { - return true; + return; } // sort to reflect the visual order @@ -35,7 +35,6 @@ bool CopyEventFilter::copySelection() const } QGuiApplication::clipboard()->setText(rows.join("\n")); - return true; } bool CopyEventFilter::eventFilter(QObject* sender, QEvent* event) @@ -44,7 +43,8 @@ bool CopyEventFilter::eventFilter(QObject* sender, QEvent* event) QKeyEvent* keyEvent = static_cast(event); if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_C) { - return copySelection(); + copySelection(); + return true; } } return QObject::eventFilter(sender, event); diff --git a/src/copyeventfilter.h b/src/copyeventfilter.h index 1243630b..a9d0a1c1 100644 --- a/src/copyeventfilter.h +++ b/src/copyeventfilter.h @@ -29,7 +29,7 @@ public: // copy the selection of the view associated with this // event filter into the clipboard // - bool copySelection() const; + void copySelection() const; bool eventFilter(QObject* sender, QEvent* event) override; -- cgit v1.3.1