1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE
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.
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.
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
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // File : BasicGUI_CurveDlg.cxx
22 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 #include "BasicGUI_CurveDlg.h"
28 #include <GeometryGUI.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_Session.h>
33 #include <SalomeApp_Application.h>
34 #include <SalomeApp_Study.h>
35 #include <LightApp_SelectionMgr.h>
37 #include <SALOME_ListIteratorOfListIO.hxx>
38 #include <SALOME_ListIO.hxx>
40 #include <TopoDS_Shape.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <GEOMImpl_Types.hxx>
48 //=================================================================================
49 // class : BasicGUI_CurveDlg()
50 // purpose : Constructs a BasicGUI_CurveDlg which is a child of 'parent', with the
51 // name 'name' and widget flags set to 'f'.
52 // The dialog will by default be modeless, unless you set 'modal' to
53 // TRUE to construct a modal dialog.
54 //=================================================================================
55 BasicGUI_CurveDlg::BasicGUI_CurveDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
56 bool modal, Qt::WindowFlags fl )
57 : GEOMBase_Skeleton( theGeometryGUI, parent, modal, fl )
59 QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_POLYLINE" ) ) );
60 QPixmap image2( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_SPLINE" ) ) );
61 QPixmap image3( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BEZIER" ) ) );
62 QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
64 setWindowTitle( tr( "GEOM_CURVE_TITLE" ) );
66 /***************************************************************/
67 mainFrame()->RadioButton1->setIcon( image0 );
68 mainFrame()->RadioButton2->setIcon( image3 );
69 mainFrame()->RadioButton3->setIcon( image2 );
71 GroupPoints = new DlgRef_1Sel( centralWidget() );
73 GroupPoints->GroupBox1->setTitle( tr( "GEOM_NODES" ) );
74 GroupPoints->TextLabel1->setText( tr( "GEOM_POINTS" ) );
75 GroupPoints->PushButton1->setIcon( image1 );
77 GroupPoints->LineEdit1->setReadOnly( true );
79 QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
80 layout->setMargin( 0 ); layout->setSpacing( 6 );
81 layout->addWidget( GroupPoints );
82 /***************************************************************/
84 setHelpFileName( "create_curve_page.html" );
90 //=================================================================================
91 // function : ~BasicGUI_CurveDlg()
92 // purpose : Destroys the object and frees any allocated resources
93 //=================================================================================
94 BasicGUI_CurveDlg::~BasicGUI_CurveDlg()
99 //=================================================================================
102 //=================================================================================
103 void BasicGUI_CurveDlg::Init()
106 myEditCurrentArgument = GroupPoints->LineEdit1;
108 myPoints = new GEOM::ListOfGO();
109 myPoints->length( 0 );
111 globalSelection(); // close local contexts, if any
112 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
114 /* signals and slots connections */
115 connect( myGeomGUI, SIGNAL( SignalDeactivateActiveDialog() ), this, SLOT( DeactivateActiveDialog( ) ) );
116 connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
118 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
119 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
121 connect( this, SIGNAL( constructorsClicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
123 connect( GroupPoints->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
124 connect( GroupPoints->LineEdit1, SIGNAL( returnPressed() ), this, SLOT( LineEditReturnPressed() ) );
126 connect( myGeomGUI->getApp()->selectionMgr(),
127 SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) ) ;
129 initName( tr( "GEOM_CURVE" ) );
130 ConstructorsClicked( 0 );
133 //=================================================================================
134 // function : ConstructorsClicked()
136 //=================================================================================
137 void BasicGUI_CurveDlg::ConstructorsClicked( int id )
139 QString aTitle = tr( id == 0 ? "GEOM_POLYLINE" : id == 1 ? "GEOM_BEZIER" : "GEOM_INTERPOL" );
140 mainFrame()->GroupConstructors->setTitle( aTitle );
142 myPoints = new GEOM::ListOfGO();
143 myPoints->length( 0 );
145 myEditCurrentArgument->setText( "" );
149 //=================================================================================
150 // function : SetEditCurrentArgument()
152 //=================================================================================
153 void BasicGUI_CurveDlg::SetEditCurrentArgument()
155 if ( sender() == GroupPoints->PushButton1 )
156 myEditCurrentArgument = GroupPoints->LineEdit1;
157 myEditCurrentArgument->setFocus();
158 SelectionIntoArgument();
162 //=================================================================================
163 // function : LineEditReturnPressed()
165 //=================================================================================
166 void BasicGUI_CurveDlg::LineEditReturnPressed()
168 if ( sender() == GroupPoints->LineEdit1 )
170 myEditCurrentArgument = GroupPoints->LineEdit1;
171 GEOMBase_Skeleton::LineEditReturnPressed();
175 //=================================================================================
176 // function : ClickOnOk()
178 //=================================================================================
179 void BasicGUI_CurveDlg::ClickOnOk()
181 if ( ClickOnApply() )
185 //=================================================================================
186 // function : ClickOnApply()
188 //=================================================================================
189 bool BasicGUI_CurveDlg::ClickOnApply()
195 globalSelection(); // close local contexts, if any
196 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
200 //=================================================================================
201 /*! function : isPointInList()
202 * purpose : Check is point (theObject) in the list \a thePoints.
204 * \retval -1, if point not in list, else 1 in list
206 //=================================================================================
207 static int isPointInList( std::list<GEOM::GEOM_Object_var>& thePoints,
208 GEOM::GEOM_Object_var& theObject )
210 int len = thePoints.size();
216 for ( std::list<GEOM::GEOM_Object_var>::iterator i = thePoints.begin(); i != thePoints.end(); i++ ) {
217 if ( std::string( (*i)->GetEntry() ) == std::string( theObject->GetEntry() ) ) {
224 //=================================================================================
225 /*! function : removeUnnecessaryPnt()
226 * purpose : Remove unnecessary points from list \a theOldPoints
228 * \li \a theOldPoints - ordered sequence with unnecessary point
229 * \li \a theNewPoints - not ordered sequence with necessary points
231 //=================================================================================
232 static void removeUnnecessaryPnt( std::list<GEOM::GEOM_Object_var>& theOldPoints,
233 GEOM::ListOfGO_var& theNewPoints )
235 std::list<GEOM::GEOM_Object_var> objs_to_remove;
236 for ( std::list<GEOM::GEOM_Object_var>::iterator i = theOldPoints.begin(); i != theOldPoints.end(); i++ ) {
238 for ( int j = 0; j < theNewPoints->length() && !found ; j++ ) {
239 if ( std::string( (*i)->GetEntry() ) == std::string( theNewPoints[j]->GetEntry() ) ) {
244 objs_to_remove.push_back( *i );
245 //cout << "removed: " << (*i)->GetEntry() << endl;
248 for ( std::list<GEOM::GEOM_Object_var>::iterator i = objs_to_remove.begin(); i != objs_to_remove.end(); i++ ) {
249 theOldPoints.remove( *i );
253 //=================================================================================
254 // function : SelectionIntoArgument()
255 // purpose : Called when selection as changed or other case
256 //=================================================================================
257 void BasicGUI_CurveDlg::SelectionIntoArgument()
259 myEditCurrentArgument->setText( "" );
261 Standard_Boolean aRes = Standard_False;
263 int IOC = IObjectCount();
264 // bool is_append = myPoints->length() < IOC; // if true - add point, else remove
265 // myPoints->length( IOC ); // this length may be greater than number of objects,
266 // that will actually be put into myPoints
268 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
269 SalomeApp_Application* app =
270 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
271 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
272 _PTR(Study) aDStudy = appStudy->studyDS();
273 GEOM::GEOM_IShapesOperations_var aShapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
277 TColStd_IndexedMapOfInteger aMapIndexes;
278 GEOM::GEOM_Object_var anObject;
279 std::list<GEOM::GEOM_Object_var> aList;
280 SALOME_ListIO selected;
281 aSelMgr->selectedObjects( selected, QString::null, false );
283 for ( SALOME_ListIteratorOfListIO anIt( selected ); anIt.More(); anIt.Next() ) {
284 GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( anIt.Value(), aRes );
285 if ( !CORBA::is_nil( aSelectedObject ) && aRes ) {
286 if ( GEOMBase::GetShape( aSelectedObject, aShape, TopAbs_SHAPE ) && !aShape.IsNull() ) {
287 aSelMgr->GetIndexes( anIt.Value(), aMapIndexes );
289 if ( aMapIndexes.Extent() > 0 ) {
290 for ( int ii = 1; ii <= aMapIndexes.Extent(); ii++ ) {
291 anIndex = aMapIndexes(ii);
292 QString aName = GEOMBase::GetName( aSelectedObject );
293 aName = aName + ":vertex_" + QString::number( anIndex );
294 anObject = aShapesOp->GetSubShape( aSelectedObject, anIndex );
295 //Find Object in study
296 _PTR(SObject) obj ( aDStudy->FindObjectID( anIt.Value()->getEntry() ) );
297 bool inStudy = false;
298 _PTR(ChildIterator) iit( aDStudy->NewChildIterator( obj ) );
299 for (; iit->More() && !inStudy; iit->Next()) {
300 _PTR(SObject) child( iit->Value() );
301 QString aChildName = child->GetName().c_str();
302 if ( aChildName == aName ) {
304 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( iit->Value() );
305 anObject = GEOM::GEOM_Object::_narrow( corbaObj );
310 GeometryGUI::GetGeomGen()->AddInStudy( GeometryGUI::ClientStudyToStudy( aDStudy ),
311 anObject, aName.toLatin1().data(), aSelectedObject );
313 int pos = isPointInList( myOrderedSel, anObject );
315 myOrderedSel.push_back( anObject );
318 aList.push_back(anObject);
321 else { // aMap.Extent() == 0
322 int pos = isPointInList( myOrderedSel, aSelectedObject );
324 myOrderedSel.push_back( aSelectedObject );
325 aList.push_back( aSelectedObject );
331 myPoints->length( aList.size() );
334 for ( std::list<GEOM::GEOM_Object_var>::iterator j = aList.begin(); j != aList.end(); j++ )
338 myOrderedSel.clear();
340 removeUnnecessaryPnt( myOrderedSel, myPoints );
342 // if ( myOrderedSel.size() == myPoints->length() ) {
343 myPoints->length( myOrderedSel.size() );
345 for ( std::list<GEOM::GEOM_Object_var>::iterator j = myOrderedSel.begin(); j != myOrderedSel.end(); j++ )
349 if ( myPoints->length() > 0 )
350 GroupPoints->LineEdit1->setText( QString::number( myPoints->length() ) + "_" + tr( "GEOM_POINT" ) + tr( "_S_" ) );
356 //=================================================================================
357 // function : ActivateThisDialog()
359 //=================================================================================
360 void BasicGUI_CurveDlg::ActivateThisDialog()
362 GEOMBase_Skeleton::ActivateThisDialog();
363 connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
364 this, SLOT( SelectionIntoArgument() ) );
366 globalSelection(); // close local contexts, if any
367 localSelection( GEOM::GEOM_Object::_nil(), TopAbs_VERTEX );
369 ConstructorsClicked( getConstructorId() );
372 //=================================================================================
373 // function : DeactivateActiveDialog()
374 // purpose : public slot to deactivate if active
375 //=================================================================================
376 void BasicGUI_CurveDlg::DeactivateActiveDialog()
378 // myGeomGUI->SetState( -1 );
379 GEOMBase_Skeleton::DeactivateActiveDialog();
382 //=================================================================================
383 // function : enterEvent()
385 //=================================================================================
386 void BasicGUI_CurveDlg::enterEvent( QEvent* )
388 if ( !mainFrame()->GroupConstructors->isEnabled() )
389 ActivateThisDialog();
392 //=================================================================================
393 // function : createOperation
395 //=================================================================================
396 GEOM::GEOM_IOperations_ptr BasicGUI_CurveDlg::createOperation()
398 return myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
401 //=================================================================================
402 // function : isValid
404 //=================================================================================
405 bool BasicGUI_CurveDlg::isValid( QString& msg )
407 return myPoints->length() > 1;
410 //=================================================================================
411 // function : execute
413 //=================================================================================
414 bool BasicGUI_CurveDlg::execute( ObjectList& objects )
418 GEOM::GEOM_Object_var anObj;
420 switch ( getConstructorId() ) {
422 anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakePolyline( myPoints );
426 anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineBezier( myPoints );
430 anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeSplineInterpolation( myPoints );
435 if ( !anObj->_is_nil() )
436 objects.push_back( anObj._retn() );