Salome HOME
c4681af62c244442a897df051f6fbe15c00f2d84
[modules/geom.git] / src / ImportExportGUI / ImportExportGUI_ImportXAODlg.cxx
1 //  Copyright (C) 2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 #include <DlgRef.h>
20 #include <GeometryGUI.h>
21 #include <GEOMBase.h>
22 #include <GEOM_Field.hxx>
23
24 #include <SUIT_Session.h>
25 #include <SUIT_ResourceMgr.h>
26 #include <SUIT_OverrideCursor.h>
27 #include <SalomeApp_Application.h>
28 #include <SalomeApp_Study.h>
29 #include <LightApp_SelectionMgr.h>
30 #include <SalomeApp_Tools.h>
31
32 #include <QLabel>
33 #include <QLineEdit>
34 #include <QButtonGroup>
35 #include <QListWidget>
36 #include <QFileDialog>
37 #include <QMap>
38
39 // OCCT Includes
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS.hxx>
42 #include <TopExp.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
45
46 #include <GEOMImpl_Types.hxx>
47 #include "ImportExportGUI_ImportXAODlg.h"
48
49 #include <SALOMEDS_wrap.hxx>
50
51 //=================================================================================
52 // Constructor
53 //=================================================================================
54 ImportExportGUI_ImportXAODlg::ImportExportGUI_ImportXAODlg(GeometryGUI* geometryGUI, QWidget* parent)
55 :
56         GEOMBase_Skeleton(geometryGUI, parent, false)
57 {
58     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
59     QPixmap imageOp(resMgr->loadPixmap("GEOM", tr("ICON_DLG_IMPORTXAO")));
60     QPixmap iconSelect(resMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
61
62     setWindowTitle(tr("GEOM_IMPORTXAO_TITLE"));
63
64     /***************************************************************/
65     mainFrame()->GroupConstructors->setTitle(tr("GEOM_IMPORTXAO_TITLE"));
66     mainFrame()->RadioButton1->setIcon(imageOp);
67     mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
68     mainFrame()->RadioButton2->close();
69     mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
70     mainFrame()->RadioButton3->close();
71
72     // hide name
73     mainFrame()->GroupBoxName->hide();
74
75     //****************************
76     // Selection Group box
77     QGroupBox* gbxExport = new QGroupBox(parent);
78
79     QGridLayout* gridLayoutExport = new QGridLayout(gbxExport);
80 #ifndef Q_OS_MAC
81     gridLayoutExport->setSpacing(6);
82     gridLayoutExport->setContentsMargins(9, 9, 9, 9);
83 #endif
84     gridLayoutExport->setObjectName(QString::fromUtf8("gridLayoutExport"));
85
86     int line = 0, col = 0;
87     QLabel* lblFileName = new QLabel(tr("GEOM_IMPORTXAO_FILENAME"), gbxExport);
88     btnFileSelect = new QPushButton(gbxExport);
89     ledFileName = new QLineEdit(gbxExport);
90     btnFileSelect->setText("...");
91
92     line++; col = 0;
93     gridLayoutExport->addWidget(lblFileName, line, col++, 1, 1);
94     gridLayoutExport->addWidget(btnFileSelect, line, col++, 1, 1);
95     gridLayoutExport->addWidget(ledFileName, line, col++, 1, 1);
96
97     //****************************
98     QVBoxLayout* layout = new QVBoxLayout(centralWidget());
99     layout->setMargin(0);
100     layout->setSpacing(6);
101     layout->addWidget(gbxExport);
102
103     // set help
104     setHelpFileName("create_importxao_page.html");
105
106     Init();
107 }
108
109 //=================================================================================
110 // Destructor
111 //=================================================================================
112 ImportExportGUI_ImportXAODlg::~ImportExportGUI_ImportXAODlg()
113 {
114     // no need to delete child widgets, Qt does it all for us
115 }
116
117 //=================================================================================
118 // function : Init()
119 // purpose  :
120 //=================================================================================
121 void ImportExportGUI_ImportXAODlg::Init()
122 {
123     // Signal/slot connections
124     connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
125     connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
126
127     connect(btnFileSelect, SIGNAL(clicked()), this, SLOT(btnFileSelectClicked()));
128
129     initName(tr("GEOM_IMPORTXAO"));
130     //SelectionIntoArgument();
131 }
132
133 //=================================================================================
134 // function : ClickOnOk()
135 // purpose  :
136 //=================================================================================
137 void ImportExportGUI_ImportXAODlg::ClickOnOk()
138 {
139     setIsApplyAndClose(true);
140     if (ClickOnApply())
141         ClickOnCancel();
142     setIsApplyAndClose(false);
143 }
144
145 //=================================================================================
146 // function : ClickOnApply()
147 // purpose  :
148 //=================================================================================
149 bool ImportExportGUI_ImportXAODlg::ClickOnApply()
150 {
151   if(!isApplyAndClose()) {
152     setIsDisableBrowsing( true );
153     setIsDisplayResult( false );
154   }
155
156   QString msg;
157   if ( !isValid( msg ) ) {
158     showError( msg );
159     return false;
160   }
161   SUIT_OverrideCursor wc;
162   SUIT_Session::session()->activeApplication()->putInfo( "" );
163
164   try {
165     if ( openCommand() )
166       if (!execute (/*isApplyAndClose()*/))
167       {
168         abortCommand();
169         showError();
170         return false;
171       }
172   }
173   catch( const SALOME::SALOME_Exception& e ) {
174     SalomeApp_Tools::QtCatchCorbaException( e );
175     abortCommand();
176     return false;
177   }
178   commitCommand();
179
180   if(!isApplyAndClose()) {
181     setIsDisableBrowsing( false );
182     setIsDisplayResult( true );
183   }
184
185   return true;
186 }
187
188 //=================================================================================
189 // function : btnFileSelectClicked()
190 // purpose  :
191 //=================================================================================
192 void ImportExportGUI_ImportXAODlg::btnFileSelectClicked()
193 {
194     QString selFile = QFileDialog::getOpenFileName(this, tr("GEOM_SELECT_EXPORT_XAO"),
195                                                    QString(), tr("XAO_FILES"));
196     if (!selFile.isEmpty())
197     {
198         ledFileName->setText(selFile);
199     }
200 }
201
202 //=================================================================================
203 // function : ActivateThisDialog()
204 // purpose  :
205 //=================================================================================
206 void ImportExportGUI_ImportXAODlg::ActivateThisDialog()
207 {
208     GEOMBase_Skeleton::ActivateThisDialog();
209 }
210
211 //=================================================================================
212 // function : enterEvent [REDEFINED]
213 // purpose  :
214 //=================================================================================
215 void ImportExportGUI_ImportXAODlg::enterEvent(QEvent*)
216 {
217     if (!mainFrame()->GroupConstructors->isEnabled())
218         ActivateThisDialog();
219 }
220
221 //=================================================================================
222 // function : createOperation
223 // purpose  :
224 //=================================================================================
225 GEOM::GEOM_IOperations_ptr ImportExportGUI_ImportXAODlg::createOperation()
226 {
227     return getGeomEngine()->GetIInsertOperations(getStudyId());
228 }
229
230 //=================================================================================
231 // function : isValid
232 // purpose  :
233 //=================================================================================
234 bool ImportExportGUI_ImportXAODlg::isValid(QString& msg)
235 {
236     // check file name
237     if (ledFileName->text().isEmpty())
238         return false;
239
240     return true;
241 }
242
243 //=================================================================================
244 // function : execute
245 // purpose  :
246 //=================================================================================
247 bool ImportExportGUI_ImportXAODlg::execute()
248 {
249     bool res = false;
250
251     QString fileName = ledFileName->text();
252     GEOM::GEOM_Object_var shape;
253     GEOM::ListOfGO_var groups, subShapes;
254     GEOM::ListOfFields_var fields;
255
256     GEOM::GEOM_IInsertOperations_var ieOp = GEOM::GEOM_IInsertOperations::_narrow(getOperation());
257     res = ieOp->ImportXAO(fileName.toStdString().c_str(), shape, subShapes, groups, fields);
258
259     if (!shape->_is_nil())
260     {
261         m_mainShape = shape;
262     }
263     else
264     {
265         m_mainShape = NULL;
266     }
267
268     if (m_mainShape != NULL)
269     {
270         QStringList anEntryList;
271         anEntryList << addInStudy(m_mainShape, m_mainShape->GetName());
272
273         for (int i = 0; i < subShapes->length(); i++)
274         {
275             addInStudy(subShapes[i].in(), subShapes[i]->GetName());
276         }
277         for (int i = 0; i < groups->length(); i++)
278         {
279             addInStudy(groups[i].in(), groups[i]->GetName());
280         }
281         for (int i = 0; i < fields->length(); i++)
282         {
283             addFieldInStudy(fields[i].in(), m_mainShape);
284         }
285
286         updateObjBrowser();
287         if( SUIT_Application* anApp = SUIT_Session::session()->activeApplication() ) {
288           LightApp_Application* aLightApp = dynamic_cast<LightApp_Application*>( anApp );
289           aLightApp->browseObjects( anEntryList );
290         }
291     }
292
293     return res;
294 }
295
296 QString ImportExportGUI_ImportXAODlg::addFieldInStudy( GEOM::GEOM_Field_ptr theField, GEOM::GEOM_Object_ptr theFather)
297 {
298   if ( !hasCommand() )
299     return QString();
300
301   _PTR(Study) aStudy = getStudy()->studyDS();
302   if ( !aStudy || theField->_is_nil() )
303     return QString();
304
305   SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy(aStudy);
306
307   SALOMEDS::SObject_var aSO =
308     getGeomEngine()->AddInStudy(aStudyDS, theField, theField->GetName(), theFather);
309
310   QString anEntry;
311   if ( !aSO->_is_nil() ) {
312     CORBA::String_var entry = aSO->GetID();
313     anEntry = entry.in();
314   }
315
316   // add steps
317   GEOM::ListOfLong_var steps = theField->GetSteps();
318   for (int i = 0; i < steps->length(); ++i)
319   {
320     GEOM::GEOM_FieldStep_ptr step = theField->GetStep(steps[i]);
321     QString stepName = (tr("STEP") + " %1 %2").arg( step->GetID() ).arg( step->GetStamp() );
322     SALOMEDS::SObject_wrap aSOField =
323       getGeomEngine()->AddInStudy( aStudyDS, step, stepName.toLatin1().constData(), theField );
324   }
325
326   aSO->UnRegister();
327
328   return anEntry;
329 }
330
331 GEOM::GEOM_Object_ptr ImportExportGUI_ImportXAODlg::getFather(GEOM::GEOM_Object_ptr object)
332 {
333     GEOM::GEOM_Object_var fatherObj;
334     if (object->GetType() != GEOM_IMPORT && m_mainShape != NULL)
335     {
336         //GEOM::GEOM_IGroupOperations_var groupOper = getGeomEngine()->GetIGroupOperations(getStudyId());
337         //fatherObj = groupOper->GetMainShape(object);
338         fatherObj = m_mainShape;
339     }
340     return fatherObj._retn();
341 }
342
343 QString ImportExportGUI_ImportXAODlg::getObjectName(GEOM::GEOM_Object_ptr object) const
344 {
345     if (object->_is_nil())
346         return QString::null;
347     return object->GetName();
348 }
349