Warning, file /education/cantor/src/lib/imageresult.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: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "imageresult.h"
0007 using namespace Cantor;
0008 
0009 #include <QImage>
0010 #include <QImageWriter>
0011 #include <KZip>
0012 #include <QDebug>
0013 #include <QBuffer>
0014 #include <QTemporaryFile>
0015 
0016 #include "jupyterutils.h"
0017 
0018 class Cantor::ImageResultPrivate
0019 {
0020   public:
0021     ImageResultPrivate() = default;
0022 
0023     QUrl url;
0024     QImage img;
0025     QString alt;
0026     QSize displaySize;
0027 
0028     QString originalFormat{JupyterUtils::pngMime};
0029     QString svgContent; // HACK: qt can't easily render svg, so, if we load the result from Jupyter svg image, store original svg
0030 };
0031 
0032 ImageResult::ImageResult(const QUrl &url, const QString& alt) :  d(new ImageResultPrivate)
0033 {
0034     d->url=url;
0035     d->alt=alt;
0036 }
0037 
0038 Cantor::ImageResult::ImageResult(const QImage& image, const QString& alt) :  d(new ImageResultPrivate)
0039 {
0040     d->img=image;
0041     d->alt=alt;
0042 
0043     QTemporaryFile imageFile;
0044     imageFile.setAutoRemove(false);
0045     if (imageFile.open())
0046     {
0047         d->img.save(imageFile.fileName(), "PNG");
0048         d->url = QUrl::fromLocalFile(imageFile.fileName());
0049     }
0050 }
0051 
0052 ImageResult::~ImageResult()
0053 {
0054     delete d;
0055 }
0056 
0057 QString ImageResult::toHtml()
0058 {
0059     return QStringLiteral("<img src=\"%1\" alt=\"%2\"/>").arg(d->url.toLocalFile(), d->alt);
0060 }
0061 
0062 QString ImageResult::toLatex()
0063 {
0064     return QStringLiteral(" \\begin{center} \n \\includegraphics[width=12cm]{%1} \n \\end{center}").arg(d->url.fileName());
0065 }
0066 
0067 QVariant ImageResult::data()
0068 {
0069     if(d->img.isNull())
0070         d->img.load(d->url.toLocalFile());
0071 
0072     return QVariant(d->img);
0073 }
0074 
0075 QUrl ImageResult::url()
0076 {
0077     return d->url;
0078 }
0079 
0080 int ImageResult::type()
0081 {
0082     return ImageResult::Type;
0083 }
0084 
0085 QString ImageResult::mimeType()
0086 {
0087     const QList<QByteArray> formats=QImageWriter::supportedImageFormats();
0088     QString mimetype;
0089     foreach(const QByteArray& format, formats)
0090     {
0091         mimetype+=QLatin1String("image/"+format.toLower()+' ');
0092     }
0093     qDebug()<<"type: "<<mimetype;
0094 
0095     return mimetype;
0096 }
0097 
0098 QDomElement ImageResult::toXml(QDomDocument& doc)
0099 {
0100     qDebug()<<"saving imageresult "<<toHtml();
0101     QDomElement e=doc.createElement(QStringLiteral("Result"));
0102     e.setAttribute(QStringLiteral("type"), QStringLiteral("image"));
0103     e.setAttribute(QStringLiteral("filename"), d->url.fileName());
0104     if (!d->alt.isEmpty())
0105         e.appendChild(doc.createTextNode(d->alt));
0106     qDebug()<<"done";
0107 
0108     return e;
0109 }
0110 
0111 QJsonValue Cantor::ImageResult::toJupyterJson()
0112 {
0113     QJsonObject root;
0114 
0115     if (executionIndex() != -1)
0116     {
0117         root.insert(QLatin1String("output_type"), QLatin1String("execute_result"));
0118         root.insert(QLatin1String("execution_count"), executionIndex());
0119     }
0120     else
0121         root.insert(QLatin1String("output_type"), QLatin1String("display_data"));
0122 
0123     QImage image;
0124     if (d->img.isNull())
0125         image.load(d->url.toLocalFile());
0126     else
0127         image = d->img;
0128 
0129     QJsonObject data;
0130 
0131     // HACK: see ImageResultPrivate::svgContent
0132     if (d->originalFormat == JupyterUtils::svgMime)
0133         data.insert(JupyterUtils::svgMime, JupyterUtils::toJupyterMultiline(d->svgContent));
0134     else
0135         data = JupyterUtils::packMimeBundle(image, d->originalFormat);
0136 
0137     data.insert(JupyterUtils::textMime, JupyterUtils::toJupyterMultiline(d->alt));
0138 
0139 
0140     root.insert(QLatin1String("data"), data);
0141 
0142     QJsonObject metadata(jupyterMetadata());
0143     if (d->displaySize.isValid())
0144     {
0145         QJsonObject size;
0146         size.insert(QLatin1String("width"), displaySize().width());
0147         size.insert(QLatin1String("height"), displaySize().height());
0148         metadata.insert(d->originalFormat, size);
0149     }
0150     root.insert(QLatin1String("metadata"), metadata);
0151 
0152     return root;
0153 }
0154 
0155 void ImageResult::saveAdditionalData(KZip* archive)
0156 {
0157     archive->addLocalFile(d->url.toLocalFile(), d->url.fileName());
0158 }
0159 
0160 void ImageResult::save(const QString& filename)
0161 {
0162     bool rc = d->img.save(filename);
0163     if (!rc)
0164         qDebug()<<"saving to " << filename << " failed.";
0165 }
0166 
0167 QSize Cantor::ImageResult::displaySize()
0168 {
0169     return d->displaySize;
0170 }
0171 
0172 void Cantor::ImageResult::setDisplaySize(QSize size)
0173 {
0174     d->displaySize = size;
0175 }
0176 
0177 void Cantor::ImageResult::setOriginalFormat(const QString& format)
0178 {
0179     d->originalFormat = format;
0180 }
0181 
0182 QString Cantor::ImageResult::originalFormat()
0183 {
0184     return d->originalFormat;
0185 }
0186 
0187 void Cantor::ImageResult::setSvgContent(const QString& svgContent)
0188 {
0189     d->svgContent = svgContent;
0190 }