itom 2.2.1
|
00001 /* ******************************************************************** 00002 itom software 00003 URL: http://www.uni-stuttgart.de/ito 00004 Copyright (C) 2016, Institut fuer Technische Optik (ITO), 00005 Universitaet Stuttgart, Germany 00006 00007 This file is part of itom. 00008 00009 itom is free software; you can redistribute it and/or modify it 00010 under the terms of the GNU Library General Public Licence as published by 00011 the Free Software Foundation; either version 2 of the Licence, or (at 00012 your option) any later version. 00013 00014 itom is distributed in the hope that it will be useful, but 00015 WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library 00017 General Public Licence for more details. 00018 00019 You should have received a copy of the GNU Library General Public License 00020 along with itom. If not, see <http://www.gnu.org/licenses/>. 00021 *********************************************************************** */ 00022 00023 #ifndef SCRIPTEDITORWIDGET_H 00024 #define SCRIPTEDITORWIDGET_H 00025 00026 #include "../models/breakPointModel.h" 00027 00028 #include "abstractPyScintillaWidget.h" 00029 #include "../global.h" 00030 00031 #include <qfilesystemwatcher.h> 00032 #include <qmutex.h> 00033 #include <qwidget.h> 00034 #include <qstring.h> 00035 #include <qmenu.h> 00036 #include <qevent.h> 00037 #include <qmetaobject.h> 00038 #include "../models/classNavigatorItem.h" 00039 00040 #if QT_VERSION >= 0x050000 00041 #include <QtPrintSupport/qprinter.h> 00042 #else 00043 #include <qprinter.h> 00044 #endif 00045 00046 #include <Qsci/qsciprinter.h> 00047 00048 QT_BEGIN_NAMESPACE 00049 00050 QT_END_NAMESPACE 00051 00052 00053 00054 namespace ito 00055 { 00056 00057 struct ScriptEditorStorage 00058 { 00059 QByteArray filename; 00060 int firstVisibleLine; 00061 QList<int> bookmarkLines; 00062 }; 00063 00064 } 00065 00066 Q_DECLARE_METATYPE(ito::ScriptEditorStorage) //must be outside of namespace 00067 Q_DECLARE_METATYPE(QList<ito::ScriptEditorStorage>) //must be outside of namespace 00068 00069 namespace ito 00070 { 00071 00072 class ScriptEditorWidget : public AbstractPyScintillaWidget 00073 { 00074 Q_OBJECT 00075 00076 public: 00077 ScriptEditorWidget(QWidget* parent = NULL); 00078 ~ScriptEditorWidget(); 00079 00080 bool m_errorMarkerVisible; 00081 int m_errorMarkerNr; //number of indicator which marks the line with current error 00082 00083 RetVal saveFile(bool askFirst = true); 00084 RetVal saveAsFile(bool askFirst = true); 00085 00086 RetVal openFile(QString file, bool ignorePresentDocument = false); 00087 00088 inline QString getFilename() const {return m_filename; } 00089 inline bool hasNoFilename() const { return m_filename.isNull(); } 00090 inline bool getCanCopy() const { return canCopy; } 00091 bool isBookmarked() const; 00092 inline QString getUntitledName() const { return tr("Untitled%1").arg(unnamedNumber); } 00093 inline QString getCurrentClass() const { return m_currentClass; } //currently chosen class in class navigator for this script editor widget 00094 inline QString getCurrentMethod() const { return m_currentMethod; } //currently chosen method in class navigator for this script editor widget 00095 00096 RetVal setCursorPosAndEnsureVisible(const int line, bool errorMessageClick = false); 00097 RetVal setCursorPosAndEnsureVisibleWithSelection(const int line, const QString ¤tClass, const QString ¤tMethod); 00098 00099 const ScriptEditorStorage saveState() const; 00100 RetVal restoreState(const ScriptEditorStorage &data); 00101 00102 protected: 00103 //void keyPressEvent (QKeyEvent *event); 00104 bool canInsertFromMimeData(const QMimeData *source) const; 00105 void autoAdaptLineNumberColumnWidth(); 00106 // void dragEnterEvent(QDragEnterEvent *event); 00107 void dropEvent(QDropEvent *event); 00108 virtual void loadSettings(); 00109 bool event(QEvent *event); 00110 void mouseReleaseEvent(QMouseEvent *event); 00111 00112 private: 00113 00114 enum markerType 00115 { 00116 markerBookmark = 1, 00117 markerPyBug = 2, 00118 markerBookmarkAndPyBug = markerBookmark | markerPyBug 00119 }; 00120 00121 RetVal initEditor(); 00122 00123 void contextMenuEvent (QContextMenuEvent * event); 00124 00125 int getMarginNumber(int xPos); 00126 00127 RetVal initMenus(); 00128 00129 RetVal toggleBookmark(int line); 00130 RetVal clearAllBookmarks(); 00131 RetVal gotoNextBookmark(); 00132 RetVal gotoPreviousBookmark(); 00133 00134 RetVal toggleBreakpoint(int line); 00135 RetVal toggleEnableBreakpoint(int line); 00136 RetVal editBreakpoint(int line); 00137 RetVal clearAllBreakpoints(); 00138 RetVal gotoNextBreakPoint(); 00139 RetVal gotoPreviousBreakPoint(); 00140 00141 bool lineAcceptsBPs(int line); 00142 00143 RetVal changeFilename(const QString &newFilename); 00144 00145 QFileSystemWatcher *m_pFileSysWatcher; 00146 QMutex fileSystemWatcherMutex; 00147 00149 struct BookmarkErrorEntry 00150 { 00151 int handle; 00152 int type; //entry of enum markerType 00153 QString errorMessage; 00154 QString errorComment; 00155 int errorPos; 00156 }; 00157 QList<BookmarkErrorEntry> bookmarkErrorHandles; 00158 int syntaxErrorHandle; 00159 00160 bool m_syntaxCheckerEnabled; 00161 int m_syntaxCheckerInterval; 00162 QTimer *m_syntaxTimer; 00163 // int m_lastTipLine; // TODO: not used anymore? 00164 00165 struct BPMarker 00166 { 00167 int bpHandle; 00168 int lineNo; 00169 bool markedForDeletion; 00170 }; 00171 00172 QList<BPMarker> m_breakPointMap; 00173 00174 unsigned int markBreakPoint; 00175 unsigned int markCBreakPoint; 00176 unsigned int markBreakPointDisabled; 00177 unsigned int markCBreakPointDisabled; 00178 unsigned int markBookmark; 00179 unsigned int markSyntaxError; 00180 unsigned int markBookmarkSyntaxError; 00181 00182 unsigned int markCurrentLine; 00183 int markCurrentLineHandle; 00184 00185 unsigned int markMask1; 00186 unsigned int markMask2; 00187 unsigned int markMaskBreakpoints; 00188 00189 //QsciLexerPython* qSciLex; 00190 //QsciAPIs* qSciApi; 00191 00193 QMenu *bookmarkMenu; 00194 QMenu *syntaxErrorMenu; 00195 QMenu *breakpointMenu; 00196 QMenu *editorMenu; 00197 00198 std::map<QString,QAction*> bookmarkMenuActions; 00199 std::map<QString,QAction*> breakpointMenuActions; 00200 std::map<QString,QAction*> editorMenuActions; 00201 00202 int contextMenuLine; 00203 00204 QString m_filename; 00205 int unnamedNumber; 00206 00207 bool pythonBusy; 00208 bool m_pythonExecutable; 00209 00210 bool canCopy; 00211 00212 static const QString lineBreak; 00213 static int unnamedAutoIncrement; 00214 00215 // Class Navigator 00216 bool m_ClassNavigatorEnabled; // Enable Class-Navigator 00217 QTimer *m_classNavigatorTimer; // Class Navigator Timer 00218 bool m_classNavigatorTimerEnabled; // Class Navigator Timer Enable 00219 int m_classNavigatorInterval; // Class Navigator Timer Interval 00220 QString m_currentClass; 00221 QString m_currentMethod; 00222 00223 int buildClassTree(ClassNavigatorItem *parent, int parentDepth, int lineNumber, int singleIndentation = -1); 00224 int getIndentationLength(const QString &str) const; 00225 00226 signals: 00227 void pythonRunFile(QString filename); 00228 void pythonRunSelection(QString selectionText); 00229 void pythonDebugFile(QString filename); 00230 void closeRequest(ScriptEditorWidget* sew, bool ignoreModifications); //signal emitted if this tab should be closed without considering any save-state 00231 void marginChanged(); 00232 void requestModelRebuild(ScriptEditorWidget *editor); 00233 00234 public slots: 00235 void menuToggleBookmark(); 00236 void checkSyntax(); 00237 void syntaxCheckResult(QString a, QString b); 00238 void errorListChange(const QStringList &errorList); 00239 void menuClearAllBookmarks(); 00240 void menuGotoNextBookmark(); 00241 void menuGotoPreviousBookmark(); 00242 00243 void menuToggleBreakpoint(); 00244 void menuToggleEnableBreakpoint(); 00245 void menuEditBreakpoint(); 00246 void menuClearAllBreakpoints(); 00247 void menuGotoNextBreakPoint(); 00248 void menuGotoPreviousBreakPoint(); 00249 00250 void menuCut(); 00251 void menuCopy(); 00252 void menuPaste(); 00253 void menuIndent(); 00254 void menuUnindent(); 00255 void menuComment(); 00256 void menuUncomment(); 00257 00258 void menuUnfoldAll(); 00259 void menuFoldUnfoldToplevel(); 00260 void menuFoldUnfoldAll(); 00261 00262 void menuRunScript(); 00263 void menuRunSelection(); 00264 void menuDebugScript(); 00265 void menuStopScript(); 00266 00267 void menuInsertCodec(); 00268 00269 void pythonStateChanged(tPythonTransitions pyTransition); 00270 void pythonDebugPositionChanged(QString filename, int lineno); 00271 00272 void breakPointAdd(BreakPointItem bp, int row); 00273 void breakPointDelete(QString filename, int lineNo, int pyBpNumber); 00274 void breakPointChange(BreakPointItem oldBp, BreakPointItem newBp); 00275 00276 void updateSyntaxCheck(); 00277 00278 // Class Navigator 00279 ClassNavigatorItem* getPythonNavigatorRoot(); //creates new tree of current python code structure and returns its root pointer. Caller must delete the root pointer after usage. 00280 00281 void print(); 00282 00283 private slots: 00284 void marginClicked(int margin, int line, Qt::KeyboardModifiers state); 00285 void copyAvailable(const bool yes); 00286 00287 void classNavTimerElapsed(); 00288 00289 void nrOfLinesChanged(); 00290 00291 RetVal preShowContextMenuMargin(); 00292 RetVal preShowContextMenuEditor(); 00293 00294 void fileSysWatcherFileChanged ( const QString & path ); 00295 void printPreviewRequested(QPrinter *printer); 00296 }; 00297 00298 class ItomQsciPrinter : public QsciPrinter 00299 { 00300 public: 00301 ItomQsciPrinter(QPrinter::PrinterMode mode=QPrinter::ScreenResolution) : QsciPrinter(mode) {} 00302 virtual void formatPage( QPainter &painter, bool drawing, QRect &area, int pagenr ); 00303 }; 00304 00305 } //end namespace ito 00306 00307 #endif