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> <x>0</x>
<y>0</y> <y>0</y>
<width>614</width> <width>614</width>
<height>213</height> <height>250</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View File

@ -23,6 +23,7 @@
#include "pdfwidgetutils.h" #include "pdfwidgetutils.h"
#include "pdfdocumentreader.h" #include "pdfdocumentreader.h"
#include "pdfdocumentwriter.h"
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
@ -551,6 +552,27 @@ void MainWindow::performOperation(Operation operation)
QString fileNameTemplate = dialog.getFileName(); QString fileNameTemplate = dialog.getFileName();
const bool isOverwriteEnabled = dialog.isOverwriteFiles(); 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) for (const std::vector<pdf::PDFDocumentManipulator::AssembledPage>& assembledPages : assembledDocuments)
{ {
pdf::PDFOperationResult currentResult = manipulator.assemble(assembledPages); pdf::PDFOperationResult currentResult = manipulator.assemble(assembledPages);
@ -562,14 +584,19 @@ void MainWindow::performOperation(Operation operation)
pdf::PDFDocumentManipulator::AssembledPage samplePage = assembledPages.front(); pdf::PDFDocumentManipulator::AssembledPage samplePage = assembledPages.front();
sourceDocumentIndex = samplePage.documentIndex == -1 ? documentCount + samplePage.imageIndex : samplePage.documentIndex; 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; QString fileName = fileNameTemplate;
replaceInString(fileName, '#', assembledDocumentIndex);
replaceInString(fileName, '@', sourcePageIndex);
replaceInString(fileName, '%', sourceDocumentIndex);
if (!fileName.endsWith(".pdf")) if (!fileName.endsWith(".pdf"))
{ {
fileName += ".pdf"; fileName += ".pdf";
} }
fileName.prepend(directory);
assembledDocumentStorage.emplace_back(std::make_pair(std::move(fileName), manipulator.takeAssembledDocument())); assembledDocumentStorage.emplace_back(std::make_pair(std::move(fileName), manipulator.takeAssembledDocument()));
++assembledDocumentIndex; ++assembledDocumentIndex;
@ -578,10 +605,31 @@ void MainWindow::performOperation(Operation operation)
if (!result) if (!result)
{ {
QMessageBox::critical(this, tr("Error"), result.getErrorMessage()); 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; break;

View File

@ -11,7 +11,7 @@
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>Workspace</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout"> <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.pageRotation = pdf::getPageRotationCombined(originalPageRotation, item.pageAdditionalRotation);
assembledPage.pageSize = pdf::PDFPage::getRotatedSize(item.rotatedPageDimensionsMM, pdf::getPageRotationInversed(item.pageAdditionalRotation)); assembledPage.pageSize = item.rotatedPageDimensionsMM;
return assembledPage; return assembledPage;
}; };

View File

@ -48,8 +48,8 @@ struct PageGroupItem
int documentIndex = -1; int documentIndex = -1;
pdf::PDFInteger pageIndex = -1; pdf::PDFInteger pageIndex = -1;
pdf::PDFInteger imageIndex = -1; pdf::PDFInteger imageIndex = -1;
QSizeF rotatedPageDimensionsMM; QSizeF rotatedPageDimensionsMM; ///< Rotated page dimensions, but without additional rotation
pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None; pdf::PageRotation pageAdditionalRotation = pdf::PageRotation::None; ///< Additional rotation applied to the page
PageType pageType = PT_DocumentPage; 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) 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) if (safeWrite)
{ {
QSaveFile file(fileName); QSaveFile file(fileName);
@ -204,6 +212,13 @@ PDFOperationResult PDFDocumentWriter::write(const QString& fileName, const PDFDo
{ {
PDFOperationResult result = write(&file, document); PDFOperationResult result = write(&file, document);
file.close(); file.close();
if (!result)
{
// If some error occured, then remove invalid file
file.remove();
}
return result; return result;
} }
else else