1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportBathymetryOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ImportBathymetryDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
31 #include <HYDROData_Bathymetry.h>
33 #include <LightApp_Application.h>
34 #include <LightApp_UpdateFlags.h>
35 #include <SUIT_ViewManager.h>
36 #include <SVTK_ViewModel.h>
40 HYDROGUI_ImportBathymetryOp::HYDROGUI_ImportBathymetryOp( HYDROGUI_Module* theModule,
41 const bool theIsEdit )
42 : HYDROGUI_Operation( theModule ),
45 setName( theIsEdit ? tr( "EDIT_IMPORTED_BATHYMETRY" ) : tr( "IMPORT_BATHYMETRY" ) );
48 HYDROGUI_ImportBathymetryOp::~HYDROGUI_ImportBathymetryOp()
52 void HYDROGUI_ImportBathymetryOp::startOperation()
54 HYDROGUI_Operation::startOperation();
56 HYDROGUI_ImportBathymetryDlg* aPanel =
57 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
65 myEditedObject = Handle(HYDROData_Bathymetry)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
66 if( !myEditedObject.IsNull() )
68 QString aName = myEditedObject->GetName();
69 QString aFileName = HYDROGUI_Tool::ToQString( myEditedObject->GetFilePath() );
70 bool anIsAltitudesInverted = myEditedObject->IsAltitudesInverted();
72 aPanel->setObjectName( aName );
73 aPanel->setFileName( aFileName );
74 aPanel->setInvertAltitudes( anIsAltitudesInverted );
79 void HYDROGUI_ImportBathymetryOp::abortOperation()
81 HYDROGUI_Operation::abortOperation();
84 void HYDROGUI_ImportBathymetryOp::commitOperation()
86 HYDROGUI_Operation::commitOperation();
89 HYDROGUI_InputPanel* HYDROGUI_ImportBathymetryOp::createInputPanel() const
91 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportBathymetryDlg( module(), getName() );
93 connect ( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
98 bool HYDROGUI_ImportBathymetryOp::processApply( int& theUpdateFlags,
99 QString& theErrorMsg )
101 HYDROGUI_ImportBathymetryDlg* aPanel =
102 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
106 QString anObjectName = aPanel->getObjectName().simplified();
107 if ( anObjectName.isEmpty() )
109 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
113 QString aFileName = aPanel->getFileName().simplified();
114 bool anIsInvertAltitudes = aPanel->isInvertAltitudes();
116 if ( aFileName.isEmpty() )
118 theErrorMsg = tr( "INCORRECT_FILE_NAME" );
122 QFileInfo aFileInfo( aFileName );
123 if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
125 theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
129 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
131 // check that there are no other objects with the same name in the document
132 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
133 if( !anObject.IsNull() )
135 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
140 Handle(HYDROData_Bathymetry) aBathymetryObj;
143 aBathymetryObj = myEditedObject;
148 Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
150 if ( aBathymetryObj.IsNull() )
153 QString anOldFileName = HYDROGUI_Tool::ToQString( aBathymetryObj->GetFilePath() );
154 if ( aFileName != anOldFileName )
156 aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes, false );
157 if ( !aBathymetryObj->ImportFromFile( HYDROGUI_Tool::ToAsciiString( aFileName ) ) )
159 theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileName );
163 else if ( anIsInvertAltitudes != aBathymetryObj->IsAltitudesInverted() )
165 aBathymetryObj->SetAltitudesInverted( anIsInvertAltitudes );
168 aBathymetryObj->SetName( anObjectName );
170 aBathymetryObj->Update();
172 // Activate VTK viewer and show the bathymetry
173 SUIT_ViewManager* aVTKMgr = 0;
174 SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
175 // Try to get a VTK viewer as an active or existing one
178 if ( aViewMgr->getType() == SVTK_Viewer::Type() )
184 aVTKMgr = module()->getApp()->viewManager( SVTK_Viewer::Type() );
187 // If there is no VTK viewer yet then create a new one
190 aVTKMgr = module()->getApp()->createViewManager( SVTK_Viewer::Type() );
192 // Set the bathymetry visible in the VTK viewer
195 module()->setObjectVisible( (size_t)aVTKMgr->getViewModel(), aBathymetryObj, true );
198 theUpdateFlags = UF_Model | UF_VTKViewer | UF_VTK_Init | UF_VTK_Forced;
202 void HYDROGUI_ImportBathymetryOp::onFileSelected()
204 HYDROGUI_ImportBathymetryDlg* aPanel =
205 ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
209 QString anObjectName = aPanel->getObjectName().simplified();
210 //if ( anObjectName.isEmpty() )
212 anObjectName = aPanel->getFileName();
213 if ( !anObjectName.isEmpty() ) {
214 anObjectName = QFileInfo( anObjectName ).baseName();
217 if ( anObjectName.isEmpty() ) {
218 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
220 aPanel->setObjectName( anObjectName );