1 ///////////////////////////////////////////////////////////
2 // File : SierpinskyGUI_RunDlg.cxx
3 // Author : Vadim SANDLER (OCN)
5 // Copyright (C) 2005 Open CASCADE
6 ///////////////////////////////////////////////////////////
8 #include "SierpinskyGUI_RunDlg.h"
9 #include "SALOME_LifeCycleCORBA.hxx"
10 #include "SalomeApp_Application.h"
11 //#include "VisuGUI_Tools.h"
14 #include <qlineedit.h>
16 #include <qpushbutton.h>
17 #include <qgroupbox.h>
18 #include <qcheckbox.h>
21 #include <qprogressbar.h>
22 #include <qapplication.h>
24 #include <SUIT_Application.h>
25 #include <SUIT_Desktop.h>
26 #include <SUIT_FileDlg.h>
27 #include <QtxDblSpinBox.h>
28 #include <SUIT_MessageBox.h>
30 #include <SALOMEconfig.h>
31 #include CORBA_SERVER_HEADER(Sierpinsky)
32 #include CORBA_CLIENT_HEADER(Randomizer)
33 #include CORBA_CLIENT_HEADER(SALOMEDS)
34 #include CORBA_CLIENT_HEADER(VISU_Gen)
37 * Get Randomizer component engine
39 static RANDOMIZER_ORB::RANDOMIZER_var getRandomizerEngine()
41 static RANDOMIZER_ORB::RANDOMIZER_var aGen;
42 if( CORBA::is_nil( aGen ) ) {
44 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
45 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServerPy","RANDOMIZER");
46 aGen = RANDOMIZER_ORB::RANDOMIZER::_narrow( aComponent );
48 catch (CORBA::Exception&){
49 MESSAGE("Caught CORBA::Exception.");
52 MESSAGE("Caught unknown exception.");
59 * Get Sierpinsky component engine
61 static SIERPINSKY_ORB::SIERPINSKY_var getSierpinskyEngine()
63 static SIERPINSKY_ORB::SIERPINSKY_var aGen;
64 if( CORBA::is_nil( aGen ) ) {
66 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
67 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","SIERPINSKY");
68 aGen = SIERPINSKY_ORB::SIERPINSKY::_narrow( aComponent );
70 catch (CORBA::Exception&){
71 MESSAGE("Caught CORBA::Exception.");
74 MESSAGE("Caught unknown exception.");
81 * Get VISU component engine
83 static VISU::VISU_Gen_var getVisuEngine()
85 static VISU::VISU_Gen_var aGen;
86 if( CORBA::is_nil( aGen ) ) {
88 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
89 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
90 aGen = VISU::VISU_Gen::_narrow( aComponent );
92 catch (CORBA::Exception&){
93 MESSAGE("Caught CORBA::Exception.");
96 MESSAGE("Caught unknown exception.");
103 #define MY_EVENT QEvent::User + 555
104 class MyEvent : public QCustomEvent
107 MyEvent( const int iter ) : QCustomEvent( MY_EVENT ), myIter( iter ) {}
108 int iter() const { return myIter; }
115 * Progress thread class
117 class MyThread : public QThread
120 MyThread( SierpinskyGUI_RunDlg* parent, const double x, const double y, const int nbIter ) :
121 myParent( parent ), myStopped( false ), myX( x ), myY( y ), myNbIter( nbIter ) { start(); }
123 void init( const double x, const double y, const int nbIter )
130 void stop() { myStopped = true; }
135 while ( !myStopped && myNbIter-- ) {
136 int ni = getRandomizerEngine()->NextIteration();
137 getSierpinskyEngine()->NextPoint( myX, myY, ni, myX, myY );
138 QApplication::postEvent( myParent, new MyEvent( myNbIter ) );
144 SierpinskyGUI_RunDlg* myParent;
151 #define MARGIN_SIZE 11
152 #define SPACING_SIZE 6
157 SierpinskyGUI_RunDlg::SierpinskyGUI_RunDlg( QWidget* parent )
158 : QDialog( parent, "SierpinskyGUI_RunDlg", true,
159 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
162 setCaption( tr( "CAPTION" ) );
163 setSizeGripEnabled( true );
165 QGridLayout* topLayout = new QGridLayout( this );
166 topLayout->setMargin( MARGIN_SIZE ); topLayout->setSpacing( SPACING_SIZE );
169 box1 = new QGroupBox( tr( "START_POINT" ), this );
170 box1->setColumnLayout( 0, Qt::Vertical );
171 box1->layout()->setMargin( 0 ); box1->layout()->setSpacing( 0 );
172 QGridLayout* box1Layout = new QGridLayout( box1->layout() );
173 box1Layout->setAlignment( Qt::AlignTop );
174 box1Layout->setMargin( MARGIN_SIZE ); box1Layout->setSpacing( SPACING_SIZE );
176 QLabel* labX = new QLabel( "X:", box1 );
177 myStartX = new QtxDblSpinBox( 0.0, 1.0, 0.1 ,box1);
178 myStartX->setMinimumWidth( 150 );
180 QLabel* labY = new QLabel( "Y:", box1 );
181 myStartY = new QtxDblSpinBox( 0.0, 1.0, 0.1 ,box1);
182 myStartY->setMinimumWidth( 150 );
184 myStartRandom = new QCheckBox( tr( "RANDOM" ), box1 );
186 box1Layout->addWidget( labX, 0, 0 );
187 box1Layout->addWidget( myStartX, 0, 1 );
188 box1Layout->addWidget( labY, 0, 2 );
189 box1Layout->addWidget( myStartY, 0, 3 );
190 box1Layout->addWidget( myStartRandom, 1, 1 );
193 box2 = new QGroupBox( tr( "BASE_POINTS" ), this );
194 box2->setColumnLayout( 0, Qt::Vertical );
195 box2->layout()->setMargin( 0 ); box2->layout()->setSpacing( 0 );
196 QGridLayout* box2Layout = new QGridLayout( box2->layout() );
197 box2Layout->setAlignment( Qt::AlignTop );
198 box2Layout->setMargin( MARGIN_SIZE ); box2Layout->setSpacing( SPACING_SIZE );
200 QLabel* labX1 = new QLabel( "X1:", box2 );
201 myX1 = new QtxDblSpinBox( 0.0, 1.0, 0.1 , box2);
202 myX1->setMinimumWidth( 150 );
204 QLabel* labY1 = new QLabel( "Y1:", box2 );
205 myY1 = new QtxDblSpinBox( 0.0, 1.0, 0.1 , box2);
206 myY1->setMinimumWidth( 150 );
208 QLabel* labX2 = new QLabel( "X2:", box2 );
209 myX2 = new QtxDblSpinBox( 0.0, 1.0, 0.1 , box2);
210 myX2->setMinimumWidth( 150 );
212 QLabel* labY2 = new QLabel( "Y2:", box2 );
213 myY2 = new QtxDblSpinBox( 0.0, 1.0, 0.1, box2);
214 myY2->setMinimumWidth( 150 );
216 QLabel* labX3 = new QLabel( "X3:", box2 );
217 myX3 = new QtxDblSpinBox( 0.0, 1.0, 0.1, box2);
218 myX3->setMinimumWidth( 150 );
220 QLabel* labY3 = new QLabel( "Y3:", box2 );
221 myY3 = new QtxDblSpinBox( 0.0, 1.0, 0.1, box2);
222 myY3->setMinimumWidth( 150 );
224 myBaseRandom = new QCheckBox( tr( "RANDOM" ), box2 );
225 myBaseDefault = new QCheckBox( tr( "DEFAULT" ), box2 );
227 box2Layout->addWidget( labX1, 0, 0 );
228 box2Layout->addWidget( myX1, 0, 1 );
229 box2Layout->addWidget( labY1, 0, 2 );
230 box2Layout->addWidget( myY1, 0, 3 );
231 box2Layout->addWidget( labX2, 1, 0 );
232 box2Layout->addWidget( myX2, 1, 1 );
233 box2Layout->addWidget( labY2, 1, 2 );
234 box2Layout->addWidget( myY2, 1, 3 );
235 box2Layout->addWidget( labX3, 2, 0 );
236 box2Layout->addWidget( myX3, 2, 1 );
237 box2Layout->addWidget( labY3, 2, 2 );
238 box2Layout->addWidget( myY3, 2, 3 );
239 box2Layout->addWidget( myBaseRandom, 3, 1 );
240 box2Layout->addWidget( myBaseDefault, 3, 3 );
242 // number of iterations
243 QLabel* labIter = new QLabel( tr( "NB_ITERATIONS" ), this );
244 myIter = new QSpinBox( 1, 100000, 1, this );
247 box3 = new QGroupBox( tr( "RESULTS" ), this );
248 box3->setColumnLayout( 0, Qt::Vertical );
249 box3->layout()->setMargin( 0 ); box3->layout()->setSpacing( 0 );
250 QGridLayout* box3Layout = new QGridLayout( box3->layout() );
251 box3Layout->setAlignment( Qt::AlignTop );
252 box3Layout->setMargin( MARGIN_SIZE ); box3Layout->setSpacing( SPACING_SIZE );
254 myJpegCheck = new QCheckBox( tr( "EXPORT_2_JPEG" ), box3 );
255 myJpegFile = new QLineEdit( box3 );
256 myJpegFile->setMinimumWidth( 300 );
258 myJpegBtn = new QPushButton( "...", box3 );
259 myJpegBtn->setFixedSize( 20, 20 );
261 QLabel* labJpeg = new QLabel( tr( "IMAGE_SIZE" ), box3 );
262 myJpegSize = new QSpinBox( 0, 1280, 10, box3 );
263 myJpegSize->setMinimumWidth( 150 );
265 QFrame* line = new QFrame( box3 );
266 line->setFrameStyle( QFrame::HLine | QFrame::Sunken );
268 myMedCheck = new QCheckBox( tr( "EXPORT_2_MED" ), box3 );
269 myMedFile = new QLineEdit( box3 );
270 myMedFile->setMinimumWidth( 300 );
272 myMedBtn = new QPushButton( "...", box3 );
273 myMedBtn->setFixedSize( 20, 20 );
275 QLabel* labMed = new QLabel( tr( "MESH_SIZE" ), box3 );
276 myMedSize = new QtxDblSpinBox( 0.0, 1000, 1.0, box3);
277 myMedSize->setMinimumWidth( 150 );
279 myVisuCheck = new QCheckBox( tr( "IMPORT_MED_2_VISU" ), box3 );
281 box3Layout->addMultiCellWidget( myJpegCheck, 0, 0, 0, 3 );
282 box3Layout->addMultiCellWidget( myJpegFile, 1, 1, 0, 2 );
283 box3Layout->addWidget( myJpegBtn, 1, 3 );
284 box3Layout->addWidget( labJpeg, 2, 0 );
285 box3Layout->addWidget( myJpegSize, 2, 1 );
286 box3Layout->addMultiCellWidget( line, 3, 3, 0, 3 );
287 box3Layout->addMultiCellWidget( myMedCheck, 4, 4, 0, 3 );
288 box3Layout->addMultiCellWidget( myMedFile, 5, 5, 0, 2 );
289 box3Layout->addWidget( myMedBtn, 5, 3 );
290 box3Layout->addWidget( labMed, 6, 0 );
291 box3Layout->addWidget( myMedSize, 6, 1 );
292 box3Layout->addMultiCellWidget( myVisuCheck, 7, 7, 0, 3 );
295 myProgress = new QProgressBar( this );
296 myProgress->setPercentageVisible( false );
297 myProgress->setFixedHeight( 10 );
300 myStartBtn = new QPushButton( tr( "START_BTN" ), this );
301 myCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
302 QHBoxLayout* btnLayout = new QHBoxLayout;
303 btnLayout->addWidget( myStartBtn );
304 btnLayout->addStretch();
305 btnLayout->addWidget( myCancelBtn );
307 topLayout->addMultiCellWidget( box1, 0, 0, 0, 1 );
308 topLayout->addMultiCellWidget( box2, 1, 1, 0, 1 );
309 topLayout->addWidget( labIter, 2, 0 );
310 topLayout->addWidget( myIter, 2, 1 );
311 topLayout->addMultiCellWidget( box3, 3, 3, 0, 1 );
312 topLayout->addMultiCellWidget( myProgress,4, 4, 0, 1 );
313 topLayout->addMultiCellLayout( btnLayout, 5, 5, 0, 1 );
315 // signals and slots connections
316 connect( myJpegBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
317 connect( myMedBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
318 connect( myStartRandom, SIGNAL( clicked() ), this, SLOT( updateState() ) );
319 connect( myBaseRandom, SIGNAL( clicked() ), this, SLOT( toggled() ) );
320 connect( myBaseDefault, SIGNAL( clicked() ), this, SLOT( toggled() ) );
321 connect( myJpegCheck, SIGNAL( clicked() ), this, SLOT( updateState() ) );
322 connect( myMedCheck, SIGNAL( clicked() ), this, SLOT( updateState() ) );
323 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
324 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( close() ) );
327 myStartX->setValue( 0.0 ); myStartY->setValue( 0.0 );
328 myStartRandom->setChecked( true );
329 myX1->setValue( 0.5 ); myY1->setValue( 1.0 );
330 myX2->setValue( 0.0 ); myY2->setValue( 0.0 );
331 myX3->setValue( 1.0 ); myY3->setValue( 0.0 );
332 myBaseRandom->setChecked( false );
333 myBaseDefault->setChecked( true );
334 myIter->setValue( 1000 );
335 myJpegCheck->setChecked( false );
336 myJpegSize->setValue( 200 );
337 myMedCheck->setChecked( false );
338 myMedSize->setValue( 100 );
339 myVisuCheck->setChecked( true );
346 SierpinskyGUI_RunDlg::~SierpinskyGUI_RunDlg()
358 void SierpinskyGUI_RunDlg::onBrowse()
360 const QObject* snd = sender();
361 bool jpg = snd == myJpegBtn;
362 QString aFileName = SUIT_FileDlg::getFileName( this,
363 jpg ? myJpegFile->text() : myMedFile->text(),
364 QStringList::split( ":", jpg ? tr( "JPEG_FILES") : tr( "MED_FILES" ) ),
365 jpg ? tr( "EXPORT_JPEG_FILE" ) : tr( "EXPORT_MED_FILE" ),
367 if ( !aFileName.isEmpty() ) {
368 jpg ? myJpegFile->setText( aFileName ) : myMedFile->setText( aFileName );
373 * Update widgets state
375 void SierpinskyGUI_RunDlg::updateState()
377 myStartX->setEnabled( !myStartRandom->isChecked() );
378 myStartY->setEnabled( !myStartRandom->isChecked() );
380 myX1->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
381 myY1->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
382 myX2->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
383 myY2->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
384 myX3->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
385 myY3->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
387 myJpegFile->setEnabled( myJpegCheck->isChecked() );
388 myJpegBtn->setEnabled( myJpegCheck->isChecked() );
389 myJpegSize->setEnabled( myJpegCheck->isChecked() );
391 myMedFile->setEnabled( myMedCheck->isChecked() );
392 myMedBtn->setEnabled( myMedCheck->isChecked() );
393 myMedSize->setEnabled( myMedCheck->isChecked() );
394 myVisuCheck->setEnabled( myMedCheck->isChecked() );
398 * Toggle 'Base points' mode
400 void SierpinskyGUI_RunDlg::toggled()
402 const QObject* snd = sender();
403 if ( snd == myBaseRandom && myBaseRandom->isChecked() ) myBaseDefault->setChecked( false );
404 if ( snd == myBaseDefault && myBaseDefault->isChecked() ) myBaseRandom->setChecked( false );
409 * Called when user presses 'Start' button
411 void SierpinskyGUI_RunDlg::accept()
413 RANDOMIZER_ORB::RANDOMIZER_var aRandGen = getRandomizerEngine();
414 SIERPINSKY_ORB::SIERPINSKY_var aSierpGen = getSierpinskyEngine();
416 if( CORBA::is_nil( aRandGen ) || CORBA::is_nil( aSierpGen ) ) {
417 SUIT_MessageBox::error1( this,
427 if ( myJpegCheck->isChecked() ) {
428 aJPEGFile = myJpegFile->text().stripWhiteSpace();
429 aJPEGSize = myJpegSize->value();
430 if ( aJPEGFile.isEmpty() || aJPEGSize <= 0 ) {
431 int res = SUIT_MessageBox::warn2( this,
433 tr( "JPEG_PARAMETERS_ERROR" ),
434 tr( "YES_BTN"), tr( "NO_BTN" ), 0, 1, 0 );
442 if ( myMedCheck->isChecked() ) {
443 aMEDFile = myMedFile->text().stripWhiteSpace();
444 aMEDSize = myMedSize->value();
445 if ( aMEDFile.isEmpty() || aMEDSize <= 0 ) {
446 int res = SUIT_MessageBox::warn2( this,
448 tr( "MED_PARAMETERS_ERROR" ),
449 tr( "YES_BTN"), tr( "NO_BTN" ), 0, 1, 0 );
455 double x = myStartX->value();
456 double y = myStartY->value();
457 if ( myStartRandom->isChecked() )
458 aRandGen->InitPoint( x, y );
461 if ( myBaseDefault->isChecked() ) {
462 aSierpGen->Reset(); // use default values from engine
465 double x1 = myX1->value();
466 double y1 = myY1->value();
467 double x2 = myX2->value();
468 double y2 = myY2->value();
469 double x3 = myX3->value();
470 double y3 = myY3->value();
471 if ( myBaseRandom->isChecked() ) {
472 aRandGen->InitPoint( x1, y1 );
473 aRandGen->InitPoint( x2, y2 );
474 aRandGen->InitPoint( x3, y3 );
476 aSierpGen->Init( x1, y1, x2, y2, x3, y3 );
480 int iter = myIter->value();
483 myProgress->setTotalSteps( iter );
484 myProgress->setProgress( 0 );
486 myThread = new MyThread( this, x, y, iter );
488 myThread->init( x, y, iter );
489 disconnect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
490 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( stop() ) );
491 myStartBtn->setText( tr( "STOP_BTN" ) );
492 box1->setEnabled( false );
493 box2->setEnabled( false );
494 box3->setEnabled( false );
495 myIter->setEnabled( false );
499 * Called when user presses 'Stop' button
501 void SierpinskyGUI_RunDlg::stop()
504 disconnect( myStartBtn, SIGNAL( clicked() ), this, SLOT( stop() ) );
505 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
506 myStartBtn->setText( tr( "START_BTN" ) );
507 myProgress->setProgress( 0 );
508 box1->setEnabled( true );
509 box2->setEnabled( true );
510 box3->setEnabled( true );
511 myIter->setEnabled( true );
515 * Progress thread calls this method to update progress bar state
517 void SierpinskyGUI_RunDlg::nextStep( const int step )
520 disconnect( myStartBtn, SIGNAL( clicked() ), this, SLOT( stop() ) );
521 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
522 myStartBtn->setText( tr( "START_BTN" ) );
523 myProgress->setProgress( 0 );
524 box1->setEnabled( true );
525 box2->setEnabled( true );
526 box3->setEnabled( true );
527 myIter->setEnabled( true );
530 if ( myJpegCheck->isChecked() ) {
531 QString aJPEGFile = myJpegFile->text().stripWhiteSpace();
532 int aJPEGSize = myJpegSize->value();
533 if ( !aJPEGFile.isEmpty() && aJPEGSize > 0 ) {
534 bool res = getSierpinskyEngine()->ExportToJPEG( aJPEGFile.latin1(), aJPEGSize );
538 if ( myMedCheck->isChecked() ) {
539 QString aMEDFile = myMedFile->text().stripWhiteSpace();
540 double aMEDSize = myMedSize->value();
541 if ( !aMEDFile.isEmpty() && aMEDSize > 0 ) {
542 bool res = getSierpinskyEngine()->ExportToMED( aMEDFile.latin1(), aMEDSize );
543 if ( res && myVisuCheck->isChecked() ) {
544 VISU::VISU_Gen_var aVisuGen = getVisuEngine();
545 if ( !CORBA::is_nil( aVisuGen ) ) {
546 VISU::Result_var aResult = aVisuGen->ImportFile( aMEDFile.latin1() );
547 if ( !CORBA::is_nil( aResult ) )
548 aVisuGen->MeshOnEntity( aResult, "Sierpinsky", VISU::NODE );
555 myProgress->setProgress( myProgress->totalSteps() - step );
560 * Process progress event
562 void SierpinskyGUI_RunDlg::customEvent( QCustomEvent* e )
564 if ( e->type() == MY_EVENT ) {
565 MyEvent* me = (MyEvent*)e;
566 nextStep( me->iter() );