Navigation Menu

Skip to content

Commit

Permalink
Support pasting single value into multiple cells
Browse files Browse the repository at this point in the history
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 0d7ca9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/ExtendedTableWidget.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/ExtendedTableWidget.h
Expand Up @@ -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;
Expand Down

0 comments on commit 0d7ca9b

Please sign in to comment.