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