Salome HOME
*** empty log message ***
[modules/geom.git] / src / BasicGUI / BasicGUI_CurveDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  OPEN CASCADE 
4 // 
5 // This library is free software; you can redistribute it and/or 
6 // modify it under the terms of the GNU Lesser General Public 
7 // License as published by the Free Software Foundation; either 
8 // version 2.1 of the License. 
9 // 
10 // This library is distributed in the hope that it will be useful, 
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 // Lesser General Public License for more details. 
14 // 
15 // You should have received a copy of the GNU Lesser General Public 
16 // License along with this library; if not, write to the Free Software 
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 // File   : BasicGUI_CurveDlg.cxx
22 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
23 //
24
25 #include "BasicGUI_CurveDlg.h"
26
27 #include <GEOM_DlgRef.h>
28 #include <GeometryGUI.h>
29 #include <GEOMBase.h>
30
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_Session.h>
33 #include <SalomeApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35
36 #include <SALOME_ListIteratorOfListIO.hxx>
37 #include <SALOME_ListIO.hxx>
38
39 #include <GEOMImpl_Types.hxx>
40
41 //=================================================================================
42 // class    : BasicGUI_CurveDlg()
43 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the 
44 //            name 'name' and widget flags set to 'f'.
45 //            The dialog will by default be modeless, unless you set 'modal' to
46 //            TRUE to construct a modal dialog.
47 //=================================================================================
48 BasicGUI_CurveDlg::BasicGUI_CurveDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
49                                       const char* name, bool modal, Qt::WindowFlags fl )
50   : GEOMBase_Skeleton( theGeometryGUI, parent, name, modal, fl )
51 {
52   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_POLYLINE" ) ) );
53   QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SPLINE" ) ) );
54   QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BEZIER" ) ) );
55   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
56
57   setWindowTitle( tr( "GEOM_CURVE_TITLE" ) );
58
59   /***************************************************************/
60   mainFrame()->RadioButton1->setIcon( image0 );
61   mainFrame()->RadioButton2->setIcon( image3 );
62   mainFrame()->RadioButton3->setIcon( image2 );
63
64   GroupPoints = new DlgRef_1Sel( centralWidget() );
65
66   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
67   GroupPoints->TextLabel1->setText( tr( "GEOM_POINTS" ) );
68   GroupPoints->PushButton1->setIcon( image1 );
69
70   GroupPoints->LineEdit1->setReadOnly( true );
71
72   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
73   layout->setMargin( 0 ); layout->setSpacing( 6 );
74   layout->addWidget( GroupPoints );
75   /***************************************************************/
76
77   setHelpFileName( "curve.htm" );
78
79   Init();
80 }
81
82
83 //=================================================================================
84 // function : ~BasicGUI_CurveDlg()
85 // purpose  : Destroys the object and frees any allocated resources
86 //=================================================================================
87 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
88 {
89 }
90
91
92 //=================================================================================
93 // function : Init()
94 // purpose  :
95 //=================================================================================
96 void BasicGUI_CurveDlg::Init()
97 {
98   /* init variables */
99   myEditCurrentArgument = GroupPoints->LineEdit1;
100
101   myPoints = new GEOM::ListOfGO();
102   myPoints->length( 0 );
103
104   globalSelection( GEOM_POINT );
105
106   /* signals and slots connections */
107   connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
108   connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ),        this, SLOT( ClickOnCancel() ) );
109   
110   connect( buttonCancel(), SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) );
111   connect( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
112   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
113
114   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
115
116   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
117   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
118
119   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
120            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
121
122   initName( tr( "GEOM_CURVE" ) );
123   ConstructorsClicked( 0 );
124 }
125
126 //=================================================================================
127 // function : ConstructorsClicked()
128 // purpose  :
129 //=================================================================================
130 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
131 {
132   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
133   mainFrame()->GroupConstructors->setTitle( aTitle );
134         
135   myPoints = new GEOM::ListOfGO();
136   myPoints->length( 0 );  
137
138   myEditCurrentArgument->setText( "" );
139 }
140
141
142 //=================================================================================
143 // function : SetEditCurrentArgument()
144 // purpose  :
145 //=================================================================================
146 void BasicGUI_CurveDlg::SetEditCurrentArgument()
147 {
148   if ( sender() == GroupPoints->PushButton1 ) 
149     myEditCurrentArgument = GroupPoints->LineEdit1;
150   myEditCurrentArgument->setFocus();
151   SelectionIntoArgument();
152 }
153
154
155 //=================================================================================
156 // function : LineEditReturnPressed()
157 // purpose  :
158 //=================================================================================
159 void BasicGUI_CurveDlg::LineEditReturnPressed()
160 {
161   if ( sender() == GroupPoints->LineEdit1 ) 
162   {
163     myEditCurrentArgument = GroupPoints->LineEdit1;
164     GEOMBase_Skeleton::LineEditReturnPressed();
165   }
166 }
167
168 //=================================================================================
169 // function : ClickOnOk()
170 // purpose  :
171 //=================================================================================
172 void BasicGUI_CurveDlg::ClickOnOk()
173 {
174   if ( ClickOnApply() )
175     ClickOnCancel();
176 }
177
178 //=================================================================================
179 // function : ClickOnApply()
180 // purpose  :
181 //=================================================================================
182 bool BasicGUI_CurveDlg::ClickOnApply()
183 {
184   if ( !onAccept() )
185     return false;
186
187   initName();
188   ConstructorsClicked( getConstructorId() );
189   return true;
190 }
191
192 //=======================================================================
193 // function : ClickOnCancel()
194 // purpose  :
195 //=======================================================================
196 void BasicGUI_CurveDlg::ClickOnCancel()
197 {
198   GEOMBase_Skeleton::ClickOnCancel();
199 }
200
201 //=================================================================================
202 /*! function : isPointInList()
203  *  purpose  : Check is point (theObject) in the list \a thePoints.
204  * \author enk
205  * \retval -1, if point not in list, else 1 in list
206  */
207 //=================================================================================
208 static int isPointInList( list<GEOM::GEOM_Object_var>& thePoints,
209                           GEOM::GEOM_Object_var& theObject )
210 {
211   int len = thePoints.size();
212   
213   if ( len < 1 ) {
214     return -1;
215   }
216   
217   for ( list<GEOM::GEOM_Object_var>::iterator i = thePoints.begin(); i != thePoints.end(); i++ ) {
218     if ( string( (*i)->GetEntry() ) == string( theObject->GetEntry() ) ) {
219       return 1;
220     }
221   }
222
223   return -1;
224 }
225 //=================================================================================
226 /*! function : removeUnnecessaryPnt()
227  *  purpose  : Remove unnecessary points from list \a theOldPoints
228  * \author enk
229  * \li \a theOldPoints - ordered sequence with unnecessary point
230  * \li \a theNewPoints - not ordered sequence with necessary points
231  */
232 //=================================================================================
233 static void removeUnnecessaryPnt( list<GEOM::GEOM_Object_var>& theOldPoints,
234                                   GEOM::ListOfGO_var& theNewPoints )
235 {
236   list<GEOM::GEOM_Object_var> objs_to_remove;
237   for ( list<GEOM::GEOM_Object_var>::iterator i = theOldPoints.begin(); i != theOldPoints.end(); i++ ) {
238     bool found = false;
239     for ( int j = 0; j < theNewPoints->length() && !found ; j++ ) {
240       if ( string( (*i)->GetEntry() ) == string( theNewPoints[j]->GetEntry() ) ) {
241         found = true;
242       }
243     }
244     if ( !found ) {
245       objs_to_remove.push_back( *i );
246       //cout << "removed: " << (*i)->GetEntry() << endl;
247     }
248   }
249   for ( list<GEOM::GEOM_Object_var>::iterator i = objs_to_remove.begin(); i != objs_to_remove.end(); i++ ) {
250     theOldPoints.remove( *i );
251   }
252 }
253
254 //=================================================================================
255 // function : SelectionIntoArgument()
256 // purpose  : Called when selection as changed or other case
257 //=================================================================================
258 void BasicGUI_CurveDlg::SelectionIntoArgument()
259 {
260   myEditCurrentArgument->setText( "" );
261
262   Standard_Boolean aRes = Standard_False;
263   int i = 0;
264   int IOC = IObjectCount();
265   bool is_append = myPoints->length() < IOC; // if true - add point, else remove
266   myPoints->length( IOC ); // this length may be greater than number of objects,
267                            // that will actually be put into myPoints
268   for ( SALOME_ListIteratorOfListIO anIt( selectedIO() ); anIt.More(); anIt.Next() ) {
269     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
270     if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
271       //TopoDS_Shape aPointShape;
272       //if ( myGeomBase->GetShape( aSelectedObject, aPointShape, TopAbs_VERTEX ) )
273       int pos = isPointInList(myOrderedSel, aSelectedObject);
274       if ( is_append && pos == -1 )
275         myOrderedSel.push_back( aSelectedObject );
276       myPoints[i++] = aSelectedObject;
277     }
278   }
279   
280   myPoints->length( i ); // this is the right length, smaller of equal to the previously set
281   if ( IOC == 0 )
282     myOrderedSel.clear();
283   else
284     removeUnnecessaryPnt( myOrderedSel, myPoints );
285
286   if ( myOrderedSel.size() == myPoints->length() ) {
287     int k = 0;
288     for ( list<GEOM::GEOM_Object_var>::iterator j = myOrderedSel.begin(); j!= myOrderedSel.end(); j++ )
289       myPoints[k++] = *j;
290   } 
291   else {
292     //cout << "ERROR: Ordered sequence size != selection sequence size! ("<<myOrderedSel.size()<<"!="<<myPoints->length()<<")"<<endl;
293   }
294   if ( i )
295     GroupPoints->LineEdit1->setText( QString::number( i ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
296   
297   displayPreview(); 
298 }
299
300
301 //=================================================================================
302 // function : ActivateThisDialog()
303 // purpose  :
304 //=================================================================================
305 void BasicGUI_CurveDlg::ActivateThisDialog()
306 {
307   GEOMBase_Skeleton::ActivateThisDialog();
308   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
309            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
310
311   // myGeomGUI->SetState( 0 );
312
313   globalSelection( GEOM_POINT );
314   ConstructorsClicked( getConstructorId() );
315 }
316
317 //=================================================================================
318 // function : DeactivateActiveDialog()
319 // purpose  : public slot to deactivate if active
320 //=================================================================================
321 void BasicGUI_CurveDlg::DeactivateActiveDialog()
322 {
323   // myGeomGUI->SetState( -1 );
324   GEOMBase_Skeleton::DeactivateActiveDialog();
325 }
326
327 //=================================================================================
328 // function : enterEvent()
329 // purpose  :
330 //=================================================================================
331 void BasicGUI_CurveDlg::enterEvent( QEvent* )
332 {
333   if ( !mainFrame()->GroupConstructors->isEnabled() )
334     ActivateThisDialog();
335 }
336
337 //=================================================================================
338 // function : createOperation
339 // purpose  :
340 //=================================================================================
341 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
342 {
343   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
344 }
345
346 //=================================================================================
347 // function : isValid
348 // purpose  :
349 //=================================================================================
350 bool BasicGUI_CurveDlg::isValid( QString& msg )
351 {
352   return myPoints->length() > 1;
353 }
354
355 //=================================================================================
356 // function : execute
357 // purpose  :
358 //=================================================================================
359 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
360 {
361   bool res = false;
362
363   GEOM::GEOM_Object_var anObj;
364
365   switch ( getConstructorId() ) {
366   case 0 :
367     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
368     res = true;
369     break;
370   case 1 :
371     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
372     res = true;
373     break;
374   case 2 :
375     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
376     res = true;
377     break;
378   }
379
380   if ( !anObj->_is_nil() )
381     objects.push_back( anObj._retn() );
382
383   return res;
384 }
385
386 //=================================================================================
387 // function : closeEvent
388 // purpose  :
389 //=================================================================================
390 void BasicGUI_CurveDlg::closeEvent( QCloseEvent* e )
391 {
392   GEOMBase_Skeleton::closeEvent( e );
393 }
394