Cleaned up exporter classes (to remove excessive code duplication)

preferencesAboutTextFull
Craig Watson 9 years ago
parent fea02af4d6
commit 8098679b23
  1. 50
      src/adaptors/UBExportAdaptor.cpp
  2. 5
      src/adaptors/UBExportAdaptor.h
  3. 38
      src/adaptors/UBExportDocument.cpp
  4. 2
      src/adaptors/UBExportDocument.h
  5. 38
      src/adaptors/UBExportFullPDF.cpp
  6. 3
      src/adaptors/UBExportFullPDF.h
  7. 20
      src/adaptors/UBExportPDF.cpp
  8. 2
      src/adaptors/UBExportPDF.h

@ -117,6 +117,56 @@ QString UBExportAdaptor::askForDirName(UBDocumentProxy* pDocument, const QString
return dirname;
}
void UBExportAdaptor::persistLocally(UBDocumentProxy* pDocumentProxy, const QString& pDialogTitle)
{
if (!pDocumentProxy)
return;
QString filename = askForFileName(pDocumentProxy, pDialogTitle);
if (filename.length() > 0) {
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if (mIsVerbose)
UBApplication::showMessage(tr("Exporting document..."));
// Check that the location is writeable
QFileInfo info(filename);
info.setFile(info.absolutePath());
if (!info.isWritable()) {
QMessageBox errorBox;
errorBox.setWindowTitle(tr("Export failed"));
errorBox.setText(tr("Unable to export to the selected location. You do not have the permissions necessary to save the file."));
errorBox.setIcon(QMessageBox::Critical);
errorBox.exec();
if (mIsVerbose)
UBApplication::showMessage(tr("Export failed: location not writable"));
QApplication::restoreOverrideCursor();
return;
}
bool persisted = this->persistsDocument(pDocumentProxy, filename);
if (mIsVerbose && persisted)
UBApplication::showMessage(tr("Export successful."));
QApplication::restoreOverrideCursor();
}
}
bool UBExportAdaptor::persistsDocument(UBDocumentProxy* pDocument, const QString& filename)
{
// Implemented in child classes
Q_UNUSED(pDocument);
Q_UNUSED(filename);
return false;
}
void UBExportAdaptor::showErrorsList(QList<QString> errorsList)
{
if (errorsList.count())

@ -43,8 +43,9 @@ class UBExportAdaptor : public QObject
virtual QString exportName() = 0;
virtual QString exportExtention() { return "";}
virtual void persist(UBDocumentProxy* pDocument) = 0;
virtual bool persistsDocument(UBDocumentProxy* pDocument, const QString& filename);
virtual void setVerbode(bool verbose)
virtual void setVerbose(bool verbose)
{
mIsVerbose = verbose;
}
@ -58,6 +59,8 @@ class UBExportAdaptor : public QObject
QString askForFileName(UBDocumentProxy* pDocument, const QString& pDialogTitle);
QString askForDirName(UBDocumentProxy* pDocument, const QString& pDialogTitle);
virtual void persistLocally(UBDocumentProxy* pDocumentProxy, const QString &pDialogTitle);
void showErrorsList(QList<QString> errorsList);
bool mIsVerbose;

@ -56,46 +56,12 @@ UBExportDocument::~UBExportDocument()
void UBExportDocument::persist(UBDocumentProxy* pDocumentProxy)
{
if (!pDocumentProxy)
return;
QString filename = askForFileName(pDocumentProxy, tr("Export as UBZ File"));
if (filename.length() > 0)
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if (mIsVerbose)
UBApplication::showMessage(tr("Exporting document..."));
bool success = persistsDocument(pDocumentProxy, filename);
if (mIsVerbose && success)
UBApplication::showMessage(tr("Export successful."));
QApplication::restoreOverrideCursor();
}
persistLocally(pDocumentProxy, tr("Export as UBZ File"));
}
bool UBExportDocument::persistsDocument(UBDocumentProxy* pDocumentProxy, QString filename)
bool UBExportDocument::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString &filename)
{
QFileInfo info(filename);
info.setFile(info.absolutePath());
if (!info.isWritable()) {
UBApplication::showMessage(tr("Export failed: location not writable"));
// The message is a bit discreet: also show a pop-up
QMessageBox errorBox;
errorBox.setWindowTitle(tr("Export failed"));
errorBox.setText(tr("Unable to export to the selected location. You do not have the permissions necessary to save the file."));
errorBox.setIcon(QMessageBox::Critical);
errorBox.exec();
return false;
}
QuaZip zip(filename);
zip.setFileNameCodec("UTF-8");
if(!zip.open(QuaZip::mdCreate))

@ -49,7 +49,7 @@ class UBExportDocument : public UBExportAdaptor, public UBProcessingProgressList
virtual QString exportExtention();
virtual void persist(UBDocumentProxy* pDocument);
virtual bool persistsDocument(UBDocumentProxy* pDocument, QString filename);
virtual bool persistsDocument(UBDocumentProxy* pDocument, const QString& filename);
virtual void processing(const QString& pObjectName, int pCurrent, int pTotal);
};

