Salome HOME
lot 6 :: import polylines form XY/XYZ + tests
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportPolylineOp.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ImportPolylineOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_UpdateFlags.h"
24 #include "HYDROGUI_Tool2.h"
25 #include <HYDROData_PolylineXY.h>
26 #include <HYDROData_Polyline3D.h>
27 #include <HYDROGUI_DataObject.h>
28 #include <HYDROData_Bathymetry.h>
29 #include <HYDROData_Iterator.h>
30 #include <HYDROData_ShapeFile.h>
31 #include <HYDROData_Tool.h>
32
33 #include <HYDROData_Profile.h>
34
35 #include <SUIT_Desktop.h>
36 #include <SUIT_FileDlg.h>
37 #include <LightApp_Application.h>
38
39 #include <QApplication>
40 #include <QFile>
41 #include <QFileInfo>
42 #include <SUIT_MessageBox.h>
43 #include <gp_XY.hxx>
44
45
46 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
47 : HYDROGUI_Operation( theModule )
48 {
49   setName( tr( "IMPORT_POLYLINE" ) );
50 }
51
52 HYDROGUI_ImportPolylineOp::~HYDROGUI_ImportPolylineOp()
53 {
54 }
55
56 void HYDROGUI_ImportPolylineOp::startOperation()
57 {
58   HYDROGUI_Operation::startOperation();
59
60   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
61   myFileDlg->setWindowTitle( getName() );
62   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
63   myFileDlg->setNameFilter( tr("POLYLINE_FILTER") );
64
65   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
66   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
67
68   myFileDlg->exec();
69 }
70
71 NCollection_Sequence<Handle(HYDROData_Entity)> HYDROGUI_ImportPolylineOp::ImportPolyOp(
72   const QStringList& aFileNames, Handle(HYDROData_Document) theDocument,
73   HYDROGUI_Module* module, HYDROData_ShapeFile::ImportShapeType theShapeTypesToImport)
74 {
75   NCollection_Sequence<Handle(HYDROData_Entity)> importedEntities;
76   foreach (QString aFileName, aFileNames) 
77   {
78     if ( aFileName.isEmpty() )
79       continue;
80
81     QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
82     anExt.toLower();
83     bool importXY = false;
84     if (anExt == "xyz")
85     {
86       importXY = SUIT_MessageBox::question( module->getApp()->desktop(),
87         tr( "IMPORT_POLYLINE" ),
88         tr( "IMPORT_POLYLINE_XY_PART_ONLY" ),
89         QMessageBox::Yes | QMessageBox::No, 
90         SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
91     }
92     if (anExt == "shp")
93     {
94       HYDROData_ShapeFile anImporter;
95       NCollection_Sequence<Handle(HYDROData_Entity)> theEntities;
96       int aShapeTypeOfFile = -1;
97       int aStat = anImporter.ImportPolylines(theDocument, aFileName, theEntities, 
98         aShapeTypeOfFile, theShapeTypesToImport ); 
99       if (aStat == 1 || aStat == 2)
100       {
101         //try to import DBF
102         QString aDBFFileName;
103         aDBFFileName = aFileName.simplified().replace( aFileName.simplified().size() - 4, 4, ".dbf");
104         bool DBF_Stat = anImporter.DBF_OpenDBF(aDBFFileName);
105         if (DBF_Stat)
106         {
107           QStringList aFieldList = anImporter.DBF_GetFieldList();
108           int nbRecords = anImporter.DBF_GetNbRecords();
109           assert (theEntities.Length() == nbRecords);
110           if (theEntities.Length() == nbRecords)
111           {
112             int indNameAttrFound = -1;
113             int k = 0;
114             bool bUseNameAttrFound = false;
115             for (QStringList::iterator it = aFieldList.begin(); it != aFieldList.end(); it++, k++)
116               if (QString::compare(*it, "name", Qt::CaseInsensitive) == 0)
117               {
118                 indNameAttrFound = k;
119                 break;
120               }
121
122               if (indNameAttrFound != -1)
123                 bUseNameAttrFound = SUIT_MessageBox::question( module->getApp()->desktop(),
124                                tr( "IMPORT_POLYLINE" ),
125                                tr( "IMPORT_POLYLINE_USE_NAME_ATTR" ),
126                                QMessageBox::Yes | QMessageBox::No, 
127                                SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
128   
129             std::vector<std::vector<HYDROData_ShapeFile::DBF_AttrValue>> anAttrVV;
130             for (int i = 0; i < aFieldList.size(); i++)
131             {
132               std::vector<HYDROData_ShapeFile::DBF_AttrValue> anAttrV;
133               anAttrVV.push_back(anAttrV);
134               anImporter.DBF_GetAttributeList(i, anAttrVV[i] );
135             }
136
137             int indNULL = 1;
138             for (int i = 1; i <= theEntities.Length(); i++)
139             {
140               Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theEntities(i) );
141               QStringList aDBFinfo;
142               aDBFinfo << aFieldList; //first, the table header
143               for (int j = 0; j < aFieldList.size(); j++)
144               {
145                 QString attr;
146                 const HYDROData_ShapeFile::DBF_AttrValue& attrV = anAttrVV[j][i-1];
147                 if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_String)
148                   aDBFinfo << attrV.myStrVal;
149                 else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Integer)
150                   aDBFinfo << QString::number(attrV.myIntVal);
151                 else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Double)
152                   aDBFinfo << QString::number(attrV.myDoubleVal);
153                 else 
154                   aDBFinfo << "";
155               }
156               assert (aDBFinfo.size() / 2 == aFieldList.size());
157               aPolylineXY->SetDBFInfo(aDBFinfo);
158               if (bUseNameAttrFound)
159               {
160                 QString nameOfPoly = aDBFinfo[aDBFinfo.size() / 2 + indNameAttrFound];
161                 if (!nameOfPoly.isEmpty())
162                   aPolylineXY->SetName(nameOfPoly);
163                 else
164                 {
165                   aPolylineXY->SetName("null_name_" + QString::number(indNULL));
166                   indNULL++;
167                 }
168               }
169             }
170           }
171         }        
172       }
173       if (aStat == 1)
174         UpdateView(module, theEntities);
175       else if (aStat == 2)
176       {
177         UpdateView(module, theEntities);
178         if (theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All) //if other flag = > no need to show this messagebox
179           SUIT_MessageBox::information(module->getApp()->desktop(), 
180             tr( "IMPORT_POLYLINE" ), tr ("POLYGON_IMPORTED_AS_POLYLINE"));
181       }
182       else
183       {
184         QString aMess;
185         if (theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType::ImportShapeType_Polygon)
186           aMess += tr ("POLYLINE_IMPORT_FAILED_AS_POLYGON") + ";\n";
187         else
188           aMess += tr ("POLYLINE_IMPORT_FAILED_AS_POLYLINE") + ";\n";
189
190         if (aStat == -1)
191           aMess += tr ("CANT_OPEN_SHP");
192         else if (aStat == -2)
193           aMess += tr ("CANT_OPEN_SHX");
194         else 
195           aMess += tr ("SHAPE_TYPE_IS") + anImporter.GetShapeTypeName(aShapeTypeOfFile);
196         SUIT_MessageBox::warning( module->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), aMess);
197       }
198       importedEntities.Append(theEntities);
199     }
200     else if ( anExt == "xy" || (importXY && anExt == "xyz"))
201     {
202       HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, true, importedEntities);   
203     }
204     else if (anExt == "xyz")
205     {
206       HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, false, importedEntities);
207     }
208   }
209   return importedEntities;
210 }
211
212 void HYDROGUI_ImportPolylineOp::onApply()
213 {
214   if ( !myFileDlg )
215   {
216     abort();
217     return;
218   }
219
220   QStringList aFileNames = myFileDlg->selectedFiles();
221   
222   QApplication::setOverrideCursor( Qt::WaitCursor );  
223   startDocOperation();
224
225   ImportPolyOp(aFileNames, doc(), module(), HYDROData_ShapeFile::ImportShapeType_All);
226  
227   if (!aFileNames.empty())
228   {
229     commitDocOperation();
230     commit();
231     module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init | UF_OCCViewer );
232   }
233   else
234     abort();
235   
236   QApplication::restoreOverrideCursor();
237 }
238
239 void HYDROGUI_ImportPolylineOp::UpdateView( HYDROGUI_Module* module, NCollection_Sequence<Handle(HYDROData_Entity)>& anEntities)
240 {
241   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module );
242   if ( anActiveViewId == 0 )
243     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module );
244
245   for (int i = 1; i <= anEntities.Size() ; i++)
246   {
247     anEntities(i)->Update();
248     module->setObjectVisible( anActiveViewId, anEntities(i), true );
249     module->setIsToUpdate( anEntities(i) );
250   }
251 }