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_UpdateFlags.h"
28 #include <HYDROData_Obstacle.h>
32 #include <SalomeApp_Study.h>
34 #include <LightApp_Application.h>
35 #include <LightApp_UpdateFlags.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_FileDlg.h>
40 static QString lastUsedFilter;
42 HYDROGUI_ImportObstacleFromFileOp::HYDROGUI_ImportObstacleFromFileOp( HYDROGUI_Module* theModule,
43 const bool theIsToShowPanel )
44 : HYDROGUI_Operation( theModule ),
45 myIsToShowPanel ( theIsToShowPanel ),
48 setName( tr( "IMPORT_OBSTACLE_FROM_FILE" ) );
51 HYDROGUI_ImportObstacleFromFileOp::~HYDROGUI_ImportObstacleFromFileOp()
55 void HYDROGUI_ImportObstacleFromFileOp::startOperation()
57 HYDROGUI_Operation::startOperation();
60 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
63 // Reset the panel state
66 // Pass the existing obstacle names to the panel
67 QStringList anObstacleNames =
68 HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
70 aPanel->setObjectNames( anObstacleNames );
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 );
79 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
80 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
86 void HYDROGUI_ImportObstacleFromFileOp::abortOperation()
88 HYDROGUI_Operation::abortOperation();
91 void HYDROGUI_ImportObstacleFromFileOp::commitOperation()
93 HYDROGUI_Operation::commitOperation();
96 bool HYDROGUI_ImportObstacleFromFileOp::processApply( int& theUpdateFlags,
98 QStringList& theBrowseObjectsEntries )
101 QString anObstacleName;
102 Handle(HYDROData_Obstacle) anObstacle;
105 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
107 // Get file name and obstacle name defined by the user
108 aFileName = aPanel->getFileName();
110 QString anEditedName = aPanel->getEditedObjectName().simplified();
112 // Get obstacle to edit
113 if ( !anEditedName.isEmpty() ) {
114 anObstacle = Handle(HYDROData_Obstacle)::DownCast(
115 HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
117 } else if ( myFileDlg ) {
118 // Get file name and file filter defined by the user
119 aFileName = myFileDlg->selectedFile();
120 lastUsedFilter = myFileDlg->selectedFilter();
123 // Check the file name
124 if ( aFileName.isEmpty() ) {
127 QFileInfo aFileInfo( aFileName );
128 if ( !aFileInfo.exists() || !aFileInfo.isReadable() ) {
129 theErrorMsg = tr( "FILE_NOT_EXISTS_OR_CANT_BE_READ" ).arg( aFileName );
133 // Check obstacle name
134 anObstacleName = aPanel->getObjectName().simplified();
135 if ( anObstacleName.isEmpty() ) {
136 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
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 );
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) );
155 anObstacle->SetFillingColor( anObstacle->DefaultFillingColor() );
156 anObstacle->SetBorderColor( anObstacle->DefaultBorderColor() );
159 if ( !anObstacle.IsNull() ) {
160 if ( anObstacle->ImportFromFile( aFileName ) ) {
162 if ( anObstacleName.isEmpty() ) {
163 anObstacleName = HYDROGUI_Tool::GenerateObjectName(
164 module(), aFileInfo.baseName(), QStringList(), true );
166 if ( anObstacle->GetName() != anObstacleName ) {
167 anObstacle->SetName( anObstacleName );
170 anObstacle->Update();
172 // Set operation status
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 );
181 theErrorMsg = tr( "BAD_IMPORTED_OBSTACLE_FILE" ).arg( aFileName );
188 HYDROGUI_InputPanel* HYDROGUI_ImportObstacleFromFileOp::createInputPanel() const
190 HYDROGUI_InputPanel* aPanel = 0;
191 if ( myIsToShowPanel ) {
192 aPanel = new HYDROGUI_GeomObjectDlg( module(), getName(),
193 tr( "DEFAULT_OBSTACLE_NAME" ), true );