Salome HOME
85320817fb01f4db895905c97e60d42ee37730a1
[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_ObstacleDlg.h"
26
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30
31 #include <HYDROData_Obstacle.h>
32
33 #include <GEOMBase.h>
34
35 #include <SalomeApp_Study.h>
36
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
39
40 #include <SUIT_Desktop.h>
41 #include <SUIT_FileDlg.h>
42
43 static QString lastUsedFilter;
44
45 HYDROGUI_ImportObstacleFromFileOp::HYDROGUI_ImportObstacleFromFileOp( HYDROGUI_Module* theModule, 
46                                                                       const bool theIsToShowPanel )
47 : HYDROGUI_Operation( theModule ),
48   myIsToShowPanel ( theIsToShowPanel ),
49   myFileDlg( 0 )
50 {
51   setName( tr( "IMPORT_OBSTACLE_FROM_FILE" ) );
52 }
53
54 HYDROGUI_ImportObstacleFromFileOp::~HYDROGUI_ImportObstacleFromFileOp()
55 {
56 }
57
58 void HYDROGUI_ImportObstacleFromFileOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   // Get panel
63   HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
64
65   if ( aPanel ) {
66     // Reset the panel state
67     aPanel->reset();
68
69     // Pass the existing obstacle names to the panel
70     QStringList anObstacles = 
71       HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
72
73     aPanel->setObstacleNames( anObstacles );
74   } else {
75     myFileDlg = new SUIT_FileDlg( module()->application()->desktop(), true );
76     myFileDlg->setWindowTitle( getName() );
77     myFileDlg->setFilter( tr("OBSTACLE_FILTER") );
78     if ( !lastUsedFilter.isEmpty() ) {
79       myFileDlg->selectFilter( lastUsedFilter );
80     }
81
82     connect( myFileDlg, SIGNAL( accepted() ),      this, SLOT( onApply()  ) );
83     connect( myFileDlg, SIGNAL( rejected() ),      this, SLOT( onCancel() ) );
84
85     myFileDlg->exec();
86   }
87 }
88
89 void HYDROGUI_ImportObstacleFromFileOp::abortOperation()
90 {
91   HYDROGUI_Operation::abortOperation();
92 }
93
94 void HYDROGUI_ImportObstacleFromFileOp::commitOperation()
95 {
96   HYDROGUI_Operation::commitOperation();
97 }
98
99 bool HYDROGUI_ImportObstacleFromFileOp::processApply( int& theUpdateFlags,
100                                                       QString& theErrorMsg )
101 {
102   QString aFileName;
103   QString anObstacleName;
104   Handle(HYDROData_Obstacle) anObstacle;
105
106   // Get panel
107   HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
108   if ( aPanel ) {
109     // Get file name and obstacle name defined by the user
110     aFileName = aPanel->getFileName();
111       
112     QString anEditedName = aPanel->getEditedObstacleName().simplified();
113
114     // Get obstacle to edit
115     if ( !anEditedName.isEmpty() ) {
116       anObstacle = Handle(HYDROData_Obstacle)::DownCast(
117         HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
118     }
119
120     anObstacleName = anEditedName; //TODO: aPanel->getObstacleName();
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   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   bool anIsOk = false;
135  
136   // If the obstacle for edit is null - create new obstacle object
137   if ( anObstacle.IsNull() ) {
138     anObstacle = Handle(HYDROData_Obstacle)::DownCast( doc()->CreateObject(KIND_OBSTACLE) );
139   }
140
141   if ( !anObstacle.IsNull() ) {
142     if ( anObstacle->ImportFromFile( aFileName ) ) {
143       // Set name
144       if ( anObstacleName.isEmpty() ) {
145         anObstacleName = HYDROGUI_Tool::GenerateObjectName( module(), aFileInfo.baseName() );
146       }
147       if ( anObstacle->GetName() != anObstacleName ) {
148         anObstacle->SetName( anObstacleName );
149       }
150
151       // Set operation status
152       anIsOk = true;
153       theUpdateFlags = UF_Model;
154     } else {
155       theErrorMsg = tr( "BAD_IMPORTED_OBSTACLE_FILE" ).arg( aFileName );
156     }
157   }
158   
159   return anIsOk;
160 }
161
162 HYDROGUI_InputPanel* HYDROGUI_ImportObstacleFromFileOp::createInputPanel() const
163 {
164   HYDROGUI_InputPanel* aPanel = 0;
165   if ( myIsToShowPanel ) {
166     aPanel = new HYDROGUI_ObstacleDlg( module(), getName(), true );
167   }
168
169   return aPanel;
170 }