From 0cbb3e0afd86c3b7ec04b1cbd71d4edaa5d80f30 Mon Sep 17 00:00:00 2001 From: Lukáš Lalinský Date: Thu, 9 Jul 2009 12:26:12 +0200 Subject: Refactor diagram exporting code --- src/export/export.pri | 15 ++++++++++ src/export/exporter.cpp | 33 ++++++++++++++++++++++ src/export/exporter.h | 39 +++++++++++++++++++++++++ src/export/exporterlist.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++ src/export/exporterlist.h | 34 ++++++++++++++++++++++ src/export/pdfexporter.cpp | 48 +++++++++++++++++++++++++++++++ src/export/pdfexporter.h | 30 ++++++++++++++++++++ src/export/pngexporter.cpp | 49 ++++++++++++++++++++++++++++++++ src/export/pngexporter.h | 30 ++++++++++++++++++++ src/export/svgexporter.cpp | 48 +++++++++++++++++++++++++++++++ src/export/svgexporter.h | 30 ++++++++++++++++++++ src/mainwindow.cpp | 59 +++++++++----------------------------- src/src.pro | 1 + 13 files changed, 440 insertions(+), 45 deletions(-) create mode 100644 src/export/export.pri create mode 100644 src/export/exporter.cpp create mode 100644 src/export/exporter.h create mode 100644 src/export/exporterlist.cpp create mode 100644 src/export/exporterlist.h create mode 100644 src/export/pdfexporter.cpp create mode 100644 src/export/pdfexporter.h create mode 100644 src/export/pngexporter.cpp create mode 100644 src/export/pngexporter.h create mode 100644 src/export/svgexporter.cpp create mode 100644 src/export/svgexporter.h diff --git a/src/export/export.pri b/src/export/export.pri new file mode 100644 index 0000000..ae7d15f --- /dev/null +++ b/src/export/export.pri @@ -0,0 +1,15 @@ +DEPENDPATH += $$PWD + +SOURCES += \ + exporter.cpp \ + exporterlist.cpp \ + pngexporter.cpp \ + pdfexporter.cpp \ + svgexporter.cpp + +HEADERS += \ + exporter.h \ + exporterlist.h \ + pngexporter.h \ + pdfexporter.h \ + svgexporter.h diff --git a/src/export/exporter.cpp b/src/export/exporter.cpp new file mode 100644 index 0000000..239a550 --- /dev/null +++ b/src/export/exporter.cpp @@ -0,0 +1,33 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include +#include "diagramdocument.h" +#include "exporter.h" + +QString +Exporter::filter() const +{ + return QString("%1 (*%2)").arg(name(), extension()); +} + +void +Exporter::renderDocument(QPainter *painter, DiagramDocument *document, const QRectF &target, const QRectF &source) +{ + document->setPrinting(true); + document->render(painter, target, source); + document->setPrinting(false); +} diff --git a/src/export/exporter.h b/src/export/exporter.h new file mode 100644 index 0000000..ab7f614 --- /dev/null +++ b/src/export/exporter.h @@ -0,0 +1,39 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef EXPORT_EXPORTER_H +#define EXPORT_EXPORTER_H + +#include +#include +class QPainter; +class DiagramDocument; + +class Exporter +{ +public: + virtual ~Exporter() {}; + + virtual QString filter() const; + virtual QString name() const = 0; + virtual QString extension() const = 0; + virtual void exportToFile(const QString &fileName, DiagramDocument *document) = 0; + +protected: + void renderDocument(QPainter *painter, DiagramDocument *document, const QRectF &target, const QRectF &source); +}; + +#endif diff --git a/src/export/exporterlist.cpp b/src/export/exporterlist.cpp new file mode 100644 index 0000000..e1c9554 --- /dev/null +++ b/src/export/exporterlist.cpp @@ -0,0 +1,69 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include +#include "exporter.h" +#include "pdfexporter.h" +#include "pngexporter.h" +#include "svgexporter.h" +#include "exporterlist.h" + +ExporterList::ExporterList() +{ + append(new PNGExporter()); + append(new PDFExporter()); + append(new SVGExporter()); +} + +ExporterList::~ExporterList() +{ + qDeleteAll(*this); +} + +QString +ExporterList::filters() const +{ + QStringList filters; + for (int i = 0; i < count(); i++) { + filters.append(at(i)->filter()); + } + return filters.join(";;"); +} + +Exporter * +ExporterList::lookup(QString *fileName, const QString &selectedFilter) const +{ + int index = fileName->lastIndexOf('.'); + if (index == -1 && !selectedFilter.isEmpty()) { + // Add missing extension based on the selected filter + for (int i = 0; i < count(); i++) { + Exporter *exporter = at(i); + if (exporter->filter() == selectedFilter) { + fileName->append(exporter->extension()); + break; + } + } + } + + for (int i = 0; i < count(); i++) { + Exporter *exporter = at(i); + if (fileName->endsWith(exporter->extension(), Qt::CaseInsensitive)) { + return exporter; + } + } + + return NULL; +} diff --git a/src/export/exporterlist.h b/src/export/exporterlist.h new file mode 100644 index 0000000..ab640d9 --- /dev/null +++ b/src/export/exporterlist.h @@ -0,0 +1,34 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef EXPORT_EXPORTERLIST_H +#define EXPORT_EXPORTERLIST_H + +#include +#include +class Exporter; + +class ExporterList : public QList +{ +public: + ExporterList(); + virtual ~ExporterList(); + + virtual QString filters() const; + virtual Exporter *lookup(QString *fileName, const QString &selectedFilter) const; +}; + +#endif diff --git a/src/export/pdfexporter.cpp b/src/export/pdfexporter.cpp new file mode 100644 index 0000000..2fb0812 --- /dev/null +++ b/src/export/pdfexporter.cpp @@ -0,0 +1,48 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include +#include +#include +#include "diagramdocument.h" +#include "pdfexporter.h" + +QString +PDFExporter::name() const +{ + return "Portable Document Format"; +} + +QString +PDFExporter::extension() const +{ + return ".pdf"; +} + +void +PDFExporter::exportToFile(const QString &fileName, DiagramDocument *document) +{ + QRectF boundingRect = document->itemsBoundingRect().adjusted(-2, -2, 2, 2); + QSize size(int(ceil(boundingRect.width())), int(ceil(boundingRect.height()))); + + QPrinter printer; + printer.setOutputFormat(QPrinter::PdfFormat); + printer.setOutputFileName(fileName); + QPainter painter(&printer); + painter.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::Antialiasing); + + renderDocument(&painter, document, QRectF(), boundingRect); +} diff --git a/src/export/pdfexporter.h b/src/export/pdfexporter.h new file mode 100644 index 0000000..c06cf6a --- /dev/null +++ b/src/export/pdfexporter.h @@ -0,0 +1,30 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef EXPORT_PDFEXPORTER_H +#define EXPORT_PDFEXPORTER_H + +#include "exporter.h" + +class PDFExporter : public Exporter +{ +public: + virtual QString name() const; + virtual QString extension() const; + virtual void exportToFile(const QString &fileName, DiagramDocument *document); +}; + +#endif diff --git a/src/export/pngexporter.cpp b/src/export/pngexporter.cpp new file mode 100644 index 0000000..32716a5 --- /dev/null +++ b/src/export/pngexporter.cpp @@ -0,0 +1,49 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include +#include +#include +#include "diagramdocument.h" +#include "pngexporter.h" + +QString +PNGExporter::name() const +{ + return "Portable Network Graphics"; +} + +QString +PNGExporter::extension() const +{ + return ".png"; +} + +void +PNGExporter::exportToFile(const QString &fileName, DiagramDocument *document) +{ + QRectF boundingRect = document->itemsBoundingRect().adjusted(-2, -2, 2, 2); + QSize size(int(ceil(boundingRect.width())), int(ceil(boundingRect.height()))); + + QImage image(size, QImage::Format_RGB32); + QPainter painter(&image); + painter.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::Antialiasing); + painter.fillRect(image.rect(), Qt::white); + + renderDocument(&painter, document, image.rect(), boundingRect); + + image.convertToFormat(QImage::Format_Indexed8).save(fileName, "PNG"); +} diff --git a/src/export/pngexporter.h b/src/export/pngexporter.h new file mode 100644 index 0000000..64cb0ab --- /dev/null +++ b/src/export/pngexporter.h @@ -0,0 +1,30 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef EXPORT_PNGEXPORTER_H +#define EXPORT_PNGEXPORTER_H + +#include "exporter.h" + +class PNGExporter : public Exporter +{ +public: + virtual QString name() const; + virtual QString extension() const; + virtual void exportToFile(const QString &fileName, DiagramDocument *document); +}; + +#endif diff --git a/src/export/svgexporter.cpp b/src/export/svgexporter.cpp new file mode 100644 index 0000000..7183dea --- /dev/null +++ b/src/export/svgexporter.cpp @@ -0,0 +1,48 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#include +#include +#include +#include "diagramdocument.h" +#include "svgexporter.h" + +QString +SVGExporter::name() const +{ + return "Scalable Vector Graphics"; +} + +QString +SVGExporter::extension() const +{ + return ".svg"; +} + +void +SVGExporter::exportToFile(const QString &fileName, DiagramDocument *document) +{ + QRectF boundingRect = document->itemsBoundingRect().adjusted(-2, -2, 2, 2); + QSize size(int(ceil(boundingRect.width())), int(ceil(boundingRect.height()))); + + QSvgGenerator generator; + generator.setSize(size); + generator.setFileName(fileName); + QPainter painter(&generator); + painter.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::Antialiasing); + + renderDocument(&painter, document, QRectF(), boundingRect); +} diff --git a/src/export/svgexporter.h b/src/export/svgexporter.h new file mode 100644 index 0000000..3279fb3 --- /dev/null +++ b/src/export/svgexporter.h @@ -0,0 +1,30 @@ +// Copyright (C) 2009 Lukas Lalinsky +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +#ifndef EXPORT_SVGEXPORTER_H +#define EXPORT_SVGEXPORTER_H + +#include "exporter.h" + +class SVGExporter : public Exporter +{ +public: + virtual QString name() const; + virtual QString extension() const; + virtual void exportToFile(const QString &fileName, DiagramDocument *document); +}; + +#endif diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eda1e14..7ba9cac 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -41,6 +41,8 @@ #include "commands.h" #include "mainwindow.h" +#include "export/exporter.h" +#include "export/exporterlist.h" using namespace std; @@ -56,6 +58,8 @@ public: QActionGroup *notationActionGroup; QMenu *notationMenu; + + ExporterList exporters; }; MainWindow::MainWindow() @@ -481,51 +485,16 @@ MainWindow::saveAs() void MainWindow::exportPNG() { - QStringList filters; - filters << "Portable Network Graphics (*.png)"; - filters << "Portable Document Format (*.pdf)"; - filters << "Scalable Vector Graphics (*.svg)"; - QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), filters.join(";;")); - if (!fileName.isNull()) { - QRectF boundingRect = m_model->itemsBoundingRect().adjusted(-2, -2, 2, 2); - QSize size(int(ceil(boundingRect.width())), int(ceil(boundingRect.height()))); - if (fileName.endsWith(".svg", Qt::CaseInsensitive)) { - QSvgGenerator generator; - generator.setSize(size); - generator.setFileName(fileName); - QPainter painter(&generator); - painter.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::Antialiasing); - m_model->setPrinting(true); - m_model->render(&painter, QRectF(), boundingRect); - m_model->setPrinting(false); - } - else if (fileName.endsWith(".pdf", Qt::CaseInsensitive)) { - QPrinter printer; - printer.setOutputFormat(QPrinter::PdfFormat); - printer.setOutputFileName(fileName); - QPainter painter(&printer); - painter.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::Antialiasing); - m_model->setPrinting(true); - m_model->render(&painter, QRectF(), boundingRect); - m_model->setPrinting(false); - } - else if (fileName.endsWith(".png", Qt::CaseInsensitive)) { - QImage image(size, QImage::Format_RGB32); - QPainter painter(&image); - painter.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::Antialiasing); - painter.fillRect(image.rect(), Qt::white); - m_model->setPrinting(true); - m_model->render(&painter, image.rect(), boundingRect); - m_model->setPrinting(false); - if (true) - image.convertToFormat(QImage::Format_Indexed8).save(fileName, "PNG"); - else - image.save(fileName, "PNG"); - } - else { - QMessageBox::critical(this, tr("Error"), tr("Unknown format.")); - } - } + QString selectedFilter; + QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), d->exporters.filters(), &selectedFilter); + if (!fileName.isNull()) { + Exporter *exporter = d->exporters.lookup(&fileName, selectedFilter); + if (!exporter) { + QMessageBox::critical(this, tr("Error"), tr("Unknown format.")); + return; + } + exporter->exportToFile(fileName, m_model); + } } void diff --git a/src/src.pro b/src/src.pro index bcc666d..4d9cc63 100644 --- a/src/src.pro +++ b/src/src.pro @@ -12,6 +12,7 @@ include(src.pri) include(diagram/diagram.pri) include(items/items.pri) include(utils/utils.pri) +include(export/export.pri) include(../updateqm.pri) -- cgit v1.2.3-54-g00ecf