File indexing completed on 2022-12-06 18:50:17

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004 */
0005 
0006 #include "imageresultitem.h"
0007 #include "commandentry.h"
0008 #include "worksheetview.h"
0009 #include "lib/imageresult.h"
0010 #include "lib/epsresult.h"
0011 
0012 #include <config-cantor.h>
0013 
0014 #include <KLocalizedString>
0015 #include <QFileDialog>
0016 #include <QImageReader>
0017 
0018 ImageResultItem::ImageResultItem(QGraphicsObject* parent, Cantor::Result* result)
0019     : WorksheetImageItem(parent), ResultItem(result)
0020 {
0021     update();
0022 }
0023 
0024 double ImageResultItem::setGeometry(double x, double y, double w)
0025 {
0026     Q_UNUSED(w);
0027     setPos(x,y);
0028     return height();
0029 }
0030 
0031 void ImageResultItem::populateMenu(QMenu* menu, QPointF pos)
0032 {
0033     ResultItem::addCommonActions(this, menu);
0034 
0035     menu->addSeparator();
0036     emit menuCreated(menu, mapToParent(pos));
0037 }
0038 
0039 void ImageResultItem::update()
0040 {
0041     Q_ASSERT(m_result->type() == Cantor::ImageResult::Type || m_result->type() == Cantor::EpsResult::Type);
0042     switch(m_result->type()) {
0043     case Cantor::ImageResult::Type:
0044     {
0045         QSize displaySize = static_cast<Cantor::ImageResult*>(m_result)->displaySize();
0046         if (displaySize.isValid())
0047             setImage(m_result->data().value<QImage>(), displaySize);
0048         else
0049             setImage(m_result->data().value<QImage>());
0050     }
0051         break;
0052     case Cantor::EpsResult::Type:
0053     {
0054         Cantor::EpsResult* epsResult = static_cast<Cantor::EpsResult*>(m_result);
0055 #ifdef WITH_EPS
0056         bool cacheVersionEnough = worksheet()->renderer()->scale() == 1.0 && !worksheet()->isPrinting();
0057         if (!epsResult->image().isNull() && cacheVersionEnough)
0058             setImage(epsResult->image());
0059         else
0060             setEps(m_result->data().toUrl());
0061 #else
0062         setImage(epsResult->image());
0063 #endif
0064     }
0065         break;
0066     default:
0067         break;
0068     }
0069 }
0070 
0071 QRectF ImageResultItem::boundingRect() const
0072 {
0073     return QRectF(0, 0, width(), height());
0074 }
0075 
0076 double ImageResultItem::width() const
0077 {
0078     return WorksheetImageItem::width();
0079 }
0080 
0081 double ImageResultItem::height() const
0082 {
0083     return WorksheetImageItem::height();
0084 }
0085 
0086 void ImageResultItem::saveResult()
0087 {
0088     QString formats;
0089     for (const auto& format : QImageReader::supportedImageFormats()) {
0090         QString f = QLatin1String("*.") + QLatin1String(format.constData());
0091         if (f == QLatin1String("*.svg")) // TODO: add SVG after we've switched internally to PDF/SVG for backend's output
0092             continue;
0093         formats += f + QLatin1Char(' ');
0094     }
0095 
0096     const auto& fileName = QFileDialog::getSaveFileName(worksheet()->worksheetView(),
0097                                                            i18n("Save image result"),
0098                                                            /*dir*/ QString(),
0099                                                            i18n("Images (%1)", formats));
0100     if (!fileName.isEmpty())
0101         result()->save(fileName);
0102 }
0103 
0104 void ImageResultItem::deleteLater()
0105 {
0106     WorksheetImageItem::deleteLater();
0107 }