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