Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[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   myStatMess.clear();
88   QString anErrorMsg;
89   if ( aStudy ) {
90     SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
91     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
92     QString anEntry;
93     if ( aCalculation->Export( aGeomEngine, aDSStudy, anEntry, theErrorMsg, myStatMess ) ) {
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   QStringList aBrowseObjectsEntries;
111
112   try {
113     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
114   }
115   catch ( Standard_Failure )
116   {
117     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
118     anErrorMsg = aFailure->GetMessageString();
119     aResult = false;
120   }
121   catch ( ... )
122   {
123     aResult = false;
124   }
125   
126   QApplication::restoreOverrideCursor();
127
128   if ( aResult ) {
129     module()->update( anUpdateFlags );
130     commit();
131     browseObjects( aBrowseObjectsEntries );
132
133     // Show message box
134     SUIT_MessageBox::information( module()->getApp()->desktop(),
135                                   tr( "EXPORT_STATUS" ),
136                                   tr( "EXPORT_FINISHED") + "\n" + getStatMess() ); 
137   } 
138   else {
139     abort();
140
141     if ( anErrorMsg.isEmpty() )
142       anErrorMsg = tr( "EXPORT_DATA_FAILED" );
143
144     anErrorMsg.prepend( tr( "EXPORT_FAILED" ) + "\n" );
145
146     SUIT_MessageBox::critical( module()->getApp()->desktop(),
147                                tr( "EXPORT_STATUS" ),
148                                anErrorMsg ); 
149   }
150 }
151
152 QString HYDROGUI_ExportCalculationOp::getStatMess()
153 {
154   return myStatMess;
155 }