@ -63,6 +63,8 @@ UBExportFullPDF::UBExportFullPDF(QObject *parent)
QDesktopWidget* desktop = UBApplication::desktop();
int dpiCommon = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2;
mScaleFactor = 72.0f / dpiCommon;
mSimpleExporter = new UBExportPDF();
}
@ -131,44 +133,12 @@ void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, const QStr
void UBExportFullPDF::persist(UBDocumentProxy* pDocumentProxy)
{
if (!pDocumentProxy)
return;
QString filename = askForFileName(pDocumentProxy, tr("Export as PDF File"));
if (filename.length() > 0)
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if (mIsVerbose)
UBApplication::showMessage(tr("Exporting document..."));
bool success = persistsDocument(pDocumentProxy, filename);
if (mIsVerbose && success)
UBApplication::showMessage(tr("Export successful."));
QApplication::restoreOverrideCursor();
}
persistLocally(pDocumentProxy, tr("Export as PDF File"));
}
bool UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename)
{
QFileInfo info(filename);
info.setFile(info.absolutePath());
if (!info.isWritable()) {
UBApplication::showMessage(tr("Export failed: location not writable"));
// The message is a bit discreet: also show a pop-up
QMessageBox errorBox;
errorBox.setWindowTitle(tr("Export failed"));
errorBox.setText(tr("Unable to export to the selected location. You do not have the permissions necessary to save the file."));
errorBox.setIcon(QMessageBox::Critical);
errorBox.exec();
return false;
}
QFile file(filename);
if (file.exists()) file.remove();
@ -277,7 +247,7 @@ bool UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QS
qDebug() << "PdfMerger failed to merge documents to " << filename << " - Exception : " << e.what();
// default to raster export
UBExportPDF::persistsDocument(pDocumentProxy, filename);
mSimpleExporter->persistsDocument(pDocumentProxy, filename);
}
if (!UBApplication::app()->isVerbose())

@ -30,6 +30,7 @@
#include <QtCore>
#include "UBExportAdaptor.h"
#include "UBExportPDF.h"
class UBDocumentProxy;
@ -53,6 +54,8 @@ class UBExportFullPDF : public UBExportAdaptor
private:
float mScaleFactor;
bool mHasPDFBackgrounds;
UBExportPDF * mSimpleExporter;
};
#endif /* UBExportFullPDF_H_ */

@ -58,25 +58,11 @@ UBExportPDF::~UBExportPDF()
void UBExportPDF::persist(UBDocumentProxy* pDocumentProxy)
{
if (!pDocumentProxy)
return;
QString filename = askForFileName(pDocumentProxy, tr("Export as PDF File"));
if (filename.length() > 0)
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
UBApplication::showMessage(tr("Exporting document..."));
persistsDocument(pDocumentProxy, filename);
UBApplication::showMessage(tr("Export successful."));
QApplication::restoreOverrideCursor();
}
persistLocally(pDocumentProxy, tr("Export as PDF File"));
}
void UBExportPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename)
bool UBExportPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename)
{
QPrinter pdfPrinter;
@ -128,6 +114,8 @@ void UBExportPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QStrin
scene->setBackground(isDark, isCrossed);
}
if(!painterNeedsBegin) pdfPainter.end();
return true;
}
QString UBExportPDF::exportExtention()

@ -45,7 +45,7 @@ class UBExportPDF : public UBExportAdaptor
virtual QString exportExtention();
virtual void persist(UBDocumentProxy* pDocument);
static void persistsDocument(UBDocumentProxy* pDocument, const QString& filename);
virtual bool persistsDocument(UBDocumentProxy* pDocument, const QString& filename);
};
#endif /* UBEXPORTPDF_H_ */

Loading…
Cancel
Save