| 457 | } |
| 458 | |
| 459 | bool DocumentPrivate::copyStyle(const QString &from, const QString &to) |
| 460 | { |
| 461 | // create a temp file because the zip writer cannot modify already existing zips |
| 462 | QTemporaryFile tempFile; |
| 463 | tempFile.open(); |
| 464 | tempFile.close(); |
| 465 | QString temFilePath = QFileInfo(tempFile).absoluteFilePath(); |
| 466 | |
| 467 | ZipWriter temporalZip(temFilePath); |
| 468 | |
| 469 | ZipReader zipReader(from); |
| 470 | QStringList filePaths = zipReader.filePaths(); |
| 471 | |
| 472 | QSharedPointer<ZipReader> toReader = QSharedPointer<ZipReader>(new ZipReader(to)); |
| 473 | |
| 474 | QStringList toFilePaths = toReader->filePaths(); |
| 475 | |
| 476 | // copy all files from "to" zip except those related to style |
| 477 | for (int i = 0; i < toFilePaths.size(); i++) { |
| 478 | if (toFilePaths[i].contains(QLatin1String("xl/styles"))) { |
| 479 | if (filePaths.contains(toFilePaths[i])) { // style file exist in 'from' as well |
| 480 | // modify style file |
| 481 | std::string fromData = QString::fromUtf8(zipReader.fileData(toFilePaths[i])).toStdString(); |
| 482 | std::string toData = QString::fromUtf8(toReader->fileData(toFilePaths[i])).toStdString(); |
| 483 | // copy default theme style from 'from' to 'to' |
| 484 | toData = xlsxDocumentCpp::copyTag(fromData, toData, "dxfs"); |
| 485 | temporalZip.addFile(toFilePaths.at(i), QString::fromUtf8(toData.c_str()).toUtf8()); |
| 486 | |
| 487 | continue; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (toFilePaths[i].contains(QLatin1String("xl/workbook"))) { |
| 492 | if (filePaths.contains(toFilePaths[i])) { // workbook file exist in 'from' as well |
| 493 | // modify workbook file |
| 494 | std::string fromData = QString::fromUtf8(zipReader.fileData(toFilePaths[i])).toStdString(); |
| 495 | std::string toData = QString::fromUtf8(toReader->fileData(toFilePaths[i])).toStdString(); |
| 496 | // copy default theme style from 'from' to 'to' |
| 497 | toData = xlsxDocumentCpp::copyTag(fromData, toData, "workbookPr"); |
| 498 | temporalZip.addFile(toFilePaths.at(i), QString::fromUtf8(toData.c_str()).toUtf8()); |
| 499 | continue; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (toFilePaths[i].contains(QLatin1String("xl/worksheets/sheet"))) { |
| 504 | if (filePaths.contains(toFilePaths[i])) { // sheet file exist in 'from' as well |
| 505 | // modify sheet file |
| 506 | std::string fromData = QString::fromUtf8(zipReader.fileData(toFilePaths[i])).toStdString(); |
| 507 | std::string toData = QString::fromUtf8(toReader->fileData(toFilePaths[i])).toStdString(); |
| 508 | // copy "conditionalFormatting" from 'from' to 'to' |
| 509 | toData = xlsxDocumentCpp::copyTag(fromData, toData, "conditionalFormatting"); |
| 510 | temporalZip.addFile(toFilePaths.at(i), QString::fromUtf8(toData.c_str()).toUtf8()); |
| 511 | continue; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | QByteArray data = toReader->fileData(toFilePaths.at(i)); |
| 516 | temporalZip.addFile(toFilePaths.at(i), data); |