itom  3.0.0
scriptEditorWidget.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2016, 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 "abstractPyScintillaWidget.h"
29 #include "../global.h"
30 
31 #include <qfilesystemwatcher.h>
32 #include <qmutex.h>
33 #include <qwidget.h>
34 #include <qstring.h>
35 #include <qmenu.h>
36 #include <qevent.h>
37 #include <qmetaobject.h>
38 #include "../models/classNavigatorItem.h"
39 
40 #if QT_VERSION >= 0x050000
41  #include <QtPrintSupport/qprinter.h>
42 #else
43  #include <qprinter.h>
44 #endif
45 
46 #include <Qsci/qsciprinter.h>
47 
48 QT_BEGIN_NAMESPACE
49 
50 QT_END_NAMESPACE
51 
52 
53 
54 namespace ito
55 {
56 
58 {
59  QByteArray filename;
60  int firstVisibleLine;
61  QList<int> bookmarkLines;
62 };
63 
64 }
65 
66 Q_DECLARE_METATYPE(ito::ScriptEditorStorage) //must be outside of namespace
67 Q_DECLARE_METATYPE(QList<ito::ScriptEditorStorage>) //must be outside of namespace
68 
69 namespace ito
70 {
71 
72 class ScriptEditorWidget : public AbstractPyScintillaWidget
73 {
74  Q_OBJECT
75 
76 public:
77  ScriptEditorWidget(QWidget* parent = NULL);
78  ~ScriptEditorWidget();
79 
80  bool m_errorMarkerVisible;
81  int m_errorMarkerNr; //number of indicator which marks the line with current error
82 
83  RetVal saveFile(bool askFirst = true);
84  RetVal saveAsFile(bool askFirst = true);
85 
86  RetVal openFile(QString file, bool ignorePresentDocument = false);
87 
88  inline QString getFilename() const {return m_filename; }
89  inline bool hasNoFilename() const { return m_filename.isNull(); }
90  inline bool getCanCopy() const { return canCopy; }
91  bool isBookmarked() const;
92  inline QString getUntitledName() const { return tr("Untitled%1").arg(unnamedNumber); }
93  inline QString getCurrentClass() const { return m_currentClass; } //currently chosen class in class navigator for this script editor widget
94  inline QString getCurrentMethod() const { return m_currentMethod; } //currently chosen method in class navigator for this script editor widget
95 
96  RetVal setCursorPosAndEnsureVisible(const int line, bool errorMessageClick = false);
97  RetVal setCursorPosAndEnsureVisibleWithSelection(const int line, const QString &currentClass, const QString &currentMethod);
98 
99  const ScriptEditorStorage saveState() const;
100  RetVal restoreState(const ScriptEditorStorage &data);
101 
102 protected:
103  //void keyPressEvent (QKeyEvent *event);
104  bool canInsertFromMimeData(const QMimeData *source) const;
105  void autoAdaptLineNumberColumnWidth();
106 // void dragEnterEvent(QDragEnterEvent *event);
107  void dropEvent(QDropEvent *event);
108  virtual void loadSettings();
109  bool event(QEvent *event);
110  void mouseReleaseEvent(QMouseEvent *event);
111 
112 private:
113 
114  enum markerType
115  {
116  markerBookmark = 1,
117  markerPyBug = 2,
118  markerBookmarkAndPyBug = markerBookmark | markerPyBug
119  };
120 
121  RetVal initEditor();
122 
123  void contextMenuEvent (QContextMenuEvent * event);
124 
125  int getMarginNumber(int xPos);
126 
127  RetVal initMenus();
128 
129  RetVal toggleBookmark(int line);
130  RetVal clearAllBookmarks();
131  RetVal gotoNextBookmark();
132  RetVal gotoPreviousBookmark();
133 
134  RetVal toggleBreakpoint(int line);
135  RetVal toggleEnableBreakpoint(int line);
136  RetVal editBreakpoint(int line);
137  RetVal clearAllBreakpoints();
138  RetVal gotoNextBreakPoint();
139  RetVal gotoPreviousBreakPoint();
140 
141  bool lineAcceptsBPs(int line);
142 
143  RetVal changeFilename(const QString &newFilename);
144 
145  QFileSystemWatcher *m_pFileSysWatcher;
146  QMutex fileSystemWatcherMutex;
147 
149  struct BookmarkErrorEntry
150  {
151  int handle;
152  int type; //entry of enum markerType
153  QString errorMessage;
154  QString errorComment;
155  int errorPos;
156  };
157  QList<BookmarkErrorEntry> bookmarkErrorHandles;
158  int syntaxErrorHandle;
159 
160  bool m_syntaxCheckerEnabled;
161  int m_syntaxCheckerInterval;
162  QTimer *m_syntaxTimer;
163  // int m_lastTipLine; // TODO: not used anymore?
164 
165  struct BPMarker
166  {
167  int bpHandle;
168  int lineNo;
169  bool markedForDeletion;
170  };
171 
172  QList<BPMarker> m_breakPointMap;
173 
174  unsigned int markBreakPoint;
175  unsigned int markCBreakPoint;
176  unsigned int markBreakPointDisabled;
177  unsigned int markCBreakPointDisabled;
178  unsigned int markBookmark;
179  unsigned int markSyntaxError;
180  unsigned int markBookmarkSyntaxError;
181 
182  unsigned int markCurrentLine;
183  int markCurrentLineHandle;
184 
185  unsigned int markMask1;
186  unsigned int markMask2;
187  unsigned int markMaskBreakpoints;
188 
189  //QsciLexerPython* qSciLex;
190  //QsciAPIs* qSciApi;
191 
193  QMenu *bookmarkMenu;
194  QMenu *syntaxErrorMenu;
195  QMenu *breakpointMenu;
196  QMenu *editorMenu;
197 
198  std::map<QString,QAction*> bookmarkMenuActions;
199  std::map<QString,QAction*> breakpointMenuActions;
200  std::map<QString,QAction*> editorMenuActions;
201 
202  int contextMenuLine;
203 
204  QString m_filename;
205  int unnamedNumber;
206 
207  bool pythonBusy;
208  bool m_pythonExecutable;
209 
210  bool canCopy;
211 
212  static const QString lineBreak;
213  static int unnamedAutoIncrement;
214 
215  // Class Navigator
216  bool m_ClassNavigatorEnabled; // Enable Class-Navigator
217  QTimer *m_classNavigatorTimer; // Class Navigator Timer
218  bool m_classNavigatorTimerEnabled; // Class Navigator Timer Enable
219  int m_classNavigatorInterval; // Class Navigator Timer Interval
220  QString m_currentClass;
221  QString m_currentMethod;
222 
223  int buildClassTree(ClassNavigatorItem *parent, int parentDepth, int lineNumber, int singleIndentation = -1);
224  int getIndentationLength(const QString &str) const;
225 
226 signals:
227  void pythonRunFile(QString filename);
228  void pythonRunSelection(QString selectionText);
229  void pythonDebugFile(QString filename);
230  void closeRequest(ScriptEditorWidget* sew, bool ignoreModifications); //signal emitted if this tab should be closed without considering any save-state
231  void marginChanged();
232  void requestModelRebuild(ScriptEditorWidget *editor);
233 
234 public slots:
235  void menuToggleBookmark();
236  void checkSyntax();
237  void syntaxCheckResult(QString a, QString b);
238  void errorListChange(const QStringList &errorList);
239  void menuClearAllBookmarks();
240  void menuGotoNextBookmark();
241  void menuGotoPreviousBookmark();
242 
243  void menuToggleBreakpoint();
244  void menuToggleEnableBreakpoint();
245  void menuEditBreakpoint();
246  void menuClearAllBreakpoints();
247  void menuGotoNextBreakPoint();
248  void menuGotoPreviousBreakPoint();
249 
250  void menuCut();
251  void menuCopy();
252  void menuPaste();
253  void menuIndent();
254  void menuUnindent();
255  void menuComment();
256  void menuUncomment();
257 
258  void menuUnfoldAll();
259  void menuFoldUnfoldToplevel();
260  void menuFoldUnfoldAll();
261 
262  void menuRunScript();
263  void menuRunSelection();
264  void menuDebugScript();
265  void menuStopScript();
266 
267  void menuInsertCodec();
268 
269  void pythonStateChanged(tPythonTransitions pyTransition);
270  void pythonDebugPositionChanged(QString filename, int lineno);
271 
272  void breakPointAdd(BreakPointItem bp, int row);
273  void breakPointDelete(QString filename, int lineNo, int pyBpNumber);
274  void breakPointChange(BreakPointItem oldBp, BreakPointItem newBp);
275 
276  void updateSyntaxCheck();
277 
278  // Class Navigator
279  ClassNavigatorItem* getPythonNavigatorRoot(); //creates new tree of current python code structure and returns its root pointer. Caller must delete the root pointer after usage.
280 
281  void print();
282 
283 private slots:
284  void marginClicked(int margin, int line, Qt::KeyboardModifiers state);
285  void copyAvailable(const bool yes);
286 
287  void classNavTimerElapsed();
288 
289  void nrOfLinesChanged();
290 
291  RetVal preShowContextMenuMargin();
292  RetVal preShowContextMenuEditor();
293 
294  void fileSysWatcherFileChanged ( const QString & path );
295  void printPreviewRequested(QPrinter *printer);
296 };
297 
298 class ItomQsciPrinter : public QsciPrinter
299 {
300 public:
301  ItomQsciPrinter(QPrinter::PrinterMode mode=QPrinter::ScreenResolution) : QsciPrinter(mode) {}
302  virtual void formatPage( QPainter &painter, bool drawing, QRect &area, int pagenr );
303 };
304 
305 } //end namespace ito
306 
307 #endif
Definition: abstractPyScintillaWidget.h:45
Class for managing status values (like errors or warning)
Definition: retVal.h:54
Definition: scriptEditorWidget.h:57
Definition: apiFunctionsGraph.cpp:39
Definition: classNavigatorItem.h:37
item of BreakPointModel
Definition: breakPointModel.h:43