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