\param sync if True the request is processed synchronously
*/
ExecCommand( PyInterp_base* theInterp,
- const char* theCommand,
- PyConsole_Editor* theListener,
+ const QString& theCommand,
+ PyConsole_Editor* theListener,
bool sync = false )
: PyInterp_LockRequest( theInterp, theListener, sync ),
myCommand( theCommand ), myState( PyInterp_Event::OK )
\param theParent parent widget
*/
PyConsole_Editor::PyConsole_Editor( PyInterp_base* theInterp,
- QWidget* theParent )
- : QTextEdit( theParent ),
- myInterp( 0 ),
- myCmdInHistory( -1 ),
- myEventLoop( 0 )
+ QWidget* theParent )
+: QTextEdit( theParent ),
+ myInterp( 0 ),
+ myCmdInHistory( -1 ),
+ myEventLoop( 0 ),
+ myIsSync( false )
{
QString fntSet( "" );
QFont aFont = SUIT_Tools::stringToFont( fntSet );
{
}
+bool PyConsole_Editor::isSync() const
+{
+ return myIsSync;
+}
+
+void PyConsole_Editor::setIsSync( const bool s )
+{
+ myIsSync = s;
+}
+
/*!
\brief Put the string \a str to the python editor.
\param str string to be put in the command line of the editor
// post a request to execute Python command;
// editor will be informed via a custom event that execution has been completed
- PyInterp_Dispatcher::Get()->Exec( new ExecCommand( myInterp,
- cmd.toLatin1(),
- this ) );
+ PyInterp_Dispatcher::Get()->Exec( createRequest( cmd ) );
+}
+
+PyInterp_Request* PyConsole_Editor::createRequest( const QString& cmd )
+{
+ return new ExecCommand( myInterp, cmd, this, isSync() );
}
/*!
// post a request to execute Python command;
// editor will be informed via a custom event that execution has been completed
- PyInterp_Dispatcher::Get()->Exec( new ExecCommand( myInterp,
- myCommandBuffer.toLatin1(),
- this ) );
+ PyInterp_Dispatcher::Get()->Exec( createRequest( myCommandBuffer ) );
}
/*!
*/
void PyConsole_Editor::customEvent( QEvent* event )
{
- switch( event->type() ) {
+ switch( event->type() )
+ {
case PyInterp_Event::OK:
case PyInterp_Event::ERROR:
+ {
+ PyInterp_Event* pe = dynamic_cast<PyInterp_Event*>( event );
+ if ( pe )
{
- PyInterp_Event* pe = dynamic_cast<PyInterp_Event*>( event );
- if ( pe ){
- ExecCommand* ec = dynamic_cast<ExecCommand*>( pe->GetRequest() );
- if ( ec ) {
- // The next line has appeared dangerous in case if
- // Python command execution has produced very large output.
- // A more clever approach is needed...
- // print python output
- addText( ec->myOutput, true );
- addText( ec->myError );
- }
+ ExecCommand* ec = dynamic_cast<ExecCommand*>( pe->GetRequest() );
+ if ( ec )
+ {
+ // The next line has appeared dangerous in case if
+ // Python command execution has produced very large output.
+ // A more clever approach is needed...
+ // print python output
+ addText( ec->myOutput, true );
+ addText( ec->myError );
}
- // clear command buffer
- myCommandBuffer.truncate( 0 );
- // set "ready" prompt
- myPrompt = READY_PROMPT;
- addText( myPrompt );
- // unset busy cursor
- unsetCursor();
- // stop event loop (if running)
- if( myEventLoop )
- myEventLoop->exit();
- break;
}
+ // clear command buffer
+ myCommandBuffer.truncate( 0 );
+ // set "ready" prompt
+ myPrompt = READY_PROMPT;
+ addText( myPrompt );
+ // unset busy cursor
+ unsetCursor();
+ // stop event loop (if running)
+ if ( myEventLoop )
+ myEventLoop->exit();
+ break;
+ }
case PyInterp_Event::INCOMPLETE:
- {
- // extend command buffer (multi-line command)
- myCommandBuffer.append( "\n" );
- // set "dot" prompt
- myPrompt = DOTS_PROMPT;
- addText( myPrompt, true );
- // unset busy cursor
- unsetCursor();
- // stop event loop (if running)
- if( myEventLoop )
- myEventLoop->exit();
- break;
- }
+ {
+ // extend command buffer (multi-line command)
+ myCommandBuffer.append( "\n" );
+ // set "dot" prompt
+ myPrompt = DOTS_PROMPT;
+ addText( myPrompt, true );
+ // unset busy cursor
+ unsetCursor();
+ // stop event loop (if running)
+ if ( myEventLoop )
+ myEventLoop->exit();
+ break;
+ }
default:
QTextEdit::customEvent( event );
}
// unset history browsing mode
myCmdInHistory = -1;
- if ( (int)event->type() == (int)PyInterp_Event::OK && myQueue.count() > 0 ) {
+ if ( (int)event->type() == (int)PyInterp_Event::OK && myQueue.count() > 0 )
+ {
// process the next sheduled command from the queue (if there is any)
QString nextcmd = myQueue[0];
myQueue.pop_front();
{
safeExecute();
- myMutex.lock();
- //if ( !IsSync() && getListener() && getEvent() )
- if ( getListener() && getEvent() )
- postEvent();
- myMutex.unlock();
+ bool isSync = IsSync();
+
+ if ( !isSync )
+ myMutex.lock();
+
+ if ( listener() )
+ processEvent( listener() );
+
+ if ( !isSync )
+ myMutex.unlock();
}
void PyInterp_Request::safeExecute()
return new PyInterp_Event( PyInterp_Event::NOTIFY, (PyInterp_Request*)this );
}
-QEvent* PyInterp_Request::getEvent()
+void PyInterp_Request::processEvent( QObject* o )
{
- //if ( !myEvent && !IsSync() )
- if ( !myEvent )
- myEvent = createEvent();
- return myEvent;
-}
+ if ( !o )
+ return;
-void PyInterp_Request::postEvent()
-{
-// MESSAGE("*** PyInterp_Request::postEvent(): for Qt 3.3.3")
- QCoreApplication::postEvent( getListener(), getEvent() );
+ QEvent* e = createEvent();
+ if ( !e )
+ return;
+
+ if ( !IsSync() )
+ QCoreApplication::postEvent( o, e );
+ else
+ {
+ QCoreApplication::sendEvent( o, e );
+ delete e;
+ }
}
void PyInterp_Request::setListener( QObject* o )
//if ( theRequest->IsSync() && !IsBusy() ) // synchronous processing - nothing is done if dispatcher is busy!
if ( theRequest->IsSync() ) // synchronous processing - nothing is done if dispatcher is busy!
processRequest( theRequest );
- else { // asynchronous processing
+ else // asynchronous processing
+ {
myQueueMutex.lock();
myQueue.push_back( theRequest );
- if ( theRequest->getListener() )
- QObject::connect( theRequest->getListener(), SIGNAL( destroyed( QObject* ) ), myWatcher, SLOT( onDestroyed( QObject* ) ) );
+ if ( theRequest->listener() )
+ QObject::connect( theRequest->listener(), SIGNAL( destroyed( QObject* ) ), myWatcher, SLOT( onDestroyed( QObject* ) ) );
myQueueMutex.unlock();
if ( !IsBusy() )
// prepare for modification of the queue
myQueueMutex.lock();
- for ( std::list<RequestPtr>::iterator it = myQueue.begin(); it != myQueue.end(); ++it ){
- if ( o == (*it)->getListener() ){
+ for ( std::list<RequestPtr>::iterator it = myQueue.begin(); it != myQueue.end(); ++it )
+ {
+ if ( o == (*it)->listener() )
+ {
(*it)->setListener( 0 ); // to prevent event posting
it = myQueue.erase( it );
}
myQueueMutex.unlock();
}
-
PyInterp_Request( const PyInterp_Request& );
protected:
- virtual ~PyInterp_Request() {};
+ virtual ~PyInterp_Request() {};
// protected destructor - to control deletion of requests
public:
PyInterp_Request( QObject* listener, bool sync = false )
- : myIsSync( sync ), myListener( listener ), myEvent( 0 ) {};
+ : myIsSync( sync ), myListener( listener ) {};
static void Destroy( PyInterp_Request* );
// Deletes a request
virtual QEvent* createEvent() const;
// This method can be overridden to customize notification event creation
+ virtual void processEvent( QObject* );
+
private:
void process();
- QObject* getListener() const { return myListener; }
+ QObject* listener() const { return myListener; }
void setListener( QObject* );
- QEvent* getEvent();
- void postEvent();
private:
+ QMutex myMutex;
bool myIsSync;
QObject* myListener;
- QEvent* myEvent;
- QMutex myMutex;
};
class PYINTERP_EXPORT PyInterp_LockRequest : public PyInterp_Request