Salome HOME
Feature 32: Image presentation in OCCViewer.
[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
36 #include <QFileInfo>
37
38 HYDROGUI_ImportBathymetryOp::HYDROGUI_ImportBathymetryOp( HYDROGUI_Module* theModule )
39 : HYDROGUI_Operation( theModule )
40 {
41   setName( tr( "IMPORT_BATHYMETRY" ) );
42 }
43
44 HYDROGUI_ImportBathymetryOp::~HYDROGUI_ImportBathymetryOp()
45 {
46 }
47
48 void HYDROGUI_ImportBathymetryOp::startOperation()
49 {
50   HYDROGUI_Operation::startOperation();
51
52   HYDROGUI_ImportBathymetryDlg* aPanel = 
53     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
54   if ( !aPanel )
55     return;
56
57   aPanel->reset();
58 }
59
60 void HYDROGUI_ImportBathymetryOp::abortOperation()
61 {
62   HYDROGUI_Operation::abortOperation();
63 }
64
65 void HYDROGUI_ImportBathymetryOp::commitOperation()
66 {
67   HYDROGUI_Operation::commitOperation();
68 }
69
70 HYDROGUI_InputPanel* HYDROGUI_ImportBathymetryOp::createInputPanel() const
71 {
72   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportBathymetryDlg( module(), getName() );
73   
74   connect ( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
75
76   return aPanel;
77 }
78
79 bool HYDROGUI_ImportBathymetryOp::processApply( int& theUpdateFlags,
80                                                 QString& theErrorMsg )
81 {
82   HYDROGUI_ImportBathymetryDlg* aPanel = 
83     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
84   if ( !aPanel )
85     return false;
86
87   QString anObjectName = aPanel->getObjectName().simplified();
88   if ( anObjectName.isEmpty() )
89   {
90     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
91     return false;
92   }
93
94   QString aFileName = aPanel->getFileName().simplified();
95   if ( aFileName.isEmpty() )
96   {
97     theErrorMsg = tr( "INCORRECT_FILE_NAME" );
98     return false;
99   }
100
101   QFileInfo aFileInfo( aFileName );
102   if ( !aFileInfo.exists() || !aFileInfo.isReadable() )
103   {
104     theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
105     return false;
106   }
107
108   // check that there are no other objects with the same name in the document
109   Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
110   if ( !anObject.IsNull() )
111   {
112     theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
113     return false;
114   }
115
116   Handle(HYDROData_Bathymetry) aBathymetryObj = 
117     Handle(HYDROData_Bathymetry)::DownCast( doc()->CreateObject( KIND_BATHYMETRY ) );
118   if ( aBathymetryObj.IsNull() )
119     return false;
120
121   if ( !aBathymetryObj->ImportFromFile( aFileName ) )
122   {
123     theErrorMsg = tr( "BAD_IMPORTED_BATHYMETRY_FILE" ).arg( aFileName );
124     return false;
125   }
126
127   aBathymetryObj->SetName( anObjectName );
128
129   theUpdateFlags = UF_Model;
130   return true;
131 }
132
133 void HYDROGUI_ImportBathymetryOp::onFileSelected()
134 {
135   HYDROGUI_ImportBathymetryDlg* aPanel = 
136     ::qobject_cast<HYDROGUI_ImportBathymetryDlg*>( inputPanel() );
137   if ( !aPanel )
138     return;
139
140   QString anObjectName = aPanel->getObjectName().simplified();
141   if ( anObjectName.isEmpty() )
142   {
143     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Bathymetry" );
144     aPanel->setObjectName( anObjectName );
145   }
146 }
147
148
149