Salome HOME
Update copyrights 2014.
[modules/gui.git] / src / PyConsole / PyConsole_Request.cxx
1 // Copyright (C) 2007-2014  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 // Author : Adrien Bruneton (CEA/DEN)
20 // Created on: 3 avr. 2013
21
22 #include "PyConsole_Request.h"
23
24 #include "PyInterp_Event.h"
25 #include "PyConsole_Event.h"
26 #include "PyConsole_EnhInterp.h"
27 #include "PyConsole_EnhEditor.h"
28
29 #include <QCoreApplication>
30
31 /**
32  * Constructor.
33  * @param theInterp interpreter that will execute the command
34  * @param theCommand command text
35  * @param theListener editor object that will receive the response events after execution
36  * of the request
37  * @param sync
38  */
39 ExecCommand::ExecCommand( PyInterp_Interp*        theInterp,
40                const QString&          theCommand,
41                PyConsole_Editor*       theListener,
42                bool                    sync )
43     : PyInterp_LockRequest( theInterp, theListener, sync ),
44       myCommand( theCommand ), myState( PyInterp_Event::ES_OK )
45   {}
46
47 /**
48  * Execute the command by calling the run() method of the embedded interpreter.
49  */
50 void ExecCommand::execute()
51 {
52   if ( myCommand != "" )
53     {
54       int ret = getInterp()->run( myCommand.toUtf8().data() );
55       if ( ret < 0 )
56         myState = PyInterp_Event::ES_ERROR;
57       else if ( ret > 0 )
58         myState = PyInterp_Event::ES_INCOMPLETE;
59     }
60 }
61
62 /**
63  * Create the event indicating the status of the request execution.
64  * @return a QEvent
65  */
66 QEvent* ExecCommand::createEvent()
67 {
68   if ( IsSync() )
69     QCoreApplication::sendPostedEvents( listener(), PrintEvent::EVENT_ID );
70   return new PyInterp_Event( myState, this );
71 }
72
73
74 /*!
75   Constructor.
76   Creates a new python completion request.
77   \param theInterp   python interpreter
78   \param input  string containing the dir() command to be executed
79   \param startMatch  part to be matched with the results of the dir() command
80   \param theListener widget to get the notification messages
81   \param sync        if True the request is processed synchronously
82 */
83 CompletionCommand::CompletionCommand( PyConsole_EnhInterp*  theInterp,
84                const QString&          input,
85                const QString&         startMatch,
86                PyConsole_EnhEditor*           theListener,
87                bool                    sync)
88      : PyInterp_LockRequest( theInterp, theListener, sync ),
89        _tabSuccess(false), _dirArg(input), _startMatch(startMatch)
90 {}
91
92 /**
93  * Execute the completion command by wrapping the runDirCommand() of the
94  * embedded enhanced interpreter.
95  */
96 void CompletionCommand::execute()
97 {
98   PyConsole_EnhInterp * interp = static_cast<PyConsole_EnhInterp *>(getInterp());
99     int ret = interp->runDirCommand( _dirArg,  _startMatch);
100     if (ret == 0)
101       _tabSuccess = true;
102     else
103       _tabSuccess = false;
104 }
105
106 /**
107  * Create the event indicating the return value of the completion command.
108  * @return
109  */
110 QEvent* CompletionCommand::createEvent()
111 {
112   int typ = _tabSuccess ? PyInterp_Event::ES_TAB_COMPLETE_OK : PyInterp_Event::ES_TAB_COMPLETE_ERR;
113
114   return new PyInterp_Event( typ, this);
115 }
116
117
118