File indexing completed on 2022-12-06 18:50:21

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2018 Yifei Wu <kqwyfg@gmail.com>
0004 */
0005 
0006 #ifndef MARKDOWNENTRY_H
0007 #define MARKDOWNENTRY_H
0008 
0009 #include <vector>
0010 
0011 #include <QSharedPointer>
0012 
0013 #include "worksheetentry.h"
0014 #include "worksheettextitem.h"
0015 
0016 #include "mathrendertask.h"
0017 #include "lib/latexrenderer.h"
0018 
0019 class QJsonObject;
0020 
0021 class MarkdownEntry : public WorksheetEntry
0022 {
0023   Q_OBJECT
0024   public:
0025     explicit MarkdownEntry(Worksheet* worksheet);
0026     ~MarkdownEntry() override = default;
0027 
0028     enum {Type = UserType + 7};
0029     int type() const override;
0030 
0031     bool isEmpty() override;
0032 
0033     bool acceptRichText() override;
0034 
0035     bool focusEntry(int pos = WorksheetTextItem::TopLeft, qreal xCoord=0) override;
0036 
0037     void setContent(const QString& content) override;
0038     void setContent(const QDomElement& content, const KZip& file) override;
0039     void setContentFromJupyter(const QJsonObject& cell) override;
0040 
0041     QDomElement toXml(QDomDocument& doc, KZip* archive) override;
0042     QJsonValue toJupyterJson() override;
0043     QString toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq) override;
0044 
0045     QString plainText() const;
0046 
0047     void layOutForWidth(qreal entry_zone_x, qreal w, bool force = false) override;
0048 
0049     WorksheetCursor search(const QString& pattern, unsigned flags,
0050                            QTextDocument::FindFlags qt_flags,
0051                            const WorksheetCursor& pos = WorksheetCursor()) override;
0052 
0053   public Q_SLOTS:
0054     bool evaluate(WorksheetEntry::EvaluationOption evalOp = FocusNext) override;
0055     void updateEntry() override;
0056     void populateMenu(QMenu* menu, QPointF pos) override;
0057 
0058   protected:
0059     bool renderMarkdown(QString& plain);
0060     bool eventFilter(QObject* object, QEvent* event) override;
0061     bool wantToEvaluate() override;
0062     void setRenderedHtml(const QString& html);
0063     void setPlainText(const QString& plain);
0064     void renderMath();
0065     void renderMathExpression(int jobId, QString mathCode);
0066     void setRenderedMath(int jobId, const QTextImageFormat& format, const QUrl& internal, const QImage& image);
0067     QTextCursor findMath(int id);
0068     void markUpMath();
0069     void addImageAttachment(const QString& name, const QImage& image);
0070 
0071     static std::pair<QString, Cantor::LatexRenderer::EquationType> parseMathCode(QString mathCode);
0072 
0073   protected Q_SLOTS:
0074     void handleMathRender(QSharedPointer<MathRenderResult> result);
0075     void insertImage();
0076     void clearAttachments();
0077     void enterEditMode();
0078 
0079   protected:
0080     WorksheetTextItem* m_textItem;
0081     QString plain;
0082     QString html;
0083     bool rendered;
0084     std::vector<std::pair<QUrl,QString>> attachedImages;
0085     std::vector<std::pair<QString, bool>> foundMath;
0086     static const int JobProperty = 10000;
0087 };
0088 
0089 #endif //MARKDOWNENTRY_H