itom  4.1.0
scriptEditorWidget.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef SCRIPTEDITORWIDGET_H
24 #define SCRIPTEDITORWIDGET_H
25 
26 #include "../models/breakPointModel.h"
27 
28 #include "abstractCodeEditorWidget.h"
29 #include "../codeEditor/panels/foldingPanel.h"
30 #include "../codeEditor/panels/checkerBookmarkPanel.h"
31 #include "../codeEditor/panels/breakpointPanel.h"
32 #include "../codeEditor/modes/errorLineHighlight.h"
33 #include "../codeEditor/modes/pyGotoAssignment.h"
34 #include "../codeEditor/modes/pyDocstringGenerator.h"
35 #include "../codeEditor/modes/wordHoverTooltip.h"
36 #include "../codeEditor/panels/lineNumber.h"
37 #include "../codeEditor/codeCheckerItem.h"
38 #include "../codeEditor/pyCodeFormatter.h"
39 
40 #include "../global.h"
41 
42 #include <qfilesystemwatcher.h>
43 #include <qwidget.h>
44 #include <qstring.h>
45 #include <qmenu.h>
46 #include <qevent.h>
47 #include <qmetaobject.h>
48 #include <qsharedpointer.h>
49 #include <qregularexpression.h>
50 #include "../models/outlineItem.h"
51 #include "../models/bookmarkModel.h"
52 
53 #include <QtPrintSupport/qprinter.h>
54 
55 QT_BEGIN_NAMESPACE
56 
57 QT_END_NAMESPACE
58 
59 
60 
61 namespace ito
62 {
63 
64 class BreakPointModel;
65 
67 {
68  QString filename;
69  int firstVisibleLine;
70  QList<int> bookmarkLines;
71 };
72 
73 
75 {
76  QString filename;
77  int UID;
78  QString shortText;
79  int line;
80  int column;
81  QString origin;
82 };
83 
84 }
85 
86 Q_DECLARE_METATYPE(ito::ScriptEditorStorage) //must be outside of namespace
87 Q_DECLARE_METATYPE(QList<ito::ScriptEditorStorage>) //must be outside of namespace
88 Q_DECLARE_METATYPE(ito::GoBackNavigationItem) //must be outside of namespace
89 Q_DECLARE_METATYPE(QList<ito::GoBackNavigationItem>) //must be outside of namespace
90 
91 namespace ito
92 {
93 
94 
95 class ScriptEditorWidget : public AbstractCodeEditorWidget
96 {
97  Q_OBJECT
98 
99 public:
100  ScriptEditorWidget(BookmarkModel *bookmarkModel, QWidget* parent = nullptr);
101  ~ScriptEditorWidget();
102 
103  RetVal saveFile(bool askFirst = true);
104  RetVal saveAsFile(bool askFirst = true);
105 
106  RetVal openFile(QString file, bool ignorePresentDocument = false);
107 
108  bool keepIndentationOnPaste() const;
109  void setKeepIndentationOnPaste(bool value);
110 
111  inline QString getFilename() const {return m_filename; }
112  inline bool hasNoFilename() const { return m_filename.isNull(); }
113  inline int getUID() const { return m_uid; }
114  bool getCanCopy() const;
115  inline QString getUntitledName() const { return tr("Untitled%1").arg(m_uid); }
116 
117  RetVal setCursorPosAndEnsureVisible(const int line, bool errorMessageClick = false, bool showSelectedCallstackLine = false);
118  RetVal showLineAndHighlightWord(const int line, const QString &highlightedText, Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive);
119 
120  void removeCurrentCallstackLine();
121 
122  const ScriptEditorStorage saveState() const;
123  RetVal restoreState(const ScriptEditorStorage &data);
124 
125  RetVal toggleBookmark(int line);
126 
127  virtual bool removeTextBlockUserData(TextBlockUserData* userData);
128 
130  void reportCurrentCursorAsGoBackNavigationItem(const QString &reason, int UIDFilter = -1);
131 
133  void startUndoRedo(bool unundoNotRedo);
134 
135  QSharedPointer<OutlineItem> parseOutline(bool forceParsing = false) const;
136 
139  bool currentLineCanHaveDocstring() const;
140 
141  static QString filenameFromUID(int UID, bool &found);
142 
143 protected:
144 
145  bool canInsertFromMimeData(const QMimeData *source) const;
146  void insertFromMimeData(const QMimeData *source);
147 
148  void dropEvent(QDropEvent *event);
149  void dragEnterEvent(QDragEnterEvent *event);
150  void dragLeaveEvent(QDragLeaveEvent *event);
151  virtual void loadSettings();
152  bool event(QEvent *event);
153  void mouseReleaseEvent(QMouseEvent *event);
154  void mousePressEvent(QMouseEvent *event);
155  void keyPressEvent(QKeyEvent *event);
156 
157  virtual void contextMenuAboutToShow(int contextMenuLine);
158 
159  void reportGoBackNavigationCursorMovement(const CursorPosition &cursor, const QString &origin) const;
160 
161  void replaceSelectionAndKeepBookmarksAndBreakpoints(QTextCursor &cursor, const QString &newString);
162  QVector<int> compareTexts(const QString &oldText, const QString &newText);
163 
164  BreakPointModel* getBreakPointModel();
165  const BreakPointModel* getBreakPointModel() const;
166 
167 private:
168  enum markerType
169  {
170  markerBookmark = 1,
171  markerPyBug = 2,
172  markerBookmarkAndPyBug = markerBookmark | markerPyBug
173  };
174 
175  RetVal initEditor();
176  void initMenus();
177 
178  bool lineAcceptsBPs(int line);
179 
180  RetVal changeFilename(const QString &newFilename);
181 
182  QFileSystemWatcher *m_pFileSysWatcher;
183 
184  // the following variables are related to the code checker feature of Python
185  bool m_codeCheckerEnabled;
186  int m_codeCheckerInterval;
187  QTimer *m_codeCheckerCallTimer;
188 
189  Qt::CaseSensitivity m_filenameCaseSensitivity;
190 
192  std::map<QString,QAction*> m_editorMenuActions;
193 
194  QString m_filename;
195  int m_uid;
196 
197  //go back navigation features
198  bool m_cursorJumpLastAction;
199  CursorPosition m_cursorBeforeMouseClick;
200 
201  bool m_pythonBusy;
202  bool m_pythonExecutable;
203 
206  bool m_wasReadonly;
207  bool m_canCopy;
208  bool m_keepIndentationOnPaste;
209  int m_textBlockLineIdxAboutToBeDeleted;
210  BookmarkModel *m_pBookmarkModel;
211 
212  QSharedPointer<PyCodeFormatter> m_pyCodeFormatter;
213  QString m_autoCodeFormatCmd;
214 
215  QSharedPointer<FoldingPanel> m_foldingPanel;
216  QSharedPointer<CheckerBookmarkPanel> m_checkerBookmarkPanel;
217  QSharedPointer<BreakpointPanel> m_breakpointPanel;
218  QSharedPointer<ErrorLineHighlighterMode> m_errorLineHighlighterMode;
219  QSharedPointer<LineNumberPanel> m_lineNumberPanel;
220  QSharedPointer<PyGotoAssignmentMode> m_pyGotoAssignmentMode;
221  QSharedPointer<WordHoverTooltipMode> m_wordHoverTooltipMode;
222  QSharedPointer<PyDocstringGeneratorMode> m_pyDocstringGeneratorMode;
223 
224  static const QString lineBreak;
225  static int currentMaximumUID;
226  static CursorPosition currentGlobalEditorCursorPos;
227  static QHash<int, ScriptEditorWidget*> editorByUID;
228 
229  // Outline
230  QTimer *m_outlineTimer;
231  bool m_outlineTimerEnabled;
232  int m_currentLineIndex;
233  mutable QSharedPointer<OutlineItem> m_rootOutlineItem;
234  mutable bool m_outlineDirty;
235  QRegularExpression m_regExpClass;
236  QRegularExpression m_regExpDecorator;
237  QRegularExpression m_regExpMethodStart;
238  QRegularExpression m_regExpMethod;
239 
240  void parseOutlineRecursive(QSharedPointer<OutlineItem> &parent) const;
241  QSharedPointer<OutlineItem> checkBlockForOutlineItem(int startLineIdx, int endLineIdx) const;
242 
243 signals:
244  void pythonRunFile(QString filename);
245  void pythonRunSelection(QString selectionText);
246  void pythonDebugFile(QString filename);
247  void closeRequest(ScriptEditorWidget* sew, bool ignoreModifications); //signal emitted if this tab should be closed without considering any save-state
248  void marginChanged();
249  void outlineModelChanged(ScriptEditorWidget *editor, QSharedPointer<OutlineItem> rootItem);
250  void addGoBackNavigationItem(const GoBackNavigationItem &item);
251  void tabChangeRequested();
252  void findSymbolsShowRequested();
253 
254 public slots:
255  void triggerCodeChecker();
256  void codeCheckResultsReady(QList<ito::CodeCheckerItem> codeCheckerItems);
257  void codeCheckerResultsChanged(const QList<ito::CodeCheckerItem> &codeCheckerItems);
258 
259  void menuCut();
260  void menuCopy();
261  void menuPaste();
262  void menuIndent();
263  void menuUnindent();
264  void menuComment();
265  void menuUncomment();
266 
267  void menuFoldAll();
268  void menuUnfoldAll();
269  void menuFoldUnfoldToplevel();
270  void menuFoldUnfoldAll();
271 
272  void menuRunScript();
273  void menuRunSelection();
274  void menuDebugScript();
275  void menuStopScript();
276 
277  void menuPyCodeFormatting();
278  void menuGenerateDocstring();
279  void menuInsertCodec();
280 
281  void pythonStateChanged(tPythonTransitions pyTransition);
282  void pythonDebugPositionChanged(QString filename, int lineno);
283 
284  void breakPointAdd(BreakPointItem bp, int row);
285  void breakPointDelete(QString filename, int lineIdx, int pyBpNumber);
286  void breakPointChange(BreakPointItem oldBp, BreakPointItem newBp);
287 
288  void updateSyntaxCheck();
289 
290  void print();
291 
292 private slots:
293  void toggleBookmarkRequested(int line);
294  void onBookmarkAdded(const BookmarkItem &item);
295  void onBookmarkDeleted(const BookmarkItem &item);
296 
297  RetVal toggleBreakpoint(int line);
298  RetVal toggleEnableBreakpoint(int line);
299  RetVal editBreakpoint(int line);
300  RetVal clearAllBreakpoints();
301  RetVal gotoNextBreakPoint();
302  RetVal gotoPreviousBreakPoint();
303 
304  void gotoAssignmentOutOfDoc(PyAssignment ref);
305 
306  void copyAvailable(const bool yes);
307 
308  void outlineTimerElapsed();
309 
310  void nrOfLinesChanged();
311 
312  void fileSysWatcherFileChanged(const QString &path);
313  void fileSysWatcherFileChangedStep2(const QString &path);
314  void printPreviewRequested(QPrinter *printer);
315 
316  void dumpFoldsToConsole(bool);
317  void onCursorPositionChanged();
318  void onTextChanged();
319  void tabChangeRequest();
320 
321  void pyCodeFormatterDone(bool success, QString code);
322 };
323 
324 } //end namespace ito
325 
326 #endif
model for management of all bookmarks. This model will be displayed by a viewer-widget in the main wi...
model for management of all breakpoints. This model will be displayed by a viewer-widget in the main ...
Definition: scriptEditorWidget.h:66
Definition: apiFunctionsGraph.cpp:39
Definition: scriptEditorWidget.h:74