From fd6f2c2508c9cbd509f5dfcd186da9c005e0ed77 Mon Sep 17 00:00:00 2001 From: Srivats P Date: Thu, 5 Mar 2020 12:43:37 +0530 Subject: [PATCH] Move copy support for Logs Window to Logs Model The model is the correct place to determine what data gets copied. This also paves way to support other models where the data to be copied is not plain text --- client/logsmodel.cpp | 26 ++++++++++++++++++++++++++ client/logsmodel.h | 2 ++ client/xtableview.h | 16 +++------------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/client/logsmodel.cpp b/client/logsmodel.cpp index 46fd646..b23feb5 100644 --- a/client/logsmodel.cpp +++ b/client/logsmodel.cpp @@ -20,6 +20,7 @@ along with this program. If not, see #include "logsmodel.h" #include +#include // XXX: Keep the enum in sync with it's string enum { @@ -108,6 +109,31 @@ QVariant LogsModel::data(const QModelIndex &index, int role) const return QVariant(); } +QStringList LogsModel::mimeTypes() const +{ + return QStringList() << "text/plain"; +} + +QMimeData* LogsModel::mimeData(const QModelIndexList &indexes) const +{ + int lastRow = -1; + QString text; + foreach(QModelIndex index, indexes) { + if (index.row() != lastRow) { + if (!text.isEmpty()) + text.append("\n"); + } + else + text.append("\t"); + text.append(data(index).toString()); + lastRow = index.row(); + } + + QMimeData *mimeData = new QMimeData(); + mimeData->setText(text); + return mimeData; // caller is responsible for freeing! +} + // --------------------------------------------- // // Slots // --------------------------------------------- // diff --git a/client/logsmodel.h b/client/logsmodel.h index a7edb17..dd27e37 100644 --- a/client/logsmodel.h +++ b/client/logsmodel.h @@ -43,6 +43,8 @@ public: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QStringList mimeTypes() const; + QMimeData* mimeData(const QModelIndexList &indexes) const; public slots: void clear(); diff --git a/client/xtableview.h b/client/xtableview.h index 91bba15..6fad59f 100644 --- a/client/xtableview.h +++ b/client/xtableview.h @@ -48,23 +48,13 @@ protected: virtual void keyPressEvent(QKeyEvent *event) { // Copy selection to clipboard (base class copies only current item) + // Selection, by default, is in the order in which items were selected + // - sort them before copying if (event->matches(QKeySequence::Copy) && selectionBehavior() == SelectRows) { - QString text; - int lastRow = -1; QModelIndexList selected = selectionModel()->selectedIndexes(); std::sort(selected.begin(), selected.end()); - foreach(QModelIndex index, selected) { - if (index.row() != lastRow) { - if (!text.isEmpty()) - text.append("\n"); - } - else - text.append("\t"); - text.append(model()->data(index).toString()); - lastRow = index.row(); - } - qApp->clipboard()->setText(text); + qApp->clipboard()->setMimeData(model()->mimeData(selected)); } else QTableView::keyPressEvent(event);