Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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 //
22 //
23 //  File   : BasicGUI_CurveDlg.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : GEOM
26 //  $Header$
27
28 #include "BasicGUI_CurveDlg.h"
29
30 #include "SUIT_Desktop.h"
31 #include "SUIT_Session.h"
32 #include "SalomeApp_Application.h"
33 #include "SalomeApp_Study.h"
34 #include "LightApp_SelectionMgr.h"
35
36 #include <qlabel.h>
37
38 #include "utilities.h"
39
40 #include "SALOME_ListIteratorOfListIO.hxx"
41 #include "SALOME_ListIO.hxx"
42
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS.hxx>
45 #include <TopExp.hxx>
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48
49 #include "GEOMImpl_Types.hxx"
50
51 using namespace std;
52 #include <string>
53
54 //=================================================================================
55 // class    : BasicGUI_CurveDlg()
56 // purpose  : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the 
57 //            name 'name' and widget flags set to 'f'.
58 //            The dialog will by default be modeless, unless you set 'modal' to
59 //            TRUE to construct a modal dialog.
60 //=================================================================================
61 BasicGUI_CurveDlg::BasicGUI_CurveDlg(GeometryGUI* theGeometryGUI, QWidget* parent,
62                                      const char* name, bool modal, WFlags fl)
63   :GEOMBase_Skeleton(theGeometryGUI, parent, name, modal, WStyle_Customize |
64                      WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
65 {
66   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_POLYLINE")));
67   QPixmap image2(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_SPLINE")));
68   QPixmap image3(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_BEZIER")));
69
70   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_SELECT")));
71
72   setCaption(tr("GEOM_CURVE_TITLE"));
73
74   /***************************************************************/
75   RadioButton1->setPixmap( image0 );
76   RadioButton2->setPixmap( image3 );
77   RadioButton3->setPixmap( image2 );
78
79   GroupPoints = new DlgRef_1Sel_QTD( this, "GroupPoints" );
80   GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
81   GroupPoints->TextLabel1->setText( tr("GEOM_POINTS") );
82   GroupPoints->PushButton1->setPixmap(image1);
83
84   GroupPoints->LineEdit1->setReadOnly( true );
85
86   Layout1->addWidget(GroupPoints, 2, 0);
87   /***************************************************************/
88
89   setHelpFileName("create_curve_page.html");
90
91   Init();
92 }
93
94
95 //=================================================================================
96 // function : ~BasicGUI_CurveDlg()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
100 {
101 }
102
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void BasicGUI_CurveDlg::Init()
109 {
110   /* init variables */
111   myEditCurrentArgument = GroupPoints->LineEdit1;
112
113   myPoints = new GEOM::ListOfGO();
114   myPoints->length( 0 );
115
116   globalSelection(); // close local contexts, if any
117   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
118
119   /* signals and slots connections */
120   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
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   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
127
128   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
130
131   connect(myGeomGUI->getApp()->selectionMgr(), 
132           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
133
134   initName( tr( "GEOM_CURVE" ) );
135   ConstructorsClicked( 0 );
136 }
137
138 //=================================================================================
139 // function : ConstructorsClicked()
140 // purpose  :
141 //=================================================================================
142 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
143 {
144   QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
145   GroupConstructors->setTitle( aTitle );
146         
147         myPoints = new GEOM::ListOfGO();
148   myPoints->length( 0 );  
149
150   myEditCurrentArgument->setText("");
151 }
152
153
154 //=================================================================================
155 // function : SetEditCurrentArgument()
156 // purpose  :
157 //=================================================================================
158 void BasicGUI_CurveDlg::SetEditCurrentArgument()
159 {
160   if ( sender() == GroupPoints->PushButton1 ) 
161     myEditCurrentArgument = GroupPoints->LineEdit1;
162   myEditCurrentArgument->setFocus();
163   SelectionIntoArgument();
164 }
165
166
167 //=================================================================================
168 // function : LineEditReturnPressed()
169 // purpose  :
170 //=================================================================================
171 void BasicGUI_CurveDlg::LineEditReturnPressed()
172 {
173   if ( sender() == GroupPoints->LineEdit1 ) 
174   {
175     myEditCurrentArgument = GroupPoints->LineEdit1;
176     GEOMBase_Skeleton::LineEditReturnPressed();
177   }
178 }
179
180 //=================================================================================
181 // function : ClickOnOk()
182 // purpose  :
183 //=================================================================================
184 void BasicGUI_CurveDlg::ClickOnOk()
185 {
186   if ( ClickOnApply() )
187     ClickOnCancel();
188 }
189
190 //=================================================================================
191 // function : ClickOnApply()
192 // purpose  :
193 //=================================================================================
194 bool BasicGUI_CurveDlg::ClickOnApply()
195 {
196   if ( !onAccept() )
197     return false;
198
199   initName();
200   ConstructorsClicked( getConstructorId() );
201   globalSelection(); // close local contexts, if any
202   localSelection(GEOM::GEOM_Object::_nil(), TopAbs_VERTEX);
203   return true;
204 }
205
206 //=======================================================================
207 // function : ClickOnCancel()
208 // purpose  :
209 //=======================================================================
210 void BasicGUI_CurveDlg::ClickOnCancel()
211 {
212   GEOMBase_Skeleton::ClickOnCancel();
213 }
214
215 //=================================================================================
216 /*! function : isPointInList()
217  *  purpose  : Check is point (theObject) in the list \a thePoints.
218  * \author enk
219  * \retval -1, if point not in list, else 1 in list
220  */
221 //=================================================================================
222 static int isPointInList(list<GEOM::GEOM_Object_var>& thePoints,
223                          GEOM::GEOM_Object_var& theObject)
224 {
225   int len = thePoints.size();
226   
227   if(len<1){
228     return -1;
229   }
230   
231   for(list<GEOM::GEOM_Object_var>::iterator i=thePoints.begin();i!=thePoints.end();i++)
232     if (string((*i)->GetEntry()) == string(theObject->GetEntry())){
233       return 1;
234     }
235
236   return -1;
237 }
238
239 //=================================================================================
240 /*! function : removeUnnecessaryPnt()
241  *  purpose  : Remove unnecessary points from list \a theOldPoints
242  * \author enk
243  * \li \a theOldPoints - ordered sequence with unnecessary point
244  * \li \a theNewPoints - not ordered sequence with necessary points
245  */
246 //=================================================================================
247 static void removeUnnecessaryPnt(list<GEOM::GEOM_Object_var>& theOldPoints,
248                                  GEOM::ListOfGO_var& theNewPoints)
249 {
250   list<GEOM::GEOM_Object_var> objs_to_remove;
251   for(list<GEOM::GEOM_Object_var>::iterator i=theOldPoints.begin();i!=theOldPoints.end();i++){
252     bool found = false;
253     for (int j=0;j<theNewPoints->length() && !found ; j++){
254       if(string((*i)->GetEntry()) == string(theNewPoints[j]->GetEntry())){
255         found = true;
256       }
257     }
258     if(!found){
259       objs_to_remove.push_back(*i);
260       //cout << "removed: " << (*i)->GetEntry() << endl;
261     }
262   }
263   for(list<GEOM::GEOM_Object_var>::iterator i=objs_to_remove.begin();i!=objs_to_remove.end();i++){
264     theOldPoints.remove(*i);
265   }
266 }
267
268 //=================================================================================
269 // function : SelectionIntoArgument()
270 // purpose  : Called when selection as changed or other case
271 //=================================================================================
272 void BasicGUI_CurveDlg::SelectionIntoArgument()
273 {
274   myEditCurrentArgument->setText("");
275
276   Standard_Boolean aRes = Standard_False;
277   int IOC = IObjectCount();
278   //  bool is_append = myPoints->length() < IOC; // if true - add point, else remove
279   //  myPoints->length( IOC ); // this length may be greater than number of objects,
280                            // that will actually be put into myPoints
281
282   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
283   SalomeApp_Application* app =
284     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
285   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
286   _PTR(Study) aDStudy = appStudy->studyDS();
287   GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
288
289   int anIndex;
290   TopoDS_Shape aShape;
291   TColStd_IndexedMapOfInteger aMapIndexes;
292   GEOM::GEOM_Object_var anObject;
293   std::list<GEOM::GEOM_Object_var> aList;
294   SALOME_ListIO selected;
295   aSelMgr->selectedObjects( selected, QString::null, false );
296
297   for ( SALOME_ListIteratorOfListIO anIt( selected ); anIt.More(); anIt.Next() )
298     {
299       GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
300
301       if ( !CORBA::is_nil( aSelectedObject ) && aRes )
302         {
303           if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() )
304             {
305               aSelMgr->GetIndexes( anIt.Value(), aMapIndexes );
306
307               if ( aMapIndexes.Extent() > 0 )
308                 {
309                   for (int ii=1; ii <= aMapIndexes.Extent(); ii++) {
310                     anIndex = aMapIndexes(ii);
311                     QString aName = GEOMBase::GetName( aSelectedObject );
312                     aName = aName + ":vertex_" + QString::number( anIndex );
313                     anObject = aShapesOp->GetSubShape(aSelectedObject, anIndex);
314                     //Find Object in study
315                     _PTR(SObject) obj ( aDStudy->FindObjectID( anIt.Value()->getEntry() ) );
316                     bool inStudy = false;
317                     for (_PTR(ChildIterator) iit (aDStudy->NewChildIterator(obj)); iit->More(); iit->Next()) {
318                       _PTR(SObject) child (iit->Value());
319                       QString aChildName = child->GetName();
320                       if (aChildName == aName) {
321                         inStudy = true;
322                         CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(iit->Value());
323                         anObject = GEOM::GEOM_Object::_narrow( corbaObj );
324                       }
325                     }
326
327                     if (!inStudy)
328                       GeometryGUI::GetGeomGen()->AddInStudy(GeometryGUI::ClientStudyToStudy(aDStudy),
329                                                             anObject, aName, aSelectedObject);
330                     
331                     int pos = isPointInList(myOrderedSel, anObject);
332                     if (pos==-1) {
333                       myOrderedSel.push_back(anObject);
334                     }
335                     //              if (!inStudy)
336                     aList.push_back(anObject);
337                   }
338                 } else { // aMap.Extent() == 0
339                   int pos = isPointInList(myOrderedSel,aSelectedObject);
340                   if(pos==-1)
341                     myOrderedSel.push_back(aSelectedObject);
342                   aList.push_back(aSelectedObject);
343                 } 
344             }
345         }
346     }
347
348   myPoints->length( aList.size()  );  
349
350   int k=0;
351   for (list<GEOM::GEOM_Object_var>::iterator j=aList.begin();j!=aList.end();j++)
352     myPoints[k++] = *j;
353
354   if(IOC == 0)
355     myOrderedSel.clear();
356   else
357     removeUnnecessaryPnt(myOrderedSel, myPoints);
358
359   // if ( myOrderedSel.size() == myPoints->length() ) {
360   myPoints->length( myOrderedSel.size()  );  
361   k=0;
362   for (list<GEOM::GEOM_Object_var>::iterator j=myOrderedSel.begin();j!=myOrderedSel.end();j++)
363     myPoints[k++] = *j;
364   //  }
365
366   if ( myPoints->length() > 0  )
367     GroupPoints->LineEdit1->setText( QString::number( myPoints->length() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
368   
369   displayPreview(); 
370 }
371
372
373 //=================================================================================
374 // function : ActivateThisDialog()
375 // purpose  :
376 //=================================================================================
377 void BasicGUI_CurveDlg::ActivateThisDialog()
378 {
379   GEOMBase_Skeleton::ActivateThisDialog();
380   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
381           this, SLOT(SelectionIntoArgument()));
382
383   globalSelection(); // close local contexts, if any
384   localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
385   ConstructorsClicked( getConstructorId() );
386 }
387
388 //=================================================================================
389 // function : DeactivateActiveDialog()
390 // purpose  : public slot to deactivate if active
391 //=================================================================================
392 void BasicGUI_CurveDlg::DeactivateActiveDialog()
393 {
394   // myGeomGUI->SetState( -1 );
395   GEOMBase_Skeleton::DeactivateActiveDialog();
396 }
397
398 //=================================================================================
399 // function : enterEvent()
400 // purpose  :
401 //=================================================================================
402 void BasicGUI_CurveDlg::enterEvent(QEvent* e)
403 {
404   if ( !GroupConstructors->isEnabled() )
405     ActivateThisDialog();
406 }
407
408 //=================================================================================
409 // function : createOperation
410 // purpose  :
411 //=================================================================================
412 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
413 {
414   return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
415 }
416
417 //=================================================================================
418 // function : isValid
419 // purpose  :
420 //=================================================================================
421 bool BasicGUI_CurveDlg::isValid( QString& msg )
422 {
423   return myPoints->length() > 1;
424 }
425
426 //=================================================================================
427 // function : execute
428 // purpose  :
429 //=================================================================================
430 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
431 {
432   bool res = false;
433
434   GEOM::GEOM_Object_var anObj;
435
436   switch ( getConstructorId() )
437   {
438   case 0 :
439     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
440     res = true;
441     break;
442   case 1 :
443     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
444     res = true;
445     break;
446   case 2 :
447     anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
448     res = true;
449     break;
450   }
451
452   if ( !anObj->_is_nil() )
453     objects.push_back( anObj._retn() );
454
455   return res;
456 }
457
458 //=================================================================================
459 // function : closeEvent
460 // purpose  :
461 //=================================================================================
462 void BasicGUI_CurveDlg::closeEvent( QCloseEvent* e )
463 {
464   GEOMBase_Skeleton::closeEvent( e );
465 }
466