Salome HOME
refs #497: redesign of HYDRO main menu.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ExportCalculationOp.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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     QString anEntry;
95     if ( aCalculation->Export( aGeomEngine, aDSStudy, anEntry ) ) {
96       theUpdateFlags = UF_ObjBrowser;
97       isOk = true;
98     }
99   }
100
101   return isOk;
102 }
103
104 void HYDROGUI_ExportCalculationOp::onApply()
105 {
106   QApplication::setOverrideCursor( Qt::WaitCursor );
107
108   int anUpdateFlags = 0;
109   QString anErrorMsg;
110
111   bool aResult = false;
112   QStringList aBrowseObjectsEntries;
113
114   try {
115     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
116   }
117   catch ( Standard_Failure )
118   {
119     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
120     anErrorMsg = aFailure->GetMessageString();
121     aResult = false;
122   }
123   catch ( ... )
124   {
125     aResult = false;
126   }
127   
128   QApplication::restoreOverrideCursor();
129
130   if ( aResult ) {
131     module()->update( anUpdateFlags );
132     commit();
133     browseObjects( aBrowseObjectsEntries );
134
135     // Show message box
136     SUIT_MessageBox::information( module()->getApp()->desktop(),
137                                   tr( "EXPORT_STATUS" ),
138                                   tr( "EXPORT_FINISHED") ); 
139   } 
140   else {
141     abort();
142
143     if ( anErrorMsg.isEmpty() )
144       anErrorMsg = tr( "EXPORT_DATA_FAILED" );
145
146     anErrorMsg.prepend( tr( "EXPORT_FAILED" ) + "\n" );
147
148     SUIT_MessageBox::critical( module()->getApp()->desktop(),
149                                tr( "EXPORT_STATUS" ),
150                                anErrorMsg ); 
151   }
152 }