Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISUGUI / VisuGUI_BuildProgressDlg.cxx
1 //  VISU VISUGUI : GUI of VISU 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
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.h>
43 #include <qgroupbox.h>
44 #include <qlabel.h>
45 #include <qlayout.h>
46 #include <qlcdnumber.h>
47 #include <qlineedit.h>
48 #include <qpushbutton.h>
49 #include <qtimer.h>
50
51 /*!
52  * Constructor
53  */
54 VisuGUI_BuildProgressDlg::VisuGUI_BuildProgressDlg( QWidget* theParent ):
55   QDialog( theParent, 
56            "VisuGUI_BuildProgressBar", 
57            false,
58            WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
59   myIsRaiseColor( false )
60 {
61   setCaption( tr( "DLG_BUILD_PROGRESS_TITLE" ) );
62
63   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
64
65   QVBoxLayout* aTopLayout = new QVBoxLayout( this );
66   aTopLayout->setSpacing( 6 );
67   aTopLayout->setMargin( 6 );
68   aTopLayout->setAutoAdd( true );
69
70   // Settings
71   mySettingsBox = new QGroupBox( tr( "IMPORT_SETTINGS" ), this );
72   mySettingsBox->setColumnLayout( 0, Qt::Vertical );
73   mySettingsBox->layout()->setSpacing( 0 );
74   mySettingsBox->layout()->setMargin( 0 );
75
76   QGridLayout* aSettingsLayout = new QGridLayout( mySettingsBox->layout() );
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->addMultiCellWidget( myFileNameLineEdit, 1, 1, 0, 1 );
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   myProgressBox->setColumnLayout( 0, Qt::Vertical );
111   myProgressBox->layout()->setSpacing( 0 );
112   myProgressBox->layout()->setMargin( 0 );
113
114   QGridLayout* aProgressLayout = new QGridLayout( myProgressBox->layout() );
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   myBuildEntitiesButton->setPaletteBackgroundColor( Qt::red );
123
124   myBuildFieldsCheckBox = new QCheckBox( tr( "BUILD_FIELDS" ), myProgressBox );
125   myBuildFieldsCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_fields", true ) );
126   myBuildFieldsButton = new QPushButton( myProgressBox );
127   myBuildFieldsButton->setEnabled( false );
128   myBuildFieldsButton->setFixedSize( 30, 30 );
129   myBuildFieldsButton->setPaletteBackgroundColor( Qt::red );
130   connect( myBuildFieldsCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
131
132   myBuildMinMaxCheckBox = new QCheckBox( tr( "BUILD_MINMAX" ), myProgressBox );
133   myBuildMinMaxCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_min_max", true ) );
134   myBuildMinMaxButton = new QPushButton( myProgressBox );
135   myBuildMinMaxButton->setEnabled( false );
136   myBuildMinMaxButton->setFixedSize( 30, 30 );
137   myBuildMinMaxButton->setPaletteBackgroundColor( Qt::red );
138   connect( myBuildMinMaxCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
139
140   myBuildGroupsCheckBox = new QCheckBox( tr( "BUILD_GROUPS" ), myProgressBox );
141   myBuildGroupsCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_groups", true ) );
142   myBuildGroupsButton = new QPushButton( myProgressBox );
143   myBuildGroupsButton->setEnabled( false );
144   myBuildGroupsButton->setFixedSize( 30, 30 );
145   myBuildGroupsButton->setPaletteBackgroundColor( Qt::red );
146   connect( myBuildGroupsCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
147
148   aProgressLayout->addWidget( aBuildEntitiesLabel, 0, 0 );
149   aProgressLayout->addWidget( myBuildEntitiesButton,   0, 1 );
150   aProgressLayout->addWidget( myBuildFieldsCheckBox,   1, 0 );
151   aProgressLayout->addWidget( myBuildFieldsButton,     1, 1 );
152   aProgressLayout->addWidget( myBuildMinMaxCheckBox,   2, 0 );
153   aProgressLayout->addWidget( myBuildMinMaxButton,     2, 1 );
154   aProgressLayout->addWidget( myBuildGroupsCheckBox,   3, 0 );
155   aProgressLayout->addWidget( myBuildGroupsButton,     3, 1 );
156
157   // Time
158   myTimeBox = new QGroupBox( tr( "IMPORT_TIME" ), this );
159   myTimeBox->setColumnLayout( 0, Qt::Vertical );
160   myTimeBox->layout()->setSpacing( 0 );
161   myTimeBox->layout()->setMargin( 0 );
162
163   QGridLayout* aTimeLayout = new QGridLayout( myTimeBox->layout() );
164   aTimeLayout->setSpacing( 6 );
165   aTimeLayout->setMargin( 11 );
166
167   QLabel* aTimeLabel = new QLabel( tr( "TIME" ), myTimeBox );
168
169   myTimeLCDNumber = new QLCDNumber( myTimeBox );
170   myTimeLCDNumber->setSegmentStyle( QLCDNumber::Filled );
171   myTimeLCDNumber->setPaletteBackgroundColor( Qt::black );
172   myTimeLCDNumber->setPaletteForegroundColor( Qt::white );
173   myTimeLCDNumber->setNumDigits( 8 );
174   myTimeLCDNumber->display( myTime.toString( "hh:mm:ss.zzz" ) );
175
176   aTimeLayout->addWidget(  aTimeLabel,     0, 0 );
177   aTimeLayout->addWidget( myTimeLCDNumber, 0, 1 );
178
179   // Start / Close
180   QGroupBox* CommonGroup = new QGroupBox( this );
181   CommonGroup->setColumnLayout(0, Qt::Vertical );
182   CommonGroup->layout()->setSpacing( 0 );
183   CommonGroup->layout()->setMargin( 0 );
184   QGridLayout* CommonGroupLayout = new QGridLayout( CommonGroup->layout() );
185   CommonGroupLayout->setAlignment( Qt::AlignTop );
186   CommonGroupLayout->setSpacing( 6 );
187   CommonGroupLayout->setMargin( 11 );
188
189   myStartButton = new QPushButton( tr( "START" ), CommonGroup );
190   myStartButton->setAutoDefault( true );
191   myStartButton->setDefault( true );
192   CommonGroupLayout->addWidget( myStartButton, 0, 0 );
193   CommonGroupLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
194
195   QPushButton* aCloseButton = new QPushButton( tr( "CLOSE" ), CommonGroup );
196   aCloseButton->setAutoDefault( true );
197   CommonGroupLayout->addWidget( aCloseButton, 0, 2 );
198
199   QPushButton* aHelpButton = new QPushButton( tr( "HELP" ), CommonGroup );
200   aHelpButton->setAutoDefault( true );
201   CommonGroupLayout->addWidget( aHelpButton, 0, 3 );
202
203   connect( myStartButton, SIGNAL( clicked() ), this, SLOT( onStart() ) );
204   connect(  aCloseButton, SIGNAL( clicked() ), this, SLOT( onClose() ) );
205   connect(  aHelpButton,  SIGNAL( clicked() ), this, SLOT( onHelp() ) );
206
207   myTimer = new QTimer( this );
208   connect( myTimer, SIGNAL( timeout() ), this, SLOT( onTimer() ) );
209 }
210
211 VisuGUI_BuildProgressDlg::~VisuGUI_BuildProgressDlg()
212 {
213 }
214
215 void VisuGUI_BuildProgressDlg::show()
216 {
217   if( !myFileName.isNull() || onBrowseFile() )
218     QWidget::show();
219 }
220
221 void VisuGUI_BuildProgressDlg::onStart()
222 {
223   if( myFileName.isNull() )
224     return;
225
226   myResult = myGenerator->CreateResult( myFileName );
227
228   if (CORBA::is_nil(myResult.in())) {
229     SUIT_MessageBox::warn1(this,
230                            tr("WRN_VISU"),
231                            tr("ERR_ERROR_IN_THE_FILE"),
232                            tr("BUT_OK"));
233     onClose();
234   }else{
235     myResult->SetBuildFields( myBuildFieldsCheckBox->isChecked(), myBuildMinMaxCheckBox->isChecked() );
236     myResult->SetBuildGroups( myBuildGroupsCheckBox->isChecked() );
237     
238     //setModal( false );
239     myFileNameLineEdit->setReadOnly( true );
240     myStartButton->setEnabled( false );
241     
242     bool aBuildAtOnce = myBuildAtOnceCheckBox->isChecked();
243     if( aBuildAtOnce )
244     {
245       QApplication::setOverrideCursor( Qt::waitCursor );
246       myCurrentTime = vtkTimerLog::GetCurrentTime();
247     }
248
249     myTime.setHMS( 0, 0, 0 );
250     myTimer->start( 100 );
251     
252     bool aBuildAll = myBuildAllCheckBox->isChecked();
253     myResult->Build( aBuildAll, aBuildAtOnce );
254   }
255 }
256
257 void VisuGUI_BuildProgressDlg::onClose()
258 {
259   done( 0 );
260 }
261
262 void VisuGUI_BuildProgressDlg::onHelp()
263 {
264   QString aHelpFileName = "importing_med_objects_page.html"; // ?
265   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
266   if (app) {
267     VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>( app->activeModule() );
268     app->onHelpContextModule(aVisuGUI ? app->moduleName(aVisuGUI->moduleName()) : QString(""), aHelpFileName);
269   }
270   else {
271                 QString platform;
272 #ifdef WIN32
273                 platform = "winapplication";
274 #else
275                 platform = "application";
276 #endif
277     SUIT_MessageBox::warn1(0, QObject::tr("WRN_WARNING"),
278                            QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
279                            arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
280                            QObject::tr("BUT_OK"));
281   }
282 }
283
284 bool VisuGUI_BuildProgressDlg::onBrowseFile()
285 {
286   QString aRootDir = QString( getenv( "VISU_ROOT_DIR") );
287
288   QStringList aFilter;
289   aFilter.append( tr( "FLT_MED_FILES" ) );
290   aFilter.append( tr( "FLT_ALL_FILES" ) );
291
292   QFileInfo aFileInfo = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_FROM_FILE" ), true );
293   QString aFileName = aFileInfo.filePath();
294
295   if( aFileName.isNull() )
296     return false;
297
298   myFileName = aFileName;
299   myFileNameLineEdit->setText( aFileName.section( '/', -1 ) );
300
301   return true;
302 }
303
304 void VisuGUI_BuildProgressDlg::onTimer()
305 {
306   try {
307     bool aBuildAtOnce = myBuildAtOnceCheckBox->isChecked();
308
309     if( !aBuildAtOnce )
310     {
311       myTime = myTime.addMSecs( 100 );
312       if( myTime.minute() > 9 && myTimeLCDNumber->numDigits() < 9 )
313         myTimeLCDNumber->setNumDigits( 9 );
314       myTimeLCDNumber->display( myTime.toString( "m:ss.zzz" ) );
315
316       bool isEntitiesDone = myResult->IsEntitiesDone();
317       bool isFieldsDone = myResult->IsFieldsDone();
318       bool isMinMaxDone = myResult->IsMinMaxDone();
319       bool isGroupsDone = myResult->IsGroupsDone();
320
321       updateButton( myBuildEntitiesButton, isEntitiesDone );
322       updateButton( myBuildFieldsButton, isFieldsDone );
323       updateButton( myBuildMinMaxButton, isMinMaxDone );
324       updateButton( myBuildGroupsButton, isGroupsDone );
325     }
326
327     if( myResult->IsDone() )
328     {
329       myTimer->stop();
330
331       if( aBuildAtOnce )
332       {
333         QApplication::restoreOverrideCursor();
334
335         QTime aTime;
336         int mSecs = ( int )( 1000 * ( vtkTimerLog::GetCurrentTime() - myCurrentTime ) );
337         aTime = aTime.addMSecs( mSecs );
338         if( aTime.minute() > 9 )
339           myTimeLCDNumber->setNumDigits( 9 );
340         myTimeLCDNumber->display( aTime.toString( "m:ss.zzz" ) );
341       }
342
343       if( myCloseCheckBox->isChecked() )
344         done( 0 );
345       else
346         SUIT_MessageBox::warn1( this, tr( "INF_VISU" ), tr( "IMPORT_DONE" ), tr( "BUT_OK" ) );
347     }
348   }
349   catch( ... ) {
350     done( 1 );
351   }
352 }
353
354 void VisuGUI_BuildProgressDlg::updateButton( QPushButton* theButton, bool theIsDone )
355 {
356   QColor aCurrentColor = theButton->paletteBackgroundColor();
357   if( aCurrentColor == Qt::gray || aCurrentColor == Qt::green )
358     return;
359
360   QColor aNewColor = Qt::green;
361
362   if( !theIsDone )
363   {
364     int r, g, b;
365     aCurrentColor.getRgb( &r, &g, &b );
366     if( g == 0 )
367       myIsRaiseColor = true;
368     else if( g == 255 )
369       myIsRaiseColor = false;
370
371     int gNew = g + ( myIsRaiseColor ? 1 : -1 ) * 51;
372
373     aNewColor.setRgb( 255, gNew, 0 );
374   }
375
376   theButton->setPaletteBackgroundColor( aNewColor );
377 }
378
379 void VisuGUI_BuildProgressDlg::done( int r )
380 {
381   QApplication::restoreOverrideCursor();
382   myTimer->stop();
383   QDialog::done( r );
384 }
385
386 void VisuGUI_BuildProgressDlg::onBuildCheckBoxClicked()
387 {
388   QCheckBox* aCheckBox = ( QCheckBox* )sender();
389   if( !aCheckBox )
390     return;
391
392   bool anIsChecked = aCheckBox->isChecked();
393
394   QColor aColor = anIsChecked ? Qt::red : Qt::gray;
395
396   if( aCheckBox == myBuildAllCheckBox )
397   {
398     if( anIsChecked && !myBuildAtOnceCheckBox->isChecked() )
399       myBuildAtOnceCheckBox->animateClick();
400   }
401   else if( aCheckBox == myBuildAtOnceCheckBox )
402   {
403     if( !anIsChecked && myBuildAllCheckBox->isChecked() )
404       myBuildAllCheckBox->animateClick();
405   }
406   else if( aCheckBox == myBuildFieldsCheckBox )
407   {
408     myBuildFieldsButton->setPaletteBackgroundColor( aColor );
409     if( !anIsChecked && myBuildMinMaxCheckBox->isChecked() )
410       myBuildMinMaxCheckBox->animateClick();
411   }
412   else if( aCheckBox == myBuildMinMaxCheckBox )
413   {
414     myBuildMinMaxButton->setPaletteBackgroundColor( aColor );
415     if( anIsChecked && !myBuildFieldsCheckBox->isChecked() )
416       myBuildFieldsCheckBox->animateClick();
417   }
418   else if( aCheckBox == myBuildGroupsCheckBox )
419     myBuildGroupsButton->setPaletteBackgroundColor( aColor );
420
421 }
422
423 void VisuGUI_BuildProgressDlg::setFileName( const QString& theFileName )
424 {
425   if ( !theFileName.isNull() )
426     {
427       myFileName = theFileName;
428       myFileNameLineEdit->setText( myFileName.section( '/', -1 ) );
429     }
430 }
431
432 void VisuGUI_BuildProgressDlg::keyPressEvent( QKeyEvent* e )
433 {
434   QDialog::keyPressEvent( e );
435   if ( e->isAccepted() )
436     return;
437
438   if ( e->key() == Key_F1 )
439     {
440       e->accept();
441       onHelp();
442     }
443 }