Salome HOME
c3728e273b719123ea1e9f64880bbf6f33c1e940
[modules/gui.git] / tools / PyConsole / src / PyConsole_Request.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File   : PyConsole_Request.cxx
20 // Author : Vadim SANDLER (OPEN CASCADE), Adrien Bruneton (CEA/DEN)
21
22 #include "PyConsole_Request.h"
23 #include "PyConsole_Interp.h"
24 #include "PyConsole_Event.h"
25
26 #include <QCoreApplication>
27
28 /*!
29   \class PyConsole_ExecCommand
30   \brief Python command execution request.
31   \internal
32 */
33
34 /*!
35   \brief Constructor.
36   
37   Creates new python command execution request.
38
39   \param theInterp   python interpreter
40   \param theCommand  python command
41   \param theListener widget to get the notification messages
42   \param theSync     if \c true, the request is processed synchronously
43 */
44 PyConsole_ExecCommand::PyConsole_ExecCommand( PyInterp_Interp*        theInterp,
45                                               const QString&          theCommand,
46                                               QObject*                theListener,
47                                               bool                    theSync )
48   : PyInterp_LockRequest( theInterp, theListener, theSync ),
49     myCommand( theCommand ), myState( PyInterp_Event::ES_OK )
50 {}
51
52 /*!
53   \brief Execute the python command in the interpreter and
54   get its execution status.
55 */
56 void PyConsole_ExecCommand::execute()
57 {
58   if ( myCommand != "" ) {
59     int ret = getInterp()->run( myCommand.toUtf8().data() );
60     if ( ret < 0 )
61       myState = PyInterp_Event::ES_ERROR;
62     else if ( ret > 0 )
63       myState = PyInterp_Event::ES_INCOMPLETE;
64   }
65 }
66
67 /*!
68   \brief Create and return a notification event.
69   \return new notification event
70 */
71 QEvent* PyConsole_ExecCommand::createEvent()
72 {
73   if ( IsSync() )
74     QCoreApplication::sendPostedEvents( listener(), PyConsole_PrintEvent::EVENT_ID );
75   return new PyInterp_Event( myState, this );
76 }
77
78 /*!
79   \class PyConsole_CompletionCommand
80   \brief Python command completion request.
81   \internal
82 */
83
84 /*!
85   \brief Constructor.
86
87   Creates a new python completion request.
88
89   \param theInterp python interpreter
90   \param theInput  string containing the dir() command to be executed
91   \param theStartMatch part to be matched with the results of the dir() command
92   \param theListener widget to get the notification messages
93   \param theSync if \c true the request is processed synchronously
94 */
95 PyConsole_CompletionCommand::PyConsole_CompletionCommand( PyInterp_Interp*   theInterp,
96                                                           const QString&     theInput,
97                                                           const QString&     theStartMatch,
98                                                           QObject*           theListener,
99                                                           bool               theSync )
100   : PyInterp_LockRequest( theInterp, theListener, theSync ),
101     myDirArg( theInput ), myStartMatch( theStartMatch ), myStatus( false )
102 {}
103
104 /*!
105   \brief Execute the completion command by invoking runDirCommand() function
106   of interpreter.
107 */
108 void PyConsole_CompletionCommand::execute()
109 {
110   myStatus = static_cast<PyConsole_Interp*>( getInterp() )->runDirCommand( myDirArg,  myStartMatch, myMatches, myDoc );
111 }
112
113 /*!
114  \brief Create and return completion event
115   \return new completion event
116  */
117 QEvent* PyConsole_CompletionCommand::createEvent()
118 {
119   return new PyConsole_CompletionEvent( this, myStatus, myMatches, myDoc );
120 }