From e6213a90ac53d80f61d14f70b88f4903ceb70348 Mon Sep 17 00:00:00 2001 From: Srivats P Date: Wed, 18 Mar 2020 20:50:29 +0530 Subject: [PATCH] Copy to clipboard in plain text format also The plain text copy is written generically and should work with any model that is attached to a XTableView --- client/deviceswidget.ui | 4 ++-- client/xtableview.h | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/client/deviceswidget.ui b/client/deviceswidget.ui index ec76836..442ca23 100644 --- a/client/deviceswidget.ui +++ b/client/deviceswidget.ui @@ -158,8 +158,8 @@ To emulate a device, click on Configuration and create a device group IP neighbor cache is empty - - QAbstractItemView::SingleSelection + + QAbstractItemView::SelectRows diff --git a/client/xtableview.h b/client/xtableview.h index 3c5e70e..eef3e16 100644 --- a/client/xtableview.h +++ b/client/xtableview.h @@ -83,6 +83,7 @@ public slots: std::sort(selected.begin(), selected.end()); QMimeData *mimeData = model()->mimeData(selected); + copyPlainText(selected, mimeData); qApp->clipboard()->setMimeData(mimeData); qDebug("Copied data in %d format(s) to clipboard", mimeData->formats().size()); @@ -150,6 +151,41 @@ protected: } private: + void copyPlainText(const QModelIndexList &indexes, QMimeData *mimeData) + { + if (mimeData->hasText()) + return; + + bool includeHeaders = (selectionBehavior() + != QAbstractItemView::SelectItems); + QString text; + if (includeHeaders) { + text.append("\t"); // column header for row number/title + for (int i = 0; i < model()->columnCount(); i++) + text.append(model()->headerData(i, Qt::Horizontal) + .toString()+"\t");; + text.append("\n"); + } + + int lastRow = -1; + foreach(QModelIndex index, indexes) { + if (index.row() != lastRow) { // row changed + if (lastRow >= 0) + text.append("\n"); + if (includeHeaders) + text.append(model()->headerData(index.row(), Qt::Vertical) + .toString()+"\t"); + } + else + text.append("\t"); + text.append(model()->data(index).toString()); + lastRow = index.row(); + } + text.append("\n"); + + mimeData->setText(text); + } + bool _modelAllowsRemove{false}; };