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