Salome HOME
0022380: EDF 2746 GEOM: Accept compounds of points as an input for "Smoothing surface"
[modules/geom.git] / src / AdvancedGUI / AdvancedGUI_SmoothingSurfaceDlg.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 "AdvancedGUI_SmoothingSurfaceDlg.h"
21
22 #include <DlgRef.h>
23 #include <GeometryGUI.h>
24 #include <GEOMBase.h>
25
26 #include <SUIT_Session.h>
27 #include <SUIT_ResourceMgr.h>
28 #include <SalomeApp_Application.h>
29 #include <LightApp_SelectionMgr.h>
30
31 // OCCT Includes
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <TopExp.hxx>
36 #include <TColStd_IndexedMapOfInteger.hxx>
37 #include <TopTools_IndexedMapOfShape.hxx>
38
39 #include <GEOMImpl_Types.hxx>
40
41 //=================================================================================
42 // Constructor
43 //=================================================================================
44 AdvancedGUI_SmoothingSurfaceDlg::AdvancedGUI_SmoothingSurfaceDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
45   : GEOMBase_Skeleton(theGeometryGUI, parent, false)
46 {
47   QPixmap imageOp  (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_SMOOTHINGSURFACE_LPOINTS")));
48   QPixmap imageSel (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
49   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
50
51   setWindowTitle(tr("GEOM_SMOOTHINGSURFACE_TITLE"));
52
53   /***************************************************************/
54   mainFrame()->GroupConstructors->setTitle(tr("GEOM_SMOOTHINGSURFACE"));
55   mainFrame()->RadioButton1->setIcon(imageOp);
56   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
57   mainFrame()->RadioButton2->close();
58   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
59   mainFrame()->RadioButton3->close();
60
61   GroupPoints = new DlgRef_1Sel( centralWidget() );
62
63   GroupPoints->GroupBox1->setTitle( tr( "GEOM_SMOOTHINGSURFACE_ARG" ) );
64   GroupPoints->TextLabel1->setText( tr( "GEOM_SMOOTHINGSURFACE_ARG_POINTS" ) );
65   GroupPoints->PushButton1->setIcon( image1 );
66   GroupPoints->LineEdit1->setReadOnly( true );
67
68   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
69   layout->setMargin(0); layout->setSpacing(6);
70   layout->addWidget(GroupPoints);
71   /***************************************************************/
72
73   setHelpFileName("create_smoothingsurface_page.html");
74
75   Init();
76 }
77
78 //=================================================================================
79 // Destructor
80 //=================================================================================
81 AdvancedGUI_SmoothingSurfaceDlg::~AdvancedGUI_SmoothingSurfaceDlg()
82 {
83   // no need to delete child widgets, Qt does it all for us
84 }
85
86 //=================================================================================
87 // function : Init()
88 // purpose  :
89 //=================================================================================
90 void AdvancedGUI_SmoothingSurfaceDlg::Init()
91 {
92   // Get setting of step value from file configuration
93   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
94   double step = resMgr->doubleValue("Geometry", "SettingsGeomStep", 100);
95
96   //@@ initialize dialog box widgets here @@//
97
98   // Signal/slot connections
99   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
100   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
101   connect(myGeomGUI,     SIGNAL(SignalDefaultStepValueChanged(double)),
102           this,          SLOT(SetDoubleSpinBoxStep(double)));
103   connect( myGeomGUI->getApp()->selectionMgr(),
104            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
105
106   initName(tr("GEOM_SMOOTHINGSURFACE"));
107   //displayPreview();
108 }
109
110 //=================================================================================
111 // function : ClickOnOk()
112 // purpose  :
113 //=================================================================================
114 void AdvancedGUI_SmoothingSurfaceDlg::ClickOnOk()
115 {
116   if (ClickOnApply())
117     ClickOnCancel();
118 }
119
120 //=================================================================================
121 // function : ClickOnApply()
122 // purpose  :
123 //=================================================================================
124 bool AdvancedGUI_SmoothingSurfaceDlg::ClickOnApply()
125 {
126   if (!onAccept())
127     return false;
128
129   initName();
130
131   return true;
132 }
133
134 //=================================================================================
135 // function : ActivateThisDialog()
136 // purpose  :
137 //=================================================================================
138 void AdvancedGUI_SmoothingSurfaceDlg::ActivateThisDialog()
139 {
140   GEOMBase_Skeleton::ActivateThisDialog();
141   //displayPreview();
142 }
143
144 //=================================================================================
145 // function : enterEvent [REDEFINED]
146 // purpose  :
147 //=================================================================================
148 void AdvancedGUI_SmoothingSurfaceDlg::enterEvent (QEvent*)
149 {
150   if (!mainFrame()->GroupConstructors->isEnabled())
151     ActivateThisDialog();
152 }
153
154 //=================================================================================
155 // function : createOperation
156 // purpose  :
157 //=================================================================================
158 GEOM::GEOM_IOperations_ptr AdvancedGUI_SmoothingSurfaceDlg::createOperation()
159 {
160   //return getGeomEngine()->GetIAdvancedOperations(getStudyId());
161   return getGeomEngine()->GetPluginOperations(getStudyId(), "AdvancedEngine");
162 }
163
164 //=================================================================================
165 // function : isValid
166 // purpose  :
167 //=================================================================================
168 bool AdvancedGUI_SmoothingSurfaceDlg::isValid (QString& msg)
169 {
170   bool ok = true;
171
172   //@@ add custom validation actions here @@//
173
174   return ok;
175 }
176
177 //=================================================================================
178 // function : execute
179 // purpose  :
180 //=================================================================================
181 bool AdvancedGUI_SmoothingSurfaceDlg::execute (ObjectList& objects)
182 {
183   bool res = false;
184
185   GEOM::GEOM_Object_var anObj;
186
187   GEOM::GEOM_IAdvancedOperations_var anOper = GEOM::GEOM_IAdvancedOperations::_narrow(getOperation());
188
189   //@@ retrieve input values from the widgets here @@//
190   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
191   points->length( myPoints.count() );
192   for ( int i = 0; i < myPoints.count(); i++ )
193     points[i] = myPoints[i].copy();
194
195
196   // call engine function
197   anObj = anOper->MakeSmoothingSurface(points);
198   res = !anObj->_is_nil();
199   if (res && !IsPreview())
200   {
201     QStringList aParameters;
202     //@@ put stringified input parameters to the string list here to store in the data model for notebook @@//
203     if ( aParameters.count() > 0 ) anObj->SetParameters(aParameters.join(":").toLatin1().constData());
204   }
205   
206   if (res)
207     objects.push_back(anObj._retn());
208
209   return res;
210 }
211
212 //=================================================================================
213 // function : getNbPoints()
214 // purpose  : Returns the number of points in myPoints list.
215 //=================================================================================
216 int AdvancedGUI_SmoothingSurfaceDlg::getNbPoints() const
217 {
218   TopTools_IndexedMapOfShape aMap;
219
220   foreach (GEOM::GeomObjPtr anObj, myPoints) {
221     TopoDS_Shape aShape;
222
223     if(anObj && GEOMBase::GetShape(anObj.get(), aShape) && !aShape.IsNull()) {
224       if (aShape.ShapeType() == TopAbs_VERTEX) {
225         aMap.Add(aShape);
226       } else {
227         // Compound.
228         TopExp::MapShapes(aShape, TopAbs_VERTEX, aMap);
229       }
230     }
231   }
232
233   const int aNbPoints = aMap.Extent();
234
235   return aNbPoints;
236 }
237
238 //=================================================================================
239 // function : SelectionIntoArgument()
240 // purpose  : Called when selection as changed or other case
241 //=================================================================================
242 void AdvancedGUI_SmoothingSurfaceDlg::SelectionIntoArgument()
243 {
244   QList<TopAbs_ShapeEnum> aTypes;
245
246   aTypes << TopAbs_VERTEX << TopAbs_COMPOUND;
247
248   QList<GEOM::GeomObjPtr> points = getSelected( aTypes, -1 );
249
250   // Check the selected compounds if they consist of points only.
251   foreach (GEOM::GeomObjPtr anObj, points) {
252     TopoDS_Shape aShape;
253
254     if(anObj && GEOMBase::GetShape(anObj.get(), aShape) && !aShape.IsNull()) {
255       if (aShape.ShapeType() == TopAbs_COMPOUND) {
256         // Check if the compound contains vertices only.
257         TopoDS_Iterator anIter(aShape);
258         Standard_Boolean isValid = Standard_False;
259
260         for (; anIter.More(); anIter.Next()) {
261           const TopoDS_Shape &aSubShape = anIter.Value();
262
263           if (aSubShape.ShapeType() == TopAbs_VERTEX) {
264             isValid = Standard_True;
265           } else {
266             isValid = Standard_False;
267             break;
268           }
269         }
270
271         if (!isValid) {
272           points.clear();
273           break;
274         }
275       }
276     } else {
277       // NEVERREACHED
278       points.clear();
279       break;
280     }
281   }
282
283   GEOMBase::Synchronize( myPoints, points );
284   if ( !myPoints.isEmpty()  )
285     GroupPoints->LineEdit1->setText( QString::number( getNbPoints() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
286   else
287     GroupPoints->LineEdit1->setText( "" );
288   processPreview();
289 }
290
291 //=================================================================================
292 // function : SetEditCurrentArgument()
293 // purpose  :
294 //=================================================================================
295 void AdvancedGUI_SmoothingSurfaceDlg::SetEditCurrentArgument()
296 {
297   if ( sender() == GroupPoints->PushButton1 )
298     myEditCurrentArgument = GroupPoints->LineEdit1;
299   myEditCurrentArgument->setFocus();
300   SelectionIntoArgument();
301 }