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