File indexing completed on 2024-04-28 11:21:04

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
0004     SPDX-FileCopyrightText: 2018-2021 Alexander Semke <alexander.semke@web.de>
0005 */
0006 
0007 #ifndef WORKSHEETENTRY_H
0008 #define WORKSHEETENTRY_H
0009 
0010 #include <QGraphicsObject>
0011 
0012 #include "worksheet.h"
0013 #include "worksheettextitem.h"
0014 #include "worksheetcursor.h"
0015 #include "worksheetcontrolitem.h"
0016 
0017 class TextEntry;
0018 class MarkdownEntry;
0019 class CommandEntry;
0020 class ImageEntry;
0021 class PageBreakEntry;
0022 class LaTeXEntry;
0023 
0024 class WorksheetTextItem;
0025 class ActionBar;
0026 
0027 class QGraphicsSceneContextMenuEvent;
0028 class QJsonObject;
0029 class QPainter;
0030 class QPropertyAnimation;
0031 class QWidget;
0032 
0033 struct AnimationData;
0034 
0035 class WorksheetEntry : public QGraphicsObject
0036 {
0037   Q_OBJECT
0038   public:
0039     explicit WorksheetEntry(Worksheet*);
0040     ~WorksheetEntry() override;
0041 
0042     enum {Type = UserType};
0043 
0044     int type() const override;
0045 
0046     virtual bool isEmpty()=0;
0047 
0048     static WorksheetEntry* create(int t, Worksheet*);
0049 
0050     WorksheetEntry* next() const;
0051     WorksheetEntry* previous() const;
0052 
0053     void forceRemove();
0054 
0055     void setNext(WorksheetEntry*);
0056     void setPrevious(WorksheetEntry*);
0057 
0058     QRectF boundingRect() const override;
0059     void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget* widget = nullptr) override;
0060 
0061     virtual bool acceptRichText() = 0;
0062 
0063     virtual void setContent(const QString&)=0;
0064     virtual void setContent(const QDomElement&, const KZip&)=0;
0065     virtual void setContentFromJupyter(const QJsonObject&)=0;
0066 
0067     virtual QDomElement toXml(QDomDocument&, KZip*)=0;
0068     virtual QJsonValue toJupyterJson()=0;
0069     virtual QString toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq)=0;
0070 
0071     virtual void interruptEvaluation() {};
0072 
0073     virtual void showCompletion();
0074 
0075     virtual bool focusEntry(int pos = WorksheetTextItem::TopLeft, qreal xCoord = 0);
0076 
0077     virtual qreal setGeometry(qreal x, qreal entry_zone_x, qreal y, qreal w);
0078     virtual void layOutForWidth(qreal entry_zone_x, qreal w, bool force = false) = 0;
0079     QPropertyAnimation* sizeChangeAnimation(QSizeF s = QSizeF());
0080 
0081     virtual void populateMenu(QMenu*, QPointF);
0082 
0083     bool aboutToBeRemoved();
0084     QSizeF size();
0085 
0086     enum EvaluationOption {
0087         InternalEvaluation, DoNothing, FocusNext, EvaluateNext
0088     };
0089 
0090     virtual WorksheetTextItem* highlightItem();
0091 
0092     bool hasActionBar();
0093 
0094     enum SearchFlag {SearchCommand=1, SearchResult=2, SearchError=4,
0095                      SearchText=8, SearchLaTeX=16, SearchAll=31};
0096 
0097     virtual WorksheetCursor search(const QString& pattern, unsigned flags,
0098                                    QTextDocument::FindFlags qt_flags,
0099                                    const WorksheetCursor& pos = WorksheetCursor());
0100 
0101     bool isCellSelected();
0102     void setCellSelected(bool);
0103 
0104     // Colors for colors menus;
0105     static constexpr int colorsCount = 26;
0106     static QColor colors[colorsCount];
0107     static QString colorNames[colorsCount];
0108 
0109     static const qreal VerticalMargin;
0110     static const qreal ControlElementWidth;
0111     static const qreal ControlElementBorder;
0112     static const qreal RightMargin;
0113     static const qreal HorizontalSpacing;
0114 
0115   public Q_SLOTS:
0116     virtual bool evaluate(WorksheetEntry::EvaluationOption evalOp = FocusNext) = 0;
0117     virtual bool evaluateCurrentItem();
0118     virtual void updateEntry() = 0;
0119     virtual void updateAfterSettingsChanges();
0120 
0121     void insertCommandEntry();
0122     void insertTextEntry();
0123     void insertMarkdownEntry();
0124     void insertLatexEntry();
0125     void insertImageEntry();
0126     void insertPageBreakEntry();
0127     void insertHorizontalRuleEntry();
0128     void insertHierarchyEntry();
0129 
0130     void insertCommandEntryBefore();
0131     void insertTextEntryBefore();
0132     void insertMarkdownEntryBefore();
0133     void insertLatexEntryBefore();
0134     void insertImageEntryBefore();
0135     void insertPageBreakEntryBefore();
0136     void insertHorizontalRuleEntryBefore();
0137     void insertHierarchyEntryBefore();
0138 
0139     void convertToCommandEntry();
0140     void convertToTextEntry();
0141     void convertToMarkdownEntry();
0142     void convertToLatexEntry();
0143     void convertToImageEntry();
0144     void converToPageBreakEntry();
0145     void convertToHorizontalRuleEntry();
0146     void convertToHierarchyEntry();
0147 
0148     virtual void sizeAnimated();
0149     virtual void startRemoving();
0150     bool stopRemoving();
0151     void moveToPreviousEntry(int pos = WorksheetTextItem::BottomRight, qreal x = 0);
0152     void moveToNextEntry(int pos = WorksheetTextItem::TopLeft, qreal x = 0);
0153     void recalculateSize();
0154 
0155     // similar to recalculateSize, but the size change is animated
0156     void animateSizeChange();
0157     // animate the size change and the opacity of item
0158     void fadeInItem(QGraphicsObject* item = nullptr, const char* slot = nullptr);
0159     void fadeOutItem(QGraphicsObject* item = nullptr, const char* slot = "deleteLater()");
0160     void endAnimation();
0161 
0162     void showActionBar();
0163     void hideActionBar();
0164 
0165     virtual void startDrag(QPointF grabPos = QPointF());
0166 
0167     void moveToNext(bool updateLayout = true);
0168     void moveToPrevious(bool updateLayout = true);
0169 
0170   Q_SIGNALS:
0171     void aboutToBeDeleted();
0172 
0173   protected:
0174     Worksheet* worksheet();
0175     WorksheetView* worksheetView();
0176     void contextMenuEvent(QGraphicsSceneContextMenuEvent*) override;
0177     void keyPressEvent(QKeyEvent*) override;
0178     void evaluateNext(EvaluationOption opt);
0179 
0180     void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
0181     void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
0182 
0183     void setSize(QSizeF);
0184 
0185     bool animationActive();
0186     void updateSizeAnimation(QSizeF);
0187 
0188     void invokeSlotOnObject(const char* slot, QObject* obj);
0189 
0190     virtual void addActionsToBar(ActionBar*);
0191 
0192     virtual bool wantToEvaluate() = 0;
0193     virtual bool wantFocus();
0194 
0195     QJsonObject jupyterMetadata() const;
0196     void setJupyterMetadata(QJsonObject);
0197 
0198     virtual void recalculateControlGeometry();
0199 
0200     WorksheetControlItem m_controlElement;
0201 
0202   protected Q_SLOTS:
0203     virtual void remove();
0204     void deleteActionBar();
0205     void deleteActionBarAnimation();
0206 
0207   private:
0208     QSizeF m_size;
0209     qreal m_entry_zone_x{0.};
0210     WorksheetEntry* m_prev{nullptr};
0211     WorksheetEntry* m_next{nullptr};
0212     Q_PROPERTY(QSizeF size READ size WRITE setSize)
0213     AnimationData* m_animation{nullptr};
0214     ActionBar* m_actionBar{nullptr};
0215     QPropertyAnimation* m_actionBarAnimation{nullptr};
0216     bool m_aboutToBeRemoved{false};
0217     QJsonObject* m_jupyterMetadata{nullptr};
0218     bool m_isCellSelected{false};
0219 };
0220 
0221 #endif // WORKSHEETENTRY_H