1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
4     SPDX-FileCopyrightText: 2012 Martin Kuettler <martin.kuettler@gmail.com>
5     SPDX-FileCopyrightText: 2018-2021 Alexander Semke <alexander.semke@web.de>
6 */
7 
8 #ifndef COMMANDENTRY_H
9 #define COMMANDENTRY_H
10 
11 #include <QPointer>
12 #include <KCompletionBox>
13 
14 #include "worksheetentry.h"
15 #include "lib/expression.h"
16 
17 class Worksheet;
18 class ResultItem;
19 class QTimer;
20 class QJsonObject;
21 
22 namespace Cantor{
23     class Result;
24     class CompletionObject;
25     class SyntaxHelpObject;
26 }
27 
28 class CommandEntry : public WorksheetEntry
29 {
30   Q_OBJECT
31   public:
32     static const QString Prompt;
33     static const QString MidPrompt;
34     static const QString HidePrompt;
35 
36     explicit CommandEntry(Worksheet*);
37     ~CommandEntry() override;
38 
39     enum {Type = UserType + 2};
40     int type() const override;
41 
42     QString command();
43     void setExpression(Cantor::Expression*);
44     Cantor::Expression* expression();
45 
46     QString currentLine();
47 
48     bool isEmpty() override;
49     bool isExcludedFromExecution();
50     bool isResultCollapsed();
51 
52     void setContent(const QString&) override;
53     void setContent(const QDomElement&, const KZip&) override;
54     void setContentFromJupyter(const QJsonObject&) override;
55 
56     QDomElement toXml(QDomDocument&, KZip*) override;
57     QJsonValue toJupyterJson() override;
58     QString toPlain(const QString& commandSep, const QString& commentStartingSeq, const QString& commentEndingSeq) override;
59 
60     void setCompletion(Cantor::CompletionObject* tc);
61     void setSyntaxHelp(Cantor::SyntaxHelpObject* sh);
62 
63     bool acceptRichText() override;
64 
65     void removeContextHelp();
66 
67     void interruptEvaluation() override;
68     bool isShowingCompletionPopup();
69 
70     bool focusEntry(int pos = WorksheetTextItem::TopLeft, qreal xCoord = 0) override;
71 
72     void layOutForWidth(qreal entry_zone_x, qreal w, bool force = false) override;
73     qreal promptItemWidth();
74 
75     WorksheetTextItem* highlightItem() override;
76 
77     WorksheetCursor search(const QString& pattern, unsigned flags,
78                            QTextDocument::FindFlags qt_flags,
79                            const WorksheetCursor& pos = WorksheetCursor()) override;
80 
81   public Q_SLOTS:
82     bool evaluateCurrentItem() override;
83     bool evaluate(WorksheetEntry::EvaluationOption evalOp = FocusNext) override;
84     void addInformation();
85     void removeResults();
86     void removeResult(Cantor::Result* result);
87     void collapseResults();
88     void expandResults();
89     void excludeFromExecution();
90     void addToExecution();
91 
92     void showCompletion() override;
93     void handleTabPress();
94     void handleBacktabPress();
95     void updateEntry() override;
96     void updatePrompt(const QString& postfix = CommandEntry::Prompt);
97     void expressionChangedStatus(Cantor::Expression::Status);
98     void showAdditionalInformationPrompt(const QString&);
99     void showCompletions();
100     void applySelectedCompletion();
101     void completedLineChanged();
102     void showSyntaxHelp();
103     void completeLineTo(const QString& line, int index);
104 
105     void startRemoving() override;
106 
107     void moveToNextItem(int pos, qreal x);
108     void moveToPreviousItem(int pos, qreal x);
109 
110     void populateMenu(QMenu*, QPointF) override;
111 
112   protected:
113     bool wantToEvaluate() override;
114 
115   private:
116     WorksheetTextItem* currentInformationItem();
117     bool informationItemHasFocus();
118     bool focusWithinThisItem();
119     QPoint getPopupPosition();
120     QPoint toGlobalPosition(QPointF);
121     void initMenus();
122     void handleExistedCompletionBox();
123     void makeCompletion(const QString& line, int position);
124 
125     enum CompletionMode {PreliminaryCompletion, FinalCompletion};
126     static const double VerticalSpacing;
127 
128     WorksheetTextItem* m_promptItem;
129     WorksheetTextItem* m_commandItem;
130     QVector<ResultItem*> m_resultItems;
131     bool m_resultsCollapsed;
132     WorksheetTextItem* m_errorItem;
133     QList<WorksheetTextItem*> m_informationItems;
134     Cantor::Expression* m_expression;
135 
136     Cantor::CompletionObject* m_completionObject;
137     QPointer<KCompletionBox> m_completionBox;
138     Cantor::SyntaxHelpObject* m_syntaxHelpObject;
139 
140     EvaluationOption m_evaluationOption;
141     QPropertyAnimation* m_promptItemAnimation;
142     bool m_menusInitialized;
143     bool m_textColorCustom;
144     bool m_backgroundColorCustom;
145 
146     //formatting
147     QActionGroup* m_backgroundColorActionGroup;
148     QMenu* m_backgroundColorMenu;
149     QActionGroup* m_textColorActionGroup;
150     QColor m_defaultDefaultTextColor;
151     QMenu* m_textColorMenu;
152     QMenu* m_fontMenu;
153 
154     bool m_isExecutionEnabled;
155     QColor m_activeExecutionTextColor;
156     QColor m_activeExecutionBackgroundColor;
157 
158   private Q_SLOTS:
159     void invalidate();
160     void resultDeleted();
161     void clearResultItems();
162     void removeResultItem(int index);
163     void replaceResultItem(int index);
164     void updateCompletions();
165     void completeCommandTo(const QString& completion, CommandEntry::CompletionMode mode = PreliminaryCompletion);
166     void changeResultCollapsingAction();
167     void showHelp();
168     void toggleEnabled();
169 
170     void backgroundColorChanged(QAction*);
171     void textColorChanged(QAction*);
172     void fontBoldTriggered();
173     void fontItalicTriggered();
174     void fontIncreaseTriggered();
175     void fontDecreaseTriggered();
176     void fontSelectTriggered();
177     void resetFontTriggered();
178 
179     void animatePromptItem();
180     void setMidPrompt();
181     void setHidePrompt();
182 };
183 
184 #endif // COMMANDENTRY_H
185