Salome HOME
Exlude the user input during process events by application (Bug #325).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ExportCalculationOp.cxx
1 // Copyright (C) 2007-2013  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 #include "HYDROGUI_ExportCalculationOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Tool.h"
28
29 #include <HYDROData_CalculationCase.h>
30
31 #include <GeometryGUI.h>
32
33 #include <SalomeApp_Study.h>
34
35 #include <LightApp_Application.h>
36 #include <LightApp_UpdateFlags.h>
37
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40
41 #include <QApplication>
42
43 HYDROGUI_ExportCalculationOp::HYDROGUI_ExportCalculationOp( HYDROGUI_Module* theModule )
44 : HYDROGUI_Operation( theModule )
45 {
46   setName( tr( "EXPORT_CALCULATION" ) );
47 }
48
49 HYDROGUI_ExportCalculationOp::~HYDROGUI_ExportCalculationOp()
50 {
51 }
52
53 void HYDROGUI_ExportCalculationOp::startOperation()
54 {
55   HYDROGUI_Operation::startOperation();
56
57   onApply();
58 }
59
60 void HYDROGUI_ExportCalculationOp::abortOperation()
61 {
62   HYDROGUI_Operation::abortOperation();
63 }
64
65 void HYDROGUI_ExportCalculationOp::commitOperation()
66 {
67   HYDROGUI_Operation::commitOperation();
68 }
69
70 bool HYDROGUI_ExportCalculationOp::processApply( int& theUpdateFlags,
71                                                 QString& theErrorMsg )
72 {
73   // Get the selected calculation case
74   Handle(HYDROData_CalculationCase) aCalculation = 
75     Handle(HYDROData_CalculationCase)::DownCast( 
76       HYDROGUI_Tool::GetSelectedObject( module() ) );
77
78   if ( aCalculation.IsNull() ) {
79     theErrorMsg = tr( "NULL_DATA_OBJECT" );
80     return false;
81   }
82
83   bool isOk = false;
84
85   // Get active study
86   SalomeApp_Study* aStudy = 
87     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
88
89   // Export
90   if ( aStudy ) {
91     SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
92     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
93     if ( aCalculation->Export( aGeomEngine, aDSStudy ) ) {
94       theUpdateFlags = UF_ObjBrowser;
95       isOk = true;
96     }
97   }
98
99   return isOk;
100 }
101
102 void HYDROGUI_ExportCalculationOp::onApply()
103 {
104   QApplication::setOverrideCursor( Qt::WaitCursor );
105
106   int anUpdateFlags = 0;
107   QString anErrorMsg;
108
109   bool aResult = false;
110   
111   try {
112     aResult = processApply( anUpdateFlags, anErrorMsg );
113   }
114   catch ( Standard_Failure )
115   {
116     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
117     anErrorMsg = aFailure->GetMessageString();
118     aResult = false;
119   }
120   catch ( ... )
121   {
122     aResult = false;
123   }
124   
125   QApplication::restoreOverrideCursor();
126
127   if ( aResult ) {
128     module()->update( anUpdateFlags );
129     commit();
130
131     // Show message box
132     SUIT_MessageBox::information( module()->getApp()->desktop(),
133                                   tr( "EXPORT_STATUS" ),
134                                   tr( "EXPORT_FINISHED") ); 
135   } 
136   else {
137     abort();
138
139     if ( anErrorMsg.isEmpty() )
140       anErrorMsg = tr( "EXPORT_DATA_FAILED" );
141
142     anErrorMsg.prepend( tr( "EXPORT_FAILED" ) + "\n" );
143
144     SUIT_MessageBox::critical( module()->getApp()->desktop(),
145                                tr( "EXPORT_STATUS" ),
146                                anErrorMsg ); 
147   }
148 }