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