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_ImportPolylineOp.h"
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>
32 #include <HYDROData_Profile.h>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_FileDlg.h>
36 #include <LightApp_Application.h>
38 #include <QApplication>
41 #include <SUIT_MessageBox.h>
44 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
45 : HYDROGUI_Operation( theModule )
47 setName( tr( "IMPORT_POLYLINE" ) );
50 HYDROGUI_ImportPolylineOp::~HYDROGUI_ImportPolylineOp()
54 void HYDROGUI_ImportPolylineOp::startOperation()
56 HYDROGUI_Operation::startOperation();
58 myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
59 myFileDlg->setWindowTitle( getName() );
60 myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
61 myFileDlg->setNameFilter( tr("POLYLINE_FILTER") );
63 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
64 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
69 void HYDROGUI_ImportPolylineOp::onApply()
77 QStringList aFileNames = myFileDlg->selectedFiles();
79 QApplication::setOverrideCursor( Qt::WaitCursor );
82 foreach (QString aFileName, aFileNames)
84 if ( aFileName.isEmpty() )
87 QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
91 HYDROData_ShapeFile anImporter;
92 NCollection_Sequence<Handle(HYDROData_Entity)> theEntities;
93 int aShapeTypeOfFile = -1;
94 int aStat = anImporter.ImportPolylines(doc(), aFileName, theEntities, aShapeTypeOfFile );
95 if (aStat == 1 || aStat == 2)
99 aDBFFileName = aFileName.simplified().replace( aFileName.simplified().size() - 4, 4, ".dbf");
100 bool DBF_Stat = anImporter.DBF_OpenDBF(aDBFFileName);
103 QStringList aFieldList = anImporter.DBF_GetFieldList();
104 int nbRecords = anImporter.DBF_GetNbRecords();
105 assert (theEntities.Length() == nbRecords);
106 if (theEntities.Length() == nbRecords)
108 int indNameAttrFound = -1;
110 bool bUseNameAttrFound = false;
111 for (QStringList::iterator it = aFieldList.begin(); it != aFieldList.end(); it++, k++)
112 if (QString::compare(*it, "name", Qt::CaseInsensitive) == 0)
114 indNameAttrFound = k;
118 if (indNameAttrFound != -1)
119 bUseNameAttrFound = SUIT_MessageBox::question( module()->getApp()->desktop(),
120 tr( "IMPORT_POLYLINE" ),
121 tr( "IMPORT_POLYLINE_USE_NAME_ATTR" ),
122 QMessageBox::Yes | QMessageBox::No,
123 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
125 std::vector<std::vector<HYDROData_ShapeFile::DBF_AttrValue>> anAttrVV;
126 for (int i = 0; i < aFieldList.size(); i++)
128 std::vector<HYDROData_ShapeFile::DBF_AttrValue> anAttrV;
129 anAttrVV.push_back(anAttrV);
130 anImporter.DBF_GetAttributeList(i, anAttrVV[i] );
134 for (int i = 1; i <= theEntities.Length(); i++)
136 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theEntities(i) );
137 QStringList aDBFinfo;
138 aDBFinfo << aFieldList; //first, the table header
139 for (int j = 0; j < aFieldList.size(); j++)
142 const HYDROData_ShapeFile::DBF_AttrValue& attrV = anAttrVV[j][i-1];
143 if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_String)
144 aDBFinfo << attrV.myStrVal;
145 else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Integer)
146 aDBFinfo << QString::number(attrV.myIntVal);
147 else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Double)
148 aDBFinfo << QString::number(attrV.myDoubleVal);
152 assert (aDBFinfo.size() / 2 == aFieldList.size());
153 aPolylineXY->SetDBFInfo(aDBFinfo);
154 if (bUseNameAttrFound)
156 QString nameOfPoly = aDBFinfo[aDBFinfo.size() / 2 + indNameAttrFound];
157 if (!nameOfPoly.isEmpty())
158 aPolylineXY->SetName(nameOfPoly);
161 aPolylineXY->SetName("null_name_" + QString::number(indNULL));
170 UpdateView(theEntities);
173 UpdateView(theEntities);
174 SUIT_MessageBox::information(module()->getApp()->desktop(),
175 tr( "IMPORT_POLYLINE" ), "Contour of the polygon(s) have been imported as polyline(s)");
179 QString aMess = "Cannot import content of this file as polyline;\n";
181 aMess += "Cannot open SHP file";
182 else if (aStat == -2)
183 aMess += "Cannot open SHX file";
185 aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
186 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), aMess);
190 if (!aFileNames.empty())
192 commitDocOperation();
194 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init | UF_OCCViewer );
199 QApplication::restoreOverrideCursor();
202 void HYDROGUI_ImportPolylineOp::UpdateView( NCollection_Sequence<Handle(HYDROData_Entity)>& anEntities)
204 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
205 if ( anActiveViewId == 0 )
206 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
208 for (int i = 1; i <= anEntities.Size() ; i++)
210 anEntities(i)->Update();
211 module()->setObjectVisible( anActiveViewId, anEntities(i), true );
212 module()->setIsToUpdate( anEntities(i) );