File indexing completed on 2025-01-19 13:26:34

0001 /* This file is part of the KDE project
0002    Copyright (C) 2002-2004 Rob Buis <buis@kde.org>
0003    Copyright (C) 2002 Lennart Kudling <kudling@kde.org>
0004    Copyright (C) 2002 Werner Trobin <trobin@kde.org>
0005    Copyright (C) 2004 Nicolas Goutte <nicolasg@snafu.de>
0006    Copyright (C) 2005 Tim Beaulen <tbscope@gmail.com>
0007    Copyright (C) 2005 Thomas Zander <zander@kde.org>
0008    Copyright (C) 2005-2006 David Faure <faure@kde.org>
0009    Copyright (C) 2006 Inge Wallin <inge@lysator.liu.se>
0010    Copyright (C) 2006 Laurent Montel <montel@kde.org>
0011    Copyright (C) 2006 Christian Mueller <cmueller@gmx.de>
0012    Copyright (C) 2007-2008,2012 Jan Hambrecht <jaham@gmx.net>
0013    Copyright (C) 2019 Dag Andersen <danders@get2net.dk>
0014 
0015    This library is free software; you can redistribute it and/or
0016    modify it under the terms of the GNU Library General Public
0017    License as published by the Free Software Foundation; either
0018    version 2 of the License, or (at your option) any later version.
0019 
0020    This library is distributed in the hope that it will be useful,
0021    but WITHOUT ANY WARRANTY; without even the implied warranty of
0022    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0023    Library General Public License for more details.
0024 
0025    You should have received a copy of the GNU Library General Public License
0026    along with this library; see the file COPYING.LIB.  If not, write to
0027    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0028  * Boston, MA 02110-1301, USA.
0029 */
0030 
0031 #include "ImageExport.h"
0032 #include "ImageExportOptionsWidget.h"
0033 
0034 #include <KarbonDocument.h>
0035 #include <KoDocument.h>
0036 
0037 #include <KoShapePainter.h>
0038 #include <KoFilter.h>
0039 #include <KoFilterChain.h>
0040 #include <KoFilterManager.h>
0041 #include <KoZoomHandler.h>
0042 #include <KoUnit.h>
0043 #include <KoDialog.h>
0044 #include <KoPAPageBase.h>
0045 
0046 #include <kpluginfactory.h>
0047 
0048 #include <QImage>
0049 
0050 K_PLUGIN_FACTORY_WITH_JSON(PngExportFactory, "calligra_filter_karbon2image.json",
0051                            registerPlugin<ImageExport>();)
0052 
0053 
0054 ImageExport::ImageExport(QObject*parent, const QVariantList&)
0055     : KoFilter(parent)
0056 {
0057 }
0058 
0059 KoFilter::ConversionStatus ImageExport::convert(const QByteArray& from, const QByteArray& to)
0060 {
0061     if (from != KoOdf::mimeType(KoOdf::Graphics)) {
0062         return KoFilter::BadMimeType;
0063     }
0064 
0065     QString format;
0066 
0067     if (to == "image/png") {
0068         format = "PNG";
0069     } else if (to == "image/jpeg") {
0070         format = "JPEG";
0071     } else {
0072         return KoFilter::BadMimeType;
0073     }
0074 
0075     KarbonDocument *doc = dynamic_cast<KarbonDocument*>(m_chain->inputDocument());
0076     if (!doc) {
0077         return KoFilter::InternalError;
0078     }
0079 
0080     KoPAPageBase *page = doc->pages().first();
0081 
0082     KoShapePainter painter;
0083     painter.setShapes(page->shapes());
0084 
0085     // get the bounding rect of the content
0086     QRectF shapesRect = painter.contentRect();
0087     // get the size in point
0088     QSizeF pointSize = shapesRect.size();
0089     // get the size in pixel (100% zoom)
0090     KoZoomHandler zoomHandler;
0091     QSize pixelSize = zoomHandler.documentToView(pointSize).toSize();
0092     //transparent white by default
0093     QColor backgroundColor(QColor(255, 255, 255, 0));
0094 
0095     if (! m_chain->manager()->getBatchMode()) {
0096         QApplication::restoreOverrideCursor();
0097         ImageExportOptionsWidget * widget = new ImageExportOptionsWidget(doc);
0098         widget->setUnit(doc->unit());
0099         widget->setBackgroundColor(backgroundColor);
0100         widget->enableBackgroundOpacity(format == "PNG");
0101 
0102         KoDialog dlg;
0103         dlg.setCaption(i18n("%1 Export Options", format));
0104         dlg.setButtons(KoDialog::Ok | KoDialog::Cancel);
0105         dlg.setMainWidget(widget);
0106         int result = dlg.exec();
0107         QApplication::setOverrideCursor(Qt::BusyCursor);
0108         if (result != QDialog::Accepted) {
0109             return KoFilter::UserCancelled;
0110         }
0111         pixelSize = widget->pixelSize();
0112         backgroundColor = widget->backgroundColor();
0113 
0114         page = widget->page();
0115         if (!page) {
0116             return KoFilter::InternalError;
0117         }
0118         painter.setShapes(page->shapes());
0119     }
0120 
0121     QImage image(pixelSize, QImage::Format_ARGB32);
0122 
0123     // draw the background of the image
0124     image.fill(backgroundColor.rgba());
0125 
0126     // paint the shapes
0127     painter.paint(image);
0128 
0129     if(!image.save(m_chain->outputFile(), format.toLatin1())) {
0130         return KoFilter::CreationError;
0131     }
0132 
0133     return KoFilter::OK;
0134 }
0135 
0136 #include "ImageExport.moc"
0137