File indexing completed on 2024-04-28 11:20:44

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "animationresult.h"
0007 using namespace Cantor;
0008 
0009 #include <QImage>
0010 #include <QImageWriter>
0011 #include <KZip>
0012 #include <QMimeDatabase>
0013 #include <QDebug>
0014 #include <KIO/Job>
0015 #include <QMovie>
0016 
0017 class Cantor::AnimationResultPrivate
0018 {
0019   public:
0020     AnimationResultPrivate() = default;
0021 
0022     QUrl url;
0023     QMovie* movie;
0024     QString alt;
0025 };
0026 
0027 AnimationResult::AnimationResult(const QUrl &url, const QString& alt ) : d(new AnimationResultPrivate)
0028 {
0029     d->url=url;
0030     d->alt=alt;
0031     d->movie=new QMovie();
0032     d->movie->setFileName(url.toLocalFile());
0033 }
0034 
0035 
0036 AnimationResult::~AnimationResult()
0037 {
0038     delete d->movie;
0039     delete d;
0040 }
0041 
0042 QString AnimationResult::toHtml()
0043 {
0044     return QStringLiteral("<img src=\"%1\" alt=\"%2\"/>").arg(d->url.toLocalFile(), d->alt);
0045 }
0046 
0047 QVariant AnimationResult::data()
0048 {
0049     return QVariant::fromValue(static_cast<QObject*>(d->movie));
0050 }
0051 
0052 QUrl AnimationResult::url()
0053 {
0054     return d->url;
0055 }
0056 
0057 int AnimationResult::type()
0058 {
0059     return AnimationResult::Type;
0060 }
0061 
0062 QString AnimationResult::mimeType()
0063 {
0064     QMimeDatabase db;
0065     QMimeType type = db.mimeTypeForUrl(d->url);
0066 
0067     return type.name();
0068 }
0069 
0070 QDomElement AnimationResult::toXml(QDomDocument& doc)
0071 {
0072     qDebug()<<"saving imageresult "<<toHtml();
0073     QDomElement e=doc.createElement(QStringLiteral("Result"));
0074     e.setAttribute(QStringLiteral("type"), QStringLiteral("animation"));
0075     e.setAttribute(QStringLiteral("filename"), d->url.fileName());
0076     qDebug()<<"done";
0077 
0078     return e;
0079 }
0080 
0081 QJsonValue Cantor::AnimationResult::toJupyterJson()
0082 {
0083     QJsonObject root;
0084 
0085     if (executionIndex() != -1)
0086     {
0087         root.insert(QLatin1String("output_type"), QLatin1String("execute_result"));
0088         root.insert(QLatin1String("execution_count"), executionIndex());
0089     }
0090     else
0091         root.insert(QLatin1String("output_type"), QLatin1String("display_data"));
0092 
0093     QJsonObject data;
0094     data.insert(QLatin1String("text/plain"), d->alt);
0095 
0096     QFile file(d->url.toLocalFile());
0097     QByteArray bytes;
0098     if (file.open(QIODevice::ReadOnly))
0099         bytes = file.readAll();
0100     data.insert(QLatin1String("image/gif"), QString::fromLatin1(bytes.toBase64()));
0101 
0102     root.insert(QLatin1String("data"), data);
0103     // Not sure, but in Jupyter size of gif doesn't controlled by metadata unlike ImageResult
0104     root.insert(QLatin1String("metadata"), jupyterMetadata());
0105 
0106     return root;
0107 }
0108 
0109 void AnimationResult::saveAdditionalData(KZip* archive)
0110 {
0111     archive->addLocalFile(d->url.toLocalFile(), d->url.fileName());
0112 }
0113 
0114 void AnimationResult::save(const QString& filename)
0115 {
0116     //just copy over the file..
0117     KIO::file_copy(d->url, QUrl::fromLocalFile(filename), -1, KIO::HideProgressInfo);
0118 }