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