Permalink
Browse files

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.
  • Loading branch information...
MKleusberg committed Nov 23, 2017
1 parent 67c5ed9 commit 0d7ca9b5bec0c83b88787fc333f3b61652dcacaf
Showing with 30 additions and 10 deletions.
  1. +28 −10 src/ExtendedTableWidget.cpp
  2. +2 −0 src/ExtendedTableWidget.h
@@ -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;row<firstRow+selectedRows;row++)
{
for(int column=firstColumn;column<firstColumn+selectedColumns;column++)
setPasteData(m->index(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<SqliteTableModel*>(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();
@@ -34,6 +34,8 @@ public slots:
private:
void copy(const bool withHeaders = false);
void paste();
void setPasteData(const QModelIndex& idx, const QString& data);

void useAsFilter();

typedef QList<QByteArray> QByteArrayList;

0 comments on commit 0d7ca9b

Please sign in to comment.