Salome HOME
Import/Export // API revision. p2
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportPolylineOp.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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_ImportPolylineOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_Tool.h"
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Polyline3D.h>
31 #include <HYDROGUI_DataObject.h>
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_ShapeFile.h>
35
36 #include <HYDROData_Profile.h>
37
38 #include <SUIT_Desktop.h>
39 #include <SUIT_FileDlg.h>
40 #include <LightApp_Application.h>
41
42 #include <QApplication>
43 #include <QFile>
44 #include <QFileInfo>
45 #include <SUIT_MessageBox.h>
46
47
48 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
49 : HYDROGUI_Operation( theModule )
50 {
51   setName( tr( "IMPORT_POLYLINE" ) );
52 }
53
54 HYDROGUI_ImportPolylineOp::~HYDROGUI_ImportPolylineOp()
55 {
56 }
57
58 void HYDROGUI_ImportPolylineOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
63   myFileDlg->setWindowTitle( getName() );
64   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
65   myFileDlg->setFilter( tr("POLYLINE_FILTER") );
66
67   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
68   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
69
70   myFileDlg->exec();
71 }
72
73 void HYDROGUI_ImportPolylineOp::onApply()
74 {
75   if ( !myFileDlg )
76   {
77     abort();
78     return;
79   }
80
81   QStringList aFileNames = myFileDlg->selectedFiles();
82   
83   QApplication::setOverrideCursor( Qt::WaitCursor );  
84   startDocOperation();
85
86   foreach (QString aFileName, aFileNames) 
87   {
88     if ( aFileName.isEmpty() )
89       continue;
90
91     QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
92
93     if (anExt == "shp")
94     {
95       HYDROData_ShapeFile anImporter;
96       NCollection_Sequence<Handle_HYDROData_Entity> theEntities;
97       if (anImporter.ImportPolylines(doc(), aFileName, theEntities ))
98         UpdateView(theEntities);
99       else
100         SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), "Cannot import polyline;\nThe shape type is incorrect" );
101     }
102   }
103   if (!aFileNames.empty())
104   {
105     commitDocOperation();
106     commit();
107     module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
108   }
109   else
110     abort();
111   
112   QApplication::restoreOverrideCursor();
113 }
114
115 void HYDROGUI_ImportPolylineOp::UpdateView( NCollection_Sequence<Handle_HYDROData_Entity>& anEntities)
116 {
117   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
118   if ( anActiveViewId == 0 )
119     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
120
121   for (int i = 1; i <= anEntities.Size() ; i++)
122   {
123     anEntities(i)->Update();
124     module()->setObjectVisible( anActiveViewId, anEntities(i), true );
125     module()->setIsToUpdate( anEntities(i) );
126   }
127 }