From 0d7ca9b5bec0c83b88787fc333f3b61652dcacaf Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 23 Nov 2017 17:03:57 +0100 Subject: [PATCH] Support pasting single value into multiple cells This adds support for pasting a single value into all selected cells in the Browse Data tab in order to mimic the behaviour of standard spreadsheet application more closely. --- src/ExtendedTableWidget.cpp | 38 +++++++++++++++++++++++++++---------- src/ExtendedTableWidget.h | 2 ++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/ExtendedTableWidget.cpp b/src/ExtendedTableWidget.cpp index 7ed5248c..e0ab7d8e 100644 --- a/src/ExtendedTableWidget.cpp +++ b/src/ExtendedTableWidget.cpp @@ -354,6 +354,17 @@ void ExtendedTableWidget::paste() int firstColumn = indices.front().column(); int selectedColumns = indices.back().column() - firstColumn + 1; + // Special case: if there is only one cell of data to be pasted, paste it into all selected fields + if(clipboardRows == 1 && clipboardColumns == 1) + { + QString data = clipboardTable.first().first(); + for(int row=firstRow;rowindex(row, column), data); + } + return; + } // If not selected only one cell then check does selection match cliboard dimensions if(selectedRows != 1 || selectedColumns != 1) @@ -383,16 +394,7 @@ void ExtendedTableWidget::paste() int column = firstColumn; for(const QString& cell : clipboardRow) { - if (cell.isEmpty()) - m->setData(m->index(row, column), QVariant()); - else - { - QString text = cell; - if (QRegExp("\".*\"").exactMatch(text)) - text = text.mid(1, cell.length() - 2); - text.replace("\"\"", "\""); - m->setData(m->index(row, column), text); - } + setPasteData(m->index(row, column), cell); column++; if(column> lastColumn) @@ -410,6 +412,22 @@ void ExtendedTableWidget::paste() } +void ExtendedTableWidget::setPasteData(const QModelIndex& idx, const QString& data) +{ + SqliteTableModel* m = qobject_cast(model()); + + if(data.isEmpty()) + { + m->setData(idx, QVariant()); + } else { + QString text = data; + if(QRegExp("\".*\"").exactMatch(text)) + text = text.mid(1, data.length() - 2); + text.replace("\"\"", "\""); + m->setData(idx, text); + } +} + void ExtendedTableWidget::useAsFilter() { QModelIndex index = selectionModel()->currentIndex(); diff --git a/src/ExtendedTableWidget.h b/src/ExtendedTableWidget.h index 29d5bb72..b2adfa4a 100644 --- a/src/ExtendedTableWidget.h +++ b/src/ExtendedTableWidget.h @@ -34,6 +34,8 @@ signals: private: void copy(const bool withHeaders = false); void paste(); + void setPasteData(const QModelIndex& idx, const QString& data); + void useAsFilter(); typedef QList QByteArrayList;