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