Back to index

salome-gui  6.5.0
PyConsole_Console.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File   : PyConsole_Console.cxx
00024 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00025 //
00031 #include "PyConsole_Interp.h"   
00032 #include "PyConsole_Console.h"
00033 #include "PyConsole_Editor.h"
00034 
00035 #include <Qtx.h>
00036 
00037 #include <QAction>
00038 #include <QApplication>
00039 #include <QClipboard>
00040 #include <QEvent>
00041 #include <QMenu>
00042 #include <QVBoxLayout>
00043 
00044 #include <utilities.h>
00045 
00053 PyConsole_Console::PyConsole_Console( QWidget* parent, PyConsole_Interp* interp )
00054 : QWidget( parent ),
00055   myEditor( 0 )
00056 {
00057   // create python interpreter
00058   myInterp = interp;
00059   if ( !myInterp )
00060     myInterp = new PyConsole_Interp();
00061   
00062   // initialize Python interpretator
00063   myInterp->initialize();
00064   
00065   // create editor console
00066   QVBoxLayout* lay = new QVBoxLayout( this );
00067   lay->setMargin( 0 );
00068   myEditor = new PyConsole_Editor( myInterp, this );
00069   char* synchronous = getenv("PYTHON_CONSOLE_SYNC");
00070   if (synchronous && atoi(synchronous))
00071   {
00072       MESSAGE("Python console is synchronous");
00073       myEditor->setIsSync(true);
00074   }
00075   myEditor->viewport()->installEventFilter( this );
00076   lay->addWidget( myEditor );
00077 
00078   createActions();
00079 }
00080 
00086 PyConsole_Console::~PyConsole_Console()
00087 {
00088 }
00089 
00094 void PyConsole_Console::exec( const QString& command )
00095 {
00096   if ( myEditor )
00097     myEditor->exec( command );
00098 }
00099 
00107 void PyConsole_Console::execAndWait( const QString& command )
00108 {
00109   if ( myEditor )
00110     myEditor->execAndWait( command );
00111 }
00112 
00119 bool PyConsole_Console::isSync() const
00120 {
00121   return myEditor->isSync();
00122 }
00123 
00134 void PyConsole_Console::setIsSync( const bool on )
00135 {
00136   myEditor->setIsSync( on );
00137 }
00138 
00145 bool PyConsole_Console::isSuppressOutput() const
00146 {
00147   return myEditor->isSuppressOutput();
00148 }
00149 
00158 void PyConsole_Console::setIsSuppressOutput( const bool on )
00159 {
00160   myEditor->setIsSuppressOutput(on);
00161 }
00162 
00169 bool PyConsole_Console::isShowBanner() const
00170 {
00171   return myEditor->isShowBanner();
00172 }
00173 
00182 void PyConsole_Console::setIsShowBanner( const bool on )
00183 {
00184   myEditor->setIsShowBanner( on );
00185 }
00186 
00191 void PyConsole_Console::setFont( const QFont& f )
00192 {
00193   if( myEditor )
00194     myEditor->setFont( f );
00195 }
00196 
00201 QFont PyConsole_Console::font() const
00202 {
00203   QFont res;
00204   if( myEditor )
00205     res = myEditor->font();
00206   return res;
00207 }
00208 
00218 bool PyConsole_Console::eventFilter( QObject* o, QEvent* e )
00219 {
00220   if ( o == myEditor->viewport() && e->type() == QEvent::ContextMenu )
00221   {
00222     contextMenuRequest( (QContextMenuEvent*)e );
00223     return true;
00224   }
00225   return QWidget::eventFilter( o, e );
00226 }
00227 
00235 void PyConsole_Console::contextMenuPopup( QMenu* menu )
00236 {
00237   if ( myEditor->isReadOnly() )
00238     return;
00239 
00240   menu->addAction( myActions[CopyId] );
00241   menu->addAction( myActions[PasteId] );
00242   menu->addAction( myActions[ClearId] );
00243   menu->addSeparator();
00244   menu->addAction( myActions[SelectAllId] );
00245   menu->addSeparator();
00246   menu->addAction( myActions[DumpCommandsId] );
00247 
00248   Qtx::simplifySeparators( menu );
00249 
00250   updateActions();
00251 }
00252 
00261 void PyConsole_Console::setMenuActions( const int flags )
00262 {
00263   myActions[CopyId]->setVisible( flags & CopyId );
00264   myActions[PasteId]->setVisible( flags & PasteId );
00265   myActions[ClearId]->setVisible( flags & ClearId );
00266   myActions[SelectAllId]->setVisible( flags & SelectAllId );
00267   myActions[DumpCommandsId]->setVisible( flags & DumpCommandsId );
00268 }
00269 
00275 int PyConsole_Console::menuActions() const
00276 {
00277   int ret = 0;
00278   ret = ret | ( myActions[CopyId]->isVisible() ? CopyId : 0 );
00279   ret = ret | ( myActions[PasteId]->isVisible() ? PasteId : 0 );
00280   ret = ret | ( myActions[ClearId]->isVisible() ? ClearId : 0 );
00281   ret = ret | ( myActions[SelectAllId]->isVisible() ? SelectAllId : 0 );
00282   ret = ret | ( myActions[DumpCommandsId]->isVisible() ? DumpCommandsId : 0 );
00283   return ret;
00284 }
00285 
00291 void PyConsole_Console::createActions()
00292 {
00293   QAction* a = new QAction( tr( "EDIT_COPY_CMD" ), this );
00294   a->setStatusTip( tr( "EDIT_COPY_CMD" ) );
00295   connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( copy() ) );
00296   myActions.insert( CopyId, a );
00297 
00298   a = new QAction( tr( "EDIT_PASTE_CMD" ), this );
00299   a->setStatusTip( tr( "EDIT_PASTE_CMD" ) );
00300   connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( paste() ) );
00301   myActions.insert( PasteId, a );
00302 
00303   a = new QAction( tr( "EDIT_CLEAR_CMD" ), this );
00304   a->setStatusTip( tr( "EDIT_CLEAR_CMD" ) );
00305   connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( clear() ) );
00306   myActions.insert( ClearId, a );
00307 
00308   a = new QAction( tr( "EDIT_SELECTALL_CMD" ), this );
00309   a->setStatusTip( tr( "EDIT_SELECTALL_CMD" ) );
00310   connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( selectAll() ) );
00311   myActions.insert( SelectAllId, a );
00312   
00313   a = new QAction( tr( "EDIT_DUMPCOMMANDS_CMD" ), this );
00314   a->setStatusTip( tr( "EDIT_DUMPCOMMANDS_CMD" ) );
00315   connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( dump() ) );
00316   myActions.insert( DumpCommandsId, a );
00317 }
00318 
00324 void PyConsole_Console::updateActions()
00325 {
00326   myActions[CopyId]->setEnabled( myEditor->textCursor().hasSelection() );
00327   myActions[PasteId]->setEnabled( !myEditor->isReadOnly() && !QApplication::clipboard()->text().isEmpty() );
00328   myActions[SelectAllId]->setEnabled( !myEditor->document()->isEmpty() );
00329 }