Salome HOME
refs #430: incorrect coordinates in dump polyline
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportObstacleFromFileOp.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_ImportObstacleFromFileOp.h"
24
25 #include "HYDROGUI_GeomObjectDlg.h"
26 #include <HYDROGUI_DataObject.h>
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Obstacle.h>
33
34 #include <GEOMBase.h>
35
36 #include <SalomeApp_Study.h>
37
38 #include <LightApp_Application.h>
39 #include <LightApp_UpdateFlags.h>
40
41 #include <SUIT_Desktop.h>
42 #include <SUIT_FileDlg.h>
43
44 static QString lastUsedFilter;
45
46 HYDROGUI_ImportObstacleFromFileOp::HYDROGUI_ImportObstacleFromFileOp( HYDROGUI_Module* theModule, 
47                                                                       const bool theIsToShowPanel )
48 : HYDROGUI_Operation( theModule ),
49   myIsToShowPanel ( theIsToShowPanel ),
50   myFileDlg( 0 )
51 {
52   setName( tr( "IMPORT_OBSTACLE_FROM_FILE" ) );
53 }
54
55 HYDROGUI_ImportObstacleFromFileOp::~HYDROGUI_ImportObstacleFromFileOp()
56 {
57 }
58
59 void HYDROGUI_ImportObstacleFromFileOp::startOperation()
60 {
61   HYDROGUI_Operation::startOperation();
62
63   // Get panel
64   HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
65
66   if ( aPanel ) {
67     // Reset the panel state
68     aPanel->reset();
69
70     // Pass the existing obstacle names to the panel
71     QStringList anObstacleNames = 
72       HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
73
74     aPanel->setObjectNames( anObstacleNames );
75   } else {
76     myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
77     myFileDlg->setWindowTitle( getName() );
78     myFileDlg->setFilter( tr("OBSTACLE_FILTER") );
79     if ( !lastUsedFilter.isEmpty() ) {
80       myFileDlg->selectFilter( lastUsedFilter );
81     }
82
83     connect( myFileDlg, SIGNAL( accepted() ),      this, SLOT( onApply()  ) );
84     connect( myFileDlg, SIGNAL( rejected() ),      this, SLOT( onCancel() ) );
85
86     myFileDlg->exec();
87   }
88 }
89
90 void HYDROGUI_ImportObstacleFromFileOp::abortOperation()
91 {
92   HYDROGUI_Operation::abortOperation();
93 }
94
95 void HYDROGUI_ImportObstacleFromFileOp::commitOperation()
96 {
97   HYDROGUI_Operation::commitOperation();
98 }
99
100 bool HYDROGUI_ImportObstacleFromFileOp::processApply( int& theUpdateFlags,
101                                                       QString& theErrorMsg,
102                                                       QStringList& theBrowseObjectsEntries )
103 {
104   QString aFileName;
105   QString anObstacleName;
106   Handle(HYDROData_Obstacle) anObstacle;
107
108   // Get panel
109   HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
110   if ( aPanel ) {
111     // Get file name and obstacle name defined by the user
112     aFileName = aPanel->getFileName();
113       
114     QString anEditedName = aPanel->getEditedObjectName().simplified();
115
116     // Get obstacle to edit
117     if ( !anEditedName.isEmpty() ) {
118       anObstacle = Handle(HYDROData_Obstacle)::DownCast(
119         HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
120     }
121   } else if ( myFileDlg ) {
122     // Get file name and file filter defined by the user
123     aFileName = myFileDlg->selectedFile();
124     lastUsedFilter = myFileDlg->selectedFilter();
125   }
126
127   // Check the file name
128   if ( aFileName.isEmpty() ) {
129     return false;
130   }
131   QFileInfo aFileInfo( aFileName );
132   if ( !aFileInfo.exists() || !aFileInfo.isReadable() ) {
133     theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
134     return false;
135   }
136
137   // Check obstacle name
138   anObstacleName = aPanel->getObjectName().simplified();
139   if ( anObstacleName.isEmpty() ) {
140     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
141     return false;
142   }
143
144   if( anObstacle.IsNull() || anObstacle->GetName() != anObstacleName ) {
145     // check that there are no other objects with the same name in the document
146     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObstacleName );
147     if( !anObject.IsNull() ) {
148       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObstacleName );
149       return false;
150     }
151   }
152
153   bool anIsOk = false;
154  
155   // If the obstacle for edit is null - create new obstacle object
156   if ( anObstacle.IsNull() ) {
157     anObstacle = Handle(HYDROData_Obstacle)::DownCast( doc()->CreateObject(KIND_OBSTACLE) );
158
159     anObstacle->SetFillingColor( HYDROData_Obstacle::DefaultFillingColor() );
160     anObstacle->SetBorderColor( HYDROData_Obstacle::DefaultBorderColor() );
161   }
162
163   if ( !anObstacle.IsNull() ) {
164     if ( anObstacle->ImportFromFile( aFileName ) ) {
165       // Set name
166       if ( anObstacleName.isEmpty() ) {
167         anObstacleName = HYDROGUI_Tool::GenerateObjectName( 
168           module(), aFileInfo.baseName(), QStringList(), true );
169       }
170       if ( anObstacle->GetName() != anObstacleName ) {
171         anObstacle->SetName( anObstacleName );
172       }
173
174       anObstacle->Update();
175
176       // Set operation status
177       anIsOk = true;
178       module()->setIsToUpdate( anObstacle );
179       theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
180       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( anObstacle );
181       theBrowseObjectsEntries.append( anEntry );
182
183
184     } else {
185       theErrorMsg = tr( "BAD_IMPORTED_OBSTACLE_FILE" ).arg( aFileName );
186     }
187   }
188   
189   return anIsOk;
190 }
191
192 HYDROGUI_InputPanel* HYDROGUI_ImportObstacleFromFileOp::createInputPanel() const
193 {
194   HYDROGUI_InputPanel* aPanel = 0;
195   if ( myIsToShowPanel ) {
196     aPanel = new HYDROGUI_GeomObjectDlg( module(), getName(), 
197                                          tr( "DEFAULT_OBSTACLE_NAME" ), true );
198   }
199
200   return aPanel;
201 }