Commit f074ddf1 authored by C. Boemann's avatar C. Boemann

When copying two tables we would get a crash as we were assuming it was all one table.

But trying to fix that it turns out we had a bigger issue. We couldn't copy a part of a table,
so I fixed that instead keeping in mind that we could have selected more than on table.

CCBUG:275990
BUG:330284
parent 3ce5023b
......@@ -121,41 +121,27 @@ void KoTextWriter::write(const QTextDocument *document, int from, int to)
QTextCursor fromcursor(fromblock);
QTextTable *currentTable = fromcursor.currentTable();
QTextList *currentList = fromcursor.currentList();
// NOTE even better would be if we create a new table/list out of multiple selected
// tablecells/listitems that contain only the selected cells/items. But following
// at least enables copying a whole list/table while still being able to copy/paste
// only parts of the text within a list/table (see also bug 275990).
if (currentTable || currentList) {
// NOTE even better would be if we create a new list out of multiple selected
// listitems that contain only the selected items. But following
// at least enables copying a whole list while still being able to copy/paste
// only parts of the text within a list (see also bug 275990).
// NOTE this has been fixed for tables now, and it looks like the list code is seriously wrong
// not just like the table code was, but more fundamentally as lists in qt is an orthogonal concept
if (currentList) {
if (from == 0 && to < 0) {
// save everything means also save current table and list
currentTable = 0;
currentList = 0;
} else {
QTextCursor tocursor(toblock);
//fromcursor.setPosition(from, QTextCursor::KeepAnchor);
tocursor.setPosition(to, QTextCursor::KeepAnchor);
QTextCursor toCursor(toblock);
toCursor.setPosition(to, QTextCursor::KeepAnchor);
if (!fromcursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor)) {
fromcursor = QTextCursor();
}
if (!tocursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor)) {
tocursor = QTextCursor();
}
// save the whole table if all cells are selected
if (currentTable) {
QTextTableCell fromcell = currentTable->cellAt(from);
QTextTableCell tocell = currentTable->cellAt(to);
if ((fromcursor.isNull() || fromcursor.currentTable() != currentTable) &&
(tocursor.isNull() || tocursor.currentTable() != currentTable) &&
fromcell.column() == 0 && fromcell.row() == 0 &&
tocell.column() == currentTable->columns()-1 && tocell.row() == currentTable->rows()-1
) {
currentTable = 0;
}
if (!toCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor)) {
toCursor = QTextCursor();
}
// save the whole list if all list-items are selected
......@@ -163,7 +149,7 @@ void KoTextWriter::write(const QTextDocument *document, int from, int to)
int fromindex = currentList->itemNumber(fromblock);
int toindex = currentList->itemNumber(toblock);
if ((fromcursor.isNull() || fromcursor.currentList() != currentList) &&
(tocursor.isNull() || tocursor.currentList() != currentList) &&
(toCursor.isNull() || toCursor.currentList() != currentList) &&
fromindex <= 0 && (toindex < 0 || toindex == currentList->count()-1)
) {
currentList = 0;
......@@ -175,5 +161,5 @@ void KoTextWriter::write(const QTextDocument *document, int from, int to)
QHash<QTextList *, QString> listStyles = d->saveListStyles(fromblock, to);
d->globalFrom = from;
d->globalTo = to;
d->writeBlocks(const_cast<QTextDocument *>(document), from, to, listStyles, currentTable, currentList);
d->writeBlocks(const_cast<QTextDocument *>(document), from, to, listStyles, 0, currentList);
}
......@@ -154,7 +154,7 @@ void KoTextWriter::Private::writeBlocks(QTextDocument *document, int from, int t
if (cursor.currentTable() && cursor.currentTable() != currentTable) {
// Call the code to save the table....
saveTable(cursor.currentTable(), listStyles);
saveTable(cursor.currentTable(), listStyles, from, to);
// We skip to the end of the table.
block = cursor.currentTable()->lastCursorPosition().block();
block = block.next();
......@@ -720,7 +720,7 @@ void KoTextWriter::Private::saveParagraph(const QTextBlock &block, int from, int
closeTagRegion();
}
void KoTextWriter::Private::saveTable(QTextTable *table, QHash<QTextList *, QString> &listStyles)
void KoTextWriter::Private::saveTable(QTextTable *table, QHash<QTextList *, QString> &listStyles, int from, int to)
{
KoTableColumnAndRowStyleManager tcarManager = KoTableColumnAndRowStyleManager::getManager(table);
int numberHeadingRows = table->format().property(KoTableStyle::NumberHeadingRows).toInt();
......@@ -772,11 +772,21 @@ void KoTextWriter::Private::saveTable(QTextTable *table, QHash<QTextList *, QStr
openTagRegion(KoTextWriter::Private::Table, tableTagInformation);
for (int c = 0 ; c < table->columns() ; c++) {
int firstColumn = 0;
int lastColumn = table->columns() -1;
int firstRow = 0;
int lastRow = table->rows() -1;
if (to != -1 && from >= table->firstPosition() && to <= table->lastPosition()) {
firstColumn = table->cellAt(from).column();
firstRow = table->cellAt(from).row();
lastColumn = table->cellAt(to).column();
lastRow = table->cellAt(to).row();
}
for (int c = firstColumn ; c <= lastColumn; c++) {
KoTableColumnStyle columnStyle = tcarManager.columnStyle(c);
int repetition = 0;
for (; repetition < (table->columns() - c) ; repetition++)
for (; repetition <= (lastColumn - c) ; repetition++)
{
if (columnStyle != tcarManager.columnStyle(c + repetition + 1))
break;
......@@ -798,7 +808,8 @@ void KoTextWriter::Private::saveTable(QTextTable *table, QHash<QTextList *, QStr
if (numberHeadingRows)
writer->startElement("table:table-header-rows");
for (int r = 0 ; r < table->rows() ; r++) {
// TODO make work for copying part of table that has header rows - copy header rows additionally or not ?
for (int r = firstRow; r <= lastRow; r++) {
TagInformation tableRowInformation;
tableRowInformation.setTagName("table:table-row");
KoTableRowStyle rowStyle = tcarManager.rowStyle(r);
......@@ -809,7 +820,7 @@ void KoTextWriter::Private::saveTable(QTextTable *table, QHash<QTextList *, QStr
}
openTagRegion(KoTextWriter::Private::TableRow, tableRowInformation);
for (int c = 0 ; c < table->columns() ; c++) {
for (int c = firstColumn; c <= lastColumn; c++) {
QTextTableCell cell = table->cellAt(r, c);
TagInformation tableCellInformation;
......
......@@ -133,7 +133,7 @@ private:
QString saveTableCellStyle(const QTextTableCellFormat &cellFormat, int columnNumber, const QString &tableStyleName);
void saveParagraph(const QTextBlock &block, int from, int to);
void saveTable(QTextTable *table, QHash<QTextList *, QString> &listStyles);
void saveTable(QTextTable *table, QHash<QTextList *, QString> &listStyles, int from, int to);
QTextBlock& saveList(QTextBlock &block, QHash<QTextList *, QString> &listStyles, int level, QTextTable *currentTable);
void saveTableOfContents(QTextDocument *document, QHash<QTextList *, QString> &listStyles, QTextBlock toc);
void saveBibliography(QTextDocument *document, QHash<QTextList *, QString> &listStyles, QTextBlock bib);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment