1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : PyConsole_Console.cxx
24 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
27 \class PyConsole_Console
28 \brief Python console widget.
31 #include "PyConsole_Interp.h" /// !!! WARNING !!! THIS INCLUDE MUST BE VERY FIRST !!!
32 #include "PyConsole_Console.h"
33 #include "PyConsole_Editor.h"
38 #include <QApplication>
42 #include <QVBoxLayout>
47 Creates new python console widget.
48 \param parent parent widget
49 \param interp python interpreter
51 PyConsole_Console::PyConsole_Console( QWidget* parent, PyConsole_Interp* interp )
55 // create python interpreter
58 myInterp = new PyConsole_Interp();
60 // initialize Python interpretator
61 myInterp->initialize();
63 // create editor console
64 QVBoxLayout* lay = new QVBoxLayout( this );
66 myEditor = new PyConsole_Editor( myInterp, this );
67 char* synchronous = getenv("PYTHON_CONSOLE_SYNC");
68 if (synchronous && atoi(synchronous))
70 myEditor->setIsSync(true);
72 myEditor->viewport()->installEventFilter( this );
73 lay->addWidget( myEditor );
81 Does nothing for the moment.
83 PyConsole_Console::~PyConsole_Console()
88 \brief Execute python command in the interpreter.
89 \param command string with command and arguments
91 void PyConsole_Console::exec( const QString& command )
94 myEditor->exec( command );
98 \brief Execute python command in the interpreter
99 and wait until it is finished.
101 Block execution of main application until the python command is executed.
102 \param command string with command and arguments
104 void PyConsole_Console::execAndWait( const QString& command )
107 myEditor->execAndWait( command );
111 \brief Get synchronous mode flag value.
114 \return True if python console works in synchronous mode
116 bool PyConsole_Console::isSync() const
118 return myEditor->isSync();
122 \brief Set synchronous mode flag value.
124 In synhronous mode the Python commands are executed in the GUI thread
125 and the GUI is blocked until the command is finished. In the asynchronous
126 mode each Python command is executed in the separate thread that does not
127 block the main GUI loop.
129 \param on synhronous mode flag
131 void PyConsole_Console::setIsSync( const bool on )
133 myEditor->setIsSync( on );
137 \brief Get suppress output flag value.
139 \sa setIsSuppressOutput()
140 \return True if python console output is suppressed.
142 bool PyConsole_Console::isSuppressOutput() const
144 return myEditor->isSuppressOutput();
148 \brief Set suppress output flag value.
150 In case if suppress output flag is true, the python
151 console output suppressed.
153 \param on suppress output flag
155 void PyConsole_Console::setIsSuppressOutput( const bool on )
157 myEditor->setIsSuppressOutput(on);
161 \brief Get 'show banner' flag value.
163 \sa setIsShowBanner()
164 \return \c true if python console shows banner
166 bool PyConsole_Console::isShowBanner() const
168 return myEditor->isShowBanner();
172 \brief Set 'show banner' flag value.
174 The banner is shown in the top of the python console window.
177 \param on 'show banner' flag
179 void PyConsole_Console::setIsShowBanner( const bool on )
181 myEditor->setIsShowBanner( on );
185 \brief Change the python console's font.
188 void PyConsole_Console::setFont( const QFont& f )
191 myEditor->setFont( f );
195 \brief Get python console font.
196 \return current python console's font
198 QFont PyConsole_Console::font() const
202 res = myEditor->font();
207 \brief Event handler.
209 Handles context menu request event.
213 \return True if the event is processed and further processing should be stopped
215 bool PyConsole_Console::eventFilter( QObject* o, QEvent* e )
217 if ( o == myEditor->viewport() && e->type() == QEvent::ContextMenu )
219 contextMenuRequest( (QContextMenuEvent*)e );
222 return QWidget::eventFilter( o, e );
226 \brief Create the context popup menu.
228 Fill in the popup menu with the commands.
230 \param menu context popup menu
232 void PyConsole_Console::contextMenuPopup( QMenu* menu )
234 if ( myEditor->isReadOnly() )
237 menu->addAction( myActions[CopyId] );
238 menu->addAction( myActions[PasteId] );
239 menu->addAction( myActions[ClearId] );
240 menu->addSeparator();
241 menu->addAction( myActions[SelectAllId] );
242 menu->addSeparator();
243 menu->addAction( myActions[DumpCommandsId] );
245 Qtx::simplifySeparators( menu );
251 \brief Set actions to be visible in the context popup menu.
253 Actions, which IDs are set in \a flags parameter, will be shown in the
254 context popup menu. Other actions will not be shown.
256 \param flags ORed together actions flags
258 void PyConsole_Console::setMenuActions( const int flags )
260 myActions[CopyId]->setVisible( flags & CopyId );
261 myActions[PasteId]->setVisible( flags & PasteId );
262 myActions[ClearId]->setVisible( flags & ClearId );
263 myActions[SelectAllId]->setVisible( flags & SelectAllId );
264 myActions[DumpCommandsId]->setVisible( flags & DumpCommandsId );
268 \brief Get menu actions which are currently visible in the context popup menu.
269 \return ORed together actions flags
272 int PyConsole_Console::menuActions() const
275 ret = ret | ( myActions[CopyId]->isVisible() ? CopyId : 0 );
276 ret = ret | ( myActions[PasteId]->isVisible() ? PasteId : 0 );
277 ret = ret | ( myActions[ClearId]->isVisible() ? ClearId : 0 );
278 ret = ret | ( myActions[SelectAllId]->isVisible() ? SelectAllId : 0 );
279 ret = ret | ( myActions[DumpCommandsId]->isVisible() ? DumpCommandsId : 0 );
284 \brief Create menu actions.
286 Create context popup menu actions.
288 void PyConsole_Console::createActions()
290 QAction* a = new QAction( tr( "EDIT_COPY_CMD" ), this );
291 a->setStatusTip( tr( "EDIT_COPY_CMD" ) );
292 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( copy() ) );
293 myActions.insert( CopyId, a );
295 a = new QAction( tr( "EDIT_PASTE_CMD" ), this );
296 a->setStatusTip( tr( "EDIT_PASTE_CMD" ) );
297 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( paste() ) );
298 myActions.insert( PasteId, a );
300 a = new QAction( tr( "EDIT_CLEAR_CMD" ), this );
301 a->setStatusTip( tr( "EDIT_CLEAR_CMD" ) );
302 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( clear() ) );
303 myActions.insert( ClearId, a );
305 a = new QAction( tr( "EDIT_SELECTALL_CMD" ), this );
306 a->setStatusTip( tr( "EDIT_SELECTALL_CMD" ) );
307 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( selectAll() ) );
308 myActions.insert( SelectAllId, a );
310 a = new QAction( tr( "EDIT_DUMPCOMMANDS_CMD" ), this );
311 a->setStatusTip( tr( "EDIT_DUMPCOMMANDS_CMD" ) );
312 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( dump() ) );
313 myActions.insert( DumpCommandsId, a );
317 \brief Update menu actions.
319 Update context popup menu action state.
321 void PyConsole_Console::updateActions()
323 myActions[CopyId]->setEnabled( myEditor->textCursor().hasSelection() );
324 myActions[PasteId]->setEnabled( !myEditor->isReadOnly() && !QApplication::clipboard()->text().isEmpty() );
325 myActions[SelectAllId]->setEnabled( !myEditor->document()->isEmpty() );