itom 1.4.0
|
00001 /* ******************************************************************** 00002 itom software 00003 URL: http://www.uni-stuttgart.de/ito 00004 Copyright (C) 2013, Institut für Technische Optik (ITO), 00005 Universität 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 00030 #include <qfilesystemwatcher.h> 00031 #include <qmutex.h> 00032 #include <qwidget.h> 00033 #include <qstring.h> 00034 #include <qmenu.h> 00035 #include <qevent.h> 00036 #include <qmetaobject.h> 00037 #include "../models/classNavigatorItem.h" 00038 00039 #if QT_VERSION >= 0x050000 00040 #include <QtPrintSupport/qprinter.h> 00041 #else 00042 #include <qprinter.h> 00043 #endif 00044 00045 #include <Qsci/qsciprinter.h> 00046 00047 QT_BEGIN_NAMESPACE 00048 00049 QT_END_NAMESPACE 00050 00051 00052 00053 namespace ito 00054 { 00055 00056 struct ScriptEditorStorage 00057 { 00058 QByteArray filename; 00059 int firstVisibleLine; 00060 QList<int> bookmarkLines; 00061 }; 00062 00063 } 00064 00065 Q_DECLARE_METATYPE(ito::ScriptEditorStorage) //must be outside of namespace 00066 Q_DECLARE_METATYPE(QList<ito::ScriptEditorStorage>) //must be outside of namespace 00067 00068 namespace ito 00069 { 00070 00071 class ScriptEditorWidget : public AbstractPyScintillaWidget 00072 { 00073 Q_OBJECT 00074 00075 public: 00076 ScriptEditorWidget(QWidget* parent = NULL); 00077 ~ScriptEditorWidget(); 00078 00079 RetVal saveFile(bool askFirst = true); 00080 RetVal saveAsFile(bool askFirst = true); 00081 00082 RetVal openFile(QString file, bool ignorePresentDocument = false); 00083 00084 inline QString getFilename() const {return m_filename; } 00085 inline bool hasNoFilename() const { return m_filename.isNull(); } 00086 inline bool getCanCopy() const { return canCopy; } 00087 inline bool isBookmarked() const { return !bookmarkErrorHandles.empty(); } 00088 inline QString getUntitledName() const { return tr("Untitled%1").arg(unnamedNumber); } 00089 inline QString getCurrentClass() const { return m_currentClass; } //currently chosen class in class navigator for this script editor widget 00090 inline QString getCurrentMethod() const { return m_currentMethod; } //currently chosen method in class navigator for this script editor widget 00091 00092 RetVal setCursorPosAndEnsureVisible(int line); 00093 RetVal setCursorPosAndEnsureVisibleWithSelection(int line, const QString ¤tClass, const QString ¤tMethod); 00094 00095 const ScriptEditorStorage saveState() const; 00096 RetVal restoreState(const ScriptEditorStorage &data); 00097 00098 protected: 00099 //void keyPressEvent (QKeyEvent *event); 00100 bool canInsertFromMimeData(const QMimeData *source) const; 00101 void autoAdaptLineNumberColumnWidth(); 00102 // void dragEnterEvent(QDragEnterEvent *event); 00103 void dropEvent(QDropEvent *event); 00104 virtual void loadSettings(); 00105 bool event (QEvent * event); 00106 00107 00108 00109 private: 00110 enum msgType 00111 { 00112 msgReturnInfo, 00113 msgReturnWarning, 00114 msgReturnError, 00115 msgTextInfo, 00116 msgTextWarning, 00117 msgTextError 00118 }; 00119 00120 enum markerType 00121 { 00122 markerBookmark = 1, 00123 markerPyBug = 2, 00124 markerBookmarkAndPyBug = markerBookmark | markerPyBug 00125 }; 00126 00127 RetVal initEditor(); 00128 00129 void contextMenuEvent (QContextMenuEvent * event); 00130 00131 int getMarginNumber(int xPos); 00132 00133 RetVal initMenus(); 00134 00135 RetVal toggleBookmark(int line); 00136 RetVal clearAllBookmarks(); 00137 RetVal gotoNextBookmark(); 00138 RetVal gotoPreviousBookmark(); 00139 00140 RetVal toggleBreakpoint(int line); 00141 RetVal toggleEnableBreakpoint(int line); 00142 RetVal editBreakpoint(int line); 00143 RetVal clearAllBreakpoints(); 00144 RetVal gotoNextBreakPoint(); 00145 RetVal gotoPreviousBreakPoint(); 00146 00147 bool lineAcceptsBPs(int line); 00148 00149 RetVal changeFilename(const QString &newFilename); 00150 00151 QFileSystemWatcher *m_pFileSysWatcher; 00152 QMutex fileSystemWatcherMutex; 00153 00155 struct BookmarkErrorEntry 00156 { 00157 int handle; 00158 int type; 00159 QString errorMessage; 00160 QString errorComment; 00161 int errorPos; 00162 }; 00163 QList<BookmarkErrorEntry> bookmarkErrorHandles; 00164 int syntaxErrorHandle; 00165 00166 bool m_syntaxCheckerEnabled; 00167 int m_syntaxCheckerInterval; 00168 QTimer *m_syntaxTimer; 00169 // int m_lastTipLine; // TODO: not used anymore? 00170 00171 struct BPMarker 00172 { 00173 int bpHandle; 00174 int lineNo; 00175 bool markedForDeletion; 00176 }; 00177 00178 QList<BPMarker> m_breakPointMap; 00179 00180 unsigned int markBreakPoint; 00181 unsigned int markCBreakPoint; 00182 unsigned int markBreakPointDisabled; 00183 unsigned int markCBreakPointDisabled; 00184 unsigned int markBookmark; 00185 unsigned int markSyntaxError; 00186 unsigned int markBookmarkSyntaxError; 00187 00188 unsigned int markCurrentLine; 00189 int markCurrentLineHandle; 00190 00191 unsigned int markMask1; 00192 unsigned int markMask2; 00193 unsigned int markMaskBreakpoints; 00194 00195 //QsciLexerPython* qSciLex; 00196 //QsciAPIs* qSciApi; 00197 00199 QMenu *bookmarkMenu; 00200 QMenu *syntaxErrorMenu; 00201 QMenu *breakpointMenu; 00202 QMenu *editorMenu; 00203 00204 std::map<QString,QAction*> bookmarkMenuActions; 00205 std::map<QString,QAction*> breakpointMenuActions; 00206 std::map<QString,QAction*> editorMenuActions; 00207 00208 int contextMenuLine; 00209 00210 QString m_filename; 00211 int unnamedNumber; 00212 00213 bool pythonBusy; 00214 bool m_pythonExecutable; 00215 00216 bool canCopy; 00217 00218 static const QString lineBreak; 00219 static int unnamedAutoIncrement; 00220 00221 // Class Navigator 00222 bool m_ClassNavigatorEnabled; // Enable Class-Navigator 00223 QTimer *m_classNavigatorTimer; // Class Navigator Timer 00224 bool m_classNavigatorTimerEnabled; // Class Navigator Timer Enable 00225 int m_classNavigatorInterval; // Class Navigator Timer Interval 00226 QString m_currentClass; 00227 QString m_currentMethod; 00228 00229 int buildClassTree(ClassNavigatorItem *parent, int parentDepth, int lineNumber); 00230 00231 signals: 00232 void pythonRunFile(QString filename); 00233 void pythonRunSelection(QString selectionText); 00234 void pythonDebugFile(QString filename); 00235 void closeRequest(ScriptEditorWidget* sew, bool ignoreModifications); //signal emitted if this tab should be closed without considering any save-state 00236 void marginChanged(); 00237 void requestModelRebuild(ScriptEditorWidget *editor); 00238 00239 public slots: 00240 void menuToggleBookmark(); 00241 void checkSyntax(); 00242 void syntaxCheckResult(QString a, QString b); 00243 void errorListChange(const QStringList &errorList); 00244 void menuClearAllBookmarks(); 00245 void menuGotoNextBookmark(); 00246 void menuGotoPreviousBookmark(); 00247 00248 void menuToggleBreakpoint(); 00249 void menuToggleEnableBreakpoint(); 00250 void menuEditBreakpoint(); 00251 void menuClearAllBreakpoints(); 00252 void menuGotoNextBreakPoint(); 00253 void menuGotoPreviousBreakPoint(); 00254 00255 void menuCut(); 00256 void menuCopy(); 00257 void menuPaste(); 00258 void menuIndent(); 00259 void menuUnindent(); 00260 void menuComment(); 00261 void menuUncomment(); 00262 00263 void menuRunScript(); 00264 void menuRunSelection(); 00265 void menuDebugScript(); 00266 void menuStopScript(); 00267 00268 void pythonStateChanged(tPythonTransitions pyTransition); 00269 void pythonDebugPositionChanged(QString filename, int lineno); 00270 00271 void breakPointAdd(BreakPointItem bp, int row); 00272 void breakPointDelete(QString filename, int lineNo, int pyBpNumber); 00273 void breakPointChange(BreakPointItem oldBp, BreakPointItem newBp); 00274 00275 void updateSyntaxCheck(); 00276 00277 // Class Navigator 00278 ClassNavigatorItem* getPythonNavigatorRoot(); //creates new tree of current python code structure and returns its root pointer. Caller must delete the root pointer after usage. 00279 00280 void print(); 00281 00282 00283 private slots: 00284 void marginClicked(int margin, int line, Qt::KeyboardModifiers state); 00285 void copyAvailable(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 00299 class ItomQsciPrinter : public QsciPrinter 00300 { 00301 public: 00302 ItomQsciPrinter(QPrinter::PrinterMode mode=QPrinter::ScreenResolution) : QsciPrinter(mode) {} 00303 virtual void formatPage( QPainter &painter, bool drawing, QRect &area, int pagenr ); 00304 }; 00305 00306 } //end namespace ito 00307 00308 #endif