From faa25d796e39c789d33a9898534e48fe038764ac Mon Sep 17 00:00:00 2001 From: stv Date: Tue, 27 Feb 2007 07:23:55 +0000 Subject: [PATCH] *** empty log message *** --- src/PyConsole/PyConsole_Console.cxx | 10 +++ src/PyConsole/PyConsole_Console.h | 5 +- src/PyConsole/PyConsole_Editor.cxx | 116 +++++++++++++++------------ src/PyConsole/PyConsole_Editor.h | 7 ++ src/PyInterp/PyInterp_Dispatcher.cxx | 53 +++++++----- src/PyInterp/PyInterp_Dispatcher.h | 13 ++- 6 files changed, 124 insertions(+), 80 deletions(-) diff --git a/src/PyConsole/PyConsole_Console.cxx b/src/PyConsole/PyConsole_Console.cxx index c7711b928..85ba6c722 100644 --- a/src/PyConsole/PyConsole_Console.cxx +++ b/src/PyConsole/PyConsole_Console.cxx @@ -102,6 +102,16 @@ void PythonConsole::execAndWait( const QString& command ) myEditor->execAndWait( command ); } +bool PythonConsole::isSync() const +{ + return myEditor->isSync(); +} + +void PythonConsole::setIsSync( const bool s ) +{ + myEditor->setIsSync( s ); +} + /*! \brief Change the python console's font. \param f - new font diff --git a/src/PyConsole/PyConsole_Console.h b/src/PyConsole/PyConsole_Console.h index 3d07a595e..b25f4d191 100644 --- a/src/PyConsole/PyConsole_Console.h +++ b/src/PyConsole/PyConsole_Console.h @@ -56,6 +56,9 @@ public: QFont font() const; virtual void setFont( const QFont& ); + bool isSync() const; + void setIsSync( const bool ); + void exec( const QString& command ); void execAndWait( const QString& command ); @@ -74,6 +77,4 @@ private: QMap myActions; }; - - #endif diff --git a/src/PyConsole/PyConsole_Editor.cxx b/src/PyConsole/PyConsole_Editor.cxx index c5a371c86..23268097e 100644 --- a/src/PyConsole/PyConsole_Editor.cxx +++ b/src/PyConsole/PyConsole_Editor.cxx @@ -131,8 +131,8 @@ public: \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 ) @@ -190,11 +190,12 @@ private: \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 ); @@ -219,6 +220,16 @@ PyConsole_Editor::~PyConsole_Editor() { } +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 @@ -274,9 +285,12 @@ void PyConsole_Editor::exec( const QString& command ) // 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() ); } /*! @@ -327,9 +341,7 @@ void PyConsole_Editor::handleReturn() // 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 ) ); } /*! @@ -837,48 +849,51 @@ void PyConsole_Editor::keyPressEvent( QKeyEvent* event ) */ 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( event ); + if ( pe ) { - PyInterp_Event* pe = dynamic_cast( event ); - if ( pe ){ - ExecCommand* ec = dynamic_cast( 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( 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 ); } @@ -888,7 +903,8 @@ void PyConsole_Editor::customEvent( QEvent* 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(); diff --git a/src/PyConsole/PyConsole_Editor.h b/src/PyConsole/PyConsole_Editor.h index d46c3920c..12deee059 100644 --- a/src/PyConsole/PyConsole_Editor.h +++ b/src/PyConsole/PyConsole_Editor.h @@ -33,6 +33,7 @@ class QMenu; class QEventLoop; class PyConsole_Interp; +class PyInterp_Request; class PYCONSOLE_EXPORT PyConsole_Editor : public QTextEdit { @@ -48,11 +49,16 @@ public: virtual void exec( const QString& command ); void execAndWait( const QString& command ); + bool isSync() const; + void setIsSync( const bool ); + protected: virtual void dropEvent( QDropEvent* event ); virtual void mouseReleaseEvent( QMouseEvent* event ); virtual void keyPressEvent ( QKeyEvent* event); virtual void customEvent( QEvent* event); + + virtual PyInterp_Request* createRequest( const QString& ); public slots: void cut(); @@ -72,6 +78,7 @@ private: QEventLoop* myEventLoop; //!< internal event loop QString myBanner; //!< current banner QStringList myQueue; //!< python commands queue + bool myIsSync; }; #endif diff --git a/src/PyInterp/PyInterp_Dispatcher.cxx b/src/PyInterp/PyInterp_Dispatcher.cxx index 4a2f3d4eb..917298417 100755 --- a/src/PyInterp/PyInterp_Dispatcher.cxx +++ b/src/PyInterp/PyInterp_Dispatcher.cxx @@ -39,11 +39,16 @@ void PyInterp_Request::process() { 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() @@ -64,18 +69,22 @@ QEvent* PyInterp_Request::createEvent() const 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 ) @@ -143,11 +152,12 @@ void PyInterp_Dispatcher::Exec( PyInterp_Request* theRequest ) //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() ) @@ -197,8 +207,10 @@ void PyInterp_Dispatcher::objectDestroyed( const QObject* o ) // prepare for modification of the queue myQueueMutex.lock(); - for ( std::list::iterator it = myQueue.begin(); it != myQueue.end(); ++it ){ - if ( o == (*it)->getListener() ){ + for ( std::list::iterator it = myQueue.begin(); it != myQueue.end(); ++it ) + { + if ( o == (*it)->listener() ) + { (*it)->setListener( 0 ); // to prevent event posting it = myQueue.erase( it ); } @@ -206,4 +218,3 @@ void PyInterp_Dispatcher::objectDestroyed( const QObject* o ) myQueueMutex.unlock(); } - diff --git a/src/PyInterp/PyInterp_Dispatcher.h b/src/PyInterp/PyInterp_Dispatcher.h index cfa43089e..6f6240794 100755 --- a/src/PyInterp/PyInterp_Dispatcher.h +++ b/src/PyInterp/PyInterp_Dispatcher.h @@ -48,12 +48,12 @@ class PYINTERP_EXPORT PyInterp_Request 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 @@ -71,18 +71,17 @@ protected: 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 -- 2.39.2