Salome HOME
refs #1830: Progress dialog for the interpolation of bathymetry.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ZIProgressIndicator.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ZIProgressIndicator.h"
20 #include "HYDROGUI_Tool.h"
21
22 #include "HYDROData_Tool.h"
23
24 #include <QLabel>
25 #include <QProgressBar>
26 #include <QVBoxLayout>
27 #include <QApplication>
28
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROGUI_ZIProgressIndicator, Message_ProgressIndicator)
30
31 HYDROGUI_ZIProgressIndicator::HYDROGUI_ZIProgressIndicator( QWidget* theParent )
32 : QtxDialog( theParent, true, false, QtxDialog::Cancel ), myUserBreak(Standard_False),
33   myQuadTreePosition(0)
34 {
35   setWindowTitle( tr( "BATHYMETRY_INTERPOLATION_TLT" ) );
36
37   QVBoxLayout* aLayout = new QVBoxLayout( mainFrame() );
38   aLayout->setMargin( 5 );
39   aLayout->setSpacing( 5 );
40
41   myQuadTreeBar = new QProgressBar( mainFrame() );
42   myTriangulationState = new QLabel( tr( "PENDING" ), mainFrame() );
43   mySummaryBar = new QProgressBar( mainFrame() );
44
45   QString aBold("<b>%1</b>");
46
47   aLayout->addWidget( new QLabel( aBold.arg( tr( "QUADTREE_STAGE" ) ) ) );
48   aLayout->addWidget( myQuadTreeBar );
49   aLayout->addSpacing( 10 );
50   aLayout->addWidget( new QLabel( aBold.arg( tr( "TRIANGULATION_STAGE" ) ) ) );
51   aLayout->addWidget( myTriangulationState );
52   aLayout->addSpacing( 10 );
53   aLayout->addWidget( new QLabel( aBold.arg( tr( "EXPLORATION_STAGE" ) ) ) );
54   aLayout->addWidget( mySummaryBar );
55
56   setButtonText( Cancel, tr( "CANCEL" ) );
57   setButtonPosition( Center, Cancel );
58   setMinimumWidth( 350 );
59 }
60
61 HYDROGUI_ZIProgressIndicator::~HYDROGUI_ZIProgressIndicator()
62 {
63 }
64
65 Standard_Boolean HYDROGUI_ZIProgressIndicator::Show(const Standard_Boolean theForce)
66 {
67   Standard_Boolean isUserBreak = UserBreak();
68   Standard_Real aPosition = GetPosition(); 
69
70   if ( !theForce ) {
71     // quadtree
72     for ( Standard_Integer i = 1; i <= GetNbScopes(); i++ ) {
73       Message_ProgressScale aScope = GetScope(i);
74       QString aName = HYDROGUI_Tool::ToQString( aScope.GetName() ); 
75       if ( aName != "QuadTree" ) {
76         continue;
77       }
78
79       Standard_Real aMin = aScope.GetMin();
80       Standard_Real aMax = aScope.GetMax();
81       Standard_Real aScopeValue = aScope.BaseToLocal( aPosition );
82       myQuadTreePosition = aScopeValue / (aMax - aMin);
83       break;
84     }
85   } else {
86     if ( !isUserBreak ) {
87       myQuadTreeBar->setValue( int( myQuadTreePosition * 100 ) );
88       mySummaryBar->setValue( int( aPosition * 100 ) );
89       HYDROData_Tool::ExecStatus aStatus = HYDROData_Tool::GetTriangulationStatus();
90       if ( aStatus == HYDROData_Tool::Running ) {
91         myTriangulationState->setText( tr("IN_PROGRESS") );
92       } else if ( aStatus == HYDROData_Tool::Finished ) {
93         myTriangulationState->setText( tr("COMPLETED") );
94       }
95     }
96
97     bool isFinished = aPosition >= 1 || ( isUserBreak && GetNbScopes() < 2 );
98     if ( isFinished  ) {
99       if ( result() != Accepted ) {
100         QDialog::accept();
101       } 
102     } else if ( !isVisible() ) {
103       open();
104     }
105
106
107     QApplication::processEvents();
108   }
109
110   return true;
111 }
112
113 Standard_Boolean HYDROGUI_ZIProgressIndicator::UserBreak()
114 {
115   return myUserBreak;
116 }
117
118 void HYDROGUI_ZIProgressIndicator::Reset()
119 {
120   Message_ProgressIndicator::Reset();
121   myTriangulationState->setText( tr( "PENDING" ) );
122   setButtonText( Cancel, tr("CANCEL") );
123   setButtonEnabled( true, Cancel );
124   myUserBreak = Standard_False;
125 }
126
127 void HYDROGUI_ZIProgressIndicator::reject()
128 {
129   myUserBreak = Standard_True;
130   setButtonText( Cancel, tr("CANCELLING") );
131   setButtonEnabled( false, Cancel );
132   QApplication::processEvents();
133 }