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.
This commit is contained in:
Martin Kleusberg
2017-11-23 17:03:57 +01:00
parent 67c5ed963e
commit 0d7ca9b5be
2 changed files with 30 additions and 10 deletions

View File

@@ -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();