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.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/copyeventfilter.h (limited to 'src/copyeventfilter.h') diff --git a/src/copyeventfilter.h b/src/copyeventfilter.h new file mode 100644 index 00000000..1243630b --- /dev/null +++ b/src/copyeventfilter.h @@ -0,0 +1,43 @@ +#ifndef COPY_EVENT_FILTER_H +#define COPY_EVENT_FILTER_H + +#include + +#include +#include +#include + +// this small class provides copy on Ctrl+C and also +// exposes a method to actual copy the selection +// +// the way the selection is copied can be customized by +// passing a functor to format each index, by default +// it only extracts the display role +// +// only works for view that selects whole row since it only +// considers the first cell in each row +// +class CopyEventFilter : public QObject +{ + Q_OBJECT + +public: + + CopyEventFilter(QAbstractItemView* view, int role = Qt::DisplayRole); + CopyEventFilter(QAbstractItemView* view, std::function format); + + // copy the selection of the view associated with this + // event filter into the clipboard + // + bool copySelection() const; + + bool eventFilter(QObject* sender, QEvent* event) override; + +private: + + QAbstractItemView* m_view; + std::function m_format; + +}; + +#endif -- 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.h') 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