File indexing completed on 2026-10-04 12:08:48

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2002-2004 Rob Buis <buis@kde.org>
0003    SPDX-FileCopyrightText: 2002 Lennart Kudling <kudling@kde.org>
0004    SPDX-FileCopyrightText: 2002 Werner Trobin <trobin@kde.org>
0005    SPDX-FileCopyrightText: 2004 Nicolas Goutte <nicolasg@snafu.de>
0006    SPDX-FileCopyrightText: 2005 Tim Beaulen <tbscope@gmail.com>
0007    SPDX-FileCopyrightText: 2005 Thomas Zander <zander@kde.org>
0008    SPDX-FileCopyrightText: 2005-2006 David Faure <faure@kde.org>
0009    SPDX-FileCopyrightText: 2006 Inge Wallin <inge@lysator.liu.se>
0010    SPDX-FileCopyrightText: 2006 Laurent Montel <montel@kde.org>
0011    SPDX-FileCopyrightText: 2006 Christian Mueller <cmueller@gmx.de>
0012    SPDX-FileCopyrightText: 2007-2008, 2012 Jan Hambrecht <jaham@gmx.net>
0013    SPDX-FileCopyrightText: 2019 Dag Andersen <danders@get2net.dk>
0014 
0015    SPDX-License-Identifier: LGPL-2.0-or-later
0016 */
0017 
0018 #include "ImageExport.h"
0019 #include "ImageExportOptionsWidget.h"
0020 
0021 #include <KarbonDocument.h>
0022 #include <KoDocument.h>
0023 
0024 #include <KoShapePainter.h>
0025 #include <KoFilter.h>
0026 #include <KoFilterChain.h>
0027 #include <KoFilterManager.h>
0028 #include <KoZoomHandler.h>
0029 #include <KoUnit.h>
0030 #include <KoDialog.h>
0031 #include <KoPAPageBase.h>
0032 
0033 #include <kpluginfactory.h>
0034 
0035 #include <QImage>
0036 
0037 K_PLUGIN_FACTORY_WITH_JSON(PngExportFactory, "calligra_filter_karbon2image.json",
0038                            registerPlugin<ImageExport>();)
0039 
0040 
0041 ImageExport::ImageExport(QObject*parent, const QVariantList&)
0042     : KoFilter(parent)
0043 {
0044 }
0045 
0046 KoFilter::ConversionStatus ImageExport::convert(const QByteArray& from, const QByteArray& to)
0047 {
0048     if (from != KoOdf::mimeType(KoOdf::Graphics)) {
0049         return KoFilter::BadMimeType;
0050     }
0051 
0052     QString format;
0053 
0054     if (to == "image/png") {
0055         format = "PNG";
0056     } else if (to == "image/jpeg") {
0057         format = "JPEG";
0058     } else {
0059         return KoFilter::BadMimeType;
0060     }
0061 
0062     KarbonDocument *doc = dynamic_cast<KarbonDocument*>(m_chain->inputDocument());
0063     if (!doc) {
0064         return KoFilter::InternalError;
0065     }
0066 
0067     KoPAPageBase *page = doc->pages().first();
0068 
0069     KoShapePainter painter;
0070     painter.setShapes(page->shapes());
0071 
0072     // get the bounding rect of the content
0073     QRectF shapesRect = painter.contentRect();
0074     // get the size in point
0075     QSizeF pointSize = shapesRect.size();
0076     // get the size in pixel (100% zoom)
0077     KoZoomHandler zoomHandler;
0078     QSize pixelSize = zoomHandler.documentToView(pointSize).toSize();
0079     //transparent white by default
0080     QColor backgroundColor(QColor(255, 255, 255, 0));
0081 
0082     if (! m_chain->manager()->getBatchMode()) {
0083         QApplication::restoreOverrideCursor();
0084         ImageExportOptionsWidget * widget = new ImageExportOptionsWidget(doc);
0085         widget->setUnit(doc->unit());
0086         widget->setBackgroundColor(backgroundColor);
0087         widget->enableBackgroundOpacity(format == "PNG");
0088 
0089         KoDialog dlg;
0090         dlg.setCaption(i18n("%1 Export Options", format));
0091         dlg.setButtons(KoDialog::Ok | KoDialog::Cancel);
0092         dlg.setMainWidget(widget);
0093         int result = dlg.exec();
0094         QApplication::setOverrideCursor(Qt::BusyCursor);
0095         if (result != QDialog::Accepted) {
0096             return KoFilter::UserCancelled;
0097         }
0098         pixelSize = widget->pixelSize();
0099         backgroundColor = widget->backgroundColor();
0100 
0101         page = widget->page();
0102         if (!page) {
0103             return KoFilter::InternalError;
0104         }
0105         painter.setShapes(page->shapes());
0106     }
0107 
0108     QImage image(pixelSize, QImage::Format_ARGB32);
0109 
0110     // draw the background of the image
0111     image.fill(backgroundColor.rgba());
0112 
0113     // paint the shapes
0114     painter.paint(image);
0115 
0116     if(!image.save(m_chain->outputFile(), format.toLatin1())) {
0117         return KoFilter::CreationError;
0118     }
0119 
0120     return KoFilter::OK;
0121 }
0122 
0123 #include "ImageExport.moc"
0124