Salome HOME
0748cf1e66f56787607b551716baa8076fe8bfb3
[modules/gui.git] / tools / PyConsole / src / PyConsole_Editor.h
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : PyConsole_Editor.h
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24
25 #ifndef PYCONSOLE_EDITOR_H
26 #define PYCONSOLE_EDITOR_H
27
28 #include "PyConsole.h"
29
30 #include "PyInterp_RefCounterObj.h"
31
32 #include <QTextEdit>
33 #include <QQueue>
34
35 class PyConsole_Interp;
36 class PyInterp_Request;
37 class QEventLoop;
38
39 class PYCONSOLE_EXPORT PyConsole_Editor : public QTextEdit
40 {
41   Q_OBJECT
42
43 public:
44   PyConsole_Editor( QWidget* = 0 );
45   PyConsole_Editor( QWidget*, PyConsole_Interp* );
46   ~PyConsole_Editor();
47
48   PyConsole_Interp* getInterp() const;
49   
50   virtual void   addText( const QString&, const bool = false, const bool = false );
51   bool           isCommand( const QString& ) const;
52
53   virtual void   exec( const QString& );
54   void           execAndWait( const QString& );
55
56   bool           isSync() const;
57   void           setIsSync( const bool );
58
59   bool           isSuppressOutput() const;
60   void           setIsSuppressOutput( const bool );
61
62   bool           isShowBanner() const;
63   void           setIsShowBanner( const bool );
64
65   void           setAutoCompletion( bool );
66   bool           autoCompletion() const;
67
68   bool           isLogging() const;
69
70   virtual QSize  sizeHint() const;
71
72   bool           startLog( const QString& );
73   bool           dump( const QString& );
74
75 signals:
76   void updateDoc( const QString& );
77
78 public slots:
79   void           cut();
80   void           paste();
81   void           clear();
82   void           dump();
83   void           startLog();
84   void           stopLog();
85
86 protected:
87   virtual void   dropEvent( QDropEvent* );
88   virtual void   mousePressEvent( QMouseEvent* );
89   virtual void   mouseReleaseEvent( QMouseEvent* );
90   virtual void   keyPressEvent ( QKeyEvent* );
91   virtual void   customEvent( QEvent* );
92
93   virtual void   insertFromMimeData( const QMimeData* );
94
95   void           putLog( const QString& );
96
97   virtual QString getDumpFileName();
98   virtual QString getLogFileName();
99
100 private:
101   void           init();
102
103   void           multilinePaste( const QString& );
104   void           multiLineProcessNextLine();
105
106   void           handleReturn();
107   void           handleTab();
108   void           handleBackTab();
109   void           clearCompletion();
110   QString        formatCompletion( const QStringList& ) const;
111   QString        formatDocHTML( const QString& ) const;
112   QString        extractCommon( const QStringList& ) const;
113
114   PyInterp_Request* createCmdRequest( const QString& );
115   PyInterp_Request* createTabRequest( const QString& );
116
117   QString        banner() const;
118   inline int     promptSize() const { return myPrompt.size(); }
119
120   PyInterp_Auto<PyConsole_Interp> myInterp;           //!< python interpreter
121   QString                         myCommandBuffer;    //!< python command buffer
122   QString                         myCurrentCommand;   //!< currently being printed command
123   QString                         myPrompt;           //!< current command line prompt
124   int                             myCmdInHistory;     //!< current history command index
125   QString                         myLogFile;          //!< current output log
126   QStringList                     myHistory;          //!< commands history buffer
127   QEventLoop*                     myEventLoop;        //!< internal event loop
128   bool                            myShowBanner;       //!< 'show banner' flag
129   QStringList                     myQueue;            //!< python commands queue
130   bool                            myIsSync;           //!< synchronous mode flag
131   bool                            myIsSuppressOutput; //!< suppress output flag
132   bool                            myMultiLinePaste;   //!< true when pasting several lines
133   QQueue<QString>                 myMultiLineContent; //!< queue of lines being pasted
134   bool                            myAutoCompletion;   //!< auto-completion mode flag
135   bool                            myTabMode;          //!< flag that is \c true when editor performs completion
136   QString                         myComplBeforePoint; //!< string on which the dir() command is executed
137   QString                         myComplAfterPoint;  //!< string on which the results of the dir() are matched
138   int                             myComplCursorPos;   //!< cursor position when <TAB> is hit
139
140   friend void PyConsole_CallbackStdout( void*, char* );
141   friend void PyConsole_CallbackStderr( void*, char* );
142 };
143
144 #endif // PYCONSOLE_EDITOR_H