]> SALOME platform Git repositories - modules/geom.git/blob - src/ImportExportGUI/ImportExportGUI_ImportXAODlg.cxx
Salome HOME
publish imported fields to study
[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     if (ClickOnApply())
140         ClickOnCancel();
141 }
142
143 //=================================================================================
144 // function : ClickOnApply()
145 // purpose  :
146 //=================================================================================
147 /*bool ImportExportGUI_ImportXAODlg::ClickOnApply()
148 {
149     if (!onAccept())
150         return false;
151
152     initName();
153
154     return true;
155 }*/
156
157 bool ImportExportGUI_ImportXAODlg::ClickOnApply()
158 {
159   if(!isApplyAndClose()) {
160     setIsDisableBrowsing( true );
161     setIsDisplayResult( false );
162   }
163
164   QString msg;
165   if ( !isValid( msg ) ) {
166     showError( msg );
167     return false;
168   }
169   SUIT_OverrideCursor wc;
170   SUIT_Session::session()->activeApplication()->putInfo( "" );
171
172   try {
173     if ( openCommand() )
174       if (!execute (/*isApplyAndClose()*/))
175       {
176         abortCommand();
177         showError();
178         return false;
179       }
180   }
181   catch( const SALOME::SALOME_Exception& e ) {
182     SalomeApp_Tools::QtCatchCorbaException( e );
183     abortCommand();
184     return false;
185   }
186   commitCommand();
187
188   if(!isApplyAndClose()) {
189     setIsDisableBrowsing( false );
190     setIsDisplayResult( true );
191   }
192
193   return true;
194 }
195
196 //=================================================================================
197 // function : btnFileSelectClicked()
198 // purpose  :
199 //=================================================================================
200 void ImportExportGUI_ImportXAODlg::btnFileSelectClicked()
201 {
202     QString selFile = QFileDialog::getOpenFileName(this, tr("GEOM_SELECT_EXPORT_XAO"),
203                                                    QString(), tr("XAO_FILES"));
204     if (!selFile.isEmpty())
205     {
206         ledFileName->setText(selFile);
207     }
208 }
209
210 //=================================================================================
211 // function : ActivateThisDialog()
212 // purpose  :
213 //=================================================================================
214 void ImportExportGUI_ImportXAODlg::ActivateThisDialog()
215 {
216     GEOMBase_Skeleton::ActivateThisDialog();
217 }
218
219 //=================================================================================
220 // function : enterEvent [REDEFINED]
221 // purpose  :
222 //=================================================================================
223 void ImportExportGUI_ImportXAODlg::enterEvent(QEvent*)
224 {
225     if (!mainFrame()->GroupConstructors->isEnabled())
226         ActivateThisDialog();
227 }
228
229 //=================================================================================
230 // function : createOperation
231 // purpose  :
232 //=================================================================================
233 GEOM::GEOM_IOperations_ptr ImportExportGUI_ImportXAODlg::createOperation()
234 {
235     return getGeomEngine()->GetIInsertOperations(getStudyId());
236 }
237
238 //=================================================================================
239 // function : isValid
240 // purpose  :
241 //=================================================================================
242 bool ImportExportGUI_ImportXAODlg::isValid(QString& msg)
243 {
244     // check file name
245     if (ledFileName->text().isEmpty())
246         return false;
247
248     return true;
249 }
250
251 //=================================================================================
252 // function : execute
253 // purpose  :
254 //=================================================================================
255 bool ImportExportGUI_ImportXAODlg::execute()
256 {
257     bool res = false;
258
259     QString fileName = ledFileName->text();
260     GEOM::GEOM_Object_var shape;
261     GEOM::ListOfGO_var groups, subShapes;
262     GEOM::ListOfFields_var fields;
263
264     GEOM::GEOM_IInsertOperations_var ieOp = GEOM::GEOM_IInsertOperations::_narrow(getOperation());
265     res = ieOp->ImportXAO(fileName.toStdString().c_str(), shape, subShapes, groups, fields);
266
267     if (!shape->_is_nil())
268     {
269         m_mainShape = shape;
270     }
271     else
272     {
273         m_mainShape = NULL;
274     }
275
276     if (m_mainShape != NULL)
277     {
278         addInStudy(m_mainShape, m_mainShape->GetName());
279
280         for (int i = 0; i < subShapes->length(); i++)
281         {
282             addInStudy(subShapes[i].in(), subShapes[i]->GetName());
283         }
284         for (int i = 0; i < groups->length(); i++)
285         {
286             addInStudy(groups[i].in(), groups[i]->GetName());
287         }
288         for (int i = 0; i < fields->length(); i++)
289         {
290             addFieldInStudy(fields[i].in(), m_mainShape);
291         }
292
293         updateObjBrowser();
294     }
295
296     return res;
297 }
298
299 QString ImportExportGUI_ImportXAODlg::addFieldInStudy( GEOM::GEOM_Field_ptr theField, GEOM::GEOM_Object_ptr theFather)
300 {
301   if ( !hasCommand() )
302     return QString();
303
304   _PTR(Study) aStudy = getStudy()->studyDS();
305   if ( !aStudy || theField->_is_nil() )
306     return QString();
307
308   SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy(aStudy);
309
310   SALOMEDS::SObject_var aSO =
311     getGeomEngine()->AddInStudy(aStudyDS, theField, theField->GetName(), theFather);
312
313   QString anEntry;
314   if ( !aSO->_is_nil() ) {
315     CORBA::String_var entry = aSO->GetID();
316     anEntry = entry.in();
317   }
318
319   // add steps
320   GEOM::ListOfLong_var steps = theField->GetSteps();
321   for (int i = 0; i < steps->length(); ++i)
322   {
323     GEOM::GEOM_FieldStep_ptr step = theField->GetStep(steps[i]);
324     QString stepName = (tr("STEP") + " %1 %2").arg( step->GetID() ).arg( step->GetStamp() );
325     SALOMEDS::SObject_wrap aSOField =
326       getGeomEngine()->AddInStudy( aStudyDS, step, stepName.toLatin1().constData(), theField );
327   }
328
329   aSO->UnRegister();
330
331   return anEntry;
332 }
333
334 GEOM::GEOM_Object_ptr ImportExportGUI_ImportXAODlg::getFather(GEOM::GEOM_Object_ptr object)
335 {
336     GEOM::GEOM_Object_var fatherObj;
337     if (object->GetType() != GEOM_IMPORT && m_mainShape != NULL)
338     {
339         //GEOM::GEOM_IGroupOperations_var groupOper = getGeomEngine()->GetIGroupOperations(getStudyId());
340         //fatherObj = groupOper->GetMainShape(object);
341         fatherObj = m_mainShape;
342     }
343     return fatherObj._retn();
344 }
345
346 QString ImportExportGUI_ImportXAODlg::getObjectName(GEOM::GEOM_Object_ptr object) const
347 {
348     if (object->_is_nil())
349         return QString::null;
350     return object->GetName();
351 }
352