itom 1.2.0
D:/git-itom/sources/itom/Qitom/widgets/scriptEditorWidget.h
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 
00038 #if QT_VERSION >= 0x050000
00039     #include <QtPrintSupport/qprinter.h>
00040 #else
00041     #include <qprinter.h>
00042 #endif
00043 
00044 #include <Qsci/qsciprinter.h>
00045 
00046 QT_BEGIN_NAMESPACE
00047 
00048 QT_END_NAMESPACE
00049 
00050 
00051 
00052 namespace ito
00053 {
00054 
00055 struct ScriptEditorStorage
00056 {
00057     QByteArray  filename;
00058     int         firstVisibleLine;
00059     QList<int>  bookmarkLines;
00060 };
00061 
00062 }
00063 
00064 Q_DECLARE_METATYPE(ito::ScriptEditorStorage) //must be outside of namespace
00065 Q_DECLARE_METATYPE(QList<ito::ScriptEditorStorage>) //must be outside of namespace
00066 
00067 namespace ito
00068 {
00069 
00070 class ScriptEditorWidget : public AbstractPyScintillaWidget
00071 {
00072     Q_OBJECT
00073 
00074 public:
00075     ScriptEditorWidget(QWidget* parent = NULL);
00076     ~ScriptEditorWidget();
00077 
00078     RetVal saveFile(bool askFirst = true);
00079     RetVal saveAsFile(bool askFirst = true);
00080 
00081     RetVal openFile(QString file, bool ignorePresentDocument = false);
00082 
00083     inline QString getFilename() const {return filename; }
00084     inline bool hasNoFilename() const { return filename.isNull(); }
00085     inline bool getCanCopy() const { return canCopy; }
00086     inline bool isBookmarked() const { return !bookmarkErrorHandles.empty(); }
00087     inline QString getUntitledName() const { return tr("Untitled%1").arg(unnamedNumber); }
00088 
00089     RetVal setCursorPosAndEnsureVisible(int line);
00090 
00091     const ScriptEditorStorage saveState() const;
00092     RetVal restoreState(const ScriptEditorStorage &data);
00093 
00094 protected:
00095     //void keyPressEvent (QKeyEvent *event);
00096     bool canInsertFromMimeData(const QMimeData *source) const;
00097 //    void dragEnterEvent(QDragEnterEvent *event);
00098     void dropEvent(QDropEvent *event);
00099     virtual void loadSettings();
00100     bool event (QEvent * event);
00101 
00102 private:
00103     enum msgType
00104     {
00105         msgReturnInfo,
00106         msgReturnWarning,
00107         msgReturnError,
00108         msgTextInfo,
00109         msgTextWarning,
00110         msgTextError
00111     };
00112 
00113     enum markerType
00114     {   
00115         markerBookmark = 1,
00116         markerPyBug = 2,
00117         markerBookmarkAndPyBug = markerBookmark | markerPyBug
00118     };
00119 
00120     RetVal initEditor();
00121 
00122     void contextMenuEvent (QContextMenuEvent * event);
00123 
00124     int getMarginNumber(int xPos);
00125 
00126     RetVal initMenus();
00127 
00128     RetVal toggleBookmark(int line);
00129     RetVal clearAllBookmarks();
00130     RetVal gotoNextBookmark();
00131     RetVal gotoPreviousBookmark();
00132 
00133     RetVal toggleBreakpoint(int line);
00134     RetVal toggleEnableBreakpoint(int line);
00135     RetVal editBreakpoint(int line);
00136     RetVal clearAllBreakpoints();
00137     RetVal gotoNextBreakPoint();
00138     RetVal gotoPreviousBreakPoint();
00139 
00140     RetVal changeFilename(QString newFilename);
00141 
00142     QFileSystemWatcher *m_pFileSysWatcher;
00143     QMutex fileSystemWatcherMutex;
00144 
00146     struct BookmarkErrorEntry
00147     {
00148         int handle;
00149         int type;
00150         QString errorMessage;
00151         QString errorComment;
00152         int errorPos;
00153     };
00154     QList<BookmarkErrorEntry> bookmarkErrorHandles;
00155     int syntaxErrorHandle;
00156 
00157     bool m_syntaxCheckerEnabled;
00158     int m_syntaxCheckerIntervall;
00159     QTimer *m_syntaxTimer;
00160     int m_lastTipLine;
00161 
00162     std::list<QPair<int,int> > breakPointMap; 
00163 
00164     unsigned int markBreakPoint;
00165     unsigned int markCBreakPoint;
00166     unsigned int markBreakPointDisabled;
00167     unsigned int markCBreakPointDisabled;
00168     unsigned int markBookmark;
00169     unsigned int markSyntaxError;
00170     unsigned int markBookmarkSyntaxError;
00171 
00172     unsigned int markCurrentLine;
00173     int markCurrentLineHandle;
00174 
00175     unsigned int markMask1;
00176     unsigned int markMask2;
00177     unsigned int markMaskBreakpoints;
00178 
00179     //QsciLexerPython* qSciLex;
00180     //QsciAPIs* qSciApi;
00181 
00183     QMenu *bookmarkMenu;
00184     QMenu *syntaxErrorMenu;
00185     QMenu *breakpointMenu;
00186     QMenu *editorMenu;
00187 
00188     std::map<QString,QAction*> bookmarkMenuActions;
00189     std::map<QString,QAction*> breakpointMenuActions;
00190     std::map<QString,QAction*> editorMenuActions;
00191 
00192     int contextMenuLine;
00193 
00194     QString filename;
00195     int unnamedNumber;
00196 
00197     bool pythonBusy; 
00198     bool m_pythonExecutable;
00199 
00200     bool canCopy;
00201 
00202     static const QString lineBreak;
00203     static int unnamedAutoIncrement;
00204 
00205 signals:
00206     void pythonRunFile(QString filename);
00207     void pythonRunSelection(QString selectionText);
00208     void pythonDebugFile(QString filename);
00209     void closeRequest(ScriptEditorWidget* sew, bool ignoreModifications); //signal emitted if this tab should be closed without considering any save-state
00210     void marginChanged();    
00211 
00212 public slots:
00213     void menuToggleBookmark();
00214     void checkSyntax();
00215     void syntaxCheckResult(QString a, QString b);
00216     void errorListChange(QStringList errorList);
00217     void menuClearAllBookmarks();
00218     void menuGotoNextBookmark();
00219     void menuGotoPreviousBookmark();
00220 
00221     void menuToggleBreakpoint();
00222     void menuToggleEnableBreakpoint();
00223     void menuEditBreakpoint();
00224     void menuClearAllBreakpoints();
00225     void menuGotoNextBreakPoint();
00226     void menuGotoPreviousBreakPoint();
00227 
00228     void menuCut();
00229     void menuCopy();
00230     void menuPaste();
00231     void menuIndent();
00232     void menuUnindent();
00233     void menuComment();
00234     void menuUncomment();
00235 
00236     void menuRunScript();
00237     void menuRunSelection();
00238     void menuDebugScript();
00239     void menuStopScript();
00240 
00241     void pythonStateChanged(tPythonTransitions pyTransition);
00242     void pythonDebugPositionChanged(QString filename, int lineno);
00243 
00244     void breakPointAdd(BreakPointItem bp, int row);
00245     void breakPointDelete(QString filename, int lineNo, int pyBpNumber);
00246     void breakPointChange(BreakPointItem oldBp, BreakPointItem newBp);
00247 
00248     void updateSyntaxCheck();
00249 
00250     void print();
00251 
00252 
00253 private slots:
00254     void marginClicked(int margin, int line, Qt::KeyboardModifiers state);
00255     void copyAvailable(bool yes);
00256 
00257     void nrOfLinesChanged();
00258 
00259     RetVal preShowContextMenuMargin();
00260     RetVal preShowContextMenuEditor();
00261 
00262     void fileSysWatcherFileChanged ( const QString & path );
00263     void printPreviewRequested(QPrinter *printer);
00264 };
00265 
00266 
00267 class ItomQsciPrinter : public QsciPrinter
00268 {
00269 public:
00270     ItomQsciPrinter(QPrinter::PrinterMode mode=QPrinter::ScreenResolution) : QsciPrinter(mode) {}
00271     virtual void formatPage( QPainter &painter, bool drawing, QRect &area, int pagenr );
00272 };
00273 
00274 } //end namespace ito
00275 
00276 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends