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