DocPage Organizer: Save assembled files

This commit is contained in:
Jakub Melka 2021-07-25 15:12:16 +02:00
parent b38bbf8468
commit 229234fc15
6 changed files with 70 additions and 7 deletions

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>614</width>
<height>213</height>
<height>250</height>
</rect>
</property>
<property name="windowTitle">

View File

@ -23,6 +23,7 @@
#include "pdfwidgetutils.h"
#include "pdfdocumentreader.h"
#include "pdfdocumentwriter.h"
#include <QFileDialog>
#include <QMessageBox>
@ -551,6 +552,27 @@ void MainWindow::performOperation(Operation operation)
QString fileNameTemplate = dialog.getFileName();
const bool isOverwriteEnabled = dialog.isOverwriteFiles();
if (!directory.endsWith('/'))
{
directory += "/";
}
auto replaceInString = [](QString& templateString, QChar character, int number)
{
int index = templateString.indexOf(character, 0, Qt::CaseSensitive);
if (index != -1)
{
int lastIndex = templateString.lastIndexOf(character, -1, Qt::CaseSensitive);
int count = lastIndex - index + 1;
QString textNumber = QString::number(number);
textNumber = textNumber.rightJustified(count, '0', false);
templateString.remove(index, count);
templateString.insert(index, textNumber);
}
};
for (const std::vector<pdf::PDFDocumentManipulator::AssembledPage>& assembledPages : assembledDocuments)
{
pdf::PDFOperationResult currentResult = manipulator.assemble(assembledPages);
@ -562,14 +584,19 @@ void MainWindow::performOperation(Operation operation)
pdf::PDFDocumentManipulator::AssembledPage samplePage = assembledPages.front();
sourceDocumentIndex = samplePage.documentIndex == -1 ? documentCount + samplePage.imageIndex : samplePage.documentIndex;
sourcePageIndex = qMax(samplePage.pageIndex + 1, 1);
sourcePageIndex = qMax(int(samplePage.pageIndex + 1), 1);
QString fileName = fileNameTemplate;
replaceInString(fileName, '#', assembledDocumentIndex);
replaceInString(fileName, '@', sourcePageIndex);
replaceInString(fileName, '%', sourceDocumentIndex);
if (!fileName.endsWith(".pdf"))
{
fileName += ".pdf";
}
fileName.prepend(directory);
assembledDocumentStorage.emplace_back(std::make_pair(std::move(fileName), manipulator.takeAssembledDocument()));
++assembledDocumentIndex;
@ -578,10 +605,31 @@ void MainWindow::performOperation(Operation operation)
if (!result)
{
QMessageBox::critical(this, tr("Error"), result.getErrorMessage());
break;
return;
}
// Now, try to save files
for (const auto& assembledDocumentItem : assembledDocumentStorage)
{
QString filename = assembledDocumentItem.first;
const pdf::PDFDocument* document = &assembledDocumentItem.second;
const bool isDocumentFileAlreadyExisting = QFile::exists(filename);
if (!isOverwriteEnabled && isDocumentFileAlreadyExisting)
{
QMessageBox::critical(this, tr("Error"), tr("Document with given filename already exists."));
return;
}
pdf::PDFDocumentWriter writer(nullptr);
pdf::PDFOperationResult result = writer.write(filename, document, isDocumentFileAlreadyExisting);
if (!result)
{
QMessageBox::critical(this, tr("Error"), result.getErrorMessage());
return;
}
}
}
break;

View File

@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>Workspace</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">

View File

@ -877,7 +877,7 @@ std::vector<std::vector<pdf::PDFDocumentManipulator::AssembledPage>> PageItemMod
}
assembledPage.pageRotation = pdf::getPageRotationCombined(originalPageRotation, item.pageAdditionalRotation);
assembledPage.pageSize = pdf::PDFPage::getRotatedSize(item.rotatedPageDimensionsMM, pdf::getPageRotationInversed(item.pageAdditionalRotation));
assembledPage.pageSize = item.rotatedPageDimensionsMM;
return assembledPage;
};

View File

@ -48,8 +48,8 @@ struct PageGroupItem
int documentIndex = -1;
pdf::PDFInteger pageIndex = -1;
pdf::PDFInteger imageIndex = -1;
QSizeF rotatedPageDimensionsMM;
pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None;
QSizeF rotatedPageDimensionsMM; ///< Rotated page dimensions, but without additional rotation
pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None; ///< Additional rotation applied to the page
PageType pageType = PT_DocumentPage;
};

View File

@ -173,6 +173,14 @@ void PDFWriteObjectVisitor::visitReference(const PDFObjectReference reference)
PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDocument* document, bool safeWrite)
{
Q_ASSERT(document);
const PDFObjectStorage& storage = document->getStorage();
if (!storage.getSecurityHandler()->isEncryptionAllowed())
{
return tr("Writing of encrypted documents is not supported.");
}
if (safeWrite)
{
QSaveFile file(fileName);
@ -204,6 +212,13 @@ PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDo
{
PDFOperationResult result = write(&file, document);
file.close();
if (!result)
{
// If some error occured, then remove invalid file
file.remove();
}
return result;
}
else