Spreadsheet: support paste into cell range

For single range copy, the range selection when pasting determines the
start cell and the number of duplications.

For example, when copying a range A1:B2 (i.e. a 2x2 square) and pasting
into a selection of C1:C5 (i.e. a 5x1 vertical line), the square will be
duplicated once in horizontal, but twice in vertical, resulting new
cells range from C1:D4. This logic is borrowed from google sheet.

For multi-ranged copy, no multi duplication is intended. If more than
one selection range exists before pasting, only the top left cell of
the last selected range is used to determine the starting cell for
pasting. The cells will be copied with the exact cell layout keeping any
empty cells in between. This logic is different from google sheet, where
it disallows unalligned multi-ranged copy, and will condense and
eliminate any empty cells for aligned multi-range copy.
This commit is contained in:
Zheng, Lei
2019-12-26 07:57:03 +08:00
committed by Chris Hennes
parent 50ab1c558b
commit 55e2c918a9
3 changed files with 119 additions and 39 deletions

View File

@@ -597,18 +597,16 @@ void SheetTableView::pasteClipboard()
if(!mimeData || !mimeData->hasText())
return;
if(selectionModel()->selectedIndexes().size()>1) {
QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Spreadsheet"),
QObject::tr("Spreadsheet does not support range selection when pasting.\n"
"Please select one cell only."));
auto ranges = selectedRanges();
if(ranges.empty())
return;
}
QModelIndex current = currentIndex();
Range range = ranges.back();
App::AutoTransaction committer("Paste cell");
try {
if (!mimeData->hasFormat(_SheetMime)) {
CellAddress current = range.from();
QStringList cells;
QString text = mimeData->text();
int i=0;
@@ -616,7 +614,7 @@ void SheetTableView::pasteClipboard()
QStringList cols = it.split(QLatin1Char('\t'));
int j=0;
for (auto jt : cols) {
QModelIndex index = model()->index(current.row()+i, current.column()+j);
QModelIndex index = model()->index(current.row()+i, current.col()+j);
model()->setData(index, jt);
j++;
}
@@ -628,7 +626,7 @@ void SheetTableView::pasteClipboard()
std::istream in(0);
in.rdbuf(&buf);
Base::XMLReader reader("<memory>", in);
sheet->getCells()->pasteCells(reader,CellAddress(current.row(),current.column()));
sheet->getCells()->pasteCells(reader,range);
}
GetApplication().getActiveDocument()->recompute();
@@ -637,7 +635,9 @@ void SheetTableView::pasteClipboard()
e.ReportException();
QMessageBox::critical(Gui::getMainWindow(), QObject::tr("Copy & Paste failed"),
QString::fromLatin1(e.what()));
return;
}
clearSelection();
}
void SheetTableView::finishEditWithMove(int keyPressed, Qt::KeyboardModifiers modifiers, bool handleTabMotion)