Salome HOME
SALOME PAL V1_4_1
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Python.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SUPERVGUI_Python.cxx
25 //  Author : Francis KLOSS
26 //  Module : SUPERV
27
28 using namespace std;
29 #include "SUPERVGUI_Def.h"
30 #include "QAD_PyEditor.h"
31 #include "QAD_PyInterp.h"
32 #include "QAD_Application.h"
33 #include "SUPERVGUI_Python.h"
34 #include <qgroupbox.h>
35 #include <qlayout.h>
36 #include <qapplication.h>
37
38 #define MARGIN_SIZE       11
39 #define SPACING_SIZE      6
40 #define MIN_PYTHON_WIDTH  300
41 #define MIN_PYTHON_HEIGHT 200
42
43 /*!
44   Constructor
45 */
46 SUPERVGUI_Python::SUPERVGUI_Python(QAD_PyInterp* pi, bool isReadOnly)
47      : QDialog( 0, "", TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
48        py_int( pi )
49 {
50   Trace("SUPERVGUI_Python::SUPERVGUI_Python");
51   setCaption("Python Control Code");
52   setSizeGripEnabled( TRUE );
53
54   QVBoxLayout* topLayout = new QVBoxLayout( this );
55   topLayout->setMargin( MARGIN_SIZE ); topLayout->setSpacing( SPACING_SIZE );
56   
57   edit = new QMultiLineEdit( this );
58   edit->setPalette( QAD_Application::getPalette( true ) );
59   edit->setReadOnly( isReadOnly );
60   edit->setMinimumSize( MIN_PYTHON_WIDTH, MIN_PYTHON_HEIGHT );
61   
62   QGroupBox* GroupButtons = new QGroupBox( this );
63   GroupButtons->setColumnLayout(0, Qt::Vertical );
64   GroupButtons->layout()->setSpacing( 0 );  GroupButtons->layout()->setMargin( 0 );
65   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons->layout() );
66   GroupButtonsLayout->setAlignment( Qt::AlignTop );
67   GroupButtonsLayout->setSpacing( SPACING_SIZE );
68   GroupButtonsLayout->setMargin( MARGIN_SIZE );
69   
70   if ( !isReadOnly ) {
71     ok = new QPushButton( tr( "BUT_OK" ), GroupButtons, "buttonOk" );
72     ok->setAutoDefault( TRUE );  ok->setDefault( TRUE );
73     GroupButtonsLayout->addWidget( ok );
74     connect( ok, SIGNAL( clicked() ), this, SLOT( accept() ) );
75   }
76   GroupButtonsLayout->addStretch();
77   ko = new QPushButton( isReadOnly ? tr( "BUT_CLOSE" ) : tr( "BUT_CANCEL" ), GroupButtons, "buttonCancel" );
78   ko->setAutoDefault( TRUE );
79   GroupButtonsLayout->addWidget( ko );
80   connect( ko, SIGNAL( clicked() ), this, SLOT( reject() ) );
81   if ( isReadOnly ) {
82     GroupButtonsLayout->addStretch();
83   }
84   topLayout->addWidget( edit );
85   topLayout->addWidget( GroupButtons );
86 }
87
88 /*!
89   Destructor
90 */
91 SUPERVGUI_Python::~SUPERVGUI_Python() {
92   Trace("SUPERVGUI_Python::~SUPERVGUI_Python")
93 }
94
95 /*!
96   <OK> button slot
97 */
98 void SUPERVGUI_Python::accept() {
99   Trace("SUPERVGUI_Python::okButton");
100   QDialog::accept();
101   qApp->processEvents();
102   int n = edit->numLines();
103   for ( int li = 0; li < n; li++ ) {
104     QString ligne = edit->textLine( li );
105     if ( !ligne.isEmpty() ) {
106       if ( py_int->run( ligne.latin1() ) ) {
107         QMessageBox::warning(0, "Supervision Error", "Can't Reload Python Code");
108       }
109     }
110   }
111 }
112
113 /*!
114   <Cancel> button slot
115 */
116 void SUPERVGUI_Python::reject() {
117   Trace("SUPERVGUI_Python::koButton");
118   QDialog::reject();
119 }