1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : BasicGUI_CurveDlg.cxx
25 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
27 #include "BasicGUI_CurveDlg.h"
30 #include <GeometryGUI.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SUIT_Session.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37 #include <LightApp_SelectionMgr.h>
39 #include <SALOME_ListIteratorOfListIO.hxx>
40 #include <SALOME_ListIO.hxx>
42 #include <TopoDS_Shape.hxx>
45 #include <TColStd_IndexedMapOfInteger.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <GEOMImpl_Types.hxx>
50 //=================================================================================
51 // class : BasicGUI_CurveDlg()
52 // purpose : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the
53 // name 'name' and widget flags set to 'f'.
54 // The dialog will by default be modeless, unless you set 'modal' to
55 // TRUE to construct a modal dialog.
56 //=================================================================================
57 BasicGUI_CurveDlg::BasicGUI_CurveDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
58 bool modal, Qt::WindowFlags fl )
59 : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
61 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_POLYLINE" ) ) );
62 QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SPLINE" ) ) );
63 QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BEZIER" ) ) );
64 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
66 setWindowTitle( tr( "GEOM_CURVE_TITLE" ) );
68 /***************************************************************/
69 mainFrame()->RadioButton1->setIcon( image0 );
70 mainFrame()->RadioButton2->setIcon( image3 );
71 mainFrame()->RadioButton3->setIcon( image2 );
73 GroupPoints = new DlgRef_1Sel1Check( centralWidget() );
75 GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
76 GroupPoints->TextLabel1->setText( tr( "GEOM_POINTS" ) );
77 GroupPoints->PushButton1->setIcon( image1 );
78 GroupPoints->PushButton1->setDown( true );
80 GroupPoints->LineEdit1->setReadOnly( true );
82 GroupPoints->CheckButton1->setText( tr( "GEOM_IS_CLOSED" ) );
83 GroupPoints->CheckButton1->setChecked(false);
84 GroupPoints->CheckButton1->hide();
86 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
87 layout->setMargin( 0 ); layout->setSpacing( 6 );
88 layout->addWidget( GroupPoints );
89 /***************************************************************/
91 setHelpFileName( "create_curve_page.html" );
97 //=================================================================================
98 // function : ~BasicGUI_CurveDlg()
99 // purpose : Destroys the object and frees any allocated resources
100 //=================================================================================
101 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
106 //=================================================================================
109 //=================================================================================
110 void BasicGUI_CurveDlg::Init()
113 myEditCurrentArgument = GroupPoints->LineEdit1;
115 myPoints = new GEOM::ListOfGO();
116 myPoints->length( 0 );
118 globalSelection(); // close local contexts, if any
119 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
121 /* signals and slots connections */
122 connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
123 connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
125 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
126 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
128 connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
130 connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
131 connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
133 connect( GroupPoints->CheckButton1,SIGNAL( toggled(bool) ), this, SLOT( CheckButtonToggled() ) );
135 connect( myGeomGUI->getApp()->selectionMgr(),
136 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
138 initName( tr( "GEOM_CURVE" ) );
140 ConstructorsClicked( 0 );
143 //=================================================================================
144 // function : ConstructorsClicked()
146 //=================================================================================
147 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
149 QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
150 mainFrame()->GroupConstructors->setTitle( aTitle );
152 if (id == 2) // b-spline
153 GroupPoints->CheckButton1->show();
155 GroupPoints->CheckButton1->hide();
157 myPoints = new GEOM::ListOfGO();
158 myPoints->length( 0 );
160 myEditCurrentArgument->setText( "" );
161 qApp->processEvents();
163 resize( minimumSizeHint() );
164 SelectionIntoArgument();
168 //=================================================================================
169 // function : SetEditCurrentArgument()
171 //=================================================================================
172 void BasicGUI_CurveDlg::SetEditCurrentArgument()
174 if ( sender() == GroupPoints->PushButton1 )
175 myEditCurrentArgument = GroupPoints->LineEdit1;
176 myEditCurrentArgument->setFocus();
177 SelectionIntoArgument();
181 //=================================================================================
182 // function : LineEditReturnPressed()
184 //=================================================================================
185 void BasicGUI_CurveDlg::LineEditReturnPressed()
187 if ( sender() == GroupPoints->LineEdit1 )
189 myEditCurrentArgument = GroupPoints->LineEdit1;
190 GEOMBase_Skeleton::LineEditReturnPressed();
194 //=================================================================================
195 // function : CheckButtonToggled()
197 //=================================================================================
198 void BasicGUI_CurveDlg::CheckButtonToggled()
203 //=================================================================================
204 // function : ClickOnOk()
206 //=================================================================================
207 void BasicGUI_CurveDlg::ClickOnOk()
209 if ( ClickOnApply() )
213 //=================================================================================
214 // function : ClickOnApply()
216 //=================================================================================
217 bool BasicGUI_CurveDlg::ClickOnApply()
223 globalSelection(); // close local contexts, if any
224 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
228 //=================================================================================
229 /*! function : isPointInList()
230 * purpose : Check is point (theObject) in the list \a thePoints.
232 * \retval -1, if point not in list, else 1 in list
234 //=================================================================================
235 static int isPointInList( std::list<GEOM::GEOM_Object_var>& thePoints,
236 GEOM::GEOM_Object_var& theObject )
238 int len = thePoints.size();
244 for ( std::list<GEOM::GEOM_Object_var>::iterator i = thePoints.begin(); i != thePoints.end(); i++ ) {
245 if ( std::string( (*i)->GetEntry() ) == std::string( theObject->GetEntry() ) ) {
252 //=================================================================================
253 /*! function : removeUnnecessaryPnt()
254 * purpose : Remove unnecessary points from list \a theOldPoints
256 * \li \a theOldPoints - ordered sequence with unnecessary point
257 * \li \a theNewPoints - not ordered sequence with necessary points
259 //=================================================================================
260 static void removeUnnecessaryPnt( std::list<GEOM::GEOM_Object_var>& theOldPoints,
261 GEOM::ListOfGO_var& theNewPoints )
263 std::list<GEOM::GEOM_Object_var> objs_to_remove;
264 for ( std::list<GEOM::GEOM_Object_var>::iterator i = theOldPoints.begin(); i != theOldPoints.end(); i++ ) {
266 for ( int j = 0; j < theNewPoints->length() && !found ; j++ ) {
267 if ( std::string( (*i)->GetEntry() ) == std::string( theNewPoints[j]->GetEntry() ) ) {
272 objs_to_remove.push_back( *i );
273 //cout << "removed: " << (*i)->GetEntry() << endl;
276 for ( std::list<GEOM::GEOM_Object_var>::iterator i = objs_to_remove.begin(); i != objs_to_remove.end(); i++ ) {
277 theOldPoints.remove( *i );
281 //=================================================================================
282 // function : SelectionIntoArgument()
283 // purpose : Called when selection as changed or other case
284 //=================================================================================
285 void BasicGUI_CurveDlg::SelectionIntoArgument()
287 myEditCurrentArgument->setText( "" );
289 Standard_Boolean aRes = Standard_False;
291 SalomeApp_Application* app = myGeomGUI->getApp();
292 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>(app->activeStudy());
293 _PTR(Study) aDStudy = appStudy->studyDS();
294 GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations(getStudyId());
298 TColStd_IndexedMapOfInteger aMapIndexes;
299 GEOM::GEOM_Object_var anObject;
300 std::list<GEOM::GEOM_Object_var> aList;
301 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
302 SALOME_ListIO selected;
303 aSelMgr->selectedObjects(selected, QString::null, false);
305 int IOC = selected.Extent();
306 // bool is_append = myPoints->length() < IOC; // if true - add point, else remove
307 // myPoints->length( IOC ); // this length may be greater than number of objects,
308 // that will actually be put into myPoints
310 for (SALOME_ListIteratorOfListIO anIt (selected); anIt.More(); anIt.Next()) {
311 GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(anIt.Value(), aRes);
312 if (!CORBA::is_nil(aSelectedObject) && aRes) {
313 if (GEOMBase::GetShape(aSelectedObject, aShape, TopAbs_SHAPE) && !aShape.IsNull()) {
314 aSelMgr->GetIndexes(anIt.Value(), aMapIndexes);
316 if (aMapIndexes.Extent() > 0) {
317 for (int ii = 1; ii <= aMapIndexes.Extent(); ii++) {
318 anIndex = aMapIndexes(ii);
319 QString aName = GEOMBase::GetName( aSelectedObject );
320 aName = aName + ":vertex_" + QString::number( anIndex );
321 anObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
322 //Find Object in study
323 _PTR(SObject) obj ( aDStudy->FindObjectID( anIt.Value()->getEntry() ) );
324 bool inStudy = false;
325 _PTR(ChildIterator) iit( aDStudy->NewChildIterator( obj ) );
326 for (; iit->More() && !inStudy; iit->Next()) {
327 _PTR(SObject) child( iit->Value() );
328 QString aChildName = child->GetName().c_str();
329 if ( aChildName == aName ) {
331 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( iit->Value() );
332 anObject = GEOM::GEOM_Object::_narrow( corbaObj );
337 GeometryGUI::GetGeomGen()->AddInStudy( GeometryGUI::ClientStudyToStudy( aDStudy ),
338 anObject, aName.toLatin1().data(), aSelectedObject );
340 int pos = isPointInList( myOrderedSel, anObject );
342 myOrderedSel.push_back( anObject );
345 aList.push_back(anObject);
348 else { // aMap.Extent() == 0
349 if ( aShape.ShapeType() == TopAbs_VERTEX ) {
350 int pos = isPointInList( myOrderedSel, aSelectedObject );
352 myOrderedSel.push_back( aSelectedObject );
353 aList.push_back( aSelectedObject );
360 myPoints->length( aList.size() );
363 for ( std::list<GEOM::GEOM_Object_var>::iterator j = aList.begin(); j != aList.end(); j++ )
367 myOrderedSel.clear();
369 removeUnnecessaryPnt( myOrderedSel, myPoints );
371 // if ( myOrderedSel.size() == myPoints->length() ) {
372 myPoints->length( myOrderedSel.size() );
374 for ( std::list<GEOM::GEOM_Object_var>::iterator j = myOrderedSel.begin(); j != myOrderedSel.end(); j++ )
378 if ( myPoints->length() > 0 )
379 GroupPoints->LineEdit1->setText( QString::number( myPoints->length() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
385 //=================================================================================
386 // function : ActivateThisDialog()
388 //=================================================================================
389 void BasicGUI_CurveDlg::ActivateThisDialog()
391 GEOMBase_Skeleton::ActivateThisDialog();
392 connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
393 this, SLOT( SelectionIntoArgument() ) );
395 globalSelection(); // close local contexts, if any
396 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
398 ConstructorsClicked( getConstructorId() );
401 //=================================================================================
402 // function : DeactivateActiveDialog()
403 // purpose : public slot to deactivate if active
404 //=================================================================================
405 void BasicGUI_CurveDlg::DeactivateActiveDialog()
407 // myGeomGUI->SetState( -1 );
408 GEOMBase_Skeleton::DeactivateActiveDialog();
411 //=================================================================================
412 // function : enterEvent()
414 //=================================================================================
415 void BasicGUI_CurveDlg::enterEvent( QEvent* )
417 if ( !mainFrame()->GroupConstructors->isEnabled() )
418 ActivateThisDialog();
421 //=================================================================================
422 // function : createOperation
424 //=================================================================================
425 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
427 return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
430 //=================================================================================
431 // function : isValid
433 //=================================================================================
434 bool BasicGUI_CurveDlg::isValid( QString& msg )
436 return myPoints->length() > 1;
439 //=================================================================================
440 // function : execute
442 //=================================================================================
443 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
447 GEOM::GEOM_Object_var anObj;
449 GEOM::GEOM_ICurvesOperations_var anOper = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() );
451 switch ( getConstructorId() ) {
453 anObj = anOper->MakePolyline( myPoints );
457 anObj = anOper->MakeSplineBezier( myPoints );
461 anObj = anOper->MakeSplineInterpolation( myPoints, GroupPoints->CheckButton1->isChecked() );
466 if ( !anObj->_is_nil() )
467 objects.push_back( anObj._retn() );