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 "SierpinskyGUI.h"
10 #include "SALOME_LifeCycleCORBA.hxx"
11 #include "SalomeApp_Application.h"
12 //#include "VisuGUI_Tools.h"
15 #include <qlineedit.h>
17 #include <qpushbutton.h>
18 #include <qgroupbox.h>
19 #include <qcheckbox.h>
22 #include <qprogressbar.h>
23 #include <qapplication.h>
25 #include <SUIT_Application.h>
26 #include <SUIT_Desktop.h>
27 #include <SUIT_FileDlg.h>
28 #include <QtxDblSpinBox.h>
29 #include <SUIT_MessageBox.h>
31 #include <SALOMEconfig.h>
32 #include CORBA_SERVER_HEADER(Sierpinsky)
33 #include CORBA_CLIENT_HEADER(Randomizer)
34 #include CORBA_CLIENT_HEADER(SALOMEDS)
35 #include CORBA_CLIENT_HEADER(VISU_Gen)
38 * Get Randomizer component engine
40 static RANDOMIZER_ORB::RANDOMIZER_var getRandomizerEngine()
42 static RANDOMIZER_ORB::RANDOMIZER_var aGen;
43 if( CORBA::is_nil( aGen ) ) {
45 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
46 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServerPy","RANDOMIZER");
47 aGen = RANDOMIZER_ORB::RANDOMIZER::_narrow( aComponent );
49 catch (CORBA::Exception&){
50 MESSAGE("Caught CORBA::Exception.");
53 MESSAGE("Caught unknown exception.");
60 * Get Sierpinsky component engine
62 static SIERPINSKY_ORB::SIERPINSKY_var getSierpinskyEngine()
64 static SIERPINSKY_ORB::SIERPINSKY_var aGen;
65 if( CORBA::is_nil( aGen ) ) {
67 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
68 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","SIERPINSKY");
69 aGen = SIERPINSKY_ORB::SIERPINSKY::_narrow( aComponent );
71 catch (CORBA::Exception&){
72 MESSAGE("Caught CORBA::Exception.");
75 MESSAGE("Caught unknown exception.");
82 * Get VISU component engine
84 static VISU::VISU_Gen_var getVisuEngine()
86 static VISU::VISU_Gen_var aGen;
87 if( CORBA::is_nil( aGen ) ) {
89 SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
90 Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
91 aGen = VISU::VISU_Gen::_narrow( aComponent );
93 catch (CORBA::Exception&){
94 MESSAGE("Caught CORBA::Exception.");
97 MESSAGE("Caught unknown exception.");
104 #define MY_EVENT QEvent::User + 555
105 class MyEvent : public QCustomEvent
108 MyEvent( const int iter ) : QCustomEvent( MY_EVENT ), myIter( iter ) {}
109 int iter() const { return myIter; }
116 * Progress thread class
118 class MyThread : public QThread
121 MyThread( SierpinskyGUI_RunDlg* parent, const double x, const double y, const int nbIter ) :
122 myParent( parent ), myStopped( false ), myX( x ), myY( y ), myNbIter( nbIter ) { start(); }
124 void init( const double x, const double y, const int nbIter )
131 void stop() { myStopped = true; }
136 while ( !myStopped && myNbIter-- ) {
137 int ni = getRandomizerEngine()->NextIteration();
138 getSierpinskyEngine()->NextPoint( myX, myY, ni, myX, myY );
139 QApplication::postEvent( myParent, new MyEvent( myNbIter ) );
145 SierpinskyGUI_RunDlg* myParent;
152 #define MARGIN_SIZE 11
153 #define SPACING_SIZE 6
158 SierpinskyGUI_RunDlg::SierpinskyGUI_RunDlg( QWidget* parent, SierpinskyGUI* module )
159 : QDialog( parent, "SierpinskyGUI_RunDlg", true,
160 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose ),
161 myThread( 0 ), myModule( module )
163 setCaption( tr( "CAPTION" ) );
164 setSizeGripEnabled( true );
166 QGridLayout* topLayout = new QGridLayout( this );
167 topLayout->setMargin( MARGIN_SIZE ); topLayout->setSpacing( SPACING_SIZE );
170 box1 = new QGroupBox( tr( "START_POINT" ), this );
171 box1->setColumnLayout( 0, Qt::Vertical );
172 box1->layout()->setMargin( 0 ); box1->layout()->setSpacing( 0 );
173 QGridLayout* box1Layout = new QGridLayout( box1->layout() );
174 box1Layout->setAlignment( Qt::AlignTop );
175 box1Layout->setMargin( MARGIN_SIZE ); box1Layout->setSpacing( SPACING_SIZE );
177 QLabel* labX = new QLabel( "X:", box1 );
178 myStartX = new QtxDblSpinBox( 0.0, 1.0, 0.1 ,box1);
179 myStartX->setMinimumWidth( 150 );
181 QLabel* labY = new QLabel( "Y:", box1 );
182 myStartY = new QtxDblSpinBox( 0.0, 1.0, 0.1 ,box1);
183 myStartY->setMinimumWidth( 150 );
185 myStartRandom = new QCheckBox( tr( "RANDOM" ), box1 );
187 box1Layout->addWidget( labX, 0, 0 );
188 box1Layout->addWidget( myStartX, 0, 1 );
189 box1Layout->addWidget( labY, 0, 2 );
190 box1Layout->addWidget( myStartY, 0, 3 );
191 box1Layout->addWidget( myStartRandom, 1, 1 );
194 box2 = new QGroupBox( tr( "BASE_POINTS" ), this );
195 box2->setColumnLayout( 0, Qt::Vertical );
196 box2->layout()->setMargin( 0 ); box2->layout()->setSpacing( 0 );
197 QGridLayout* box2Layout = new QGridLayout( box2->layout() );
198 box2Layout->setAlignment( Qt::AlignTop );
199 box2Layout->setMargin( MARGIN_SIZE ); box2Layout->setSpacing( SPACING_SIZE );
201 QLabel* labX1 = new QLabel( "X1:", box2 );
202 myX1 = new QtxDblSpinBox( 0.0, 1.0, 0.1 , box2);
203 myX1->setMinimumWidth( 150 );
205 QLabel* labY1 = new QLabel( "Y1:", box2 );
206 myY1 = new QtxDblSpinBox( 0.0, 1.0, 0.1 , box2);
207 myY1->setMinimumWidth( 150 );
209 QLabel* labX2 = new QLabel( "X2:", box2 );
210 myX2 = new QtxDblSpinBox( 0.0, 1.0, 0.1 , box2);
211 myX2->setMinimumWidth( 150 );
213 QLabel* labY2 = new QLabel( "Y2:", box2 );
214 myY2 = new QtxDblSpinBox( 0.0, 1.0, 0.1, box2);
215 myY2->setMinimumWidth( 150 );
217 QLabel* labX3 = new QLabel( "X3:", box2 );
218 myX3 = new QtxDblSpinBox( 0.0, 1.0, 0.1, box2);
219 myX3->setMinimumWidth( 150 );
221 QLabel* labY3 = new QLabel( "Y3:", box2 );
222 myY3 = new QtxDblSpinBox( 0.0, 1.0, 0.1, box2);
223 myY3->setMinimumWidth( 150 );
225 myBaseRandom = new QCheckBox( tr( "RANDOM" ), box2 );
226 myBaseDefault = new QCheckBox( tr( "DEFAULT" ), box2 );
228 box2Layout->addWidget( labX1, 0, 0 );
229 box2Layout->addWidget( myX1, 0, 1 );
230 box2Layout->addWidget( labY1, 0, 2 );
231 box2Layout->addWidget( myY1, 0, 3 );
232 box2Layout->addWidget( labX2, 1, 0 );
233 box2Layout->addWidget( myX2, 1, 1 );
234 box2Layout->addWidget( labY2, 1, 2 );
235 box2Layout->addWidget( myY2, 1, 3 );
236 box2Layout->addWidget( labX3, 2, 0 );
237 box2Layout->addWidget( myX3, 2, 1 );
238 box2Layout->addWidget( labY3, 2, 2 );
239 box2Layout->addWidget( myY3, 2, 3 );
240 box2Layout->addWidget( myBaseRandom, 3, 1 );
241 box2Layout->addWidget( myBaseDefault, 3, 3 );
243 // number of iterations
244 QLabel* labIter = new QLabel( tr( "NB_ITERATIONS" ), this );
245 myIter = new QSpinBox( 1, 100000, 1, this );
248 box3 = new QGroupBox( tr( "RESULTS" ), this );
249 box3->setColumnLayout( 0, Qt::Vertical );
250 box3->layout()->setMargin( 0 ); box3->layout()->setSpacing( 0 );
251 QGridLayout* box3Layout = new QGridLayout( box3->layout() );
252 box3Layout->setAlignment( Qt::AlignTop );
253 box3Layout->setMargin( MARGIN_SIZE ); box3Layout->setSpacing( SPACING_SIZE );
255 myJpegCheck = new QCheckBox( tr( "EXPORT_2_JPEG" ), box3 );
256 myJpegFile = new QLineEdit( box3 );
257 myJpegFile->setMinimumWidth( 300 );
259 myJpegBtn = new QPushButton( "...", box3 );
260 myJpegBtn->setFixedSize( 20, 20 );
262 QLabel* labJpeg = new QLabel( tr( "IMAGE_SIZE" ), box3 );
263 myJpegSize = new QSpinBox( 0, 1280, 10, box3 );
264 myJpegSize->setMinimumWidth( 150 );
266 QFrame* line = new QFrame( box3 );
267 line->setFrameStyle( QFrame::HLine | QFrame::Sunken );
269 myMedCheck = new QCheckBox( tr( "EXPORT_2_MED" ), box3 );
270 myMedFile = new QLineEdit( box3 );
271 myMedFile->setMinimumWidth( 300 );
273 myMedBtn = new QPushButton( "...", box3 );
274 myMedBtn->setFixedSize( 20, 20 );
276 QLabel* labMed = new QLabel( tr( "MESH_SIZE" ), box3 );
277 myMedSize = new QtxDblSpinBox( 0.0, 1000, 1.0, box3);
278 myMedSize->setMinimumWidth( 150 );
280 myVisuCheck = new QCheckBox( tr( "IMPORT_MED_2_VISU" ), box3 );
282 box3Layout->addMultiCellWidget( myJpegCheck, 0, 0, 0, 3 );
283 box3Layout->addMultiCellWidget( myJpegFile, 1, 1, 0, 2 );
284 box3Layout->addWidget( myJpegBtn, 1, 3 );
285 box3Layout->addWidget( labJpeg, 2, 0 );
286 box3Layout->addWidget( myJpegSize, 2, 1 );
287 box3Layout->addMultiCellWidget( line, 3, 3, 0, 3 );
288 box3Layout->addMultiCellWidget( myMedCheck, 4, 4, 0, 3 );
289 box3Layout->addMultiCellWidget( myMedFile, 5, 5, 0, 2 );
290 box3Layout->addWidget( myMedBtn, 5, 3 );
291 box3Layout->addWidget( labMed, 6, 0 );
292 box3Layout->addWidget( myMedSize, 6, 1 );
293 box3Layout->addMultiCellWidget( myVisuCheck, 7, 7, 0, 3 );
296 myProgress = new QProgressBar( this );
297 myProgress->setPercentageVisible( false );
298 myProgress->setFixedHeight( 10 );
301 myStartBtn = new QPushButton( tr( "START_BTN" ), this );
302 myCancelBtn = new QPushButton( tr( "CANCEL_BTN" ), this );
303 QHBoxLayout* btnLayout = new QHBoxLayout;
304 btnLayout->addWidget( myStartBtn );
305 btnLayout->addStretch();
306 btnLayout->addWidget( myCancelBtn );
308 topLayout->addMultiCellWidget( box1, 0, 0, 0, 1 );
309 topLayout->addMultiCellWidget( box2, 1, 1, 0, 1 );
310 topLayout->addWidget( labIter, 2, 0 );
311 topLayout->addWidget( myIter, 2, 1 );
312 topLayout->addMultiCellWidget( box3, 3, 3, 0, 1 );
313 topLayout->addMultiCellWidget( myProgress,4, 4, 0, 1 );
314 topLayout->addMultiCellLayout( btnLayout, 5, 5, 0, 1 );
316 // signals and slots connections
317 connect( myJpegBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
318 connect( myMedBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
319 connect( myStartRandom, SIGNAL( clicked() ), this, SLOT( updateState() ) );
320 connect( myBaseRandom, SIGNAL( clicked() ), this, SLOT( toggled() ) );
321 connect( myBaseDefault, SIGNAL( clicked() ), this, SLOT( toggled() ) );
322 connect( myJpegCheck, SIGNAL( clicked() ), this, SLOT( updateState() ) );
323 connect( myMedCheck, SIGNAL( clicked() ), this, SLOT( updateState() ) );
324 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
325 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( close() ) );
328 myStartX->setValue( 0.0 ); myStartY->setValue( 0.0 );
329 myStartRandom->setChecked( true );
330 myX1->setValue( 0.5 ); myY1->setValue( 1.0 );
331 myX2->setValue( 0.0 ); myY2->setValue( 0.0 );
332 myX3->setValue( 1.0 ); myY3->setValue( 0.0 );
333 myBaseRandom->setChecked( false );
334 myBaseDefault->setChecked( true );
335 myIter->setValue( 1000 );
336 myJpegCheck->setChecked( false );
337 myJpegSize->setValue( 200 );
338 myMedCheck->setChecked( false );
339 myMedSize->setValue( 100 );
340 myVisuCheck->setChecked( true );
347 SierpinskyGUI_RunDlg::~SierpinskyGUI_RunDlg()
359 void SierpinskyGUI_RunDlg::onBrowse()
361 const QObject* snd = sender();
362 bool jpg = snd == myJpegBtn;
363 QString aFileName = SUIT_FileDlg::getFileName( this,
364 jpg ? myJpegFile->text() : myMedFile->text(),
365 QStringList::split( ":", jpg ? tr( "JPEG_FILES") : tr( "MED_FILES" ) ),
366 jpg ? tr( "EXPORT_JPEG_FILE" ) : tr( "EXPORT_MED_FILE" ),
368 if ( !aFileName.isEmpty() ) {
369 jpg ? myJpegFile->setText( aFileName ) : myMedFile->setText( aFileName );
374 * Update widgets state
376 void SierpinskyGUI_RunDlg::updateState()
378 myStartX->setEnabled( !myStartRandom->isChecked() );
379 myStartY->setEnabled( !myStartRandom->isChecked() );
381 myX1->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
382 myY1->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
383 myX2->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
384 myY2->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
385 myX3->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
386 myY3->setEnabled( !myBaseRandom->isChecked() && !myBaseDefault->isChecked() );
388 myJpegFile->setEnabled( myJpegCheck->isChecked() );
389 myJpegBtn->setEnabled( myJpegCheck->isChecked() );
390 myJpegSize->setEnabled( myJpegCheck->isChecked() );
392 myMedFile->setEnabled( myMedCheck->isChecked() );
393 myMedBtn->setEnabled( myMedCheck->isChecked() );
394 myMedSize->setEnabled( myMedCheck->isChecked() );
395 myVisuCheck->setEnabled( myMedCheck->isChecked() );
399 * Toggle 'Base points' mode
401 void SierpinskyGUI_RunDlg::toggled()
403 const QObject* snd = sender();
404 if ( snd == myBaseRandom && myBaseRandom->isChecked() ) myBaseDefault->setChecked( false );
405 if ( snd == myBaseDefault && myBaseDefault->isChecked() ) myBaseRandom->setChecked( false );
410 * Called when user presses 'Start' button
412 void SierpinskyGUI_RunDlg::accept()
414 RANDOMIZER_ORB::RANDOMIZER_var aRandGen = getRandomizerEngine();
415 SIERPINSKY_ORB::SIERPINSKY_var aSierpGen = getSierpinskyEngine();
417 if( CORBA::is_nil( aRandGen ) || CORBA::is_nil( aSierpGen ) ) {
418 SUIT_MessageBox::error1( this,
428 if ( myJpegCheck->isChecked() ) {
429 aJPEGFile = myJpegFile->text().stripWhiteSpace();
430 aJPEGSize = myJpegSize->value();
431 if ( aJPEGFile.isEmpty() || aJPEGSize <= 0 ) {
432 int res = SUIT_MessageBox::warn2( this,
434 tr( "JPEG_PARAMETERS_ERROR" ),
435 tr( "YES_BTN"), tr( "NO_BTN" ), 0, 1, 0 );
443 if ( myMedCheck->isChecked() ) {
444 aMEDFile = myMedFile->text().stripWhiteSpace();
445 aMEDSize = myMedSize->value();
446 if ( aMEDFile.isEmpty() || aMEDSize <= 0 ) {
447 int res = SUIT_MessageBox::warn2( this,
449 tr( "MED_PARAMETERS_ERROR" ),
450 tr( "YES_BTN"), tr( "NO_BTN" ), 0, 1, 0 );
456 double x = myStartX->value();
457 double y = myStartY->value();
458 if ( myStartRandom->isChecked() )
459 aRandGen->InitPoint( x, y );
462 if ( myBaseDefault->isChecked() ) {
463 aSierpGen->Reset(); // use default values from engine
466 double x1 = myX1->value();
467 double y1 = myY1->value();
468 double x2 = myX2->value();
469 double y2 = myY2->value();
470 double x3 = myX3->value();
471 double y3 = myY3->value();
472 if ( myBaseRandom->isChecked() ) {
473 aRandGen->InitPoint( x1, y1 );
474 aRandGen->InitPoint( x2, y2 );
475 aRandGen->InitPoint( x3, y3 );
477 aSierpGen->Init( x1, y1, x2, y2, x3, y3 );
481 int iter = myIter->value();
484 myProgress->setTotalSteps( iter );
485 myProgress->setProgress( 0 );
487 myThread = new MyThread( this, x, y, iter );
489 myThread->init( x, y, iter );
490 disconnect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
491 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( stop() ) );
492 myStartBtn->setText( tr( "STOP_BTN" ) );
493 box1->setEnabled( false );
494 box2->setEnabled( false );
495 box3->setEnabled( false );
496 myIter->setEnabled( false );
500 * Called when user presses 'Stop' button
502 void SierpinskyGUI_RunDlg::stop()
505 disconnect( myStartBtn, SIGNAL( clicked() ), this, SLOT( stop() ) );
506 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
507 myStartBtn->setText( tr( "START_BTN" ) );
508 myProgress->setProgress( 0 );
509 box1->setEnabled( true );
510 box2->setEnabled( true );
511 box3->setEnabled( true );
512 myIter->setEnabled( true );
516 * Progress thread calls this method to update progress bar state
518 void SierpinskyGUI_RunDlg::nextStep( const int step )
521 disconnect( myStartBtn, SIGNAL( clicked() ), this, SLOT( stop() ) );
522 connect( myStartBtn, SIGNAL( clicked() ), this, SLOT( accept() ) );
523 myStartBtn->setText( tr( "START_BTN" ) );
524 myProgress->setProgress( 0 );
525 box1->setEnabled( true );
526 box2->setEnabled( true );
527 box3->setEnabled( true );
528 myIter->setEnabled( true );
531 if ( myJpegCheck->isChecked() ) {
532 QString aJPEGFile = myJpegFile->text().stripWhiteSpace();
533 int aJPEGSize = myJpegSize->value();
534 if ( !aJPEGFile.isEmpty() && aJPEGSize > 0 ) {
535 bool res = getSierpinskyEngine()->ExportToJPEG( aJPEGFile.latin1(), aJPEGSize );
537 // error: can't create JPEG image
542 if ( myMedCheck->isChecked() ) {
543 QString aMEDFile = myMedFile->text().stripWhiteSpace();
544 double aMEDSize = myMedSize->value();
545 if ( !aMEDFile.isEmpty() && aMEDSize > 0 ) {
546 bool res = getSierpinskyEngine()->ExportToMED( aMEDFile.latin1(), aMEDSize );
547 if ( res && myVisuCheck->isChecked() ) {
548 VISU::VISU_Gen_var aVisuGen = getVisuEngine();
549 if ( !CORBA::is_nil( aVisuGen ) ) {
550 VISU::Result_var aResult = aVisuGen->ImportFile( aMEDFile.latin1() );
551 if ( !CORBA::is_nil( aResult ) )
552 aVisuGen->MeshOnEntity( aResult, "Sierpinsky", VISU::NODE );
553 myModule->getApp()->updateObjectBrowser( true );
560 myProgress->setProgress( myProgress->totalSteps() - step );
565 * Process progress event
567 void SierpinskyGUI_RunDlg::customEvent( QCustomEvent* e )
569 if ( e->type() == MY_EVENT ) {
570 MyEvent* me = (MyEvent*)e;
571 nextStep( me->iter() );