1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportObstacleFromFileOp.h"
25 #include "HYDROGUI_ObstacleDlg.h"
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
32 #include <HYDROData_Obstacle.h>
36 #include <SalomeApp_Study.h>
38 #include <LightApp_Application.h>
39 #include <LightApp_UpdateFlags.h>
41 #include <SUIT_Desktop.h>
42 #include <SUIT_FileDlg.h>
44 static QString lastUsedFilter;
46 HYDROGUI_ImportObstacleFromFileOp::HYDROGUI_ImportObstacleFromFileOp( HYDROGUI_Module* theModule,
47 const bool theIsToShowPanel )
48 : HYDROGUI_Operation( theModule ),
49 myIsToShowPanel ( theIsToShowPanel ),
52 setName( tr( "IMPORT_OBSTACLE_FROM_FILE" ) );
55 HYDROGUI_ImportObstacleFromFileOp::~HYDROGUI_ImportObstacleFromFileOp()
59 void HYDROGUI_ImportObstacleFromFileOp::startOperation()
61 HYDROGUI_Operation::startOperation();
64 HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
67 // Reset the panel state
70 // Pass the existing obstacle names to the panel
71 QStringList anObstacles =
72 HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
74 aPanel->setObstacleNames( anObstacles );
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 );
83 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
84 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
90 void HYDROGUI_ImportObstacleFromFileOp::abortOperation()
92 HYDROGUI_Operation::abortOperation();
95 void HYDROGUI_ImportObstacleFromFileOp::commitOperation()
97 HYDROGUI_Operation::commitOperation();
100 bool HYDROGUI_ImportObstacleFromFileOp::processApply( int& theUpdateFlags,
101 QString& theErrorMsg )
104 QString anObstacleName;
105 Handle(HYDROData_Obstacle) anObstacle;
108 HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
110 // Get file name and obstacle name defined by the user
111 aFileName = aPanel->getFileName();
113 QString anEditedName = aPanel->getEditedObstacleName().simplified();
115 // Get obstacle to edit
116 if ( !anEditedName.isEmpty() ) {
117 anObstacle = Handle(HYDROData_Obstacle)::DownCast(
118 HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
120 } else if ( myFileDlg ) {
121 // Get file name and file filter defined by the user
122 aFileName = myFileDlg->selectedFile();
123 lastUsedFilter = myFileDlg->selectedFilter();
126 // Check the file name
127 if ( aFileName.isEmpty() ) {
130 QFileInfo aFileInfo( aFileName );
131 if ( !aFileInfo.exists() || !aFileInfo.isReadable() ) {
132 theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
136 // Check obstacle name
137 anObstacleName = aPanel->getObstacleName().simplified();
138 if ( anObstacleName.isEmpty() ) {
139 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
143 if( anObstacle.IsNull() || anObstacle->GetName() != anObstacleName ) {
144 // check that there are no other objects with the same name in the document
145 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObstacleName );
146 if( !anObject.IsNull() ) {
147 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObstacleName );
154 // If the obstacle for edit is null - create new obstacle object
155 if ( anObstacle.IsNull() ) {
156 anObstacle = Handle(HYDROData_Obstacle)::DownCast( doc()->CreateObject(KIND_OBSTACLE) );
158 anObstacle->SetFillingColor( HYDROData_Obstacle::DefaultFillingColor() );
159 anObstacle->SetBorderColor( HYDROData_Obstacle::DefaultBorderColor() );
162 if ( !anObstacle.IsNull() ) {
163 if ( anObstacle->ImportFromFile( aFileName ) ) {
165 if ( anObstacleName.isEmpty() ) {
166 anObstacleName = HYDROGUI_Tool::GenerateObjectName(
167 module(), aFileInfo.baseName(), QStringList(), true );
169 if ( anObstacle->GetName() != anObstacleName ) {
170 anObstacle->SetName( anObstacleName );
173 anObstacle->Update();
175 // Set operation status
177 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;;
179 theErrorMsg = tr( "BAD_IMPORTED_OBSTACLE_FILE" ).arg( aFileName );
186 HYDROGUI_InputPanel* HYDROGUI_ImportObstacleFromFileOp::createInputPanel() const
188 HYDROGUI_InputPanel* aPanel = 0;
189 if ( myIsToShowPanel ) {
190 aPanel = new HYDROGUI_ObstacleDlg( module(), getName(), true );