3 \class PyConsole_Console
4 \brief Python console widget.
7 #include "PyConsole_Interp.h" /// !!! WARNING !!! THIS INCLUDE MUST BE VERY FIRST !!!
8 #include "PyConsole_Console.h"
9 #include "PyConsole_EnhEditor.h"
10 #include "PyConsole_EnhInterp.h"
15 #include <QApplication>
19 #include <QVBoxLayout>
24 Creates new python console widget.
25 \param parent parent widget
26 \param interp python interpreter
28 PyConsole_Console::PyConsole_Console( QWidget* parent, PyConsole_Interp* interp )
31 // create python interpreter
34 myInterp = new PyConsole_Interp();
36 // initialize Python interpretator
37 myInterp->initialize();
39 // create editor console
40 QVBoxLayout* lay = new QVBoxLayout( this );
42 myEditor = new PyConsole_Editor( myInterp, this );
43 char* synchronous = getenv("PYTHON_CONSOLE_SYNC");
44 if (synchronous && atoi(synchronous))
46 myEditor->setIsSync(true);
48 myEditor->viewport()->installEventFilter( this );
49 lay->addWidget( myEditor );
55 * Protected constructor.
57 PyConsole_Console::PyConsole_Console( QWidget* parent, PyConsole_Interp* i, PyConsole_Editor* e)
58 : QWidget (parent), myEditor(e), myInterp(i)
64 Does nothing for the moment.
66 PyConsole_Console::~PyConsole_Console()
71 \brief Execute python command in the interpreter.
72 \param command string with command and arguments
74 void PyConsole_Console::exec( const QString& command )
77 myEditor->exec( command );
81 \brief Execute python command in the interpreter
82 and wait until it is finished.
84 Block execution of main application until the python command is executed.
85 \param command string with command and arguments
87 void PyConsole_Console::execAndWait( const QString& command )
90 myEditor->execAndWait( command );
94 \brief Get synchronous mode flag value.
97 \return True if python console works in synchronous mode
99 bool PyConsole_Console::isSync() const
101 return myEditor->isSync();
105 \brief Set synchronous mode flag value.
107 In synhronous mode the Python commands are executed in the GUI thread
108 and the GUI is blocked until the command is finished. In the asynchronous
109 mode each Python command is executed in the separate thread that does not
110 block the main GUI loop.
112 \param on synhronous mode flag
114 void PyConsole_Console::setIsSync( const bool on )
116 myEditor->setIsSync( on );
120 \brief Get suppress output flag value.
122 \sa setIsSuppressOutput()
123 \return True if python console output is suppressed.
125 bool PyConsole_Console::isSuppressOutput() const
127 return myEditor->isSuppressOutput();
131 \brief Set suppress output flag value.
133 In case if suppress output flag is true, the python
134 console output suppressed.
136 \param on suppress output flag
138 void PyConsole_Console::setIsSuppressOutput( const bool on )
140 myEditor->setIsSuppressOutput(on);
144 \brief Get 'show banner' flag value.
146 \sa setIsShowBanner()
147 \return \c true if python console shows banner
149 bool PyConsole_Console::isShowBanner() const
151 return myEditor->isShowBanner();
155 \brief Set 'show banner' flag value.
157 The banner is shown in the top of the python console window.
160 \param on 'show banner' flag
162 void PyConsole_Console::setIsShowBanner( const bool on )
164 myEditor->setIsShowBanner( on );
168 \brief Change the python console's font.
171 void PyConsole_Console::setFont( const QFont& f )
174 myEditor->setFont( f );
178 \brief Get python console font.
179 \return current python console's font
181 QFont PyConsole_Console::font() const
185 res = myEditor->font();
190 \brief Event handler.
192 Handles context menu request event.
196 \return True if the event is processed and further processing should be stopped
198 bool PyConsole_Console::eventFilter( QObject* o, QEvent* e )
200 if ( o == myEditor->viewport() && e->type() == QEvent::ContextMenu )
202 //contextMenuRequest( (QContextMenuEvent*)e );
205 return QWidget::eventFilter( o, e );
209 \brief Create the context popup menu.
211 Fill in the popup menu with the commands.
213 \param menu context popup menu
215 void PyConsole_Console::contextMenuPopup( QMenu* menu )
217 if ( myEditor->isReadOnly() )
220 menu->addAction( myActions[CopyId] );
221 menu->addAction( myActions[PasteId] );
222 menu->addAction( myActions[ClearId] );
223 menu->addSeparator();
224 menu->addAction( myActions[SelectAllId] );
225 menu->addSeparator();
226 menu->addAction( myActions[DumpCommandsId] );
228 //Qtx::simplifySeparators( menu );
234 \brief Set actions to be visible in the context popup menu.
236 Actions, which IDs are set in \a flags parameter, will be shown in the
237 context popup menu. Other actions will not be shown.
239 \param flags ORed together actions flags
241 void PyConsole_Console::setMenuActions( const int flags )
243 myActions[CopyId]->setVisible( flags & CopyId );
244 myActions[PasteId]->setVisible( flags & PasteId );
245 myActions[ClearId]->setVisible( flags & ClearId );
246 myActions[SelectAllId]->setVisible( flags & SelectAllId );
247 myActions[DumpCommandsId]->setVisible( flags & DumpCommandsId );
251 \brief Get menu actions which are currently visible in the context popup menu.
252 \return ORed together actions flags
255 int PyConsole_Console::menuActions() const
258 ret = ret | ( myActions[CopyId]->isVisible() ? CopyId : 0 );
259 ret = ret | ( myActions[PasteId]->isVisible() ? PasteId : 0 );
260 ret = ret | ( myActions[ClearId]->isVisible() ? ClearId : 0 );
261 ret = ret | ( myActions[SelectAllId]->isVisible() ? SelectAllId : 0 );
262 ret = ret | ( myActions[DumpCommandsId]->isVisible() ? DumpCommandsId : 0 );
267 \brief Create menu actions.
269 Create context popup menu actions.
271 void PyConsole_Console::createActions()
273 QAction* a = new QAction( tr( "&Copy" ), this );
274 a->setStatusTip( tr( "Copy" ) );
275 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( copy() ) );
276 myActions.insert( CopyId, a );
278 a = new QAction( tr( "&Paste" ), this );
279 a->setStatusTip( tr( "Paste" ) );
280 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( paste() ) );
281 myActions.insert( PasteId, a );
283 a = new QAction( tr( "Clea&r" ), this );
284 a->setStatusTip( tr( "Clear" ) );
285 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( clear() ) );
286 myActions.insert( ClearId, a );
288 a = new QAction( tr( "Select &All" ), this );
289 a->setStatusTip( tr( "Select all" ) );
290 connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( selectAll() ) );
291 myActions.insert( SelectAllId, a );
293 // a = new QAction( tr( "EDIT_DUMPCOMMANDS_CMD" ), this );
294 // a->setStatusTip( tr( "EDIT_DUMPCOMMANDS_CMD" ) );
295 // connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( dump() ) );
296 // myActions.insert( DumpCommandsId, a );
300 \brief Update menu actions.
302 Update context popup menu action state.
304 void PyConsole_Console::updateActions()
306 myActions[CopyId]->setEnabled( myEditor->textCursor().hasSelection() );
307 myActions[PasteId]->setEnabled( !myEditor->isReadOnly() && !QApplication::clipboard()->text().isEmpty() );
308 myActions[SelectAllId]->setEnabled( !myEditor->document()->isEmpty() );
312 * Similar to constructor of the base class but using enhanced objects.
313 * TODO: this should really be done in a factory to avoid code duplication.
317 PyConsole_EnhConsole::PyConsole_EnhConsole( QWidget* parent, PyConsole_EnhInterp* interp)
318 : PyConsole_Console(parent, interp, 0)
320 // create python interpreter
323 myInterp = new PyConsole_EnhInterp();
325 // initialize Python interpretator
326 myInterp->initialize();
328 // create editor console
329 QVBoxLayout* lay = new QVBoxLayout( this );
331 myEditor = new PyConsole_EnhEditor( static_cast<PyConsole_EnhInterp*>(myInterp), this );
332 char* synchronous = getenv("PYTHON_CONSOLE_SYNC");
333 if (synchronous && atoi(synchronous))
335 myEditor->setIsSync(true);
337 myEditor->viewport()->installEventFilter( this );
338 lay->addWidget( myEditor );