Salome HOME
e8255f369c8cd483b51759bf39f72f54b4b0ce3e
[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                                       bool modal, Qt::WindowFlags fl )
50   : GEOMBase_Skeleton( theGeometryGUI, parent, "BasicGUI_CurveDlg", 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( buttonOk(),     SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
111   connect( buttonApply(),  SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
112
113   connect( this,           SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
114
115   connect( GroupPoints->PushButton1, SIGNAL( clicked() ),       this, SLOT( SetEditCurrentArgument() ) );
116   connect( GroupPoints->LineEdit1,   SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
117
118   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
119            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
120
121   initName( tr( "GEOM_CURVE" ) );
122   ConstructorsClicked( 0 );
123 }
124
125 //=================================================================================
126 // function : ConstructorsClicked()
127 // purpose  :
128 //=================================================================================
129 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
130 {
131   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
132   mainFrame()->GroupConstructors->setTitle( aTitle );
133         
134   myPoints = new GEOM::ListOfGO();
135   myPoints->length( 0 );  
136
137   myEditCurrentArgument->setText( "" );
138 }
139
140
141 //=================================================================================
142 // function : SetEditCurrentArgument()
143 // purpose  :
144 //=================================================================================
145 void BasicGUI_CurveDlg::SetEditCurrentArgument()
146 {
147   if ( sender() == GroupPoints->PushButton1 ) 
148     myEditCurrentArgument = GroupPoints->LineEdit1;
149   myEditCurrentArgument->setFocus();
150   SelectionIntoArgument();
151 }
152
153
154 //=================================================================================
155 // function : LineEditReturnPressed()
156 // purpose  :
157 //=================================================================================
158 void BasicGUI_CurveDlg::LineEditReturnPressed()
159 {
160   if ( sender() == GroupPoints->LineEdit1 ) 
161   {
162     myEditCurrentArgument = GroupPoints->LineEdit1;
163     GEOMBase_Skeleton::LineEditReturnPressed();
164   }
165 }
166
167 //=================================================================================
168 // function : ClickOnOk()
169 // purpose  :
170 //=================================================================================
171 void BasicGUI_CurveDlg::ClickOnOk()
172 {
173   if ( ClickOnApply() )
174     ClickOnCancel();
175 }
176
177 //=================================================================================
178 // function : ClickOnApply()
179 // purpose  :
180 //=================================================================================
181 bool BasicGUI_CurveDlg::ClickOnApply()
182 {
183   if ( !onAccept() )
184     return false;
185
186   initName();
187   ConstructorsClicked( getConstructorId() );
188   return true;
189 }
190
191 //=================================================================================
192 /*! function : isPointInList()
193  *  purpose  : Check is point (theObject) in the list \a thePoints.
194  * \author enk
195  * \retval -1, if point not in list, else 1 in list
196  */
197 //=================================================================================
198 static int isPointInList( list<GEOM::GEOM_Object_var>& thePoints,
199                           GEOM::GEOM_Object_var& theObject )
200 {
201   int len = thePoints.size();
202   
203   if ( len < 1 ) {
204     return -1;
205   }
206   
207   for ( list<GEOM::GEOM_Object_var>::iterator i = thePoints.begin(); i != thePoints.end(); i++ ) {
208     if ( string( (*i)->GetEntry() ) == string( theObject->GetEntry() ) ) {
209       return 1;
210     }
211   }
212
213   return -1;
214 }
215 //=================================================================================
216 /*! function : removeUnnecessaryPnt()
217  *  purpose  : Remove unnecessary points from list \a theOldPoints
218  * \author enk
219  * \li \a theOldPoints - ordered sequence with unnecessary point
220  * \li \a theNewPoints - not ordered sequence with necessary points
221  */
222 //=================================================================================
223 static void removeUnnecessaryPnt( list<GEOM::GEOM_Object_var>& theOldPoints,
224                                   GEOM::ListOfGO_var& theNewPoints )
225 {
226   list<GEOM::GEOM_Object_var> objs_to_remove;
227   for ( list<GEOM::GEOM_Object_var>::iterator i = theOldPoints.begin(); i != theOldPoints.end(); i++ ) {
228     bool found = false;
229     for ( int j = 0; j < theNewPoints->length() && !found ; j++ ) {
230       if ( string( (*i)->GetEntry() ) == string( theNewPoints[j]->GetEntry() ) ) {
231         found = true;
232       }
233     }
234     if ( !found ) {
235       objs_to_remove.push_back( *i );
236       //cout << "removed: " << (*i)->GetEntry() << endl;
237     }
238   }
239   for ( list<GEOM::GEOM_Object_var>::iterator i = objs_to_remove.begin(); i != objs_to_remove.end(); i++ ) {
240     theOldPoints.remove( *i );
241   }
242 }
243
244 //=================================================================================
245 // function : SelectionIntoArgument()
246 // purpose  : Called when selection as changed or other case
247 //=================================================================================
248 void BasicGUI_CurveDlg::SelectionIntoArgument()
249 {
250   myEditCurrentArgument->setText( "" );
251
252   Standard_Boolean aRes = Standard_False;
253   int i = 0;
254   int IOC = IObjectCount();
255   bool is_append = myPoints->length() < IOC; // if true - add point, else remove
256   myPoints->length( IOC ); // this length may be greater than number of objects,
257                            // that will actually be put into myPoints
258   for ( SALOME_ListIteratorOfListIO anIt( selectedIO() ); anIt.More(); anIt.Next() ) {
259     GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
260     if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
261       //TopoDS_Shape aPointShape;
262       //if ( myGeomBase->GetShape( aSelectedObject, aPointShape, TopAbs_VERTEX ) )
263       int pos = isPointInList(myOrderedSel, aSelectedObject);
264       if ( is_append && pos == -1 )
265         myOrderedSel.push_back( aSelectedObject );
266       myPoints[i++] = aSelectedObject;
267     }
268   }
269   
270   myPoints->length( i ); // this is the right length, smaller of equal to the previously set
271   if ( IOC == 0 )
272     myOrderedSel.clear();
273   else
274     removeUnnecessaryPnt( myOrderedSel, myPoints );
275
276   if ( myOrderedSel.size() == myPoints->length() ) {
277     int k = 0;
278     for ( list<GEOM::GEOM_Object_var>::iterator j = myOrderedSel.begin(); j!= myOrderedSel.end(); j++ )
279       myPoints[k++] = *j;
280   } 
281   else {
282     //cout << "ERROR: Ordered sequence size != selection sequence size! ("<<myOrderedSel.size()<<"!="<<myPoints->length()<<")"<<endl;
283   }
284   if ( i )
285     GroupPoints->LineEdit1->setText( QString::number( i ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
286   
287   displayPreview(); 
288 }
289
290
291 //=================================================================================
292 // function : ActivateThisDialog()
293 // purpose  :
294 //=================================================================================
295 void BasicGUI_CurveDlg::ActivateThisDialog()
296 {
297   GEOMBase_Skeleton::ActivateThisDialog();
298   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
299            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
300
301   // myGeomGUI->SetState( 0 );
302
303   globalSelection( GEOM_POINT );
304   ConstructorsClicked( getConstructorId() );
305 }
306
307 //=================================================================================
308 // function : DeactivateActiveDialog()
309 // purpose  : public slot to deactivate if active
310 //=================================================================================
311 void BasicGUI_CurveDlg::DeactivateActiveDialog()
312 {
313   // myGeomGUI->SetState( -1 );
314   GEOMBase_Skeleton::DeactivateActiveDialog();
315 }
316
317 //=================================================================================
318 // function : enterEvent()
319 // purpose  :
320 //=================================================================================
321 void BasicGUI_CurveDlg::enterEvent( QEvent* )
322 {
323   if ( !mainFrame()->GroupConstructors->isEnabled() )
324     ActivateThisDialog();
325 }
326
327 //=================================================================================
328 // function : createOperation
329 // purpose  :
330 //=================================================================================
331 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
332 {
333   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
334 }
335
336 //=================================================================================
337 // function : isValid
338 // purpose  :
339 //=================================================================================
340 bool BasicGUI_CurveDlg::isValid( QString& msg )
341 {
342   return myPoints->length() > 1;
343 }
344
345 //=================================================================================
346 // function : execute
347 // purpose  :
348 //=================================================================================
349 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
350 {
351   bool res = false;
352
353   GEOM::GEOM_Object_var anObj;
354
355   switch ( getConstructorId() ) {
356   case 0 :
357     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
358     res = true;
359     break;
360   case 1 :
361     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
362     res = true;
363     break;
364   case 2 :
365     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
366     res = true;
367     break;
368   }
369
370   if ( !anObj->_is_nil() )
371     objects.push_back( anObj._retn() );
372
373   return res;
374 }
375
376 //=================================================================================
377 // function : closeEvent
378 // purpose  :
379 //=================================================================================
380 void BasicGUI_CurveDlg::closeEvent( QCloseEvent* e )
381 {
382   GEOMBase_Skeleton::closeEvent( e );
383 }
384