Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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_Tools.h"
31
32 #include "SUIT_FileDlg.h"
33 #include "SUIT_MessageBox.h"
34 #include "SUIT_ResourceMgr.h"
35
36 #include <vtkTimerLog.h>
37
38 #include <qcheckbox.h>
39 #include <qgroupbox.h>
40 #include <qlabel.h>
41 #include <qlayout.h>
42 #include <qlcdnumber.h>
43 #include <qlineedit.h>
44 #include <qpushbutton.h>
45 #include <qtimer.h>
46
47 /*!
48  * Constructor
49  */
50 VisuGUI_BuildProgressDlg::VisuGUI_BuildProgressDlg( QWidget* theParent ):
51   QDialog( theParent, 
52            "VisuGUI_BuildProgressBar", 
53            false,
54            WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose),
55   myIsRaiseColor( false )
56 {
57   setCaption( tr( "DLG_BUILD_PROGRESS_TITLE" ) );
58
59   SUIT_ResourceMgr* aResourceMgr = VISU::GetResourceMgr();
60
61   QVBoxLayout* aTopLayout = new QVBoxLayout( this );
62   aTopLayout->setSpacing( 6 );
63   aTopLayout->setMargin( 6 );
64   aTopLayout->setAutoAdd( true );
65
66   // Settings
67   mySettingsBox = new QGroupBox( tr( "IMPORT_SETTINGS" ), this );
68   mySettingsBox->setColumnLayout( 0, Qt::Vertical );
69   mySettingsBox->layout()->setSpacing( 0 );
70   mySettingsBox->layout()->setMargin( 0 );
71
72   QGridLayout* aSettingsLayout = new QGridLayout( mySettingsBox->layout() );
73   aSettingsLayout->setSpacing( 6 );
74   aSettingsLayout->setMargin( 11 );
75
76   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), mySettingsBox );
77   myFileNameLineEdit = new QLineEdit( mySettingsBox );
78   myFileNameLineEdit->setMinimumWidth( 250 );
79   myFileNameLineEdit->setReadOnly( true );
80   /*
81   QPushButton* aFileNameButton = new QPushButton( mySettingsBox );
82   aFileNameButton->setAutoDefault( false );
83   aFileNameButton->setPixmap( aResourceMgr->loadPixmap( "VISU", tr( "ICON_LOAD_TEXTURE" ) ) );
84   connect( aFileNameButton, SIGNAL( clicked() ), this, SLOT( onBrowseFile() ) );
85   */
86   myBuildAllCheckBox = new QCheckBox( tr( "BUILD_ALL" ), mySettingsBox );
87   myBuildAllCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "full_med_loading", false ) );
88   connect( myBuildAllCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
89
90   myBuildAtOnceCheckBox = new QCheckBox( tr( "BUILD_AT_ONCE" ), mySettingsBox );
91   myBuildAtOnceCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_at_once", false ) );
92   connect( myBuildAtOnceCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
93
94   myCloseCheckBox = new QCheckBox( tr( "CLOSE_AT_FINISH" ), mySettingsBox );
95   myCloseCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "close_at_finish", true ) );
96
97   aSettingsLayout->addWidget(  aFileNameLabel,       0, 0 );
98   aSettingsLayout->addMultiCellWidget( myFileNameLineEdit, 1, 1, 0, 1 );
99   //aSettingsLayout->addWidget(  aFileNameButton,      1, 2 );
100   aSettingsLayout->addWidget( myBuildAllCheckBox,    2, 0 );
101   aSettingsLayout->addWidget( myBuildAtOnceCheckBox, 3, 0 );
102   aSettingsLayout->addWidget( myCloseCheckBox,       4, 0 );
103
104   // Progress
105   myProgressBox = new QGroupBox( tr( "IMPORT_PROGRESS" ), this );
106   myProgressBox->setColumnLayout( 0, Qt::Vertical );
107   myProgressBox->layout()->setSpacing( 0 );
108   myProgressBox->layout()->setMargin( 0 );
109
110   QGridLayout* aProgressLayout = new QGridLayout( myProgressBox->layout() );
111   aProgressLayout->setSpacing( 6 );
112   aProgressLayout->setMargin( 11 );
113
114   myBuildEntitiesCheckBox = new QCheckBox( tr( "BUILD_ENTITIES" ), myProgressBox );
115   myBuildEntitiesCheckBox->setChecked( true );
116   myBuildEntitiesCheckBox->setEnabled( false );
117   myBuildEntitiesButton = new QPushButton( myProgressBox );
118   myBuildEntitiesButton->setEnabled( false );
119   myBuildEntitiesButton->setFixedSize( 30, 30 );
120   myBuildEntitiesButton->setPaletteBackgroundColor( Qt::red );
121   connect( myBuildEntitiesCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
122
123   myBuildFieldsCheckBox = new QCheckBox( tr( "BUILD_FIELDS" ), myProgressBox );
124   myBuildFieldsCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_fields", true ) );
125   myBuildFieldsButton = new QPushButton( myProgressBox );
126   myBuildFieldsButton->setEnabled( false );
127   myBuildFieldsButton->setFixedSize( 30, 30 );
128   myBuildFieldsButton->setPaletteBackgroundColor( Qt::red );
129   connect( myBuildFieldsCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
130
131   myBuildMinMaxCheckBox = new QCheckBox( tr( "BUILD_MINMAX" ), myProgressBox );
132   myBuildMinMaxCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_min_max", true ) );
133   myBuildMinMaxButton = new QPushButton( myProgressBox );
134   myBuildMinMaxButton->setEnabled( false );
135   myBuildMinMaxButton->setFixedSize( 30, 30 );
136   myBuildMinMaxButton->setPaletteBackgroundColor( Qt::red );
137   connect( myBuildMinMaxCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
138
139   myBuildGroupsCheckBox = new QCheckBox( tr( "BUILD_GROUPS" ), myProgressBox );
140   myBuildGroupsCheckBox->setChecked( aResourceMgr->booleanValue( "VISU", "build_groups", true ) );
141   myBuildGroupsButton = new QPushButton( myProgressBox );
142   myBuildGroupsButton->setEnabled( false );
143   myBuildGroupsButton->setFixedSize( 30, 30 );
144   myBuildGroupsButton->setPaletteBackgroundColor( Qt::red );
145   connect( myBuildGroupsCheckBox, SIGNAL( clicked() ), this, SLOT( onBuildCheckBoxClicked() ) );
146
147   aProgressLayout->addWidget( myBuildEntitiesCheckBox, 0, 0 );
148   aProgressLayout->addWidget( myBuildEntitiesButton,   0, 1 );
149   aProgressLayout->addWidget( myBuildFieldsCheckBox,   1, 0 );
150   aProgressLayout->addWidget( myBuildFieldsButton,     1, 1 );
151   aProgressLayout->addWidget( myBuildMinMaxCheckBox,   2, 0 );
152   aProgressLayout->addWidget( myBuildMinMaxButton,     2, 1 );
153   aProgressLayout->addWidget( myBuildGroupsCheckBox,   3, 0 );
154   aProgressLayout->addWidget( myBuildGroupsButton,     3, 1 );
155
156   // Time
157   myTimeBox = new QGroupBox( tr( "IMPORT_TIME" ), this );
158   myTimeBox->setColumnLayout( 0, Qt::Vertical );
159   myTimeBox->layout()->setSpacing( 0 );
160   myTimeBox->layout()->setMargin( 0 );
161
162   QGridLayout* aTimeLayout = new QGridLayout( myTimeBox->layout() );
163   aTimeLayout->setSpacing( 6 );
164   aTimeLayout->setMargin( 11 );
165
166   QLabel* aTimeLabel = new QLabel( tr( "TIME" ), myTimeBox );
167
168   myTimeLCDNumber = new QLCDNumber( myTimeBox );
169   myTimeLCDNumber->setSegmentStyle( QLCDNumber::Filled );
170   myTimeLCDNumber->setPaletteBackgroundColor( Qt::black );
171   myTimeLCDNumber->setPaletteForegroundColor( Qt::white );
172   myTimeLCDNumber->setNumDigits( 8 );
173   myTimeLCDNumber->display( myTime.toString( "hh:mm:ss.zzz" ) );
174
175   aTimeLayout->addWidget(  aTimeLabel,     0, 0 );
176   aTimeLayout->addWidget( myTimeLCDNumber, 0, 1 );
177
178   // Start / Close
179   QGroupBox* CommonGroup = new QGroupBox( this );
180   CommonGroup->setColumnLayout(0, Qt::Vertical );
181   CommonGroup->layout()->setSpacing( 0 );
182   CommonGroup->layout()->setMargin( 0 );
183   QGridLayout* CommonGroupLayout = new QGridLayout( CommonGroup->layout() );
184   CommonGroupLayout->setAlignment( Qt::AlignTop );
185   CommonGroupLayout->setSpacing( 6 );
186   CommonGroupLayout->setMargin( 11 );
187
188   myStartButton = new QPushButton( tr( "START" ), CommonGroup );
189   myStartButton->setAutoDefault( true );
190   myStartButton->setDefault( true );
191   CommonGroupLayout->addWidget( myStartButton, 0, 0 );
192   CommonGroupLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
193
194   QPushButton* aCloseButton = new QPushButton( tr( "CLOSE" ), CommonGroup );
195   aCloseButton->setAutoDefault( true );
196   CommonGroupLayout->addWidget( aCloseButton, 0, 2 );
197
198   connect( myStartButton, SIGNAL( clicked() ), this, SLOT( onStart() ) );
199   connect(  aCloseButton, SIGNAL( clicked() ), this, SLOT( onClose() ) );
200
201   myTimer = new QTimer( this );
202   connect( myTimer, SIGNAL( timeout() ), this, SLOT( onTimer() ) );
203 }
204
205 VisuGUI_BuildProgressDlg::~VisuGUI_BuildProgressDlg()
206 {
207 }
208
209 void VisuGUI_BuildProgressDlg::show()
210 {
211   if( !myFileName.isNull() || onBrowseFile() )
212     QWidget::show();
213 }
214
215 void VisuGUI_BuildProgressDlg::onStart()
216 {
217   if( myFileName.isNull() )
218     return;
219
220   myResult = myGenerator->CreateResult( myFileName );
221
222   if (CORBA::is_nil(myResult.in())) {
223     SUIT_MessageBox::warn1(this,
224                            tr("WRN_VISU"),
225                            tr("ERR_ERROR_IN_THE_FILE"),
226                            tr("BUT_OK"));
227     onClose();
228   }else{
229     myResult->SetBuildFields( myBuildFieldsCheckBox->isChecked(), myBuildMinMaxCheckBox->isChecked() );
230     myResult->SetBuildGroups( myBuildGroupsCheckBox->isChecked() );
231     
232     //setModal( false );
233     myFileNameLineEdit->setReadOnly( true );
234     /*
235       myBuildAllCheckBox->setNoChange();
236       myBuildAtOnceCheckBox->setNoChange();
237       myBuildEntitiesCheckBox->setNoChange();
238       myBuildFieldsCheckBox->setNoChange();
239       myBuildMinMaxCheckBox->setNoChange();
240       myBuildGroupsCheckBox->setNoChange();
241     */
242
243     myStartButton->setEnabled( false );
244     
245     bool aBuildAtOnce = myBuildAtOnceCheckBox->isChecked();
246     if( aBuildAtOnce )
247     {
248       QApplication::setOverrideCursor( Qt::waitCursor );
249       myCurrentTime = vtkTimerLog::GetCurrentTime();
250     }
251
252     myTime.setHMS( 0, 0, 0 );
253     myTimer->start( 100 );
254     
255     bool aBuildAll = myBuildAllCheckBox->isChecked();
256     myResult->Build( aBuildAll, aBuildAtOnce );
257   }
258 }
259
260 void VisuGUI_BuildProgressDlg::onClose()
261 {
262   done( 0 );
263 }
264
265 bool VisuGUI_BuildProgressDlg::onBrowseFile()
266 {
267   QString aRootDir = QString( getenv( "VISU_ROOT_DIR") );
268
269   QStringList aFilter;
270   aFilter.append( tr( "FLT_MED_FILES" ) );
271   aFilter.append( tr( "FLT_ALL_FILES" ) );
272
273   QFileInfo aFileInfo = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_FROM_FILE" ), true );
274   QString aFileName = aFileInfo.filePath();
275
276   if( aFileName.isNull() )
277     return false;
278
279   myFileName = aFileName;
280   myFileNameLineEdit->setText( aFileName.section( '/', -1 ) );
281
282   return true;
283 }
284
285 void VisuGUI_BuildProgressDlg::onTimer()
286 {
287   try {
288     bool aBuildAtOnce = myBuildAtOnceCheckBox->isChecked();
289
290     if( !aBuildAtOnce )
291     {
292       myTime = myTime.addMSecs( 100 );
293       if( myTime.minute() > 9 && myTimeLCDNumber->numDigits() < 9 )
294         myTimeLCDNumber->setNumDigits( 9 );
295       myTimeLCDNumber->display( myTime.toString( "m:ss.zzz" ) );
296
297       bool isEntitiesDone = myResult->IsEntitiesDone();
298       bool isFieldsDone = myResult->IsFieldsDone();
299       bool isMinMaxDone = myResult->IsMinMaxDone();
300       bool isGroupsDone = myResult->IsGroupsDone();
301
302       updateButton( myBuildEntitiesButton, isEntitiesDone );
303       updateButton( myBuildFieldsButton, isFieldsDone );
304       updateButton( myBuildMinMaxButton, isMinMaxDone );
305       updateButton( myBuildGroupsButton, isGroupsDone );
306     }
307
308     if( myResult->IsDone() )
309     {
310       myTimer->stop();
311
312       if( aBuildAtOnce )
313       {
314         QApplication::restoreOverrideCursor();
315
316         QTime aTime;
317         int mSecs = ( int )( 1000 * ( vtkTimerLog::GetCurrentTime() - myCurrentTime ) );
318         aTime = aTime.addMSecs( mSecs );
319         if( aTime.minute() > 9 )
320           myTimeLCDNumber->setNumDigits( 9 );
321         myTimeLCDNumber->display( aTime.toString( "m:ss.zzz" ) );
322       }
323
324       if( myCloseCheckBox->isChecked() )
325         done( 0 );
326       else
327         SUIT_MessageBox::warn1( this, tr( "INF_VISU" ), tr( "IMPORT_DONE" ), tr( "BUT_OK" ) );
328     }
329   }
330   catch( ... ) {
331     done( 1 );
332   }
333 }
334
335 void VisuGUI_BuildProgressDlg::updateButton( QPushButton* theButton, bool theIsDone )
336 {
337   QColor aCurrentColor = theButton->paletteBackgroundColor();
338   if( aCurrentColor == Qt::gray || aCurrentColor == Qt::green )
339     return;
340
341   QColor aNewColor = Qt::green;
342
343   if( !theIsDone )
344   {
345     int r, g, b;
346     aCurrentColor.getRgb( &r, &g, &b );
347     if( g == 0 )
348       myIsRaiseColor = true;
349     else if( g == 255 )
350       myIsRaiseColor = false;
351
352     int gNew = g + ( myIsRaiseColor ? 1 : -1 ) * 51;
353
354     aNewColor.setRgb( 255, gNew, 0 );
355   }
356
357   theButton->setPaletteBackgroundColor( aNewColor );
358 }
359
360 void VisuGUI_BuildProgressDlg::done( int r )
361 {
362   QApplication::restoreOverrideCursor();
363   myTimer->stop();
364   QDialog::done( r );
365 }
366
367 void VisuGUI_BuildProgressDlg::onBuildCheckBoxClicked()
368 {
369   QCheckBox* aCheckBox = ( QCheckBox* )sender();
370   if( !aCheckBox )
371     return;
372
373   bool anIsChecked = aCheckBox->isChecked();
374
375   QColor aColor = anIsChecked ? Qt::red : Qt::gray;
376
377   if( aCheckBox == myBuildAllCheckBox )
378   {
379     if( anIsChecked && !myBuildAtOnceCheckBox->isChecked() )
380       myBuildAtOnceCheckBox->animateClick();
381   }
382   else if( aCheckBox == myBuildAtOnceCheckBox )
383   {
384     if( !anIsChecked && myBuildAllCheckBox->isChecked() )
385       myBuildAllCheckBox->animateClick();
386   }
387   else if( aCheckBox == myBuildFieldsCheckBox )
388   {
389     myBuildFieldsButton->setPaletteBackgroundColor( aColor );
390     if( !anIsChecked && myBuildMinMaxCheckBox->isChecked() )
391       myBuildMinMaxCheckBox->animateClick();
392   }
393   else if( aCheckBox == myBuildMinMaxCheckBox )
394   {
395     myBuildMinMaxButton->setPaletteBackgroundColor( aColor );
396     if( anIsChecked && !myBuildFieldsCheckBox->isChecked() )
397       myBuildFieldsCheckBox->animateClick();
398   }
399   else if( aCheckBox == myBuildGroupsCheckBox )
400     myBuildGroupsButton->setPaletteBackgroundColor( aColor );
401
402 }
403
404 void VisuGUI_BuildProgressDlg::setFileName( const QString& theFileName )
405 {
406   if ( !theFileName.isNull() )
407     {
408       myFileName = theFileName;
409       myFileNameLineEdit->setText( myFileName.section( '/', -1 ) );
410     }
411 }