]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_CurveDlg.cxx
Salome HOME
Correct wrong addition
[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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BasicGUI_CurveDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
26 //
27 #include "BasicGUI_CurveDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37 #include <LightApp_SelectionMgr.h>
38
39 #include <SALOME_ListIteratorOfListIO.hxx>
40 #include <SALOME_ListIO.hxx>
41
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS.hxx>
44 #include <TopExp.hxx>
45 #include <TColStd_IndexedMapOfInteger.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47
48 #include <GEOMImpl_Types.hxx>
49
50 //=================================================================================
51 // class    : BasicGUI_CurveDlg()
52 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the
53 //            name 'name' and widget flags set to 'f'.
54 //            The dialog will by default be modeless, unless you set 'modal' to
55 //            TRUE to construct a modal dialog.
56 //=================================================================================
57 BasicGUI_CurveDlg::BasicGUI_CurveDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
58                                       bool modal, Qt::WindowFlags fl )
59   : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
60 {
61   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_POLYLINE" ) ) );
62   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SPLINE" ) ) );
63   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BEZIER" ) ) );
64   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
65
66   setWindowTitle( tr( "GEOM_CURVE_TITLE" ) );
67
68   /***************************************************************/
69   mainFrame()->RadioButton1->setIcon( image0 );
70   mainFrame()->RadioButton2->setIcon( image3 );
71   mainFrame()->RadioButton3->setIcon( image2 );
72
73   GroupPoints = new DlgRef_1Sel1Check( centralWidget() );
74
75   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
76   GroupPoints->TextLabel1->setText( tr( "GEOM_POINTS" ) );
77   GroupPoints->PushButton1->setIcon( image1 );
78   GroupPoints->PushButton1->setDown( true );
79
80   GroupPoints->LineEdit1->setReadOnly( true );
81
82   GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
83   GroupPoints->CheckButton1->setChecked(false);
84   GroupPoints->CheckButton1->hide();
85
86   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
87   layout->setMargin( 0 ); layout->setSpacing( 6 );
88   layout->addWidget( GroupPoints );
89   /***************************************************************/
90
91   setHelpFileName( "create_curve_page.html" );
92
93   Init();
94 }
95
96
97 //=================================================================================
98 // function : ~BasicGUI_CurveDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
102 {
103 }
104
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void BasicGUI_CurveDlg::Init()
111 {
112   /* init variables */
113   myEditCurrentArgument = GroupPoints->LineEdit1;
114
115   myPoints.clear();
116
117   globalSelection(); // close local contexts, if any
118   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
119
120   /* signals and slots connections */
121   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
122   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
123
124   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
125   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
126
127   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
128
129   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
130
131   connect( GroupPoints->CheckButton1,SIGNAL( toggled(bool) ),   this, SLOT( CheckButtonToggled() ) );
132
133   connect( myGeomGUI->getApp()->selectionMgr(),
134            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
135
136   initName( tr( "GEOM_CURVE" ) );
137   resize(100,100);
138   ConstructorsClicked( 0 );
139 }
140
141 //=================================================================================
142 // function : ConstructorsClicked()
143 // purpose  :
144 //=================================================================================
145 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
146 {
147   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
148   mainFrame()->GroupConstructors->setTitle( aTitle );
149
150   if (id == 2) // b-spline
151     GroupPoints->CheckButton1->show();
152   else
153     GroupPoints->CheckButton1->hide();
154
155   myPoints.clear();
156
157   myEditCurrentArgument->setText( "" );
158   qApp->processEvents();
159   updateGeometry();
160   resize( minimumSizeHint() );
161   SelectionIntoArgument();
162 }
163
164
165 //=================================================================================
166 // function : SetEditCurrentArgument()
167 // purpose  :
168 //=================================================================================
169 void BasicGUI_CurveDlg::SetEditCurrentArgument()
170 {
171   if ( sender() == GroupPoints->PushButton1 )
172     myEditCurrentArgument = GroupPoints->LineEdit1;
173   myEditCurrentArgument->setFocus();
174   SelectionIntoArgument();
175 }
176
177 //=================================================================================
178 // function : CheckButtonToggled()
179 // purpose  :
180 //=================================================================================
181 void BasicGUI_CurveDlg::CheckButtonToggled()
182 {
183   displayPreview();
184 }
185
186 //=================================================================================
187 // function : ClickOnOk()
188 // purpose  :
189 //=================================================================================
190 void BasicGUI_CurveDlg::ClickOnOk()
191 {
192   if ( ClickOnApply() )
193     ClickOnCancel();
194 }
195
196 //=================================================================================
197 // function : ClickOnApply()
198 // purpose  :
199 //=================================================================================
200 bool BasicGUI_CurveDlg::ClickOnApply()
201 {
202   if ( !onAccept() )
203     return false;
204
205   initName();
206   globalSelection(); // close local contexts, if any
207   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
208   return true;
209 }
210
211 static void synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
212 {
213   // 1. remove items from the "left" list that are not in the "right" list
214   QMutableListIterator<GEOM::GeomObjPtr> it1( left );
215   while ( it1.hasNext() ) {
216     GEOM::GeomObjPtr o1 = it1.next();
217     bool found = false;
218     QMutableListIterator<GEOM::GeomObjPtr> it2( right );
219     while ( it2.hasNext() && !found )
220       found = o1 == it2.next();
221     if ( !found )
222       it1.remove();
223   }
224   // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
225   it1 = right;
226   while ( it1.hasNext() ) {
227     GEOM::GeomObjPtr o1 = it1.next();
228     bool found = false;
229     QMutableListIterator<GEOM::GeomObjPtr> it2( left );
230     while ( it2.hasNext() && !found )
231       found = o1 == it2.next();
232     if ( !found )
233       left << o1;
234   }
235 }
236
237 //=================================================================================
238 // function : SelectionIntoArgument()
239 // purpose  : Called when selection as changed or other case
240 //=================================================================================
241 void BasicGUI_CurveDlg::SelectionIntoArgument()
242 {
243   QList<GEOM::GeomObjPtr> points = getSelected( TopAbs_VERTEX, -1 );
244   synchronize( myPoints, points );
245   if ( !myPoints.isEmpty()  )
246     GroupPoints->LineEdit1->setText( QString::number( myPoints.count() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
247   else
248     GroupPoints->LineEdit1->setText( "" );
249   displayPreview();
250 }
251
252
253 //=================================================================================
254 // function : ActivateThisDialog()
255 // purpose  :
256 //=================================================================================
257 void BasicGUI_CurveDlg::ActivateThisDialog()
258 {
259   GEOMBase_Skeleton::ActivateThisDialog();
260   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
261            this, SLOT( SelectionIntoArgument() ) );
262
263   globalSelection(); // close local contexts, if any
264   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
265
266   ConstructorsClicked( getConstructorId() );
267 }
268
269 //=================================================================================
270 // function : DeactivateActiveDialog()
271 // purpose  : public slot to deactivate if active
272 //=================================================================================
273 void BasicGUI_CurveDlg::DeactivateActiveDialog()
274 {
275   // myGeomGUI->SetState( -1 );
276   GEOMBase_Skeleton::DeactivateActiveDialog();
277 }
278
279 //=================================================================================
280 // function : enterEvent()
281 // purpose  :
282 //=================================================================================
283 void BasicGUI_CurveDlg::enterEvent( QEvent* )
284 {
285   if ( !mainFrame()->GroupConstructors->isEnabled() )
286     ActivateThisDialog();
287 }
288
289 //=================================================================================
290 // function : createOperation
291 // purpose  :
292 //=================================================================================
293 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
294 {
295   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
296 }
297
298 //=================================================================================
299 // function : isValid
300 // purpose  :
301 //=================================================================================
302 bool BasicGUI_CurveDlg::isValid( QString& msg )
303 {
304   return myPoints.count() > 1;
305 }
306
307 //=================================================================================
308 // function : execute
309 // purpose  :
310 //=================================================================================
311 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
312 {
313   bool res = false;
314
315   GEOM::GEOM_Object_var anObj;
316
317   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
318
319   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
320   points->length( myPoints.count() );
321   for ( int i = 0; i < myPoints.count(); i++ )
322     points[i] = myPoints[i].copy();
323
324   switch ( getConstructorId() ) {
325   case 0 :
326     anObj = anOper->MakePolyline( points.in() );
327     res = true;
328     break;
329   case 1 :
330     anObj = anOper->MakeSplineBezier( points.in() );
331     res = true;
332     break;
333   case 2 :
334     anObj = anOper->MakeSplineInterpolation( points.in(), GroupPoints->CheckButton1->isChecked() );
335     res = true;
336     break;
337   }
338
339   if ( !anObj->_is_nil() )
340     objects.push_back( anObj._retn() );
341
342   return res;
343 }
344
345 //=================================================================================
346 // function : addSubshapeToStudy
347 // purpose  : virtual method to add new SubObjects if local selection
348 //=================================================================================
349 void BasicGUI_CurveDlg::addSubshapesToStudy()
350 {
351   for ( int i = 0; i < myPoints.count(); i++ )
352     GEOMBase::PublishSubObject( myPoints[i].get() );
353 }