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