File indexing completed on 2024-05-05 11:56:04

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "epsresult.h"
0007 using namespace Cantor;
0008 
0009 #include <config-cantorlib.h>
0010 
0011 #include <QDebug>
0012 #include <QJsonValue>
0013 
0014 #include <KZip>
0015 #include <KIO/Job>
0016 #include <QBuffer>
0017 #include <QJsonObject>
0018 
0019 #include "renderer.h"
0020 #include "jupyterutils.h"
0021 
0022 class Cantor::EpsResultPrivate{
0023     public:
0024         QUrl url;
0025         QImage image;
0026 };
0027 
0028 
0029 EpsResult::EpsResult(const QUrl& url, const QImage& image) : d(new EpsResultPrivate)
0030 {
0031     d->url=url;
0032     d->image = image;
0033 }
0034 
0035 EpsResult::~EpsResult()
0036 {
0037     delete d;
0038 }
0039 
0040 QString EpsResult::toHtml()
0041 {
0042     return QStringLiteral("<img src=\"%1\" />").arg(d->url.url());
0043 }
0044 
0045 QString EpsResult::toLatex()
0046 {
0047     return QStringLiteral(" \\begin{center} \n \\includegraphics[width=12cm]{%1}\n \\end{center}").arg(d->url.fileName());
0048 }
0049 
0050 QVariant EpsResult::data()
0051 {
0052     return QVariant(d->url);
0053 }
0054 
0055 QUrl EpsResult::url()
0056 {
0057     return d->url;
0058 }
0059 
0060 QImage Cantor::EpsResult::image()
0061 {
0062     return d->image;
0063 }
0064 
0065 int EpsResult::type()
0066 {
0067     return EpsResult::Type;
0068 }
0069 
0070 QString EpsResult::mimeType()
0071 {
0072     return QStringLiteral("image/x-eps");
0073 }
0074 
0075 QDomElement EpsResult::toXml(QDomDocument& doc)
0076 {
0077     qDebug()<<"saving imageresult "<<toHtml();
0078     QDomElement e=doc.createElement(QStringLiteral("Result"));
0079     e.setAttribute(QStringLiteral("type"), QStringLiteral("epsimage"));
0080     e.setAttribute(QStringLiteral("filename"), d->url.fileName());
0081 
0082 #ifdef WITH_EPS
0083     const QImage& image = Renderer::epsRenderToImage(d->url, 1.0, false);
0084     qDebug() << image.size() << image.isNull();
0085     if (!image.isNull())
0086     {
0087         QByteArray ba;
0088         QBuffer buffer(&ba);
0089         buffer.open(QIODevice::WriteOnly);
0090         image.save(&buffer, "PNG");
0091         e.setAttribute(QLatin1String("image"), QString::fromLatin1(ba.toBase64()));
0092     }
0093 #else
0094     if (!d->image.isNull())
0095     {
0096         QByteArray ba;
0097         QBuffer buffer(&ba);
0098         buffer.open(QIODevice::WriteOnly);
0099         d->image.save(&buffer, "PNG");
0100         e.setAttribute(QLatin1String("image"), QString::fromLatin1(ba.toBase64()));
0101     }
0102 #endif
0103 
0104     qDebug()<<"done";
0105     return e;
0106 }
0107 
0108 QJsonValue Cantor::EpsResult::toJupyterJson()
0109 {
0110     QJsonObject root;
0111 
0112     if (executionIndex() != -1)
0113     {
0114         root.insert(QLatin1String("output_type"), QLatin1String("execute_result"));
0115         root.insert(QLatin1String("execution_count"), executionIndex());
0116     }
0117     else
0118         root.insert(QLatin1String("output_type"), QLatin1String("display_data"));
0119 
0120     const QImage& image = d->image.isNull() ? Renderer::epsRenderToImage(d->url, 1.0, false) : d->image;
0121 
0122     QJsonObject data;
0123     data = JupyterUtils::packMimeBundle(image, JupyterUtils::pngMime);
0124     root.insert(QLatin1String("data"), data);
0125 
0126     root.insert(QLatin1String("metadata"), jupyterMetadata());
0127 
0128     return root;
0129 }
0130 
0131 void EpsResult::saveAdditionalData(KZip* archive)
0132 {
0133     archive->addLocalFile(d->url.toLocalFile(), d->url.fileName());
0134 }
0135 
0136 void EpsResult::save(const QString& filename)
0137 {
0138     //just copy over the eps file..
0139     KIO::file_copy(d->url, QUrl::fromLocalFile(filename), -1, KIO::HideProgressInfo);
0140 }