markdownentry.h 2.78 KB
Newer Older
Yifei Wu's avatar
Yifei Wu committed
1
/*
2
3
4
    SPDX-License-Identifier: GPL-2.0-or-later
    SPDX-FileCopyrightText: 2018 Yifei Wu <kqwyfg@gmail.com>
*/
Yifei Wu's avatar
Yifei Wu committed
5
6
7
8

#ifndef MARKDOWNENTRY_H
#define MARKDOWNENTRY_H

9
10
#include <vector>

11
12
#include <QSharedPointer>

Yifei Wu's avatar
Yifei Wu committed
13
14
15
#include "worksheetentry.h"
#include "worksheettextitem.h"

16
#include "mathrendertask.h"
17
#include "lib/latexrenderer.h"
18

19
20
class QJsonObject;

Yifei Wu's avatar
Yifei Wu committed
21
22
23
24
class MarkdownEntry : public WorksheetEntry
{
  Q_OBJECT
  public:
Yuri Chornoivan's avatar
Yuri Chornoivan committed
25
    explicit MarkdownEntry(Worksheet* worksheet);
26
    ~MarkdownEntry() override = default;
Yifei Wu's avatar
Yifei Wu committed
27
28

    enum {Type = UserType + 7};
29
    int type() const override;
Yifei Wu's avatar
Yifei Wu committed
30

31
    bool isEmpty() override;
Yifei Wu's avatar
Yifei Wu committed
32

33
    bool acceptRichText() override;
Yifei Wu's avatar
Yifei Wu committed
34

35
    bool focusEntry(int pos = WorksheetTextItem::TopLeft, qreal xCoord=0) override;
Yifei Wu's avatar
Yifei Wu committed
36

37
38
    void setContent(const QString& content) override;
    void setContent(const QDomElement& content, const KZip& file) override;
39
    void setContentFromJupyter(const QJsonObject& cell) override;
Yifei Wu's avatar
Yifei Wu committed
40

41
    QDomElement toXml(QDomDocument& doc, KZip* archive) override;
42
    QJsonValue toJupyterJson() override;
43
    QString toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq) override;
Yifei Wu's avatar
Yifei Wu committed
44

45
46
    QString plainText() const;

47
    void interruptEvaluation() override;
Yifei Wu's avatar
Yifei Wu committed
48

49
    void layOutForWidth(qreal entry_zone_x, qreal w, bool force = false) override;
Yifei Wu's avatar
Yifei Wu committed
50
51
52

    WorksheetCursor search(const QString& pattern, unsigned flags,
                           QTextDocument::FindFlags qt_flags,
53
                           const WorksheetCursor& pos = WorksheetCursor()) override;
Yifei Wu's avatar
Yifei Wu committed
54
55

  public Q_SLOTS:
56
57
    bool evaluate(WorksheetEntry::EvaluationOption evalOp = FocusNext) override;
    void updateEntry() override;
58
    void populateMenu(QMenu* menu, QPointF pos) override;
Yifei Wu's avatar
Yifei Wu committed
59
60
61

  protected:
    bool renderMarkdown(QString& plain);
62
63
    bool eventFilter(QObject* object, QEvent* event) override;
    bool wantToEvaluate() override;
64
65
    void setRenderedHtml(const QString& html);
    void setPlainText(const QString& plain);
66
    void renderMath();
67
68
69
70
    void renderMathExpression(int jobId, QString mathCode);
    void setRenderedMath(int jobId, const QTextImageFormat& format, const QUrl& internal, const QImage& image);
    QTextCursor findMath(int id);
    void markUpMath();
71
    void addImageAttachment(const QString& name, const QImage& image);
72
73

    static std::pair<QString, Cantor::LatexRenderer::EquationType> parseMathCode(QString mathCode);
74
75
76

  protected Q_SLOTS:
    void handleMathRender(QSharedPointer<MathRenderResult> result);
77
78
    void insertImage();
    void clearAttachments();
79
    void enterEditMode();
Yifei Wu's avatar
Yifei Wu committed
80
81
82
83
84
85

  protected:
    WorksheetTextItem* m_textItem;
    QString plain;
    QString html;
    bool rendered;
86
    std::vector<std::pair<QUrl,QString>> attachedImages;
87
    std::vector<std::pair<QString, bool>> foundMath;
88
    static const int JobProperty = 10000;
Yifei Wu's avatar
Yifei Wu committed
89
90
91
};

#endif //MARKDOWNENTRY_H