diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-01-03 12:51:54 +0100 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2021-01-03 12:51:54 +0100 |
| commit | 3366d9f192ef88cccc2d901c666e15061db20b4e (patch) | |
| tree | d65ce6bfdcd6ddbc4ce48df453613057eca18b1e /src/copyeventfilter.cpp | |
| parent | c36d1126df94d879181a95d63862117a62d5ff60 (diff) | |
Create CopyEventFilter. Add Ctrl+C to the plugin list.
Diffstat (limited to 'src/copyeventfilter.cpp')
| -rw-r--r-- | src/copyeventfilter.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
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 <QClipboard> +#include <QGuiApplication> +#include <QKeyEvent> + +CopyEventFilter::CopyEventFilter(QAbstractItemView* view, int role) : + CopyEventFilter(view, [=](auto& index) { return index.data(role).toString(); }) +{ + +} + +CopyEventFilter::CopyEventFilter( + QAbstractItemView* view, std::function<QString(const QModelIndex&)> 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<QKeyEvent*>(event); + if (keyEvent->modifiers() == Qt::ControlModifier + && keyEvent->key() == Qt::Key_C) { + return copySelection(); + } + } + return QObject::eventFilter(sender, event); +} |
