Salome HOME
Merge branch 'BR_LAND_COVER_MAP' into BR_quadtree
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ExportCalculationOp.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_ExportCalculationOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Tool2.h"
24
25 #include <HYDROData_CalculationCase.h>
26
27 #include <GeometryGUI.h>
28
29 #include <SalomeApp_Study.h>
30
31 #include <LightApp_Application.h>
32 #include <LightApp_UpdateFlags.h>
33
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_Desktop.h>
36
37 #include <QApplication>
38
39 HYDROGUI_ExportCalculationOp::HYDROGUI_ExportCalculationOp( HYDROGUI_Module* theModule )
40 : HYDROGUI_Operation( theModule )
41 {
42   setName( tr( "EXPORT_CALCULATION" ) );
43 }
44
45 HYDROGUI_ExportCalculationOp::~HYDROGUI_ExportCalculationOp()
46 {
47 }
48
49 void HYDROGUI_ExportCalculationOp::startOperation()
50 {
51   HYDROGUI_Operation::startOperation();
52
53   onApply();
54 }
55
56 void HYDROGUI_ExportCalculationOp::abortOperation()
57 {
58   HYDROGUI_Operation::abortOperation();
59 }
60
61 void HYDROGUI_ExportCalculationOp::commitOperation()
62 {
63   HYDROGUI_Operation::commitOperation();
64 }
65
66 bool HYDROGUI_ExportCalculationOp::processApply( int& theUpdateFlags,
67                                                  QString& theErrorMsg,
68                                                  QStringList& theBrowseObjectsEntries )
69 {
70   // Get the selected calculation case
71   Handle(HYDROData_CalculationCase) aCalculation = 
72     Handle(HYDROData_CalculationCase)::DownCast( 
73       HYDROGUI_Tool::GetSelectedObject( module() ) );
74
75   if ( aCalculation.IsNull() ) {
76     theErrorMsg = tr( "NULL_DATA_OBJECT" );
77     return false;
78   }
79
80   bool isOk = false;
81
82   // Get active study
83   SalomeApp_Study* aStudy = 
84     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
85
86   // Export
87   QString anErrorMsg;
88   if ( aStudy ) {
89     SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
90     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
91     QString anEntry;
92     if ( aCalculation->Export( aGeomEngine, aDSStudy, anEntry, theErrorMsg ) ) {
93       theUpdateFlags = UF_ObjBrowser;
94       isOk = true;
95     }
96   }
97
98   return isOk;
99 }
100
101 void HYDROGUI_ExportCalculationOp::onApply()
102 {
103   QApplication::setOverrideCursor( Qt::WaitCursor );
104
105   int anUpdateFlags = 0;
106   QString anErrorMsg;
107
108   bool aResult = false;
109   QStringList aBrowseObjectsEntries;
110
111   try {
112     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
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     browseObjects( aBrowseObjectsEntries );
131
132     // Show message box
133     SUIT_MessageBox::information( module()->getApp()->desktop(),
134                                   tr( "EXPORT_STATUS" ),
135                                   tr( "EXPORT_FINISHED") ); 
136   } 
137   else {
138     abort();
139
140     if ( anErrorMsg.isEmpty() )
141       anErrorMsg = tr( "EXPORT_DATA_FAILED" );
142
143     anErrorMsg.prepend( tr( "EXPORT_FAILED" ) + "\n" );
144
145     SUIT_MessageBox::critical( module()->getApp()->desktop(),
146                                tr( "EXPORT_STATUS" ),
147                                anErrorMsg ); 
148   }
149 }