diff --git a/filters/karbon/image/ImageExport.cpp b/filters/karbon/image/ImageExport.cpp index 6dda2c6b422..51eecee56fe 100644 --- a/filters/karbon/image/ImageExport.cpp +++ b/filters/karbon/image/ImageExport.cpp @@ -1,134 +1,137 @@ /* This file is part of the KDE project Copyright (C) 2002-2004 Rob Buis Copyright (C) 2002 Lennart Kudling Copyright (C) 2002 Werner Trobin Copyright (C) 2004 Nicolas Goutte Copyright (C) 2005 Tim Beaulen Copyright (C) 2005 Thomas Zander Copyright (C) 2005-2006 David Faure Copyright (C) 2006 Inge Wallin Copyright (C) 2006 Laurent Montel Copyright (C) 2006 Christian Mueller Copyright (C) 2007-2008,2012 Jan Hambrecht + Copyright (C) 2019 Dag Andersen This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "ImageExport.h" #include "ImageExportOptionsWidget.h" #include #include #include #include #include #include #include #include #include #include #include #include K_PLUGIN_FACTORY_WITH_JSON(PngExportFactory, "calligra_filter_karbon2image.json", registerPlugin();) ImageExport::ImageExport(QObject*parent, const QVariantList&) : KoFilter(parent) { } -KoFilter::ConversionStatus -ImageExport::convert(const QByteArray& from, const QByteArray& to) +KoFilter::ConversionStatus ImageExport::convert(const QByteArray& from, const QByteArray& to) { + if (from != KoOdf::mimeType(KoOdf::Graphics)) { + return KoFilter::BadMimeType; + } + QString format; + if (to == "image/png") { format = "PNG"; - } else if(to == "image/jpeg") { - format = "JPG"; - } - if (format.isEmpty()) { - return KoFilter::NotImplemented; - } - if (from != "application/vnd.oasis.opendocument.graphics") { - return KoFilter::NotImplemented; + } else if (to == "image/jpeg") { + format = "JPEG"; + } else { + return KoFilter::BadMimeType; } - KoDocument* document = m_chain->inputDocument(); - if (! document) - return KoFilter::ParsingError; + KarbonDocument *doc = dynamic_cast(m_chain->inputDocument()); + if (!doc) { + KoFilter::InternalError; + } - KarbonDocument* doc = dynamic_cast(document); - if (doc) { - KoShapePainter painter; - QList pages = doc->pages(); - if (pages.isEmpty()) { - return KoFilter::WrongFormat; + KoPAPageBase *page = doc->pages().first(); + + KoShapePainter painter; + painter.setShapes(page->shapes()); + + // get the bounding rect of the content + QRectF shapesRect = painter.contentRect(); + // get the size in point + QSizeF pointSize = shapesRect.size(); + // get the size in pixel (100% zoom) + KoZoomHandler zoomHandler; + QSize pixelSize = zoomHandler.documentToView(pointSize).toSize(); + //transparent white by default + QColor backgroundColor(QColor(255, 255, 255, 0)); + + if (! m_chain->manager()->getBatchMode()) { + QApplication::restoreOverrideCursor(); + ImageExportOptionsWidget * widget = new ImageExportOptionsWidget(doc); + widget->setUnit(doc->unit()); + widget->setBackgroundColor(backgroundColor); + widget->enableBackgroundOpacity(format == "PNG"); + + KoDialog dlg; + dlg.setCaption(i18n("%1 Export Options", format)); + dlg.setButtons(KoDialog::Ok | KoDialog::Cancel); + dlg.setMainWidget(widget); + int result = dlg.exec(); + QApplication::setOverrideCursor(Qt::BusyCursor); + if (result != QDialog::Accepted) { + return KoFilter::UserCancelled; } - // TODO: Handle multiple pages - painter.setShapes(pages.at(0)->shapes()); - - // get the bounding rect of the content - QRectF shapesRect = painter.contentRect(); - // get the size in point - QSizeF pointSize = shapesRect.size(); - // get the size in pixel (100% zoom) - KoZoomHandler zoomHandler; - QSize pixelSize = zoomHandler.documentToView(pointSize).toSize(); - //transparent white by default - QColor backgroundColor(QColor(255, 255, 255, 0)); - - if (! m_chain->manager()->getBatchMode()) { - ImageExportOptionsWidget * widget = new ImageExportOptionsWidget(pointSize); - widget->setUnit(document->unit()); - widget->setBackgroundColor(backgroundColor); - widget->enableBackgroundOpacity(format == "PNG"); - - KoDialog dlg; - dlg.setCaption(i18n("PNG Export Options")); - dlg.setButtons(KoDialog::Ok | KoDialog::Cancel); - dlg.setMainWidget(widget); - if (dlg.exec() != QDialog::Accepted) - return KoFilter::UserCancelled; - - pixelSize = widget->pixelSize(); - backgroundColor = widget->backgroundColor(); + pixelSize = widget->pixelSize(); + backgroundColor = widget->backgroundColor(); + + page = widget->page(); + if (!page) { + return KoFilter::InternalError; } - QImage image(pixelSize, QImage::Format_ARGB32); + painter.setShapes(page->shapes()); + } - // draw the background of the image - image.fill(backgroundColor.rgba()); + QImage image(pixelSize, QImage::Format_ARGB32); - // paint the shapes - painter.paint(image); + // draw the background of the image + image.fill(backgroundColor.rgba()); - if(!image.save(m_chain->outputFile(), format.toLatin1())) { - return KoFilter::CreationError; - } + // paint the shapes + painter.paint(image); - } else { - return KoFilter::WrongFormat; + if(!image.save(m_chain->outputFile(), format.toLatin1())) { + return KoFilter::CreationError; } return KoFilter::OK; } #include "ImageExport.moc" diff --git a/filters/karbon/image/ImageExportOptionsWidget.cpp b/filters/karbon/image/ImageExportOptionsWidget.cpp index e424cabca5d..f77ec79914c 100644 --- a/filters/karbon/image/ImageExportOptionsWidget.cpp +++ b/filters/karbon/image/ImageExportOptionsWidget.cpp @@ -1,222 +1,254 @@ /* This file is part of the KDE project * Copyright (C) 2008 Jan Hambrecht + * Copyright (C) 2019 Dag Andersen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "ImageExportOptionsWidget.h" +#include +#include + #include // for POINT_TO_INCH #include +#include +#include -ImageExportOptionsWidget::ImageExportOptionsWidget(const QSizeF &pointSize, QWidget * parent) - : QWidget(parent), m_pointSize(pointSize) +ImageExportOptionsWidget::ImageExportOptionsWidget(KarbonDocument *doc, QWidget * parent) + : QWidget(parent), m_doc(doc) { KoUnit unit; widget.setupUi(this); widget.pxWidth->setRange(1, 10000); widget.pxWidth->setAlignment(Qt::AlignRight); widget.pxWidth->setSuffix(" px"); widget.pxHeight->setRange(1, 10000); widget.pxHeight->setAlignment(Qt::AlignRight); widget.pxHeight->setSuffix(" px"); widget.unitWidth->setMinMaxStep(0, 10000, 1); widget.unitHeight->setMinMaxStep(0, 10000, 1); widget.dpi->setRange(1, 10000); widget.dpi->setValue(KoDpi::dpiX()); widget.dpi->setAlignment(Qt::AlignRight); widget.dpi->setSuffix(" DPI"); widget.pxAspect->setKeepAspectRatio(true); widget.unitAspect->setKeepAspectRatio(true); widget.unit->addItems(KoUnit::listOfUnitNameForUi(KoUnit::HidePixel)); widget.unit->setCurrentIndex(unit.indexInListForUi(KoUnit::HidePixel)); widget.backColor->setColor(Qt::white); widget.opacity->setMinimum(0.0); widget.opacity->setMaximum(100.0); widget.opacity->setValue(0.0); - widget.unitWidth->changeValue(pointSize.width()); - widget.unitHeight->changeValue(pointSize.height()); - updateFromPointSize(pointSize); + + for (int i = 0; i < doc->pageCount(); ++i) { + widget.pageCombo->addItem(i18n("Page %1", i+1)); + } + setPage(0); + + widget.unitWidth->changeValue(m_pointSize.width()); + widget.unitHeight->changeValue(m_pointSize.height()); connect(widget.unitWidth, SIGNAL(valueChangedPt(qreal)), this, SLOT(unitWidthChanged(qreal))); connect(widget.unitHeight, SIGNAL(valueChangedPt(qreal)), this, SLOT(unitHeightChanged(qreal))); connect(widget.pxWidth, SIGNAL(valueChanged(int)), this, SLOT(pxWidthChanged(int))); connect(widget.pxHeight, SIGNAL(valueChanged(int)), this, SLOT(pxHeightChanged(int))); connect(widget.dpi, SIGNAL(valueChanged(int)), this, SLOT(dpiChanged(int))); connect(widget.unit, SIGNAL(activated(int)), this, SLOT(unitChanged(int))); connect(widget.pxAspect, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(aspectChanged(bool))); connect(widget.unitAspect, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(aspectChanged(bool))); + connect(widget.pageCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setPage(int))); +} + +void ImageExportOptionsWidget::setPage(int idx) +{ + KoPAPageBase *page = m_doc->pages().value(idx); + if (!page) { + page = m_doc->pages().first(); + } + KoShapePainter painter; + painter.setShapes(page->shapes()); + // get the bounding rect of the content + QRectF shapesRect = painter.contentRect(); + // get the size in point + m_pointSize = shapesRect.size(); + updateFromPointSize(m_pointSize); +} + +KoPAPageBase *ImageExportOptionsWidget::page() const +{ + return m_doc->pages().value(widget.pageCombo->currentIndex()); } void ImageExportOptionsWidget::setUnit(const KoUnit &unit) { widget.unitWidth->setUnit(unit); widget.unitHeight->setUnit(unit); widget.unit->setCurrentIndex(unit.indexInListForUi(KoUnit::HidePixel)); } QSize ImageExportOptionsWidget::pixelSize() const { return QSize(widget.pxWidth->value(), widget.pxHeight->value()); } QSizeF ImageExportOptionsWidget::pointSize() const { return QSizeF(widget.unitWidth->value(), widget.unitHeight->value()); } void ImageExportOptionsWidget::setBackgroundColor(const QColor &color) { blockChildSignals(true); widget.backColor->setColor(color); widget.opacity->setValue(color.alphaF() * 100.0); blockChildSignals(false); } QColor ImageExportOptionsWidget::backgroundColor() const { QColor color = widget.backColor->color(); color.setAlphaF(0.01 * widget.opacity->value()); return color; } void ImageExportOptionsWidget::updateFromPointSize(const QSizeF &pointSize) { blockChildSignals(true); widget.pxWidth->setValue(qRound(POINT_TO_INCH(pointSize.width()) * widget.dpi->value())); widget.pxHeight->setValue(qRound(POINT_TO_INCH(pointSize.height()) * widget.dpi->value())); blockChildSignals(false); } void ImageExportOptionsWidget::updateFromPixelSize(const QSize &pixelSize) { blockChildSignals(true); double inchWidth = static_cast(pixelSize.width()) / static_cast(widget.dpi->value()); double inchHeight = static_cast(pixelSize.height()) / static_cast(widget.dpi->value()); widget.unitWidth->changeValue(INCH_TO_POINT(inchWidth)); widget.unitHeight->changeValue(INCH_TO_POINT(inchHeight)); blockChildSignals(false); } void ImageExportOptionsWidget::blockChildSignals(bool block) { widget.pxWidth->blockSignals(block); widget.pxHeight->blockSignals(block); widget.unitWidth->blockSignals(block); widget.unitHeight->blockSignals(block); widget.dpi->blockSignals(block); widget.backColor->blockSignals(block); widget.opacity->blockSignals(block); } void ImageExportOptionsWidget::unitWidthChanged(qreal newWidth) { blockChildSignals(true); double newHeight = widget.unitHeight->value(); if (widget.unitAspect->keepAspectRatio()) { newHeight = newWidth * m_pointSize.height() / m_pointSize.width(); widget.unitHeight->changeValue(newHeight); } updateFromPointSize(QSizeF(newWidth, newHeight)); blockChildSignals(false); } void ImageExportOptionsWidget::unitHeightChanged(qreal newHeight) { blockChildSignals(true); double newWidth = widget.unitWidth->value(); if (widget.unitAspect->keepAspectRatio()) { newWidth = newHeight * m_pointSize.width() / m_pointSize.height(); widget.unitWidth->changeValue(newWidth); } updateFromPointSize(QSizeF(newWidth, newHeight)); blockChildSignals(false); } void ImageExportOptionsWidget::pxWidthChanged(int newWidth) { blockChildSignals(true); int newHeight = widget.pxHeight->value(); if (widget.pxAspect->keepAspectRatio()) { newHeight = qRound(newWidth * m_pointSize.height() / m_pointSize.width()); widget.pxHeight->setValue(newHeight); } updateFromPixelSize(QSize(newWidth, newHeight)); blockChildSignals(false); } void ImageExportOptionsWidget::pxHeightChanged(int newHeight) { blockChildSignals(true); int newWidth = widget.pxWidth->value(); if (widget.pxAspect->keepAspectRatio()) { newWidth = qRound(newHeight * m_pointSize.width() / m_pointSize.height()); widget.pxWidth->setValue(newWidth); } updateFromPixelSize(QSize(newWidth, newHeight)); blockChildSignals(false); } void ImageExportOptionsWidget::dpiChanged(int) { blockChildSignals(true); updateFromPointSize(QSizeF(widget.unitWidth->value(), widget.unitHeight->value())); blockChildSignals(false); } void ImageExportOptionsWidget::unitChanged(int newUnit) { blockChildSignals(true); const KoUnit unit = KoUnit::fromListForUi(newUnit, KoUnit::HidePixel); widget.unitWidth->setUnit(unit); widget.unitHeight->setUnit(unit); blockChildSignals(false); } void ImageExportOptionsWidget::aspectChanged(bool keepAspect) { blockChildSignals(true); widget.pxAspect->setKeepAspectRatio(keepAspect); widget.unitAspect->setKeepAspectRatio(keepAspect); blockChildSignals(false); if (keepAspect) unitWidthChanged(widget.unitWidth->value()); } void ImageExportOptionsWidget::enableBackgroundOpacity(bool enable) { widget.opacity->setVisible(enable); widget.labelOpacity->setVisible(enable); } diff --git a/filters/karbon/image/ImageExportOptionsWidget.h b/filters/karbon/image/ImageExportOptionsWidget.h index 59036b080cc..d7b1b154aa4 100644 --- a/filters/karbon/image/ImageExportOptionsWidget.h +++ b/filters/karbon/image/ImageExportOptionsWidget.h @@ -1,70 +1,78 @@ /* This file is part of the KDE project * Copyright (C) 2008 Jan Hambrecht * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef IMAGEEXPORTOPTIONSWIDGET_H #define IMAGEEXPORTOPTIONSWIDGET_H #include "ui_ImageExportOptionsWidget.h" #include +class KarbonDocument; +class KoPAPageBase; + class ImageExportOptionsWidget : public QWidget { Q_OBJECT public: - explicit ImageExportOptionsWidget(const QSizeF &pointSize, QWidget *parent = 0); + explicit ImageExportOptionsWidget(KarbonDocument *doc, QWidget *parent = 0); /// Sets the initial unit to use void setUnit(const KoUnit &unit); /// Returns the chosen export size in pixel QSize pixelSize() const; /// Returns the chosen export size in point QSizeF pointSize() const; /// Sets the background color void setBackgroundColor(const QColor &color); /// Returns the chosen background color QColor backgroundColor() const; /// Enables/disables setting the background opacity void enableBackgroundOpacity(bool enable); + KoPAPageBase *page() const; + private Q_SLOTS: void unitWidthChanged(qreal); void unitHeightChanged(qreal); void pxWidthChanged(int); void pxHeightChanged(int); void dpiChanged(int); void unitChanged(int); void aspectChanged(bool); + void setPage(int idx); private: void updateFromPointSize(const QSizeF &pointSize); void updateFromPixelSize(const QSize &pixelSize); void blockChildSignals(bool block); Ui_ImageExportOptionsWidget widget; QSizeF m_pointSize; + + KarbonDocument *m_doc; }; #endif // IMAGEEXPORTOPTIONSWIDGET_H diff --git a/filters/karbon/image/ImageExportOptionsWidget.ui b/filters/karbon/image/ImageExportOptionsWidget.ui index 710fa49aba1..d23f62c355b 100644 --- a/filters/karbon/image/ImageExportOptionsWidget.ui +++ b/filters/karbon/image/ImageExportOptionsWidget.ui @@ -1,176 +1,193 @@ ImageExportOptionsWidget 0 0 361 454 - - + + + + + + + Page: + + + false + + + + + + + + + Size in Pixels true Image width: Image height: - + Size in Units true Image width: Image height: Resolution: Unit: - + Background Color true Color Opacity - + Qt::Vertical 20 12 KColorButton QPushButton
kcolorbutton.h
KoUnitDoubleSpinBox QDoubleSpinBox
KoUnitDoubleSpinBox.h
KoAspectButton QWidget
KoAspectButton.h
1
KoSliderCombo QComboBox
KoSliderCombo.h