Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / PyConsole / PyConsole_Request.cpp
1
2
3 #include "PyConsole_Request.h"
4
5 #include "PyInterp_Event.h"
6 #include "PyConsole_Event.h"
7 #include "PyConsole_EnhInterp.h"
8 #include "PyConsole_EnhEditor.h"
9
10 #include <QCoreApplication>
11
12 /**
13  * Constructor.
14  * @param theInterp interpreter that will execute the command
15  * @param theCommand command text
16  * @param theListener editor object that will receive the response events after execution
17  * of the request
18  * @param sync
19  */
20 ExecCommand::ExecCommand( PyInterp_Interp*        theInterp,
21                const QString&          theCommand,
22                PyConsole_Editor*       theListener,
23                bool                    sync )
24     : PyInterp_LockRequest( theInterp, theListener, sync ),
25       myCommand( theCommand ), myState( PyInterp_Event::ES_OK )
26   {}
27
28 /**
29  * Execute the command by calling the run() method of the embedded interpreter.
30  */
31 void ExecCommand::execute()
32 {
33   if ( myCommand != "" )
34     {
35       int ret = getInterp()->run( myCommand.toUtf8().data() );
36       if ( ret < 0 )
37         myState = PyInterp_Event::ES_ERROR;
38       else if ( ret > 0 )
39         myState = PyInterp_Event::ES_INCOMPLETE;
40     }
41 }
42
43 /**
44  * Create the event indicating the status of the request execution.
45  * @return a QEvent
46  */
47 QEvent* ExecCommand::createEvent()
48 {
49   if ( IsSync() )
50     QCoreApplication::sendPostedEvents( listener(), PrintEvent::EVENT_ID );
51   return new PyInterp_Event( myState, this );
52 }
53
54
55 /*!
56   Constructor.
57   Creates a new python completion request.
58   \param theInterp   python interpreter
59   \param input  string containing the dir() command to be executed
60   \param startMatch  part to be matched with the results of the dir() command
61   \param theListener widget to get the notification messages
62   \param sync        if True the request is processed synchronously
63 */
64 CompletionCommand::CompletionCommand( PyConsole_EnhInterp*  theInterp,
65                const QString&          input,
66                const QString&         startMatch,
67                PyConsole_EnhEditor*           theListener,
68                bool                    sync)
69      : PyInterp_LockRequest( theInterp, theListener, sync ),
70        _tabSuccess(false), _dirArg(input), _startMatch(startMatch)
71 {}
72
73 /**
74  * Execute the completion command by wrapping the runDirCommand() of the
75  * embedded enhanced interpreter.
76  */
77 void CompletionCommand::execute()
78 {
79   PyConsole_EnhInterp * interp = static_cast<PyConsole_EnhInterp *>(getInterp());
80     int ret = interp->runDirCommand( _dirArg,  _startMatch);
81     if (ret == 0)
82       _tabSuccess = true;
83     else
84       _tabSuccess = false;
85 }
86
87 /**
88  * Create the event indicating the return value of the completion command.
89  * @return
90  */
91 QEvent* CompletionCommand::createEvent()
92 {
93   int typ = _tabSuccess ? PyInterp_Event::ES_TAB_COMPLETE_OK : PyInterp_Event::ES_TAB_COMPLETE_ERR;
94
95   return new PyInterp_Event( typ, this);
96 }
97
98
99