File indexing completed on 2024-05-05 11:56:16

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     SPDX-FileCopyrightText: 2018-2021 Alexander Semke <alexander.semke@web.de>
0006 */
0007 
0008 #ifndef COMMANDENTRY_H
0009 #define COMMANDENTRY_H
0010 
0011 #include <QPointer>
0012 #include <KCompletionBox>
0013 
0014 #include "worksheetentry.h"
0015 #include "lib/expression.h"
0016 
0017 class Worksheet;
0018 class ResultItem;
0019 class QTimer;
0020 class QJsonObject;
0021 
0022 namespace Cantor{
0023     class Result;
0024     class CompletionObject;
0025     class SyntaxHelpObject;
0026 }
0027 
0028 class CommandEntry : public WorksheetEntry
0029 {
0030   Q_OBJECT
0031   public:
0032     static const QString Prompt;
0033     static const QString MidPrompt;
0034     static const QString HidePrompt;
0035 
0036     explicit CommandEntry(Worksheet*);
0037     ~CommandEntry() override;
0038 
0039     enum {Type = UserType + 2};
0040     int type() const override;
0041 
0042     QString command();
0043     void setExpression(Cantor::Expression*);
0044     Cantor::Expression* expression();
0045 
0046     QString currentLine();
0047 
0048     bool isEmpty() override;
0049     bool isExcludedFromExecution();
0050     bool isResultCollapsed();
0051 
0052     void setContent(const QString&) override;
0053     void setContent(const QDomElement&, const KZip&) override;
0054     void setContentFromJupyter(const QJsonObject&) override;
0055 
0056     QDomElement toXml(QDomDocument&, KZip*) override;
0057     QJsonValue toJupyterJson() override;
0058     QString toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq) override;
0059 
0060     void setCompletion(Cantor::CompletionObject* tc);
0061     void setSyntaxHelp(Cantor::SyntaxHelpObject* sh);
0062 
0063     bool acceptRichText() override;
0064 
0065     void removeContextHelp();
0066 
0067     void interruptEvaluation() override;
0068     bool isShowingCompletionPopup();
0069 
0070     bool focusEntry(int pos = WorksheetTextItem::TopLeft, qreal xCoord = 0) override;
0071 
0072     void layOutForWidth(qreal entry_zone_x, qreal w, bool force = false) override;
0073     qreal promptItemWidth();
0074 
0075     WorksheetTextItem* highlightItem() override;
0076 
0077     WorksheetCursor search(const QString& pattern, unsigned flags,
0078                            QTextDocument::FindFlags qt_flags,
0079                            const WorksheetCursor& pos = WorksheetCursor()) override;
0080 
0081   public Q_SLOTS:
0082     bool evaluateCurrentItem() override;
0083     bool evaluate(WorksheetEntry::EvaluationOption evalOp = FocusNext) override;
0084     void addInformation();
0085     void removeResults();
0086     void removeResult(Cantor::Result* result);
0087     void collapseResults();
0088     void expandResults();
0089     void excludeFromExecution();
0090     void addToExecution();
0091 
0092     void showCompletion() override;
0093     void handleTabPress();
0094     void handleBacktabPress();
0095     void updateEntry() override;
0096     void updatePrompt(const QString& postfix = CommandEntry::Prompt);
0097     void expressionChangedStatus(Cantor::Expression::Status);
0098     void showAdditionalInformationPrompt(const QString&);
0099     void showCompletions();
0100     void applySelectedCompletion();
0101     void completedLineChanged();
0102     void showSyntaxHelp();
0103     void completeLineTo(const QString& line, int index);
0104 
0105     void startRemoving() override;
0106 
0107     void moveToNextItem(int pos, qreal x);
0108     void moveToPreviousItem(int pos, qreal x);
0109 
0110     void populateMenu(QMenu*, QPointF) override;
0111 
0112   protected:
0113     bool wantToEvaluate() override;
0114 
0115   private:
0116     WorksheetTextItem* currentInformationItem();
0117     bool informationItemHasFocus();
0118     bool focusWithinThisItem();
0119     QPoint getPopupPosition();
0120     QPoint toGlobalPosition(QPointF);
0121     void initMenus();
0122     void handleExistedCompletionBox();
0123     void makeCompletion(const QString& line, int position);
0124 
0125     enum CompletionMode {PreliminaryCompletion, FinalCompletion};
0126     static const double VerticalSpacing;
0127 
0128     WorksheetTextItem* m_promptItem;
0129     WorksheetTextItem* m_commandItem;
0130     QVector<ResultItem*> m_resultItems;
0131     bool m_resultsCollapsed;
0132     WorksheetTextItem* m_errorItem;
0133     QList<WorksheetTextItem*> m_informationItems;
0134     Cantor::Expression* m_expression;
0135 
0136     Cantor::CompletionObject* m_completionObject;
0137     QPointer<KCompletionBox> m_completionBox;
0138     Cantor::SyntaxHelpObject* m_syntaxHelpObject;
0139 
0140     EvaluationOption m_evaluationOption;
0141     QPropertyAnimation* m_promptItemAnimation;
0142     bool m_menusInitialized;
0143     bool m_textColorCustom;
0144     bool m_backgroundColorCustom;
0145 
0146     //formatting
0147     QActionGroup* m_backgroundColorActionGroup;
0148     QMenu* m_backgroundColorMenu;
0149     QActionGroup* m_textColorActionGroup;
0150     QColor m_defaultDefaultTextColor;
0151     QMenu* m_textColorMenu;
0152     QMenu* m_fontMenu;
0153 
0154     bool m_isExecutionEnabled;
0155     QColor m_activeExecutionTextColor;
0156     QColor m_activeExecutionBackgroundColor;
0157 
0158   private Q_SLOTS:
0159     void invalidate();
0160     void resultDeleted();
0161     void clearResultItems();
0162     void removeResultItem(int index);
0163     void replaceResultItem(int index);
0164     void updateCompletions();
0165     void completeCommandTo(const QString& completion, CommandEntry::CompletionMode mode = PreliminaryCompletion);
0166     void changeResultCollapsingAction();
0167     void showHelp();
0168     void toggleEnabled();
0169 
0170     void backgroundColorChanged(QAction*);
0171     void textColorChanged(QAction*);
0172     void fontBoldTriggered();
0173     void fontItalicTriggered();
0174     void fontIncreaseTriggered();
0175     void fontDecreaseTriggered();
0176     void fontSelectTriggered();
0177     void resetFontTriggered();
0178 
0179     void animatePromptItem();
0180     void setMidPrompt();
0181     void setHidePrompt();
0182 };
0183 
0184 #endif // COMMANDENTRY_H