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