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