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