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