Salome HOME
updated copyright message
[modules/geom.git] / src / XAOPlugin / XAOPlugin_ExportDlg.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_ExportDlg.h"
22 #include "XAOPlugin_IOperations_i.hh"
23
24 // GUI includes
25 #include <SUIT_Session.h>
26 #include <SUIT_ResourceMgr.h>
27 #include <SUIT_OverrideCursor.h>
28 #include <SUIT_FileDlg.h>
29
30 #include <SalomeApp_Application.h>
31 #include <SalomeApp_Study.h>
32 #include <SalomeApp_Tools.h>
33 #include <LightApp_SelectionMgr.h>
34
35 // GEOM includes
36 #include "GeometryGUI.h"
37 #include "GEOMBase.h"
38 #include "GEOMImpl_Types.hxx"
39
40 // OCCT Includes
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS.hxx>
43 #include <TopExp.hxx>
44 #include <TColStd_IndexedMapOfInteger.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46
47 // QT includes
48 #include <QLabel>
49 #include <QLineEdit>
50 #include <QButtonGroup>
51 #include <QListWidget>
52 #include <QFileDialog>
53 #include <QGroupBox>
54 #include <QRadioButton>
55 #include <QGridLayout>
56 #include <QPushButton>
57 #include <QCheckBox>
58 #include <QMap>
59
60 //=================================================================================
61 // Constructor
62 //=================================================================================
63 XAOPlugin_ExportDlg::XAOPlugin_ExportDlg(GeometryGUI* geometryGUI, QWidget* parent)
64 :
65         GEOMBase_Skeleton(geometryGUI, parent, false)
66 {
67   m_mainObj = GEOM::GEOM_Object::_nil();
68
69   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
70   QPixmap imageOp(resMgr->loadPixmap("GEOM", tr("XAOPLUGIN_EXPORT_ICON")));
71   QPixmap iconSelect(resMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
72
73   setWindowTitle(tr("XAOPLUGIN_EXPORT_TITLE"));
74
75   /***************************************************************/
76   mainFrame()->GroupConstructors->setTitle(tr("XAOPLUGIN_EXPORT_TITLE"));
77   mainFrame()->RadioButton1->setIcon(imageOp);
78   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
79   mainFrame()->RadioButton2->close();
80   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
81   mainFrame()->RadioButton3->close();
82
83   // hide name
84   mainFrame()->GroupBoxName->hide();
85
86   //****************************
87   // Selection Group box
88   QGroupBox* gbxExport = new QGroupBox(parent);
89
90   QGridLayout* gridLayoutExport = new QGridLayout(gbxExport);
91 #ifndef Q_OS_MAC
92   gridLayoutExport->setSpacing(6);
93   gridLayoutExport->setContentsMargins(9, 9, 9, 9);
94 #endif
95   gridLayoutExport->setObjectName(QString::fromUtf8("gridLayoutExport"));
96
97   // Line 0
98   QLabel* lblShape = new QLabel(tr("XAOPLUGIN_EXPORT_INGSHAPE"), gbxExport);
99   btnShapeSelect = new QPushButton(gbxExport);
100   btnShapeSelect->setIcon(iconSelect);
101   ledShape = new QLineEdit(gbxExport);
102   ledShape->setMinimumSize(QSize(100, 0));
103
104   int line = 0, col = 0;
105   // adWidget(widget, fromRow, fromColumn, rowSpan, columnSpan)
106   gridLayoutExport->addWidget(lblShape, line, col++, 1, 1);
107   gridLayoutExport->addWidget(btnShapeSelect, line, col++, 1, 1);
108   gridLayoutExport->addWidget(ledShape, line, col++, 1, 1);
109
110   // Line 1
111   QLabel* lblFileName = new QLabel(tr("XAOPLUGIN_EXPORT_FILENAME"), gbxExport);
112   btnFileSelect = new QPushButton(gbxExport);
113   ledFileName = new QLineEdit(gbxExport);
114   btnFileSelect->setText("...");
115
116   line++; col = 0;
117   gridLayoutExport->addWidget(lblFileName, line, col++, 1, 1);
118   gridLayoutExport->addWidget(btnFileSelect, line, col++, 1, 1);
119   gridLayoutExport->addWidget(ledFileName, line, col++, 1, 1);
120
121   // Line 2
122   QLabel* lblAuthor = new QLabel(tr("XAOPLUGIN_EXPORT_AUTHOR"), gbxExport);
123   ledAuthor = new QLineEdit(gbxExport);
124
125   line++; col = 0;
126   gridLayoutExport->addWidget(lblAuthor, line, col++, 1, 1);
127   col++; // span
128   gridLayoutExport->addWidget(ledAuthor, line, col++, 1, 1);
129
130   // Line 3
131   ckxUseSeparateFile = new QCheckBox(tr("XAOPLUGIN_EXPORT_SHAPEFILE"), gbxExport);
132
133   line++; col = 0;
134   gridLayoutExport->addWidget(ckxUseSeparateFile, line, col++, 1, 2);
135
136
137   //****************************
138   // Filter Group box
139   QGroupBox* gbxFilter = new QGroupBox(parent);
140
141   QGridLayout* gridLayoutFilter = new QGridLayout(gbxFilter);
142 #ifndef Q_OS_MAC
143   gridLayoutFilter->setSpacing(6);
144   gridLayoutFilter->setContentsMargins(9, 9, 9, 9);
145 #endif
146   gridLayoutFilter->setObjectName(QString::fromUtf8("gbxFilter"));
147
148   // Line 0
149   QLabel* lblGroups = new QLabel(tr("XAOPLUGIN_EXPORT_LGROUPS"), gbxFilter);
150   QLabel* lblFields = new QLabel(tr("XAOPLUGIN_EXPORT_LFIELDS"), gbxFilter);
151
152   line = 0, col = 0;
153   gridLayoutFilter->addWidget(lblGroups, line, col++, 1, 1);
154   gridLayoutFilter->addWidget(lblFields, line, col++, 1, 1);
155
156   // Line 1
157   lstGroups = new QListWidget(gbxFilter);
158   lstGroups->setSelectionMode(QAbstractItemView::NoSelection);
159   lstFields = new QListWidget(gbxFilter);
160   lstFields->setSelectionMode(QAbstractItemView::NoSelection);
161
162   line++; col = 0;
163   gridLayoutFilter->addWidget(lstGroups, line, col++, 1, 1);
164   gridLayoutFilter->addWidget(lstFields, line, col++, 1, 1);
165
166   //****************************
167   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
168   layout->setMargin(0);
169   layout->setSpacing(6);
170   layout->addWidget(gbxExport);
171   layout->addWidget(gbxFilter);
172
173   // set help
174   setHelpFileName("import_export_geom_obj_page.html#io_xao");
175
176   Init();
177 }
178
179 //=================================================================================
180 // Destructor
181 //=================================================================================
182 XAOPlugin_ExportDlg::~XAOPlugin_ExportDlg()
183 {
184   // no need to delete child widgets, Qt does it all for us
185 }
186
187 //=================================================================================
188 // function : Init()
189 // purpose  :
190 //=================================================================================
191 void XAOPlugin_ExportDlg::Init()
192 {
193   // Get setting of step value from file configuration
194   m_groups.clear();
195   m_fields.clear();
196
197   // Signal/slot connections
198   connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
199   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
200
201   connect(btnShapeSelect, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
202   connect(((SalomeApp_Application*) (SUIT_Session::session()->activeApplication()))->selectionMgr(),
203           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
204
205   connect(btnFileSelect, SIGNAL(clicked()), this, SLOT(btnFileSelectClicked()));
206
207   initName(tr("XAOPLUGIN_EXPORTXAO"));
208   SelectionIntoArgument();
209 }
210
211 //=================================================================================
212 // function : processObject()
213 // purpose  : Fill dialog fields in accordance with myObj
214 //=================================================================================
215 void XAOPlugin_ExportDlg::processObject()
216 {
217   lstGroups->clear();
218   lstFields->clear();
219   m_groups.clear();
220   m_fields.clear();
221
222   if (m_mainObj->_is_nil())
223   {
224     ledShape->setText("");
225   }
226   else
227   {
228     ledShape->setText(GEOMBase::GetName(m_mainObj));
229     GEOM::GEOM_IShapesOperations_var shapeOp = getGeomEngine()->GetIShapesOperations();
230
231     // add groups names
232     GEOM::ListOfGO_var groups = shapeOp->GetExistingSubObjects(m_mainObj, true);
233     for (int i = 0, n = groups->length(); i < n; i++)
234     {
235       QListWidgetItem* item = new QListWidgetItem();
236       item->setData(Qt::UserRole, QVariant(i));
237       item->setText(GEOMBase::GetName(groups[i]));
238       item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
239       item->setCheckState(Qt::Checked);
240       lstGroups->addItem(item);
241       m_groups.append(GEOM::GeomObjPtr(groups[i].in()));
242     }
243     lstGroups->sortItems(Qt::AscendingOrder);
244
245     // add fields
246     GEOM::GEOM_IFieldOperations_var fieldOp = getGeomEngine()->GetIFieldOperations();
247
248     GEOM::ListOfFields_var fields = fieldOp->GetFields(m_mainObj);
249     for (int i = 0, n = fields->length(); i < n; i++)
250     {
251       QListWidgetItem* item = new QListWidgetItem();
252       item->setData(Qt::UserRole, QVariant(i));
253       item->setText(fields[i]->GetName());
254       item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
255       item->setCheckState(Qt::Checked);
256       lstFields->addItem(item);
257       m_fields.append(GEOM::GeomFieldPtr(fields[i].in()));
258     }
259     lstFields->sortItems(Qt::AscendingOrder);
260   }
261 }
262
263 //=================================================================================
264 // function : ClickOnOk()
265 // purpose  :
266 //=================================================================================
267 void XAOPlugin_ExportDlg::ClickOnOk()
268 {
269   setIsApplyAndClose(true);
270   if (ClickOnApply())
271     ClickOnCancel();
272   setIsApplyAndClose(false);
273 }
274
275 //=================================================================================
276 // function : ClickOnApply()
277 // purpose  :
278 //=================================================================================
279 bool XAOPlugin_ExportDlg::ClickOnApply()
280 {
281   if (!isApplyAndClose())
282   {
283     setIsDisableBrowsing(true);
284     setIsDisplayResult(false);
285   }
286
287   QString msg;
288   if (!isValid(msg))
289   {
290     showError(msg);
291     return false;
292   }
293   SUIT_OverrideCursor wc;
294   SUIT_Session::session()->activeApplication()->putInfo("");
295
296   try
297   {
298     if (openCommand())
299       if (!execute())
300       {
301         abortCommand();
302         showError();
303         return false;
304       }
305   }
306   catch (const SALOME::SALOME_Exception& e)
307   {
308     SalomeApp_Tools::QtCatchCorbaException(e);
309     abortCommand();
310     return false;
311   }
312   commitCommand();
313
314   if (!isApplyAndClose())
315   {
316     setIsDisableBrowsing(false);
317     setIsDisplayResult(true);
318   }
319
320   processObject();
321   return true;
322 }
323
324 //=================================================================================
325 // function : SelectionIntoArgument()
326 // purpose  : Called when selection as changed or other case
327 //=================================================================================
328 void XAOPlugin_ExportDlg::SelectionIntoArgument()
329 {
330   m_mainObj = GEOM::GEOM_Object::_nil();
331   LightApp_SelectionMgr* selMgr = myGeomGUI->getApp()->selectionMgr();
332   SALOME_ListIO selList;
333   selMgr->selectedObjects(selList);
334
335   if (selList.Extent() == 1)
336   {
337     m_mainObj = GEOMBase::ConvertIOinGEOMObject(selList.First());
338   }
339
340   processObject();
341 }
342
343 //=================================================================================
344 // function : SetEditCurrentArgument()
345 // purpose  :
346 //=================================================================================
347 void XAOPlugin_ExportDlg::SetEditCurrentArgument()
348 {
349   ledShape->setFocus();
350   myEditCurrentArgument = ledShape;
351   SelectionIntoArgument();
352 }
353
354 //=================================================================================
355 // function : btnFileSelectClicked()
356 // purpose  :
357 //=================================================================================
358 void XAOPlugin_ExportDlg::btnFileSelectClicked()
359 {
360   QString file = SUIT_FileDlg::getFileName(this, ledFileName->text(), 
361                                            tr("XAOPLUGIN_FILES"),
362                                            tr("XAOPLUGIN_EXPORT_SELECT"), false);
363   if ( !file.isEmpty() )
364     ledFileName->setText( file );
365 }
366
367 //=================================================================================
368 // function : ActivateThisDialog()
369 // purpose  :
370 //=================================================================================
371 void XAOPlugin_ExportDlg::ActivateThisDialog()
372 {
373   GEOMBase_Skeleton::ActivateThisDialog();
374 }
375
376 //=================================================================================
377 // function : enterEvent [REDEFINED]
378 // purpose  :
379 //=================================================================================
380 void XAOPlugin_ExportDlg::enterEvent(QEvent*)
381 {
382   if (!mainFrame()->GroupConstructors->isEnabled())
383     ActivateThisDialog();
384 }
385
386 //=================================================================================
387 // function : createOperation
388 // purpose  :
389 //=================================================================================
390 GEOM::GEOM_IOperations_ptr XAOPlugin_ExportDlg::createOperation()
391 {
392   return getGeomEngine()->GetPluginOperations( "XAOPluginEngine" );
393 }
394
395 //=================================================================================
396 // function : isValid
397 // purpose  :
398 //=================================================================================
399 bool XAOPlugin_ExportDlg::isValid(QString& /*msg*/)
400 {
401   // check shape
402   if (ledShape->text().isEmpty())
403     return false;
404
405   // check file name
406   if (ledFileName->text().isEmpty())
407     return false;
408
409   return true;
410 }
411
412 //=================================================================================
413 // function : execute
414 // purpose  :
415 //=================================================================================
416 bool XAOPlugin_ExportDlg::execute()
417 {
418   bool res = false;
419
420   QString author = ledAuthor->text();
421   QString fileName = ledFileName->text();
422   QString shapeFileName = QString("");//ledShapeFile->text();
423   if (ckxUseSeparateFile->isChecked())
424   {
425     shapeFileName = fileName;
426     shapeFileName.append(".brep");
427   }
428
429   // get selected groups
430   QList<QListWidgetItem*> selGroups;
431   for (int j = 0; j < lstGroups->count(); ++j)
432   {
433     if (lstGroups->item(j)->checkState() == Qt::Checked)
434       selGroups.append(lstGroups->item(j));
435   }
436
437   GEOM::ListOfGO_var groups = new GEOM::ListOfGO();
438   groups->length(selGroups.count());
439   int i = 0;
440   for (QList<QListWidgetItem*>::iterator it = selGroups.begin(); it != selGroups.end(); ++it)
441   {
442     QListWidgetItem* item = (*it);
443     int index = item->data(Qt::UserRole).toInt();
444     groups[i++] = m_groups[index].copy();
445   }
446
447   // get selected fields
448   QList<QListWidgetItem*> selFields;
449   for (int j = 0; j < lstFields->count(); ++j)
450   {
451     if (lstFields->item(j)->checkState() == Qt::Checked)
452       selFields.append(lstFields->item(j));
453   }
454
455   GEOM::ListOfFields_var fields = new GEOM::ListOfFields();
456   fields->length(selFields.count());
457   i = 0;
458   for (QList<QListWidgetItem*>::iterator it = selFields.begin(); it != selFields.end(); ++it)
459   {
460     QListWidgetItem* item = (*it);
461     int index = item->data(Qt::UserRole).toInt();
462     fields[i++] = m_fields[index].copy();
463   }
464
465   // call engine function
466   GEOM::IXAOOperations_var aXAOOp = GEOM::IXAOOperations::_narrow( getOperation() );
467   res = aXAOOp->ExportXAO(m_mainObj, groups, fields,
468                           author.toUtf8().constData(),
469                           fileName.toUtf8().constData(),
470                           shapeFileName.toStdString().c_str());
471   return res;
472 }