Warning, file /education/cantor/src/imageresultitem.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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