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