itom
Loading...
Searching...
No Matches
scriptEditorWidget.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität 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#include "../codeEditor/pyCodeReferenceRenamer.h"
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 <qpointer.h>
48#include <qmetaobject.h>
49#include <qsharedpointer.h>
50#include <qregularexpression.h>
51#include "../models/outlineItem.h"
52#include "../models/bookmarkModel.h"
53#include "../helper/IOHelper.h"
54#include "../ui/dialogScriptCharsetEncoding.h"
55
56#include <QtPrintSupport/qprinter.h>
57
58QT_BEGIN_NAMESPACE
59
60QT_END_NAMESPACE
61
62
63
64namespace ito
65{
66
67class BreakPointModel;
68
70{
71 QString filename;
72 int firstVisibleLine;
73 QList<int> bookmarkLines;
74};
75
76
78{
79 QString filename;
80 int UID;
81 QString shortText;
82 int line;
83 int column;
84 QString origin;
85};
86
87}
88
89Q_DECLARE_METATYPE(ito::ScriptEditorStorage) //must be outside of namespace
90Q_DECLARE_METATYPE(QList<ito::ScriptEditorStorage>) //must be outside of namespace
91Q_DECLARE_METATYPE(ito::GoBackNavigationItem) //must be outside of namespace
92Q_DECLARE_METATYPE(QList<ito::GoBackNavigationItem>) //must be outside of namespace
93
94namespace ito
95{
96
97
99{
100 Q_OBJECT
101
102public:
103 ScriptEditorWidget(BookmarkModel *bookmarkModel, QWidget* parent = nullptr);
105
106 RetVal saveFile(bool askFirst = true);
107 RetVal saveAsFile(bool askFirst = true);
108
109 RetVal openFile(const QString &fileName, bool ignorePresentDocument = false, QWidget *parent = nullptr);
110
111 bool keepIndentationOnPaste() const;
112 void setKeepIndentationOnPaste(bool value);
113
114 inline QString getFilename() const {return m_filename; }
115 inline bool hasNoFilename() const { return m_filename.isNull(); }
116 inline int getUID() const { return m_uid; }
117 bool getCanCopy() const;
118 inline QString getUntitledName() const { return tr("Untitled%1").arg(m_uid); }
119
120 RetVal setCursorPosAndEnsureVisible(const int line, bool errorMessageClick = false, bool showSelectedCallstackLine = false);
121 RetVal showLineAndHighlightWord(const int line, const QString &highlightedText, Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive);
122
124
125 const ScriptEditorStorage saveState() const;
126 RetVal restoreState(const ScriptEditorStorage &data);
127
128 RetVal toggleBookmark(int line);
129
130 virtual bool removeTextBlockUserData(TextBlockUserData* userData);
131
133 void reportCurrentCursorAsGoBackNavigationItem(const QString &reason, int UIDFilter = -1);
134
136 void startUndoRedo(bool unundoNotRedo);
137
138 QSharedPointer<OutlineItem> parseOutline(bool forceParsing = false) const;
139
142 bool currentLineCanHaveDocstring() const;
143
144 IOHelper::CharsetEncodingItem charsetEncoding() const { return m_charsetEncoding; }
145
147 void replaceOccurencesInCurrentScript(const QString &newValue, const QVector<ito::FileRenameItem> &renameItems);
148
149 static QString filenameFromUID(int UID, bool &found);
150
151protected:
152
153 bool canInsertFromMimeData(const QMimeData *source) const;
154 void insertFromMimeData(const QMimeData *source);
155
156 void dropEvent(QDropEvent *event);
157 void dragEnterEvent(QDragEnterEvent *event);
158 void dragLeaveEvent(QDragLeaveEvent *event);
159 virtual void loadSettings();
160 bool event(QEvent *event);
161 void mouseReleaseEvent(QMouseEvent *event);
162 void mousePressEvent(QMouseEvent *event);
163 void keyPressEvent(QKeyEvent *event);
164
165 virtual void contextMenuAboutToShow(int contextMenuLine);
166
167 void reportGoBackNavigationCursorMovement(const CursorPosition &cursor, const QString &origin) const;
168
169 void replaceSelectionAndKeepBookmarksAndBreakpoints(QTextCursor &cursor, const QString &newString);
170 QVector<int> compareTexts(const QString &oldText, const QString &newText);
171
172 BreakPointModel* getBreakPointModel();
173 const BreakPointModel* getBreakPointModel() const;
174
175private:
176 enum markerType
177 {
178 markerBookmark = 1,
179 markerPyBug = 2,
180 markerBookmarkAndPyBug = markerBookmark | markerPyBug
181 };
182
183 RetVal initEditor();
184 void initMenus();
185
186 bool lineAcceptsBPs(int line);
187
188 RetVal changeFilename(const QString &newFilename);
189
190 IOHelper::CharsetEncodingItem guessEncoding(const QByteArray &content) const;
191
192 void changeFileSaveEncoding(const IOHelper::CharsetEncodingItem &encoding);
193
194 QFileSystemWatcher *m_pFileSysWatcher;
195
196 // the following variables are related to the code checker feature of Python
197 bool m_codeCheckerEnabled;
200
201 Qt::CaseSensitivity m_filenameCaseSensitivity;
202
204 std::map<QString,QAction*> m_editorMenuActions;
205
206 QString m_filename;
207 int m_uid;
208
209 //go back navigation features
211 CursorPosition m_cursorBeforeMouseClick;
212
215
218 bool m_wasReadonly;
219 bool m_canCopy;
220 bool m_keepIndentationOnPaste;
222 BookmarkModel *m_pBookmarkModel;
223
224 QSharedPointer<PyCodeFormatter> m_pyCodeFormatter;
225 QSharedPointer<PyCodeReferenceRenamer> m_pyCodeReferenceRenamer;
226
229
232
236 IOHelper::CharsetEncodingItem m_charsetEncoding;
237 bool m_charsetDefined;
238 bool m_charsetEncodingAutoGuess;
239
240 QSharedPointer<FoldingPanel> m_foldingPanel;
241 QSharedPointer<CheckerBookmarkPanel> m_checkerBookmarkPanel;
242 QSharedPointer<BreakpointPanel> m_breakpointPanel;
243 QSharedPointer<ErrorLineHighlighterMode> m_errorLineHighlighterMode;
244 QSharedPointer<LineNumberPanel> m_lineNumberPanel;
245 QSharedPointer<PyGotoAssignmentMode> m_pyGotoAssignmentMode;
246 QSharedPointer<WordHoverTooltipMode> m_wordHoverTooltipMode;
247 QSharedPointer<PyDocstringGeneratorMode> m_pyDocstringGeneratorMode;
248
249 static const QString lineBreak;
250 static int currentMaximumUID;
251 static CursorPosition currentGlobalEditorCursorPos;
252 static QHash<int, ScriptEditorWidget*> editorByUID;
253
254 // Outline
256 bool m_outlineTimerEnabled;
258 mutable QSharedPointer<OutlineItem> m_rootOutlineItem;
259 mutable bool m_outlineDirty;
260 QRegularExpression m_regExpClass;
261 QRegularExpression m_regExpDecorator;
262 QRegularExpression m_regExpMethodStart;
263 QRegularExpression m_regExpMethod;
264
265 void parseOutlineRecursive(QSharedPointer<OutlineItem> &parent) const;
266 QSharedPointer<OutlineItem> checkBlockForOutlineItem(int startLineIdx, int endLineIdx) const;
267
268signals:
269 void pythonRunFile(QString filename);
270 void pythonRunSelection(QString selectionText);
271 void pythonDebugFile(QString filename);
272 void closeRequest(ScriptEditorWidget* sew, bool ignoreModifications); //signal emitted if this tab should be closed without considering any save-state
273 void marginChanged();
274 void outlineModelChanged(ScriptEditorWidget *editor, QSharedPointer<OutlineItem> rootItem);
275 void addGoBackNavigationItem(const GoBackNavigationItem &item);
276 void tabChangeRequested();
277 void findSymbolsShowRequested();
278
279public slots:
280 void triggerCodeChecker();
281 void codeCheckResultsReady(QList<ito::CodeCheckerItem> codeCheckerItems);
282 void codeCheckerResultsChanged(const QList<ito::CodeCheckerItem> &codeCheckerItems);
283
284 void menuCut();
285 void menuCopy();
286 void menuPaste();
287 void menuIndent();
288 void menuUnindent();
289 void menuComment();
290 void menuUncomment();
291
292 void menuFoldAll();
293 void menuUnfoldAll();
294 void menuFoldUnfoldToplevel();
295 void menuFoldUnfoldAll();
296
297 void menuRunScript();
298 void menuRunSelection();
299 void menuDebugScript();
300 void menuStopScript();
301
302 void menuPyCodeFormatting();
303 bool menuPyCodeReferenceRenaming();
304 void menuGenerateDocstring();
305 void menuScriptCharsetEncoding();
306
307 void pythonStateChanged(tPythonTransitions pyTransition);
308 void pythonDebugPositionChanged(QString filename, int lineno);
309
310 void breakPointAdd(BreakPointItem bp, int row);
311 void breakPointDelete(QString filename, int lineIdx, int pyBpNumber);
313
314 void updateSyntaxCheck();
315
316 void print();
317
318private slots:
319 void toggleBookmarkRequested(int line);
320 void onBookmarkAdded(const BookmarkItem &item);
321 void onBookmarkDeleted(const BookmarkItem &item);
322
323 RetVal toggleBreakpoint(int line);
324 RetVal toggleEnableBreakpoint(int line);
325 RetVal editBreakpoint(int line);
326 RetVal clearAllBreakpoints();
327 RetVal gotoNextBreakPoint();
328 RetVal gotoPreviousBreakPoint();
329
330 void gotoAssignmentOutOfDoc(PyAssignment ref);
331
332 void copyAvailable(const bool yes);
333
334 void outlineTimerElapsed();
335
336 void nrOfLinesChanged();
337
338 void fileSysWatcherFileChanged(const QString &path);
339 void fileSysWatcherFileChangedStep2(const QString &path);
340 void printPreviewRequested(QPrinter *printer);
341
342 void dumpFoldsToConsole(bool);
343 void onCursorPositionChanged();
344 void onTextChanged();
345 void tabChangeRequest();
346
347 void pyCodeFormatterDone(bool success, QString code);
348};
349
350} //end namespace ito
351
352#endif
Definition abstractCodeEditorWidget.h:47
model for management of all bookmarks. This model will be displayed by a viewer-widget in the main wi...
Definition bookmarkModel.h:65
model for management of all breakpoints. This model will be displayed by a viewer-widget in the main ...
Definition breakPointModel.h:69
Class for managing status values (like errors or warning)
Definition retVal.h:54
Definition scriptEditorWidget.h:99
void breakPointAdd(BreakPointItem bp, int row)
< slot, invoked by BreakPointModel
Definition scriptEditorWidget.cpp:2952
bool m_cursorJumpLastAction
true if the last cursor movement was a jump by a mouse click
Definition scriptEditorWidget.h:210
void reportCurrentCursorAsGoBackNavigationItem(const QString &reason, int UIDFilter=-1)
wrapper for undo() or redo() that tries to keep breakpoints and bookmarks
Definition scriptEditorWidget.cpp:3825
QString m_autoCodeFormatCmd
the current command string for the python imports sorting (or empty, if this pre-step is not enabled)
Definition scriptEditorWidget.h:228
RetVal openFile(const QString &fileName, bool ignorePresentDocument=false, QWidget *parent=nullptr)
Definition scriptEditorWidget.cpp:1989
RetVal toggleBookmark(int line)
< bookmark handling
Definition scriptEditorWidget.cpp:2594
void triggerCodeChecker()
Sends the code to the Syntax Checker.
Definition scriptEditorWidget.cpp:2445
int m_codeCheckerInterval
timeout time after the last key press, when the next code check is called.
Definition scriptEditorWidget.h:198
Qt::CaseSensitivity m_filenameCaseSensitivity
menus
Definition scriptEditorWidget.h:201
QRegularExpression m_regExpMethodStart
regular expression to parse the start of a method definition
Definition scriptEditorWidget.h:262
virtual bool removeTextBlockUserData(TextBlockUserData *userData)
if UidFilter is -1, the current cursor position is always reported, else only if its editorUID is equ...
Definition scriptEditorWidget.cpp:3176
ScriptEditorWidget(BookmarkModel *bookmarkModel, QWidget *parent=nullptr)
Definition scriptEditorWidget.cpp:75
QSharedPointer< PyCodeFormatter > m_pyCodeFormatter
borrowed reference to the bookmark model. The owner of this model is the ScriptEditorOrganizer.
Definition scriptEditorWidget.h:224
static QHash< int, ScriptEditorWidget * > editorByUID
the current cursor position within all opened editor widgets
Definition scriptEditorWidget.h:252
QString m_filename
canonical filename of the script or empty if no script name has been given yet
Definition scriptEditorWidget.h:206
void startUndoRedo(bool unundoNotRedo)
< executes and undo or redo action and tries to preserve bookmarks and breakpoints
Definition scriptEditorWidget.cpp:1629
QRegularExpression m_regExpDecorator
regular expression to parse a decorator
Definition scriptEditorWidget.h:261
QRegularExpression m_regExpMethod
regular expression to parse a full method definition
Definition scriptEditorWidget.h:263
QRegularExpression m_regExpClass
regular expression to parse the definition of a class
Definition scriptEditorWidget.h:260
bool m_pythonBusy
true: python is executing or debugging a script, a command...
Definition scriptEditorWidget.h:213
QSharedPointer< PyCodeReferenceRenamer > m_pyCodeReferenceRenamer
the current command string for the python auto code formatting.
Definition scriptEditorWidget.h:225
int m_textBlockLineIdxAboutToBeDeleted
if != -1, a TextBlockUserData in the line index is about to be removed.
Definition scriptEditorWidget.h:221
QSharedPointer< OutlineItem > m_rootOutlineItem
cache for the latest outline items
Definition scriptEditorWidget.h:258
static const QString lineBreak
< static variable initialization
Definition scriptEditorWidget.h:249
void removeCurrentCallstackLine()
removes the current-callstack-line arrow from the breakpoint panel, if currently displayed
Definition scriptEditorWidget.cpp:1113
IOHelper::CharsetEncodingItem charsetEncoding() const
the replacement will be handled as one undo-action. The script will be marked as modified afterwards.
Definition scriptEditorWidget.h:144
QString m_autoCodeFormatPreCmd
Definition scriptEditorWidget.h:231
void codeCheckerResultsChanged(const QList< ito::CodeCheckerItem > &codeCheckerItems)
Updates the list of code checker results if changes are available.
Definition scriptEditorWidget.cpp:2407
bool m_pythonExecutable
Definition scriptEditorWidget.h:214
QTimer * m_outlineTimer
hash table that maps the UID to its instance of ScriptEditorWidget*
Definition scriptEditorWidget.h:255
void codeCheckResultsReady(QList< ito::CodeCheckerItem > codeCheckerItems)
slot invoked by pythonEnginge::pythonCodeCheck
Definition scriptEditorWidget.cpp:2397
RetVal toggleBreakpoint(int line)
Definition scriptEditorWidget.cpp:2690
QTimer * m_codeCheckerCallTimer
timer, that is used to call a new code check after a certain time after the key press
Definition scriptEditorWidget.h:199
void updateSyntaxCheck()
slot invoked by timer
Definition scriptEditorWidget.cpp:2469
int m_currentLineIndex
current line index of the cursor
Definition scriptEditorWidget.h:257
void breakPointDelete(QString filename, int lineIdx, int pyBpNumber)
< slot, invoked by BreakPointModel
Definition scriptEditorWidget.cpp:3006
QSharedPointer< OutlineItem > parseOutline(bool forceParsing=false) const
Definition scriptEditorWidget.cpp:3662
void breakPointChange(BreakPointItem oldBp, BreakPointItem newBp)
< slot, invoked by BreakPointModel
Definition scriptEditorWidget.cpp:3034
Definition textBlockUserData.h:56
Definition apiFunctionsGraph.cpp:40
item of BookmarkModel
Definition bookmarkModel.h:43
item of BreakPointModel
Definition breakPointModel.h:43
Definition codeEditor.h:344
Definition scriptEditorWidget.h:78
Definition IOHelper.h:81
Definition pyGotoAssignment.h:56
Definition scriptEditorWidget.h:70