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

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0005 */
0006 
0007 #ifndef TEXTENTRY_H
0008 #define TEXTENTRY_H
0009 
0010 #include <QString>
0011 #include <QDomElement>
0012 #include <QDomDocument>
0013 #include <QIODevice>
0014 #include <KZip>
0015 #include <QTextCursor>
0016 #include <KArchive>
0017 
0018 #include "worksheetentry.h"
0019 #include "worksheettextitem.h"
0020 #include "mathrendertask.h"
0021 
0022 class TextEntry : public WorksheetEntry
0023 {
0024   Q_OBJECT
0025   public:
0026     explicit TextEntry(Worksheet* worksheet);
0027     ~TextEntry() override;
0028 
0029     enum {Type = UserType + 1};
0030     int type() const override;
0031 
0032     QString text() const;
0033 
0034     bool isEmpty() override;
0035 
0036     bool acceptRichText() override;
0037 
0038     bool focusEntry(int pos = WorksheetTextItem::TopLeft, qreal xCoord=0) override;
0039 
0040     // do we need/get this?
0041     //bool worksheetContextMenuEvent(...);
0042     //void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
0043 
0044     void setContent(const QString& content) override;
0045     void setContent(const QDomElement& content, const KZip& file) override;
0046     void setContentFromJupyter(const QJsonObject& cell) override;
0047     static bool isConvertableToTextEntry(const QJsonObject& cell);
0048 
0049     QDomElement toXml(QDomDocument& doc, KZip* archive) override;
0050     QJsonValue toJupyterJson() override;
0051     QString toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq) override;
0052 
0053     void layOutForWidth(qreal entry_zone_x, qreal w, bool force = false) override;
0054 
0055     int searchText(const QString& text, const QString& pattern,
0056                    QTextDocument::FindFlags qt_flags);
0057     WorksheetCursor search(const QString& pattern, unsigned flags,
0058                            QTextDocument::FindFlags qt_flags,
0059                            const WorksheetCursor& pos = WorksheetCursor()) override;
0060 
0061   public Q_SLOTS:
0062     bool evaluate(WorksheetEntry::EvaluationOption evalOp = FocusNext) override;
0063     void resolveImagesAtCursor();
0064     void updateEntry() override;
0065     void populateMenu(QMenu* menu, QPointF pos) override;
0066     void convertToRawCell();
0067     void convertToTextEntry();
0068     void convertTargetChanged(QAction* action);
0069 
0070   protected:
0071     bool wantToEvaluate() override;
0072 
0073   protected Q_SLOTS:
0074     void handleMathRender(QSharedPointer<MathRenderResult> result);
0075 
0076   private:
0077     QTextCursor findLatexCode(const QTextCursor& cursor = QTextCursor()) const;
0078     QString showLatexCode(QTextCursor& cursor);
0079     void addNewTarget(const QString& target);
0080 
0081   private:
0082     bool m_rawCell;
0083     QString m_convertTarget;
0084     QActionGroup* m_targetActionGroup;
0085     QAction* m_ownTarget;
0086     QMenu* m_targetMenu;
0087 
0088     WorksheetTextItem* m_textItem;
0089 };
0090 
0091 #endif //TEXTENTRY_H