Salome HOME
61165fd33a6d2e17d3f0db4c3fb99c5824b5dfab
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportBathymetryOp.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_ImportBathymetryOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_DataObject.h"
27 #include "HYDROGUI_ImportBathymetryDlg.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Bathymetry.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_UpdateFlags.h>
36 #include <SUIT_ViewManager.h>
37 #include <SVTK_ViewModel.h>
38
39 #include <QFileInfo>
40
41 HYDROGUI_ImportBathymetryOp::HYDROGUI_ImportBathymetryOp( HYDROGUI_Module* theModule, 
42                                                          const bool theIsEdit  )
43 : HYDROGUI_Operation( theModule ),
44   myIsEdit( theIsEdit )
45 {
46   setName( theIsEdit ? tr( "EDIT_IMPORTED_BATHYMETRY" ) : tr( "IMPORT_BATHYMETRY" ) );
47 }
48
49 HYDROGUI_ImportBathymetryOp::~HYDROGUI_ImportBathymetryOp()
50 {
51 }
52
53 void HYDROGUI_ImportBathymetryOp::startOperation()
54 {
55   HYDROGUI_Operation::startOperation();
56
57   HYDROGUI_ImportBathymetryDlg* aPanel = 
58     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
59   if ( !aPanel )
60     return;
61
62   aPanel->reset();
63
64   if( myIsEdit )
65   {
66     if ( isApplyAndClose() )
67       myEditedObject = Handle(HYDROData_Bathymetry)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
68     if( !myEditedObject.IsNull() )
69     {
70       QString aName = myEditedObject->GetName();
71       QString aFileName = HYDROGUI_Tool::ToQString( myEditedObject->GetFilePath() );
72       bool anIsAltitudesInverted = myEditedObject->IsAltitudesInverted();
73
74       aPanel->setObjectName( aName );
75       aPanel->setFileName( aFileName );
76       aPanel->setInvertAltitudes( anIsAltitudesInverted );
77     }
78   }
79 }
80
81 void HYDROGUI_ImportBathymetryOp::abortOperation()
82 {
83   HYDROGUI_Operation::abortOperation();
84 }
85
86 void HYDROGUI_ImportBathymetryOp::commitOperation()
87 {
88   HYDROGUI_Operation::commitOperation();
89 }
90
91 HYDROGUI_InputPanel* HYDROGUI_ImportBathymetryOp::createInputPanel() const
92 {
93   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportBathymetryDlg( module(), getName() );
94   
95   connect ( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
96
97   return aPanel;
98 }
99
100 bool HYDROGUI_ImportBathymetryOp::processApply( int& theUpdateFlags,
101                                                 QString& theErrorMsg,
102                                                 QStringList& theBrowseObjectsEntries )
103 {
104   HYDROGUI_ImportBathymetryDlg* aPanel = 
105     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
106   if ( !aPanel )
107     return false;
108
109   QString anObjectName = aPanel->getObjectName().simplified();
110   if ( anObjectName.isEmpty() )
111   {
112     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
113     return false;
114   }
115
116   QString aFileName = aPanel->getFileName().simplified();
117   bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
118
119   if ( aFileName.isEmpty() )
120   {
121     theErrorMsg = tr( "INCORRECT_FILE_NAME" );
122     return false;
123   }
124
125   QFileInfo aFileInfo( aFileName );
126   if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
127   {
128     theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
129     return false;
130   }
131
132   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
133   {
134     // check that there are no other objects with the same name in the document
135     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
136     if( !anObject.IsNull() )
137     {
138       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
139       return false;
140     }
141   }
142
143   Handle(HYDROData_Bathymetry) aBathymetryObj;
144   if ( myIsEdit )
145   {
146     aBathymetryObj = myEditedObject;
147   }
148   else
149   {
150     aBathymetryObj = 
151       Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
152   }
153   if ( aBathymetryObj.IsNull() )
154     return false;
155
156   QString anOldFileName = HYDROGUI_Tool::ToQString( aBathymetryObj->GetFilePath() );
157   if ( aFileName != anOldFileName )
158   {
159     aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
160     if ( !aBathymetryObj->ImportFromFile( HYDROGUI_Tool::ToAsciiString( aFileName ) ) )
161     {
162       theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileName );
163       return false;
164     }
165   }
166   else if ( anIsInvertAltitudes != aBathymetryObj->IsAltitudesInverted() )
167   {
168     aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes );
169   }
170
171   aBathymetryObj->SetName( anObjectName );
172
173   aBathymetryObj->Update();
174
175   // Activate VTK viewer and show the bathymetry
176   SUIT_ViewManager* aVTKMgr = 0;
177   SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
178   // Try to get a VTK viewer as an active or existing one
179   if ( aViewMgr )
180   {
181     if ( aViewMgr->getType() == SVTK_Viewer::Type() )
182     {
183       aVTKMgr = aViewMgr;
184     }
185     else
186     {
187       aVTKMgr = module()->getApp()->viewManager( SVTK_Viewer::Type() );
188     }
189   }
190
191   /*
192   // If there is no VTK viewer yet then create a new one
193   if ( !aVTKMgr )
194   {
195     aVTKMgr = module()->getApp()->createViewManager( SVTK_Viewer::Type() );
196   }
197   // Set the bathymetry visible in the VTK viewer
198   if ( aVTKMgr )
199   {
200     module()->setObjectVisible( (size_t)aVTKMgr->getViewModel(), aBathymetryObj, true );
201   }*/
202
203   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced |
204                               UF_VTKViewer | UF_VTK_Forced;
205
206   if( !myIsEdit )
207   {
208     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBathymetryObj );
209     theBrowseObjectsEntries.append( anEntry );
210   }
211
212   return true;
213 }
214
215 void HYDROGUI_ImportBathymetryOp::onFileSelected()
216 {
217   HYDROGUI_ImportBathymetryDlg* aPanel = 
218     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
219   if ( !aPanel )
220     return;
221
222   QString anObjectName = aPanel->getObjectName().simplified();
223   //if ( anObjectName.isEmpty() )
224   {
225     anObjectName = aPanel->getFileName();
226     if ( !anObjectName.isEmpty() ) {
227         anObjectName = QFileInfo( anObjectName ).baseName();
228     }
229
230     if ( anObjectName.isEmpty() ) {
231       anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
232     }
233     aPanel->setObjectName( anObjectName );
234   }
235 }
236
237
238