Salome HOME
Merge from OCC_development_generic_2006
[modules/geom.git] / src / GroupGUI / GroupGUI_GroupDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GroupGUI_GroupDlg.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GroupGUI_GroupDlg.h"
30
31 #include "SUIT_Desktop.h"
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_Study.h"
35 #include "LightApp_SelectionMgr.h"
36
37 #include "GEOMBase.h"
38
39 #include "GEOMImpl_Types.hxx"
40
41 #include <qlabel.h>
42 #include <qlistbox.h>
43 #include <qlineedit.h>
44 #include <qmap.h>
45
46 #include <TColStd_IndexedMapOfInteger.hxx>
47 #include <TColStd_MapOfInteger.hxx>
48
49
50 GroupGUI_GroupDlg::GroupGUI_GroupDlg(Mode mode,
51                                      QWidget* parent,
52                                      const char* name,
53                                      bool modal,
54                                      WFlags fl)
55   :GEOMBase_Skeleton( parent, "GroupGUI_GroupDlg", false,
56                       WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu),
57    myMode( mode ),
58    myBusy( false )
59 {
60   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
61
62   QPixmap image0     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_VERTEX")));
63   QPixmap image1     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_EDGE")));
64   QPixmap image2     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_FACE")));
65   QPixmap image3     (resMgr->loadPixmap("GEOM", tr("ICON_OBJBROWSER_SOLID")));
66   QPixmap iconSelect (resMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
67
68   setCaption( myMode == CreateGroup ? tr( "CREATE_GROUP_TITLE" ) : tr( "EDIT_GROUP_TITLE" ) );
69
70   // Shape type button group
71   GroupConstructors->setEnabled( myMode == CreateGroup );
72   GroupConstructors->setTitle( tr( "SHAPE_TYPE" ) );
73   RadioButton1->setPixmap( image0 );
74   RadioButton2->setPixmap( image1 );
75   RadioButton3->setPixmap( image2 );
76   RadioButton4->setPixmap( image3 );
77   RadioButton4->show();
78
79   // Group name
80   GroupBoxName->setTitle( tr( "GROUP_NAME" ) );
81
82   // Main shape and sub-shapes
83   GroupMedium = new QGroupBox( 1, Qt::Vertical, tr( "MAIN_SUB_SHAPES" ), this );
84   GroupMedium->setInsideMargin( 10 );
85   Layout1->addWidget( GroupMedium, 2, 0 );
86
87   QWidget* aFrame = new QWidget( GroupMedium );
88   QGridLayout* aMedLayout = new QGridLayout( aFrame, 4, 4, 0, 6 );
89
90   QLabel* aMainLabel = new QLabel( tr( "MAIN_SHAPE" ), aFrame );
91
92   mySelBtn = new QPushButton( aFrame );
93   mySelBtn->setPixmap( iconSelect );
94   mySelBtn->setEnabled( myMode == CreateGroup );
95
96   myMainName = new QLineEdit( aFrame );
97   myMainName->setReadOnly( true );
98   myMainName->setEnabled( myMode == CreateGroup );
99
100   mySelSubBtn = new QPushButton( tr( "SELECT_SUB_SHAPES" ), aFrame );
101   mySelAllBtn = new QPushButton( tr( "SELECT_ALL" ), aFrame );
102   myAddBtn    = new QPushButton( tr( "ADD" ), aFrame );
103   myRemBtn    = new QPushButton( tr( "REMOVE" ), aFrame );
104   myIdList    = new QListBox( aFrame );
105
106   myIdList->setSelectionMode( QListBox::Extended );
107   myIdList->setRowMode( QListBox::FitToWidth );
108
109   aMedLayout->addWidget( aMainLabel, 0, 0 );
110   aMedLayout->addWidget( mySelBtn, 0, 1 );
111   aMedLayout->addMultiCellWidget( myMainName, 0, 0, 2, 3 );
112   aMedLayout->addMultiCellWidget( mySelSubBtn, 1, 1, 0, 2 );
113   aMedLayout->addWidget( mySelAllBtn, 1, 3 );
114   aMedLayout->addMultiCellWidget( myIdList, 2, 3, 0, 2 );
115   aMedLayout->addWidget( myAddBtn, 2, 3 );
116   aMedLayout->addWidget( myRemBtn, 3, 3 );
117
118   Init();
119 }
120
121 GroupGUI_GroupDlg::~GroupGUI_GroupDlg()
122 {
123 }
124
125
126 //=================================================================================
127 // function : Init()
128 // purpose  :
129 //=================================================================================
130 void GroupGUI_GroupDlg::Init()
131 {
132   // san -- TODO: clear selected sub-shapes...
133
134   if ( myMode == CreateGroup ) {
135     initName( tr( "GROUP_PREFIX" ) );
136
137     // Get ready for main shape selection
138     myEditCurrentArgument = myMainName;
139
140     connect( GroupConstructors, SIGNAL( clicked( int ) ), this, SLOT( ConstructorsClicked( int ) ) );
141     connect( mySelBtn,          SIGNAL( clicked() ),      this, SLOT( SetEditCurrentArgument() ) );
142   }
143   else if ( myMode == EditGroup && IObjectCount() ) {
144     Standard_Boolean aResult = Standard_False;
145     GEOM::GEOM_Object_var anObj =
146       GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
147
148     if ( aResult && !CORBA::is_nil( anObj ) && anObj->GetType() == GEOM_GROUP ) {
149       myGroup = anObj;
150
151       ResultName->setText( GEOMBase::GetName( myGroup ) );
152
153       GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
154       myMainObj = anOp->GetMainShape( myGroup );
155       if ( !CORBA::is_nil( myMainObj ) )
156         myMainName->setText( GEOMBase::GetName( myMainObj ) );
157
158       setShapeType( (TopAbs_ShapeEnum)anOp->GetType( myGroup ) );
159
160       GEOM::ListOfLong_var aCurrList = anOp->GetObjects( myGroup );
161       QListBoxItem* anItem;
162       for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
163         anItem = new QListBoxText( QString( "%1" ).arg(aCurrList[i] ) );
164         myIdList->insertItem( anItem );
165       }
166
167       myEditCurrentArgument = 0;
168     }
169   }
170
171   LightApp_SelectionMgr* aSelMgr =
172     ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
173
174   connect( aSelMgr,     SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
175
176   connect( buttonOk   , SIGNAL( clicked() ), this, SLOT( ClickOnOk()    ) );
177   connect( buttonApply, SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
178
179   connect( mySelSubBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
180   connect( mySelAllBtn, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
181   connect( myAddBtn,    SIGNAL( clicked() ), this, SLOT( add() ) );
182   connect( myRemBtn,    SIGNAL( clicked() ), this, SLOT( remove() ) );
183   connect( myIdList,    SIGNAL( selectionChanged() ), this, SLOT( selectionChanged() ) );
184
185   activateSelection();
186 }
187
188 //=================================================================================
189 // function : enterEvent()
190 // purpose  :
191 //=================================================================================
192 void GroupGUI_GroupDlg::enterEvent( QEvent* e )
193 {
194   if ( !buttonCancel->isEnabled() )
195     this->ActivateThisDialog();
196 }
197
198 //=================================================================================
199 // function : ClickOnOk()
200 // purpose  :
201 //=================================================================================
202 void GroupGUI_GroupDlg::ClickOnOk()
203 {
204   if ( ClickOnApply() )
205     ClickOnCancel();
206 }
207
208
209 //=================================================================================
210 // function : ClickOnApply()
211 // purpose  :
212 //=================================================================================
213 bool GroupGUI_GroupDlg::ClickOnApply()
214 {
215   if ( !onAccept( myMode == CreateGroup, true ) )
216     return false;
217
218   if ( myMode == CreateGroup )
219     initName();
220   return true;
221 }
222
223
224 //=================================================================================
225 // function : ActivateThisDialog()
226 // purpose  :
227 //=================================================================================
228 void GroupGUI_GroupDlg::ActivateThisDialog()
229 {
230   GEOMBase_Skeleton::ActivateThisDialog();
231
232   connect( ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(),
233            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
234
235   activateSelection();
236 }
237
238 //=================================================================================
239 // function : LineEditReturnPressed()
240 // purpose  :
241 //=================================================================================
242 void GroupGUI_GroupDlg::LineEditReturnPressed()
243 {
244   QLineEdit* send = ( QLineEdit* )sender();
245
246   if ( send == myMainName && !myEditCurrentArgument ) {
247     myEditCurrentArgument = myMainName;
248     activateSelection();
249   }
250   else
251     GEOMBase_Skeleton::LineEditReturnPressed();
252
253   updateState();
254 }
255
256
257 //=================================================================================
258 // function : SetEditCurrentArgument()
259 // purpose  :
260 //=================================================================================
261 void GroupGUI_GroupDlg::SetEditCurrentArgument()
262 {
263   QPushButton* send = (QPushButton*)sender();
264
265   if ( send == mySelBtn )
266     myEditCurrentArgument = myMainName;
267   else if ( send == mySelSubBtn || send == mySelAllBtn )
268     myEditCurrentArgument = 0;
269
270   activateSelection();
271
272   if ( send == mySelAllBtn )
273     selectAllSubShapes();
274   else
275     updateState();
276 }
277
278
279 //=================================================================================
280 // function : SelectionIntoArgument()
281 // purpose  : Called when selection has changed
282 //=================================================================================
283 void GroupGUI_GroupDlg::SelectionIntoArgument()
284 {
285   if ( myEditCurrentArgument ) {  // Selection of a main shape is active
286     myEditCurrentArgument->setText( "" );
287     myIdList->clear();
288
289     if ( IObjectCount() == 1 ) {
290       Standard_Boolean aResult = Standard_False;
291       GEOM::GEOM_Object_var anObj =
292         GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
293
294       if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
295         myMainObj = anObj;
296         myEditCurrentArgument->setText( GEOMBase::GetName( anObj ) );
297         updateState();
298         return;
299       }
300     }
301
302     myMainObj = GEOM::GEOM_Object::_nil();
303   }
304   else { // an attempt to synchronize list box selection with 3d viewer
305     if ( myBusy )
306       return;
307
308     bool isBlocked = myIdList->signalsBlocked();
309     myIdList->blockSignals( true );
310     myIdList->clearSelection();
311
312     TColStd_IndexedMapOfInteger aMapIndex;
313
314     if ( IObjectCount() == 1 ) {
315       Standard_Boolean aResult = Standard_False;
316       GEOM::GEOM_Object_var anObj =
317         GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
318
319       if ( aResult && !anObj->_is_nil() )
320         ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->
321           selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
322     }
323
324     // try to find out and process the object browser selection
325     if ( !aMapIndex.Extent() ) {
326       GEOM::ListOfGO anObjects;
327       GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), anObjects);
328       GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
329       for (int i = 0; i < anObjects.length(); i++) {
330         TopoDS_Shape aShape;
331         if ( GEOMBase::GetShape(anObjects[i], aShape, getShapeType()) ) {
332           CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, anObjects[i] );
333           if ( anIndex >= 0 )
334             aMapIndex.Add( anIndex );
335         }
336       }
337     }
338
339     if (aMapIndex.Extent() >= 1) {
340       QMap<int, int> aMap;
341       for ( int i = 0, n = myIdList->count(); i < n; i++ )
342         aMap.insert( myIdList->item( i )->text().toInt(), i );
343
344       for ( int ii = 1, nn = aMapIndex.Extent(); ii <= nn; ii++ ) {
345         if ( aMap.contains( aMapIndex( ii ) ) )
346           myIdList->setSelected( aMap[aMapIndex( ii )], true );
347       }
348     }
349     myIdList->blockSignals( isBlocked );
350   }
351
352   updateState();
353 }
354
355 //=================================================================================
356 // function : ConstructorsClicked()
357 // purpose  : Radio button management
358 //=================================================================================
359 void GroupGUI_GroupDlg::ConstructorsClicked( int constructorId )
360 {
361   myIdList->clear();
362   activateSelection();
363   updateState();
364 }
365
366 //=================================================================================
367 // function : selectAllSubShapes
368 // purpose  : 
369 //=================================================================================
370 void GroupGUI_GroupDlg::selectAllSubShapes()
371 {
372   if ( CORBA::is_nil( myMainObj ) )
373     return;
374
375   GEOM::GEOM_IShapesOperations_var aShOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
376   GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
377
378   GEOM::ListOfGO_var aSubShapes = aShOp->MakeExplode( myMainObj, getShapeType(), false );
379   if ( !aShOp->IsDone() )
380     return;
381
382   bool isBlocked = myIdList->signalsBlocked();
383   myIdList->blockSignals( true );
384   myIdList->clear();
385
386   QListBoxItem* anItem;
387   for ( int i = 0, n = aSubShapes->length(); i < n; i++ ) {
388     CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, aSubShapes[i] );
389     if ( anIndex < 0 )
390       continue;
391
392     anItem = new QListBoxText( QString( "%1" ).arg( anIndex ) );
393     myIdList->insertItem( anItem );
394     myIdList->setSelected( anItem, true );
395   }
396
397   myIdList->blockSignals( isBlocked );
398   highlightSubShapes();
399   updateState();
400 }
401
402 //=================================================================================
403 // function : add
404 // purpose  : 
405 //=================================================================================
406 void GroupGUI_GroupDlg::add()
407 {
408   TColStd_MapOfInteger aMap;
409   for ( int i = 0, n = myIdList->count(); i < n; i++ )
410     aMap.Add( myIdList->item( i )->text().toInt() );
411
412   TColStd_IndexedMapOfInteger aMapIndex;
413
414   if ( IObjectCount() == 1 ) {
415     Standard_Boolean aResult = Standard_False;
416     GEOM::GEOM_Object_var anObj =
417       GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
418
419     if ( aResult && !anObj->_is_nil() )
420       ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->
421         selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
422   }
423
424   // try to find out and process the object browser selection
425   if ( !aMapIndex.Extent() ) {
426     GEOM::ListOfGO anObjects;
427     GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), anObjects);
428     GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
429     for (int i = 0; i < anObjects.length(); i++) {
430       TopoDS_Shape aShape;
431       if ( GEOMBase::GetShape(anObjects[i], aShape, getShapeType()) ) {
432         CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, anObjects[i] );
433         if ( anIndex >= 0 )
434           aMapIndex.Add( anIndex );
435       }
436     }
437   }
438
439   if ( aMapIndex.Extent() >= 1 ) {
440     QListBoxItem* anItem;
441     bool isBlocked = myIdList->signalsBlocked();
442     myIdList->blockSignals( true );
443
444     for ( int i = 1, n = aMapIndex.Extent(); i <= n; i++ ) {
445       if ( aMap.Contains( aMapIndex( i ) ) )
446         continue;
447
448       anItem = new QListBoxText( QString( "%1" ).arg( aMapIndex( i ) ) );
449       myIdList->insertItem( anItem );
450       myIdList->setSelected( anItem, true );
451     }
452
453     myIdList->blockSignals( isBlocked );
454   }
455
456   updateState();
457 }
458
459 //=================================================================================
460 // function : remove
461 // purpose  : 
462 //=================================================================================
463 void GroupGUI_GroupDlg::remove()
464 {
465   for ( int i = myIdList->count() - 1; i >= 0; i-- ) {
466     if ( myIdList->isSelected( i ) )
467       myIdList->removeItem( i );
468   }
469   updateState();
470 }
471
472
473 //=================================================================================
474 // function : getConstructorId()
475 // purpose  :
476 //=================================================================================
477 int GroupGUI_GroupDlg::getConstructorId() const
478 {
479   return GroupConstructors->id( GroupConstructors->selected() );
480 }
481
482 //=================================================================================
483 // function : getShapeType()
484 // purpose  :
485 //=================================================================================
486 TopAbs_ShapeEnum GroupGUI_GroupDlg::getShapeType() const
487 {
488   switch ( getConstructorId() ) {
489   case 0:  return TopAbs_VERTEX;
490   case 1:  return TopAbs_EDGE;
491   case 2:  return TopAbs_FACE;
492   case 3:  return TopAbs_SOLID;
493   default: return TopAbs_SHAPE;
494   }
495 }
496
497 //=================================================================================
498 // function : setShapeType()
499 // purpose  :
500 //=================================================================================
501 void GroupGUI_GroupDlg::setShapeType( const TopAbs_ShapeEnum theType )
502 {
503   int anId = 0;
504   switch ( theType ) {
505   case TopAbs_VERTEX: anId = 0; break;
506   case TopAbs_EDGE:   anId = 1; break;
507   case TopAbs_FACE:   anId = 2; break;
508   case TopAbs_SOLID:  anId = 3; break;
509   }
510   GroupConstructors->setButton( anId );
511 }
512
513
514 //=================================================================================
515 // function : activateSelection
516 // purpose  : Activate selection in accordance with myEditCurrentArgument
517 //=================================================================================
518 void GroupGUI_GroupDlg::activateSelection()
519 {
520   globalSelection( GEOM_ALLSHAPES );
521
522   if ( !myMainObj->_is_nil() && !myEditCurrentArgument ) {
523     localSelection( myMainObj, getShapeType() );
524   }
525
526   SelectionIntoArgument();
527 }
528
529 //=================================================================================
530 // function : updateState
531 // purpose  : 
532 //=================================================================================
533 void GroupGUI_GroupDlg::updateState()
534 {
535   bool isAdd = false;
536
537   TColStd_IndexedMapOfInteger aMapIndex;
538
539   if ( IObjectCount() == 1 ) {
540     Standard_Boolean aResult = Standard_False;
541     GEOM::GEOM_Object_var anObj =
542       GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
543
544     if ( aResult && !anObj->_is_nil() )
545       ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->
546         selectionMgr()->GetIndexes( firstIObject(), aMapIndex );
547   }
548
549   // try to find out and process the object browser selection
550   if ( !aMapIndex.Extent() && !CORBA::is_nil( myMainObj ) ) {
551     isAdd = true;
552     GEOM::ListOfGO anObjects;
553     GEOMBase::ConvertListOfIOInListOfGO(selectedIO(), anObjects);
554     GEOM::GEOM_ILocalOperations_var aLocOp = getGeomEngine()->GetILocalOperations( getStudyId() );
555     for (int i = 0; i < anObjects.length(); i++) {
556       TopoDS_Shape aShape;
557       if ( GEOMBase::GetShape(anObjects[i], aShape, getShapeType()) ) {
558         CORBA::Long anIndex = aLocOp->GetSubShapeIndex( myMainObj, anObjects[i] );
559         if ( anIndex >= 0 )
560           aMapIndex.Add( anIndex );
561         else
562           isAdd = false;
563       }
564       else
565         isAdd = false;
566
567       if ( !isAdd ) {
568         aMapIndex.Clear();
569         break;
570       }
571     }
572   }
573
574   isAdd = aMapIndex.Extent() > 0;
575
576   myAddBtn->setEnabled( !myEditCurrentArgument && !CORBA::is_nil( myMainObj ) && isAdd );
577   bool hasSel = false;
578   for ( int ii = 0, nn = myIdList->count(); !hasSel && ii < nn; ii++ )
579     hasSel =  myIdList->isSelected( ii );
580   myRemBtn->setEnabled( hasSel );
581   mySelSubBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
582   mySelAllBtn->setEnabled( !CORBA::is_nil( myMainObj ) );
583 }
584
585 //=================================================================================
586 // function : selectionChanged
587 // purpose  :
588 //=================================================================================
589 void GroupGUI_GroupDlg::selectionChanged()
590 {
591   highlightSubShapes();
592 }
593
594 //=================================================================================
595 // function : highlightSubShapes
596 // purpose  :
597 //=================================================================================
598 void GroupGUI_GroupDlg::highlightSubShapes()
599 {
600   if ( CORBA::is_nil( myMainObj ) )
601     return;
602
603   Standard_Boolean isOk;
604   Handle(GEOM_AISShape) aSh =
605     GEOMBase::ConvertIORinGEOMAISShape( GEOMBase::GetIORFromObject( myMainObj ), isOk, true );
606   if ( !isOk || aSh.IsNull() )
607     return;
608
609   TColStd_MapOfInteger anIds;
610
611   myBusy = true;
612
613   for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ )
614     if ( myIdList->isSelected( ii ) )
615       anIds.Add( myIdList->item( ii )->text().toInt() );
616
617   LightApp_SelectionMgr* aSelMgr =
618     ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr();
619   aSelMgr->clearSelected();
620   aSelMgr->AddOrRemoveIndex( aSh->getIO(), anIds, false );
621
622   myBusy = false;
623
624   updateState();
625 }
626
627 //=================================================================================
628 // function : createOperation
629 // purpose  :
630 //=================================================================================
631 GEOM::GEOM_IOperations_ptr GroupGUI_GroupDlg::createOperation()
632 {
633   return getGeomEngine()->GetIGroupOperations( getStudyId() );
634 }
635
636 #define RETURN_WITH_MSG(a, b) \
637   if ( !(a) ) { \
638     theMessage += (b); \
639     return false; \
640   }
641
642 //=================================================================================
643 // function : isValid()
644 // purpose  : Verify validity of input data
645 //=================================================================================
646 bool GroupGUI_GroupDlg::isValid( QString& theMessage )
647 {
648   SalomeApp_Study* study = getStudy();
649   ASSERT(study);
650   RETURN_WITH_MSG  ( !study->studyDS()->GetProperties()->IsLocked(), tr( "GEOM_STUDY_LOCKED" ) )
651
652   if ( myMode == CreateGroup ) {
653     RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_MAIN_OBJ" ) )
654   }
655   else {
656     RETURN_WITH_MSG( !CORBA::is_nil( myMainObj ), tr( "NO_GROUP" ) )
657   }
658
659   const char* aName = getNewObjectName();
660   RETURN_WITH_MSG  ( aName && strlen( aName ), tr( "EMPTY_NAME" ) )
661
662   RETURN_WITH_MSG  ( myIdList->count(), tr( "EMPTY_LIST" ) )
663   return true;
664 }
665
666 //=================================================================================
667 // function : execute
668 // purpose  :
669 //=================================================================================
670 bool GroupGUI_GroupDlg::execute( ObjectList& objects )
671 {
672   GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
673
674   GEOM::GEOM_Object_var aGroup;
675   if ( myMode == CreateGroup )
676     aGroup = anOp->CreateGroup( myMainObj, getShapeType() );
677   else if ( myMode == EditGroup )
678     aGroup = myGroup;
679
680   if ( CORBA::is_nil( aGroup ) || ( myMode == CreateGroup && !anOp->IsDone() ) )
681     return false;
682
683   GEOM::ListOfLong_var aCurrList = anOp->GetObjects( aGroup );
684   if ( !anOp->IsDone()  )
685     return false;
686
687   for ( int i = 0, n = aCurrList->length(); i < n; i++ ) {
688     anOp->RemoveObject( aGroup, aCurrList[i] );
689     if ( !anOp->IsDone()  )
690       return false;
691   }
692
693   for ( int ii = 0, nn = myIdList->count(); ii < nn; ii++ ) {
694     anOp->AddObject( aGroup, myIdList->item( ii )->text().toInt() );
695     if ( !anOp->IsDone()  )
696       return false;
697   }
698
699   SalomeApp_Study* study = getStudy();
700   if ( study ) {
701     string IOR = GEOMBase::GetIORFromObject( aGroup );
702     if ( IOR != "" ) {
703       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
704       if ( SO ) {
705         _PTR(StudyBuilder) aBuilder (study->studyDS()->NewBuilder());
706         aBuilder->SetName( SO, getNewObjectName() );
707       }
708     }
709   }
710
711   objects.push_back( aGroup._retn() );
712
713   return true;
714 }
715
716 //================================================================
717 // Function : getFather
718 // Purpose  : Get father object for object to be added in study
719 //            ( called with addInStudy method )
720 //================================================================
721 GEOM::GEOM_Object_ptr GroupGUI_GroupDlg::getFather( GEOM::GEOM_Object_ptr theObj )
722 {
723   GEOM::GEOM_Object_var aFatherObj;
724   if ( theObj->GetType() == GEOM_GROUP ) {
725     GEOM::GEOM_IGroupOperations_var anOp = GEOM::GEOM_IGroupOperations::_narrow( getOperation() );
726     aFatherObj = anOp->GetMainShape( theObj );
727   }
728   return aFatherObj._retn();
729 }