Salome HOME
Merge from BR_V7_main_Field branch (02/09/2013)
[modules/geom.git] / src / ImportExportGUI / ImportExportGUI_ImportXAODlg.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 <SalomeApp_Application.h>
26 #include <SalomeApp_Study.h>
27 #include <LightApp_SelectionMgr.h>
28
29 #include <QLabel>
30 #include <QLineEdit>
31 #include <QButtonGroup>
32 #include <QListWidget>
33 #include <QFileDialog>
34 #include <QMap>
35
36 // OCCT Includes
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS.hxx>
39 #include <TopExp.hxx>
40 #include <TColStd_IndexedMapOfInteger.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42
43 #include <GEOMImpl_Types.hxx>
44 #include "ImportExportGUI_ImportXAODlg.h"
45
46 //=================================================================================
47 // Constructor
48 //=================================================================================
49 ImportExportGUI_ImportXAODlg::ImportExportGUI_ImportXAODlg(GeometryGUI* geometryGUI, QWidget* parent)
50 :
51         GEOMBase_Skeleton(geometryGUI, parent, false)
52 {
53     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
54     QPixmap imageOp(resMgr->loadPixmap("GEOM", tr("ICON_DLG_IMPORTXAO")));
55     QPixmap iconSelect(resMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
56
57     setWindowTitle(tr("GEOM_IMPORTXAO_TITLE"));
58
59     /***************************************************************/
60     mainFrame()->GroupConstructors->setTitle(tr("GEOM_IMPORTXAO_TITLE"));
61     mainFrame()->RadioButton1->setIcon(imageOp);
62     mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
63     mainFrame()->RadioButton2->close();
64     mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
65     mainFrame()->RadioButton3->close();
66
67     // hide name
68     mainFrame()->GroupBoxName->hide();
69
70     //****************************
71     // Selection Group box
72     QGroupBox* gbxExport = new QGroupBox(parent);
73
74     QGridLayout* gridLayoutExport = new QGridLayout(gbxExport);
75 #ifndef Q_OS_MAC
76     gridLayoutExport->setSpacing(6);
77     gridLayoutExport->setContentsMargins(9, 9, 9, 9);
78 #endif
79     gridLayoutExport->setObjectName(QString::fromUtf8("gridLayoutExport"));
80
81     int line = 0, col = 0;
82     QLabel* lblFileName = new QLabel(tr("GEOM_IMPORTXAO_FILENAME"), gbxExport);
83     btnFileSelect = new QPushButton(gbxExport);
84     ledFileName = new QLineEdit(gbxExport);
85     btnFileSelect->setText("...");
86
87     line++; col = 0;
88     gridLayoutExport->addWidget(lblFileName, line, col++, 1, 1);
89     gridLayoutExport->addWidget(btnFileSelect, line, col++, 1, 1);
90     gridLayoutExport->addWidget(ledFileName, line, col++, 1, 1);
91
92     //****************************
93     QVBoxLayout* layout = new QVBoxLayout(centralWidget());
94     layout->setMargin(0);
95     layout->setSpacing(6);
96     layout->addWidget(gbxExport);
97
98     // set help
99     setHelpFileName("create_importxao_page.html");
100
101     Init();
102 }
103
104 //=================================================================================
105 // Destructor
106 //=================================================================================
107 ImportExportGUI_ImportXAODlg::~ImportExportGUI_ImportXAODlg()
108 {
109     // no need to delete child widgets, Qt does it all for us
110 }
111
112 //=================================================================================
113 // function : Init()
114 // purpose  :
115 //=================================================================================
116 void ImportExportGUI_ImportXAODlg::Init()
117 {
118     // Signal/slot connections
119     connect(buttonOk(), SIGNAL(clicked()), this, SLOT(ClickOnOk()));
120     connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
121
122     connect(btnFileSelect, SIGNAL(clicked()), this, SLOT(btnFileSelectClicked()));
123
124     initName(tr("GEOM_IMPORTXAO"));
125     //SelectionIntoArgument();
126 }
127
128 //=================================================================================
129 // function : ClickOnOk()
130 // purpose  :
131 //=================================================================================
132 void ImportExportGUI_ImportXAODlg::ClickOnOk()
133 {
134     if (ClickOnApply())
135         ClickOnCancel();
136 }
137
138 //=================================================================================
139 // function : ClickOnApply()
140 // purpose  :
141 //=================================================================================
142 bool ImportExportGUI_ImportXAODlg::ClickOnApply()
143 {
144     if (!onAccept())
145         return false;
146
147     initName();
148
149     return true;
150 }
151
152 //=================================================================================
153 // function : btnFileSelectClicked()
154 // purpose  :
155 //=================================================================================
156 void ImportExportGUI_ImportXAODlg::btnFileSelectClicked()
157 {
158     QString selFile = QFileDialog::getOpenFileName(this, tr("GEOM_SELECT_EXPORT_XAO"),
159             QString(), tr("XAO_FILES"));
160     if (!selFile.isEmpty())
161     {
162         ledFileName->setText(selFile);
163     }
164 }
165
166 //=================================================================================
167 // function : ActivateThisDialog()
168 // purpose  :
169 //=================================================================================
170 void ImportExportGUI_ImportXAODlg::ActivateThisDialog()
171 {
172     GEOMBase_Skeleton::ActivateThisDialog();
173 }
174
175 //=================================================================================
176 // function : enterEvent [REDEFINED]
177 // purpose  :
178 //=================================================================================
179 void ImportExportGUI_ImportXAODlg::enterEvent(QEvent*)
180 {
181     if (!mainFrame()->GroupConstructors->isEnabled())
182         ActivateThisDialog();
183 }
184
185 //=================================================================================
186 // function : createOperation
187 // purpose  :
188 //=================================================================================
189 GEOM::GEOM_IOperations_ptr ImportExportGUI_ImportXAODlg::createOperation()
190 {
191     return getGeomEngine()->GetIInsertOperations(getStudyId());
192 }
193
194 //=================================================================================
195 // function : isValid
196 // purpose  :
197 //=================================================================================
198 bool ImportExportGUI_ImportXAODlg::isValid(QString& msg)
199 {
200     // check file name
201     if (ledFileName->text().isEmpty())
202         return false;
203
204     return true;
205 }
206
207 //=================================================================================
208 // function : execute
209 // purpose  :
210 //=================================================================================
211 bool ImportExportGUI_ImportXAODlg::execute(ObjectList& objects)
212 {
213     bool res = false;
214
215     QString fileName = ledFileName->text();
216     GEOM::GEOM_Object_var shape;
217     GEOM::ListOfGO_var groups, fields, subShapes;
218
219     GEOM::GEOM_IInsertOperations_var ieOp = GEOM::GEOM_IInsertOperations::_narrow(getOperation());
220     res = ieOp->ImportXAO(fileName.toStdString().c_str(), shape, subShapes, groups, fields);
221
222     if (!shape->_is_nil())
223     {
224         m_mainShape = shape;
225         objects.push_back(shape._retn());
226     }
227     else
228     {
229         m_mainShape = NULL;
230     }
231
232     for (int i = 0; i < subShapes->length(); i++)
233     {
234         objects.push_back(GEOM::GEOM_Object::_duplicate(subShapes[i]));
235     }
236     for (int i = 0; i < groups->length(); i++)
237     {
238         objects.push_back(GEOM::GEOM_Object::_duplicate(groups[i]));
239     }
240     for (int i = 0; i < fields->length(); i++)
241     {
242         objects.push_back(GEOM::GEOM_Object::_duplicate(fields[i]));
243     }
244
245     return res;
246 }
247
248 GEOM::GEOM_Object_ptr ImportExportGUI_ImportXAODlg::getFather(GEOM::GEOM_Object_ptr object)
249 {
250     GEOM::GEOM_Object_var fatherObj;
251     if (object->GetType() != GEOM_IMPORT && m_mainShape != NULL)
252     {
253         //GEOM::GEOM_IGroupOperations_var groupOper = getGeomEngine()->GetIGroupOperations(getStudyId());
254         //fatherObj = groupOper->GetMainShape(object);
255         fatherObj = m_mainShape;
256     }
257     return fatherObj._retn();
258 }
259
260 QString ImportExportGUI_ImportXAODlg::getObjectName(GEOM::GEOM_Object_ptr object) const
261 {
262     if (object->_is_nil())
263         return QString::null;
264     return object->GetName();
265 }
266