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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ImportObstacleFromFileOp.h"
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"
29 #include <HYDROData_Obstacle.h>
33 #include <SalomeApp_Study.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_UpdateFlags.h>
38 #include <SUIT_Desktop.h>
39 #include <SUIT_FileDlg.h>
41 static QString lastUsedFilter;
43 HYDROGUI_ImportObstacleFromFileOp::HYDROGUI_ImportObstacleFromFileOp( HYDROGUI_Module* theModule,
44 const bool theIsToShowPanel )
45 : HYDROGUI_Operation( theModule ),
46 myIsToShowPanel ( theIsToShowPanel ),
49 setName( tr( "IMPORT_OBSTACLE_FROM_FILE" ) );
52 HYDROGUI_ImportObstacleFromFileOp::~HYDROGUI_ImportObstacleFromFileOp()
56 void HYDROGUI_ImportObstacleFromFileOp::startOperation()
58 HYDROGUI_Operation::startOperation();
61 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
64 // Reset the panel state
67 // Pass the existing obstacle names to the panel
68 QStringList anObstacleNames =
69 HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
71 aPanel->setObjectNames( anObstacleNames );
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 );
80 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
81 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
87 void HYDROGUI_ImportObstacleFromFileOp::abortOperation()
89 HYDROGUI_Operation::abortOperation();
92 void HYDROGUI_ImportObstacleFromFileOp::commitOperation()
94 HYDROGUI_Operation::commitOperation();
97 bool HYDROGUI_ImportObstacleFromFileOp::processApply( int& theUpdateFlags,
99 QStringList& theBrowseObjectsEntries )
102 QString anObstacleName;
103 Handle(HYDROData_Obstacle) anObstacle;
106 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
108 // Get file name and obstacle name defined by the user
109 aFileName = aPanel->getFileName();
111 QString anEditedName = aPanel->getEditedObjectName().simplified();
113 // Get obstacle to edit
114 if ( !anEditedName.isEmpty() ) {
115 anObstacle = Handle(HYDROData_Obstacle)::DownCast(
116 HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
118 } else if ( myFileDlg ) {
119 // Get file name and file filter defined by the user
120 aFileName = myFileDlg->selectedFile();
121 lastUsedFilter = myFileDlg->selectedNameFilter();
124 // Check the file name
125 if ( aFileName.isEmpty() ) {
128 QFileInfo aFileInfo( aFileName );
129 if ( !aFileInfo.exists() || !aFileInfo.isReadable() ) {
130 theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
134 // Check obstacle name
135 anObstacleName = aPanel->getObjectName().simplified();
136 if ( anObstacleName.isEmpty() ) {
137 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
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 );
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) );
156 anObstacle->SetFillingColor( anObstacle->DefaultFillingColor() );
157 anObstacle->SetBorderColor( anObstacle->DefaultBorderColor() );
160 if ( !anObstacle.IsNull() ) {
161 if ( anObstacle->ImportFromFile( aFileName ) ) {
163 if ( anObstacleName.isEmpty() ) {
164 anObstacleName = HYDROGUI_Tool::GenerateObjectName(
165 module(), aFileInfo.baseName(), QStringList(), true );
167 if ( anObstacle->GetName() != anObstacleName ) {
168 anObstacle->SetName( anObstacleName );
171 anObstacle->Update();
173 // Set operation status
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 );
182 theErrorMsg = tr( "BAD_IMPORTED_OBSTACLE_FILE" ).arg( aFileName );
189 HYDROGUI_InputPanel* HYDROGUI_ImportObstacleFromFileOp::createInputPanel() const
191 HYDROGUI_InputPanel* aPanel = 0;
192 if ( myIsToShowPanel ) {
193 aPanel = new HYDROGUI_GeomObjectDlg( module(), getName(),
194 tr( "DEFAULT_OBSTACLE_NAME" ), true );