Salome HOME
refs #417: showing new objects
[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                                                  QStringList& theBrowseObjectsEntries )
73 {
74   // Get the selected calculation case
75   Handle(HYDROData_CalculationCase) aCalculation = 
76     Handle(HYDROData_CalculationCase)::DownCast( 
77       HYDROGUI_Tool::GetSelectedObject( module() ) );
78
79   if ( aCalculation.IsNull() ) {
80     theErrorMsg = tr( "NULL_DATA_OBJECT" );
81     return false;
82   }
83
84   bool isOk = false;
85
86   // Get active study
87   SalomeApp_Study* aStudy = 
88     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
89
90   // Export
91   if ( aStudy ) {
92     SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
93     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
94     if ( aCalculation->Export( aGeomEngine, aDSStudy ) ) {
95       theUpdateFlags = UF_ObjBrowser;
96       isOk = true;
97     }
98   }
99
100   return isOk;
101 }
102
103 void HYDROGUI_ExportCalculationOp::onApply()
104 {
105   QApplication::setOverrideCursor( Qt::WaitCursor );
106
107   int anUpdateFlags = 0;
108   QString anErrorMsg;
109
110   bool aResult = false;
111   QStringList aBrowseObjectsEntries;
112
113   try {
114     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
115   }
116   catch ( Standard_Failure )
117   {
118     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
119     anErrorMsg = aFailure->GetMessageString();
120     aResult = false;
121   }
122   catch ( ... )
123   {
124     aResult = false;
125   }
126   
127   QApplication::restoreOverrideCursor();
128
129   if ( aResult ) {
130     module()->update( anUpdateFlags );
131     commit();
132     browseObjects( aBrowseObjectsEntries );
133
134     // Show message box
135     SUIT_MessageBox::information( module()->getApp()->desktop(),
136                                   tr( "EXPORT_STATUS" ),
137                                   tr( "EXPORT_FINISHED") ); 
138   } 
139   else {
140     abort();
141
142     if ( anErrorMsg.isEmpty() )
143       anErrorMsg = tr( "EXPORT_DATA_FAILED" );
144
145     anErrorMsg.prepend( tr( "EXPORT_FAILED" ) + "\n" );
146
147     SUIT_MessageBox::critical( module()->getApp()->desktop(),
148                                tr( "EXPORT_STATUS" ),
149                                anErrorMsg ); 
150   }
151 }