]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_BuildProgressDlg.cxx
Salome HOME
1ac19ffdd07fcb7e0d5cdeb5d9b58a369f46edb0
[modules/visu.git] / src / VISUGUI / VisuGUI_BuildProgressDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  VISU VISUGUI : GUI of VISU component
23 //  File   : VisuGUI_BuildProgressDlg.cxx
24 //  Author : Oleg UVAROV
25 //  Module : VISU
26 //
27 #include "VisuGUI_BuildProgressDlg.h"
28
29 #include "VisuGUI.h"
30 #include "VisuGUI_Tools.h"
31
32 #include "SUIT_FileDlg.h"
33 #include "SUIT_Session.h"
34 #include "SUIT_MessageBox.h"
35 #include "SUIT_ResourceMgr.h"
36
37 #include "LightApp_Application.h"
38
39 #include <vtkTimerLog.h>
40
41 #include <QCheckBox>
42 #include <QGroupBox>
43 #include <QLabel>
44 #include <QLayout>
45 #include <QLCDNumber>
46 #include <QLineEdit>
47 #include <QPushButton>
48 #include <QTimer>
49 #include <QKeyEvent>
50
51 /*!
52  * Constructor
53  */
54 VisuGUI_BuildProgressDlg::VisuGUI_BuildProgressDlg( QWidget* theParent ):
55   QDialog( theParent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint),
56   myIsRaiseColor( false )
57 {
58   setAttribute( Qt::WA_DeleteOnClose );
59   setWindowTitle( tr( "DLG_BUILD_PROGRESS_TITLE" ) );
60
61   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
62
63   QVBoxLayout* aTopLayout = new QVBoxLayout( this );
64   aTopLayout->setSpacing( 6 );
65   aTopLayout->setMargin( 6 );
66   //aTopLayout->setAutoAdd( true );
67
68   // Settings
69   mySettingsBox = new QGroupBox( tr( "IMPORT_SETTINGS" ), this );
70   aTopLayout->addWidget( mySettingsBox );
71   //mySettingsBox->setColumnLayout( 0, Qt::Vertical );
72   //mySettingsBox->layout()->setSpacing( 0 );
73   //mySettingsBox->layout()->setMargin( 0 );
74
75   QGridLayout* aSettingsLayout = new QGridLayout( mySettingsBox );
76   aSettingsLayout->setSpacing( 6 );
77   aSettingsLayout->setMargin( 11 );
78
79   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), mySettingsBox );
80   myFileNameLineEdit = new QLineEdit( mySettingsBox );
81   myFileNameLineEdit->setMinimumWidth( 250 );
82   myFileNameLineEdit->setReadOnly( true );
83   /*
84   QPushButton* aFileNameButton = new QPushButton( mySettingsBox );
85   aFileNameButton->setAutoDefault( false );
86   aFileNameButton->setPixmap( aResourceMgr->loadPixmap( "VISU", tr( "ICON_LOAD_TEXTURE" ) ) );
87   connect( aFileNameButton, SIGNAL( clicked() ), this, SLOT( onBrowseFile() ) );
88   */
89   myBuildAllCheckBox = new QCheckBox( tr( "BUILD_ALL" ), mySettingsBox );
90   myBuildAllCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "full_med_loading", false ) );
91   connect( myBuildAllCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
92
93   myBuildAtOnceCheckBox = new QCheckBox( tr( "BUILD_AT_ONCE" ), mySettingsBox );
94   myBuildAtOnceCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_at_once", false ) );
95   connect( myBuildAtOnceCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
96
97   myCloseCheckBox = new QCheckBox( tr( "CLOSE_AT_FINISH" ), mySettingsBox );
98   myCloseCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "close_at_finish", true ) );
99
100   aSettingsLayout->addWidget(  aFileNameLabel,       0, 0 );
101   aSettingsLayout->addWidget( myFileNameLineEdit,    1, 0, 1, 2 );
102   //aSettingsLayout->addWidget(  aFileNameButton,      1, 2 );
103   aSettingsLayout->addWidget( myBuildAllCheckBox,    2, 0 );
104   aSettingsLayout->addWidget( myBuildAtOnceCheckBox, 3, 0 );
105   aSettingsLayout->addWidget( myCloseCheckBox,       4, 0 );
106
107   // Progress
108   myProgressBox = new QGroupBox( tr( "IMPORT_PROGRESS" ), this );
109   aTopLayout->addWidget( myProgressBox );
110   //myProgressBox->setColumnLayout( 0, Qt::Vertical );
111   //myProgressBox->layout()->setSpacing( 0 );
112   //myProgressBox->layout()->setMargin( 0 );
113
114   QGridLayout* aProgressLayout = new QGridLayout( myProgressBox );
115   aProgressLayout->setSpacing( 6 );
116   aProgressLayout->setMargin( 11 );
117
118   QLabel* aBuildEntitiesLabel = new QLabel( tr( "BUILD_ENTITIES" ), myProgressBox );
119   myBuildEntitiesButton = new QPushButton( myProgressBox );
120   myBuildEntitiesButton->setEnabled( false );
121   myBuildEntitiesButton->setFixedSize( 30, 30 );
122   QPalette aPal = myBuildEntitiesButton->palette();
123   aPal.setColor( myBuildEntitiesButton->backgroundRole(), Qt::red );
124   myBuildEntitiesButton->setPalette( aPal );
125
126   myBuildFieldsCheckBox = new QCheckBox( tr( "BUILD_FIELDS" ), myProgressBox );
127   myBuildFieldsCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_fields", true ) );
128   myBuildFieldsButton = new QPushButton( myProgressBox );
129   myBuildFieldsButton->setEnabled( false );
130   myBuildFieldsButton->setFixedSize( 30, 30 );
131   aPal = myBuildFieldsButton->palette();
132   aPal.setColor( myBuildFieldsButton->backgroundRole(), myBuildFieldsCheckBox->isChecked() ? Qt::red : Qt::gray );
133   myBuildFieldsButton->setPalette( aPal );
134
135   connect( myBuildFieldsCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
136
137   myBuildMinMaxCheckBox = new QCheckBox( tr( "BUILD_MINMAX" ), myProgressBox );
138   myBuildMinMaxCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_min_max", true ) );
139   myBuildMinMaxButton = new QPushButton( myProgressBox );
140   myBuildMinMaxButton->setEnabled( false );
141   myBuildMinMaxButton->setFixedSize( 30, 30 );
142   aPal = myBuildMinMaxButton->palette();
143   aPal.setColor( myBuildMinMaxButton->backgroundRole(), myBuildMinMaxCheckBox->isChecked() ? Qt::red : Qt::gray );
144   myBuildMinMaxButton->setPalette( aPal );
145   
146   connect( myBuildMinMaxCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
147
148   myBuildGroupsCheckBox = new QCheckBox( tr( "BUILD_GROUPS" ), myProgressBox );
149   myBuildGroupsCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_groups", true ) );
150   myBuildGroupsButton = new QPushButton( myProgressBox );
151   myBuildGroupsButton->setEnabled( false );
152   myBuildGroupsButton->setFixedSize( 30, 30 );
153   aPal = myBuildGroupsButton->palette();
154   aPal.setColor( myBuildGroupsButton->backgroundRole(), myBuildGroupsCheckBox->isChecked() ? Qt::red : Qt::gray );
155   myBuildGroupsButton->setPalette( aPal );
156
157   connect( myBuildGroupsCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
158
159   aProgressLayout->addWidget( aBuildEntitiesLabel, 0, 0 );
160   aProgressLayout->addWidget( myBuildEntitiesButton,   0, 1 );
161   aProgressLayout->addWidget( myBuildFieldsCheckBox,   1, 0 );
162   aProgressLayout->addWidget( myBuildFieldsButton,     1, 1 );
163   aProgressLayout->addWidget( myBuildMinMaxCheckBox,   2, 0 );
164   aProgressLayout->addWidget( myBuildMinMaxButton,     2, 1 );
165   aProgressLayout->addWidget( myBuildGroupsCheckBox,   3, 0 );
166   aProgressLayout->addWidget( myBuildGroupsButton,     3, 1 );
167
168   // Time
169   myTime = QTime( 0, 0, 0, 0 );
170
171   myTimeBox = new QGroupBox( tr( "IMPORT_TIME" ), this );
172   aTopLayout->addWidget( myTimeBox );
173   //myTimeBox->setColumnLayout( 0, Qt::Vertical );
174   //myTimeBox->layout()->setSpacing( 0 );
175   //myTimeBox->layout()->setMargin( 0 );
176
177   QGridLayout* aTimeLayout = new QGridLayout( myTimeBox );
178   aTimeLayout->setSpacing( 6 );
179   aTimeLayout->setMargin( 11 );
180
181   QLabel* aTimeLabel = new QLabel( tr( "TIME" ), myTimeBox );
182
183   myTimeLCDNumber = new QLCDNumber( myTimeBox );
184   myTimeLCDNumber->setSegmentStyle( QLCDNumber::Filled );
185   myTimeLCDNumber->setStyleSheet( "color: white; background-color: black" );
186   //QPalette aPal = myTimeLCDNumber->palette();
187   //aPal.setColor( myTimeLCDNumber->backgroundRole(), Qt::black );
188   //aPal.setColor( myTimeLCDNumber->foregroundRole(), Qt::white );
189   //myTimeLCDNumber->setPalette( aPal );
190     
191   myTimeLCDNumber->setNumDigits( 8 );
192   myTimeLCDNumber->display( myTime.toString( "hh:mm:ss.zzz" ) );
193
194   aTimeLayout->addWidget(  aTimeLabel,     0, 0 );
195   aTimeLayout->addWidget( myTimeLCDNumber, 0, 1 );
196
197   // Start / Close
198   QGroupBox* CommonGroup = new QGroupBox( this );
199   aTopLayout->addWidget( CommonGroup );
200   //CommonGroup->setColumnLayout(0, Qt::Vertical );
201   //CommonGroup->layout()->setSpacing( 0 );
202   //CommonGroup->layout()->setMargin( 0 );
203   QGridLayout* CommonGroupLayout = new QGridLayout( CommonGroup );
204   CommonGroupLayout->setAlignment( Qt::AlignTop );
205   CommonGroupLayout->setSpacing( 6 );
206   CommonGroupLayout->setMargin( 11 );
207
208   myStartButton = new QPushButton( tr( "START" ), CommonGroup );
209   myStartButton->setAutoDefault( true );
210   myStartButton->setDefault( true );
211   CommonGroupLayout->addWidget( myStartButton, 0, 0 );
212   CommonGroupLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
213
214   QPushButton* aCloseButton = new QPushButton( tr( "CLOSE" ), CommonGroup );
215   aCloseButton->setAutoDefault( true );
216   CommonGroupLayout->addWidget( aCloseButton, 0, 2 );
217
218   QPushButton* aHelpButton = new QPushButton( tr( "HELP" ), CommonGroup );
219   aHelpButton->setAutoDefault( true );
220   CommonGroupLayout->addWidget( aHelpButton, 0, 3 );
221
222   connect( myStartButton, SIGNAL( clicked() ), this, SLOT( onStart() ) );
223   connect(  aCloseButton, SIGNAL( clicked() ), this, SLOT( onClose() ) );
224   connect(  aHelpButton,  SIGNAL( clicked() ), this, SLOT( onHelp() ) );
225
226   myTimer = new QTimer( this );
227   connect( myTimer, SIGNAL( timeout() ), this, SLOT( onTimer() ) );
228 }
229
230 VisuGUI_BuildProgressDlg::~VisuGUI_BuildProgressDlg()
231 {
232 }
233
234 void VisuGUI_BuildProgressDlg::show()
235 {
236   if( !myFileName.isNull() || onBrowseFile() )
237     QWidget::show();
238 }
239
240 void VisuGUI_BuildProgressDlg::onStart()
241 {
242   if( myFileName.isNull() )
243     return;
244
245   myResult = myGenerator->CreateResult( (const char*)myFileName.toLatin1() );
246
247   if (CORBA::is_nil(myResult.in())) {
248     SUIT_MessageBox::warning(this,
249                              tr("WRN_VISU"),
250                              tr("ERR_ERROR_IN_THE_FILE"),
251                              tr("BUT_OK"));
252     onClose();
253   }else{
254     myResult->SetBuildFields( myBuildFieldsCheckBox->isChecked(), myBuildMinMaxCheckBox->isChecked() );
255     myResult->SetBuildGroups( myBuildGroupsCheckBox->isChecked() );
256     
257     //setModal( false );
258     myFileNameLineEdit->setReadOnly( true );
259     myStartButton->setEnabled( false );
260     
261     bool aBuildAtOnce = myBuildAtOnceCheckBox->isChecked();
262     if( aBuildAtOnce )
263     {
264       QApplication::setOverrideCursor( Qt::WaitCursor );
265       myCurrentTime = vtkTimerLog::GetCurrentTime();
266     }
267
268     myTime.setHMS( 0, 0, 0 );
269     myTimer->start( 100 );
270     
271     bool aBuildAll = myBuildAllCheckBox->isChecked();
272     myResult->Build( aBuildAll, aBuildAtOnce );
273   }
274 }
275
276 void VisuGUI_BuildProgressDlg::onClose()
277 {
278   done( 0 );
279 }
280
281 void VisuGUI_BuildProgressDlg::onHelp()
282 {
283   QString aHelpFileName = "importing_med_objects_page.html"; // ?
284   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
285   if (app) {
286     VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>( app->activeModule() );
287     app->onHelpContextModule(aVisuGUI ? app->moduleName(aVisuGUI->moduleName()) : QString(""), aHelpFileName);
288   }
289   else {
290                 QString platform;
291 #ifdef WIN32
292                 platform = "winapplication";
293 #else
294                 platform = "application";
295 #endif
296     SUIT_MessageBox::warning(0, ("WRN_WARNING"),
297                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
298                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
299                              tr("BUT_OK"));
300   }
301 }
302
303 bool VisuGUI_BuildProgressDlg::onBrowseFile()
304 {
305   QString aRootDir = QString( getenv( "VISU_ROOT_DIR") );
306
307   QStringList aFilter;
308   aFilter.append( tr( "FLT_MED_FILES" ) );
309   aFilter.append( tr( "FLT_ALL_FILES" ) );
310
311   QFileInfo aFileInfo = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_FROM_FILE" ), true );
312   QString aFileName = aFileInfo.filePath();
313
314   if( aFileName.isNull() )
315     return false;
316
317   myFileName = aFileName;
318   myFileNameLineEdit->setText( aFileName.section( '/', -1 ) );
319
320   return true;
321 }
322
323 void VisuGUI_BuildProgressDlg::onTimer()
324 {
325   try {
326     bool aBuildAtOnce = myBuildAtOnceCheckBox->isChecked();
327
328     if( !aBuildAtOnce )
329     {
330       myTime = myTime.addMSecs( 100 );
331       if( myTime.minute() > 9 && myTimeLCDNumber->numDigits() < 9 )
332         myTimeLCDNumber->setNumDigits( 9 );
333       myTimeLCDNumber->display( myTime.toString( "m:ss.zzz" ) );
334
335       bool isEntitiesDone = myResult->IsEntitiesDone();
336       bool isFieldsDone = myResult->IsFieldsDone();
337       bool isMinMaxDone = myResult->IsMinMaxDone();
338       bool isGroupsDone = myResult->IsGroupsDone();
339
340       updateButton( myBuildEntitiesButton, isEntitiesDone );
341       updateButton( myBuildFieldsButton, isFieldsDone );
342       updateButton( myBuildMinMaxButton, isMinMaxDone );
343       updateButton( myBuildGroupsButton, isGroupsDone );
344     }
345
346     if( myResult->IsDone() )
347     {
348       myTimer->stop();
349
350       if( aBuildAtOnce )
351       {
352         updateButton( myBuildEntitiesButton, true );
353         updateButton( myBuildFieldsButton, true );
354         updateButton( myBuildMinMaxButton, true );
355         updateButton( myBuildGroupsButton, true );
356
357         QApplication::restoreOverrideCursor();
358
359         QTime aTime;
360         int mSecs = ( int )( 1000 * ( vtkTimerLog::GetCurrentTime() - myCurrentTime ) );
361         aTime = aTime.addMSecs( mSecs );
362         if( aTime.minute() > 9 )
363           myTimeLCDNumber->setNumDigits( 9 );
364         myTimeLCDNumber->display( aTime.toString( "m:ss.zzz" ) );
365       }
366
367       if( myCloseCheckBox->isChecked() )
368         done( 0 );
369       else
370         SUIT_MessageBox::warning( this,
371                                   tr( "INF_VISU" ),
372                                   tr( "IMPORT_DONE" ),
373                                   tr( "BUT_OK" ) );
374     }
375   }
376   catch( ... ) {
377     done( 1 );
378   }
379 }
380
381 void VisuGUI_BuildProgressDlg::updateButton( QPushButton* theButton, bool theIsDone )
382 {
383   QPalette aPal = theButton->palette();
384   QColor aCurrentColor = aPal.color( theButton->backgroundRole() );
385   if( aCurrentColor == Qt::gray || aCurrentColor == Qt::green )
386     return;
387
388   QColor aNewColor = Qt::green;
389
390   if( !theIsDone )
391   {
392     int r, g, b;
393     aCurrentColor.getRgb( &r, &g, &b );
394     if( g == 0 )
395       myIsRaiseColor = true;
396     else if( g == 255 )
397       myIsRaiseColor = false;
398
399     int gNew = g + ( myIsRaiseColor ? 1 : -1 ) * 51;
400
401     aNewColor.setRgb( 255, gNew, 0 );    
402   }
403   aPal.setColor( theButton->backgroundRole(), aNewColor );
404   theButton->setPalette( aPal );
405 }
406
407 void VisuGUI_BuildProgressDlg::done( int r )
408 {
409   QApplication::restoreOverrideCursor();
410   myTimer->stop();
411   QDialog::done( r );
412 }
413
414 void VisuGUI_BuildProgressDlg::onBuildCheckBoxClicked()
415 {
416   QCheckBox* aCheckBox = ( QCheckBox* )sender();
417   if( !aCheckBox )
418     return;
419
420   bool anIsChecked = aCheckBox->isChecked();
421
422   QColor aColor = anIsChecked ? Qt::red : Qt::gray;
423
424   if( aCheckBox == myBuildAllCheckBox )
425   {
426     if( anIsChecked && !myBuildAtOnceCheckBox->isChecked() )
427       myBuildAtOnceCheckBox->animateClick();
428   }
429   else if( aCheckBox == myBuildAtOnceCheckBox )
430   {
431     if( !anIsChecked && myBuildAllCheckBox->isChecked() )
432       myBuildAllCheckBox->animateClick();
433   }
434   else if( aCheckBox == myBuildFieldsCheckBox )
435   {
436     QPalette aPal = myBuildFieldsButton->palette();
437     aPal.setColor( myBuildFieldsButton->backgroundRole(), aColor );
438     myBuildFieldsButton->setPalette( aPal );
439     if( !anIsChecked && myBuildMinMaxCheckBox->isChecked() )
440       myBuildMinMaxCheckBox->animateClick();
441   }
442   else if( aCheckBox == myBuildMinMaxCheckBox )
443   {
444     QPalette aPal = myBuildMinMaxButton->palette();
445     aPal.setColor( myBuildMinMaxButton->backgroundRole(), aColor );
446     myBuildMinMaxButton->setPalette( aPal );
447     if( anIsChecked && !myBuildFieldsCheckBox->isChecked() )
448       myBuildFieldsCheckBox->animateClick();
449   }
450   else if( aCheckBox == myBuildGroupsCheckBox )
451   {
452     QPalette aPal = myBuildGroupsButton->palette();
453     aPal.setColor( myBuildGroupsButton->backgroundRole(), aColor );
454     myBuildGroupsButton->setPalette( aPal );
455   }
456
457 }
458
459 void VisuGUI_BuildProgressDlg::setFileName( const QString& theFileName )
460 {
461   if ( !theFileName.isNull() )
462     {
463       myFileName = theFileName;
464       myFileNameLineEdit->setText( myFileName.section( '/', -1 ) );
465     }
466 }
467
468 void VisuGUI_BuildProgressDlg::keyPressEvent( QKeyEvent* e )
469 {
470   QDialog::keyPressEvent( e );
471   if ( e->isAccepted() )
472     return;
473
474   if ( e->key() == Qt::Key_F1 )
475     {
476       e->accept();
477       onHelp();
478     }
479 }