Salome HOME
The type of section for profile added (Bug #172).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportBathymetryOp.cxx
1 // Copyright (C) 2007-2013  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.
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_ImportBathymetryDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_Bathymetry.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_UpdateFlags.h>
35 #include <SUIT_ViewManager.h>
36 #include <SVTK_ViewModel.h>
37
38 #include <QFileInfo>
39
40 HYDROGUI_ImportBathymetryOp::HYDROGUI_ImportBathymetryOp( HYDROGUI_Module* theModule, 
41                                                          const bool theIsEdit  )
42 : HYDROGUI_Operation( theModule ),
43   myIsEdit( theIsEdit )
44 {
45   setName( theIsEdit ? tr( "EDIT_IMPORTED_BATHYMETRY" ) : tr( "IMPORT_BATHYMETRY" ) );
46 }
47
48 HYDROGUI_ImportBathymetryOp::~HYDROGUI_ImportBathymetryOp()
49 {
50 }
51
52 void HYDROGUI_ImportBathymetryOp::startOperation()
53 {
54   HYDROGUI_Operation::startOperation();
55
56   HYDROGUI_ImportBathymetryDlg* aPanel = 
57     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
58   if ( !aPanel )
59     return;
60
61   aPanel->reset();
62
63   if( myIsEdit )
64   {
65     myEditedObject = Handle(HYDROData_Bathymetry)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
66     if( !myEditedObject.IsNull() )
67     {
68       QString aName = myEditedObject->GetName();
69       QString aFileName = myEditedObject->GetFilePath();
70       aPanel->setObjectName( aName );
71       aPanel->setFileName( aFileName );
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 {
98   HYDROGUI_ImportBathymetryDlg* aPanel = 
99     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
100   if ( !aPanel )
101     return false;
102
103   QString anObjectName = aPanel->getObjectName().simplified();
104   if ( anObjectName.isEmpty() )
105   {
106     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
107     return false;
108   }
109
110   QString aFileName = aPanel->getFileName().simplified();
111   if ( aFileName.isEmpty() )
112   {
113     theErrorMsg = tr( "INCORRECT_FILE_NAME" );
114     return false;
115   }
116
117   QFileInfo aFileInfo( aFileName );
118   if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
119   {
120     theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
121     return false;
122   }
123
124   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
125   {
126     // check that there are no other objects with the same name in the document
127     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
128     if( !anObject.IsNull() )
129     {
130       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
131       return false;
132     }
133   }
134
135   Handle(HYDROData_Bathymetry) aBathymetryObj;
136   if ( myIsEdit )
137   {
138     aBathymetryObj = myEditedObject;
139   }
140   else
141   {
142     aBathymetryObj = 
143       Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
144   }
145   if ( aBathymetryObj.IsNull() )
146     return false;
147
148   if ( !aBathymetryObj->ImportFromFile( aFileName ) )
149   {
150     theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileName );
151     return false;
152   }
153
154   aBathymetryObj->SetName( anObjectName );
155
156   aBathymetryObj->Update();
157
158   // Activate VTK viewer and show the bathymetry
159   SUIT_ViewManager* aVTKMgr = 0;
160   SUIT_ViewManager* aViewMgr = module()->getApp()->activeViewManager();
161   // Try to get a VTK viewer as an active or existing one
162   if ( aViewMgr )
163   {
164     if ( aViewMgr->getType() == SVTK_Viewer::Type() )
165     {
166       aVTKMgr = aViewMgr;
167     }
168     else
169     {
170       aVTKMgr = module()->getApp()->viewManager( SVTK_Viewer::Type() );
171     }
172   }
173   // If there is no VTK viewer yet then create a new one
174   if ( !aVTKMgr )
175   {
176     aVTKMgr = module()->getApp()->createViewManager( SVTK_Viewer::Type() );
177   }
178   // Set the bathymetry visible in the VTK viewer
179   if ( aVTKMgr )
180   {
181     module()->setObjectVisible( (size_t)aVTKMgr->getViewModel(), aBathymetryObj, true );
182   }
183
184   theUpdateFlags = UF_Model | UF_VTKViewer | UF_VTK_Init | UF_VTK_Forced;
185   return true;
186 }
187
188 void HYDROGUI_ImportBathymetryOp::onFileSelected()
189 {
190   HYDROGUI_ImportBathymetryDlg* aPanel = 
191     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
192   if ( !aPanel )
193     return;
194
195   QString anObjectName = aPanel->getObjectName().simplified();
196   if ( anObjectName.isEmpty() )
197   {
198     anObjectName = aPanel->getFileName();
199     if ( !anObjectName.isEmpty() ) {
200         anObjectName = QFileInfo( anObjectName ).baseName();
201     }
202
203     if ( anObjectName.isEmpty() ) {
204       anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
205     }
206     aPanel->setObjectName( anObjectName );
207   }
208 }
209
210
211