Salome HOME
refs #567: add "POLYLINES" partition and modified icon for Land Cover object.
[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_Tool.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   if ( aStudy ) {
88     SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() );
89     GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen();
90     QString anEntry;
91     if ( aCalculation->Export( aGeomEngine, aDSStudy, anEntry ) ) {
92       theUpdateFlags = UF_ObjBrowser;
93       isOk = true;
94     }
95   }
96
97   return isOk;
98 }
99
100 void HYDROGUI_ExportCalculationOp::onApply()
101 {
102   QApplication::setOverrideCursor( Qt::WaitCursor );
103
104   int anUpdateFlags = 0;
105   QString anErrorMsg;
106
107   bool aResult = false;
108   QStringList aBrowseObjectsEntries;
109
110   try {
111     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
112   }
113   catch ( Standard_Failure )
114   {
115     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
116     anErrorMsg = aFailure->GetMessageString();
117     aResult = false;
118   }
119   catch ( ... )
120   {
121     aResult = false;
122   }
123   
124   QApplication::restoreOverrideCursor();
125
126   if ( aResult ) {
127     module()->update( anUpdateFlags );
128     commit();
129     browseObjects( aBrowseObjectsEntries );
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 }