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

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "latexresult.h"
0007 
0008 #include <QFile>
0009 #include <QTextStream>
0010 #include <QJsonValue>
0011 #include <QJsonObject>
0012 #include <QDebug>
0013 
0014 #include "jupyterutils.h"
0015 
0016 using namespace Cantor;
0017 
0018 class Cantor::LatexResultPrivate
0019 {
0020   public:
0021     LatexResultPrivate()
0022     {
0023         showCode=false;
0024     }
0025 
0026     bool showCode;
0027     QString code;
0028     QString plain;
0029 };
0030 
0031 LatexResult::LatexResult(const QString& code, const QUrl &url, const QString& plain, const QImage& image) : EpsResult( url, image ),
0032                                                                                        d(new LatexResultPrivate)
0033 {
0034     d->code=code;
0035     d->plain=plain;
0036 }
0037 
0038 LatexResult::~LatexResult()
0039 {
0040     delete d;
0041 }
0042 
0043 int LatexResult::type()
0044 {
0045     return LatexResult::Type;
0046 }
0047 
0048 QString LatexResult::mimeType()
0049 {
0050     if(isCodeShown())
0051         return QStringLiteral("text/plain");
0052     else
0053         return EpsResult::mimeType();
0054 }
0055 
0056 QString LatexResult::code()
0057 {
0058     return d->code;
0059 }
0060 
0061 QString LatexResult::plain()
0062 {
0063     return d->plain;
0064 }
0065 
0066 bool LatexResult::isCodeShown()
0067 {
0068     return d->showCode;
0069 }
0070 
0071 void LatexResult::showCode()
0072 {
0073     d->showCode=true;
0074 }
0075 
0076 void LatexResult::showRendered()
0077 {
0078     d->showCode=false;
0079 }
0080 
0081 QVariant LatexResult::data()
0082 {
0083     if(isCodeShown())
0084         return QVariant(code());
0085     else
0086         return EpsResult::data();
0087 }
0088 
0089 QString LatexResult::toHtml()
0090 {
0091     if (isCodeShown())
0092     {
0093             QString s=code();
0094             return s.toHtmlEscaped();
0095     }
0096     else
0097     {
0098         return EpsResult::toHtml();
0099     }
0100 }
0101 
0102 QString LatexResult::toLatex()
0103 {
0104     return code();
0105 }
0106 
0107 QDomElement LatexResult::toXml(QDomDocument& doc)
0108 {
0109     qDebug()<<"saving textresult "<<toHtml();
0110     QDomElement e = EpsResult::toXml(doc);
0111     e.setAttribute(QStringLiteral("type"), QStringLiteral("latex"));
0112     QDomText txt=doc.createTextNode(code());
0113     e.appendChild(txt);
0114 
0115     return e;
0116 }
0117 
0118 QJsonValue Cantor::LatexResult::toJupyterJson()
0119 {
0120     QJsonObject root;
0121 
0122     if (executionIndex() != -1)
0123     {
0124         root.insert(QLatin1String("output_type"), QLatin1String("execute_result"));
0125         root.insert(QLatin1String("execution_count"), executionIndex());
0126     }
0127     else
0128         root.insert(QLatin1String("output_type"), QLatin1String("display_data"));
0129 
0130     QJsonObject data;
0131     data.insert(QLatin1String("text/plain"), JupyterUtils::toJupyterMultiline(d->plain));
0132     data.insert(QLatin1String("text/latex"), JupyterUtils::toJupyterMultiline(d->code));
0133     if (!image().isNull())
0134         data.insert(JupyterUtils::pngMime, JupyterUtils::packMimeBundle(image(), JupyterUtils::pngMime));
0135     root.insert(QLatin1String("data"), data);
0136 
0137     root.insert(QLatin1String("metadata"), jupyterMetadata());
0138 
0139     return root;
0140 }
0141 
0142 void LatexResult::save(const QString& filename)
0143 {
0144     if(isCodeShown())
0145     {
0146         QFile file(filename);
0147 
0148         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
0149             return;
0150 
0151         QTextStream stream(&file);
0152 
0153         stream<<code();
0154 
0155         file.close();
0156     }else
0157     {
0158         EpsResult::save(filename);
0159     }
0160 }
0161