]> SALOME platform Git repositories - modules/geom.git/blob - src/BasicGUI/BasicGUI_CurveDlg.cxx
Salome HOME
Implementation of the "21187: EDF 1137 GEOM: Performance issue when manipulating...
[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   if ( ClickOnApply() )
212     ClickOnCancel();
213 }
214
215 //=================================================================================
216 // function : ClickOnApply()
217 // purpose  :
218 //=================================================================================
219 bool BasicGUI_CurveDlg::ClickOnApply()
220 {
221   if ( !onAccept() )
222     return false;
223
224   initName();
225   globalSelection(); // close local contexts, if any
226   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
227   return true;
228 }
229
230 static void synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
231 {
232   // 1. remove items from the "left" list that are not in the "right" list
233   QMutableListIterator<GEOM::GeomObjPtr> it1( left );
234   while ( it1.hasNext() ) {
235     GEOM::GeomObjPtr o1 = it1.next();
236     bool found = false;
237     QMutableListIterator<GEOM::GeomObjPtr> it2( right );
238     while ( it2.hasNext() && !found )
239       found = o1 == it2.next();
240     if ( !found )
241       it1.remove();
242   }
243   // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
244   it1 = right;
245   while ( it1.hasNext() ) {
246     GEOM::GeomObjPtr o1 = it1.next();
247     bool found = false;
248     QMutableListIterator<GEOM::GeomObjPtr> it2( left );
249     while ( it2.hasNext() && !found )
250       found = o1 == it2.next();
251     if ( !found )
252       left << o1;
253   }
254 }
255
256 //=================================================================================
257 // function : SelectionIntoArgument()
258 // purpose  : Called when selection as changed or other case
259 //=================================================================================
260 void BasicGUI_CurveDlg::SelectionIntoArgument()
261 {
262   QList<GEOM::GeomObjPtr> points = getSelected( TopAbs_VERTEX, -1 );
263   synchronize( myPoints, points );
264   if ( !myPoints.isEmpty()  )
265     GroupPoints->LineEdit1->setText( QString::number( myPoints.count() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
266   else
267     GroupPoints->LineEdit1->setText( "" );
268   processPreview();
269 }
270
271
272 //=================================================================================
273 // function : ActivateThisDialog()
274 // purpose  :
275 //=================================================================================
276 void BasicGUI_CurveDlg::ActivateThisDialog()
277 {
278   GEOMBase_Skeleton::ActivateThisDialog();
279   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
280            this, SLOT( SelectionIntoArgument() ) );
281
282   globalSelection(); // close local contexts, if any
283   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
284
285   ConstructorsClicked( getConstructorId() );
286 }
287
288 //=================================================================================
289 // function : DeactivateActiveDialog()
290 // purpose  : public slot to deactivate if active
291 //=================================================================================
292 void BasicGUI_CurveDlg::DeactivateActiveDialog()
293 {
294   // myGeomGUI->SetState( -1 );
295   GEOMBase_Skeleton::DeactivateActiveDialog();
296 }
297
298 //=================================================================================
299 // function : enterEvent()
300 // purpose  :
301 //=================================================================================
302 void BasicGUI_CurveDlg::enterEvent( QEvent* )
303 {
304   if ( !mainFrame()->GroupConstructors->isEnabled() )
305     ActivateThisDialog();
306 }
307
308 //=================================================================================
309 // function : createOperation
310 // purpose  :
311 //=================================================================================
312 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
313 {
314   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
315 }
316
317 //=================================================================================
318 // function : isValid
319 // purpose  :
320 //=================================================================================
321 bool BasicGUI_CurveDlg::isValid( QString& msg )
322 {
323   return myPoints.count() > 1;
324 }
325
326 //=================================================================================
327 // function : execute
328 // purpose  :
329 //=================================================================================
330 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
331 {
332   bool res = false;
333
334   GEOM::GEOM_Object_var anObj;
335
336   GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
337
338   GEOM::ListOfGO_var points = new GEOM::ListOfGO();
339   points->length( myPoints.count() );
340   for ( int i = 0; i < myPoints.count(); i++ )
341     points[i] = myPoints[i].copy();
342
343   switch ( getConstructorId() ) {
344   case 0 :
345     anObj = anOper->MakePolyline( points.in(), GroupPoints->CheckButton1->isChecked() );
346     res = true;
347     break;
348   case 1 :
349     anObj = anOper->MakeSplineBezier( points.in(), GroupPoints->CheckButton1->isChecked() );
350     res = true;
351     break;
352   case 2 :
353     anObj = anOper->MakeSplineInterpolation( points.in(), GroupPoints->CheckButton1->isChecked(),
354                                              GroupPoints->CheckButton2->isChecked() );
355     res = true;
356     break;
357   }
358
359   if ( !anObj->_is_nil() )
360     objects.push_back( anObj._retn() );
361
362   return res;
363 }
364
365 //=================================================================================
366 // function : addSubshapeToStudy
367 // purpose  : virtual method to add new SubObjects if local selection
368 //=================================================================================
369 void BasicGUI_CurveDlg::addSubshapesToStudy()
370 {
371   for ( int i = 0; i < myPoints.count(); i++ )
372     GEOMBase::PublishSubObject( myPoints[i].get() );
373 }