Salome HOME
IMPs 21188, 21053, 21064
[modules/geom.git] / src / BasicGUI / BasicGUI_CurveDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : BasicGUI_CurveDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25
26 #include "BasicGUI_CurveDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_ResourceMgr.h>
33 #include <SUIT_Session.h>
34 #include <SalomeApp_Application.h>
35 #include <SalomeApp_Study.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <SALOME_ListIteratorOfListIO.hxx>
39 #include <SALOME_ListIO.hxx>
40
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
49 //=================================================================================
50 // class    : BasicGUI_CurveDlg()
51 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the
52 //            name 'name' and widget flags set to 'f'.
53 //            The dialog will by default be modeless, unless you set 'modal' to
54 //            TRUE to construct a modal dialog.
55 //=================================================================================
56 BasicGUI_CurveDlg::BasicGUI_CurveDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
57                                       bool modal, Qt::WindowFlags fl )
58   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
59 {
60   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_POLYLINE" ) ) );
61   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SPLINE" ) ) );
62   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BEZIER" ) ) );
63   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
64
65   setWindowTitle( tr( "GEOM_CURVE_TITLE" ) );
66
67   /***************************************************************/
68   mainFrame()->RadioButton1->setIcon( image0 );
69   mainFrame()->RadioButton2->setIcon( image3 );
70   mainFrame()->RadioButton3->setIcon( image2 );
71
72   GroupPoints = new DlgRef_1Sel3Check( centralWidget() );
73
74   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
75   GroupPoints->TextLabel1->setText( tr( "GEOM_POINTS" ) );
76   GroupPoints->PushButton1->setIcon( image1 );
77   GroupPoints->PushButton1->setDown( true );
78
79   GroupPoints->LineEdit1->setReadOnly( true );
80
81   GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
82   GroupPoints->CheckButton1->setChecked(false);
83   //GroupPoints->CheckButton1->hide();
84
85   GroupPoints->CheckButton2->setText( tr( "GEOM_IS_REORDER" ) );
86   GroupPoints->CheckButton2->setChecked(false);
87   GroupPoints->CheckButton2->hide();
88
89   GroupPoints->CheckButton3->hide();
90
91   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
92   layout->setMargin( 0 ); layout->setSpacing( 6 );
93   layout->addWidget( GroupPoints );
94   /***************************************************************/
95
96   setHelpFileName( "create_curve_page.html" );
97
98   Init();
99 }
100
101
102 //=================================================================================
103 // function : ~BasicGUI_CurveDlg()
104 // purpose  : Destroys the object and frees any allocated resources
105 //=================================================================================
106 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
107 {
108 }
109
110
111 //=================================================================================
112 // function : Init()
113 // purpose  :
114 //=================================================================================
115 void BasicGUI_CurveDlg::Init()
116 {
117   /* init variables */
118   myEditCurrentArgument = GroupPoints->LineEdit1;
119
120   myPoints.clear();
121
122   globalSelection(); // close local contexts, if any
123   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
124
125   showOnlyPreviewControl();
126
127   /* signals and slots connections */
128   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
129   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
130
131   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
132   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
133
134   connect( this,          SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
135
136   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
137
138   connect( GroupPoints->CheckButton1, SIGNAL( toggled(bool) ),  this, SLOT( CheckButtonToggled() ) );
139   connect( GroupPoints->CheckButton2, SIGNAL( toggled(bool) ),  this, SLOT( CheckButtonToggled() ) );
140
141   connect( myGeomGUI->getApp()->selectionMgr(),
142            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
143
144   initName( tr( "GEOM_CURVE" ) );
145   resize(100,100);
146   ConstructorsClicked( 0 );  
147 }
148
149 //=================================================================================
150 // function : ConstructorsClicked()
151 // purpose  :
152 //=================================================================================
153 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
154 {
155   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
156   mainFrame()->GroupConstructors->setTitle( aTitle );
157
158   if (id == 0) { // polyline (wire)
159     //GroupPoints->CheckButton1->hide();
160     GroupPoints->CheckButton1->setText( tr( "GEOM_BUILD_CLOSED_WIRE" ) );
161     GroupPoints->CheckButton2->hide();
162   }
163   else if (id == 1) { // bezier
164     //GroupPoints->CheckButton1->hide();
165     GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
166     GroupPoints->CheckButton2->hide();
167   }
168   else { // b-spline
169     //GroupPoints->CheckButton1->show();
170     GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
171     GroupPoints->CheckButton2->show();
172   }
173
174   myPoints.clear();
175
176   myEditCurrentArgument->setText( "" );
177   qApp->processEvents();
178   updateGeometry();
179   resize( minimumSizeHint() );
180   SelectionIntoArgument();
181 }
182
183
184 //=================================================================================
185 // function : SetEditCurrentArgument()
186 // purpose  :
187 //=================================================================================
188 void BasicGUI_CurveDlg::SetEditCurrentArgument()
189 {
190   if ( sender() == GroupPoints->PushButton1 )
191     myEditCurrentArgument = GroupPoints->LineEdit1;
192   myEditCurrentArgument->setFocus();
193   SelectionIntoArgument();
194 }
195
196 //=================================================================================
197 // function : CheckButtonToggled()
198 // purpose  :
199 //=================================================================================
200 void BasicGUI_CurveDlg::CheckButtonToggled()
201 {
202   processPreview();
203 }
204
205 //=================================================================================
206 // function : ClickOnOk()
207 // purpose  :
208 //=================================================================================
209 void BasicGUI_CurveDlg::ClickOnOk()
210 {
211   setIsApplyAndClose( true );
212   if ( ClickOnApply() )
213     ClickOnCancel();
214 }
215
216 //=================================================================================
217 // function : ClickOnApply()
218 // purpose  :
219 //=================================================================================
220 bool BasicGUI_CurveDlg::ClickOnApply()
221 {
222   if ( !onAccept() )
223     return false;
224
225   initName();
226   globalSelection(); // close local contexts, if any
227   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
228   return true;
229 }
230
231 static void synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
232 {
233   // 1. remove items from the "left" list that are not in the "right" list
234   QMutableListIterator<GEOM::GeomObjPtr> it1( left );
235   while ( it1.hasNext() ) {
236     GEOM::GeomObjPtr o1 = it1.next();
237     bool found = false;
238     QMutableListIterator<GEOM::GeomObjPtr> it2( right );
239     while ( it2.hasNext() && !found )
240       found = o1 == it2.next();
241     if ( !found )
242       it1.remove();
243   }
244   // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
245   it1 = right;
246   while ( it1.hasNext() ) {
247     GEOM::GeomObjPtr o1 = it1.next();
248     bool found = false;
249     QMutableListIterator<GEOM::GeomObjPtr> it2( left );
250     while ( it2.hasNext() && !found )
251       found = o1 == it2.next();
252     if ( !found )
253       left << o1;
254   }
255 }
256
257 //=================================================================================
258 // function : SelectionIntoArgument()
259 // purpose  : Called when selection as changed or other case
260 //=================================================================================
261 void BasicGUI_CurveDlg::SelectionIntoArgument()
262 {
263   QList<GEOM::GeomObjPtr> points = getSelected( TopAbs_VERTEX, -1 );
264   synchronize( myPoints, points );
265   if ( !myPoints.isEmpty()  )
266     GroupPoints->LineEdit1->setText( QString::number( myPoints.count() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
267   else
268     GroupPoints->LineEdit1->setText( "" );
269   processPreview();
270 }
271
272
273 //=================================================================================
274 // function : ActivateThisDialog()
275 // purpose  :
276 //=================================================================================
277 void BasicGUI_CurveDlg::ActivateThisDialog()
278 {
279   GEOMBase_Skeleton::ActivateThisDialog();
280   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
281            this, SLOT( SelectionIntoArgument() ) );
282
283   globalSelection(); // close local contexts, if any
284   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
285
286   ConstructorsClicked( getConstructorId() );
287 }
288
289 //=================================================================================
290 // function : DeactivateActiveDialog()
291 // purpose  : public slot to deactivate if active
292 //=================================================================================
293 void BasicGUI_CurveDlg::DeactivateActiveDialog()
294 {
295   // myGeomGUI->SetState( -1 );
296   GEOMBase_Skeleton::DeactivateActiveDialog();
297 }
298
299 //=================================================================================
300 // function : enterEvent()
301 // purpose  :
302 //=================================================================================
303 void BasicGUI_CurveDlg::enterEvent( QEvent* )
304 {
305   if ( !mainFrame()->GroupConstructors->isEnabled() )
306     ActivateThisDialog();
307 }
308
309 //=================================================================================
310 // function : createOperation
311 // purpose  :
312 //=================================================================================
313 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
314 {
315   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
316 }
317
318 //=================================================================================
319 // function : isValid
320 // purpose  :
321 //=================================================================================
322 bool BasicGUI_CurveDlg::isValid( QString& msg )
323 {
324   return myPoints.count() > 1;
325 }
326
327 //=================================================================================
328 // function : execute
329 // purpose  :
330 //=================================================================================
331 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
332 {
333   bool res = false;
334
335   GEOM::GEOM_Object_var anObj;
336
337   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
338
339   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
340   points->length( myPoints.count() );
341   for ( int i = 0; i < myPoints.count(); i++ )
342     points[i] = myPoints[i].copy();
343
344   switch ( getConstructorId() ) {
345   case 0 :
346     anObj = anOper->MakePolyline( points.in(), GroupPoints->CheckButton1->isChecked() );
347     res = true;
348     break;
349   case 1 :
350     anObj = anOper->MakeSplineBezier( points.in(), GroupPoints->CheckButton1->isChecked() );
351     res = true;
352     break;
353   case 2 :
354     anObj = anOper->MakeSplineInterpolation( points.in(), GroupPoints->CheckButton1->isChecked(),
355                                              GroupPoints->CheckButton2->isChecked() );
356     res = true;
357     break;
358   }
359
360   if ( !anObj->_is_nil() )
361     objects.push_back( anObj._retn() );
362
363   return res;
364 }
365
366 //=================================================================================
367 // function : addSubshapeToStudy
368 // purpose  : virtual method to add new SubObjects if local selection
369 //=================================================================================
370 void BasicGUI_CurveDlg::addSubshapesToStudy()
371 {
372   for ( int i = 0; i < myPoints.count(); i++ )
373     GEOMBase::PublishSubObject( myPoints[i].get() );
374 }