Salome HOME
Updated copyright comment
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_ReduceStudyDlg.cxx
1 // Copyright (C) 2014-2024  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "GEOMToolsGUI_ReduceStudyDlg.h"
22
23 // GEOM includes
24 #include <GEOMBase.h>
25
26 // GUI includes
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_MessageBox.h>
30
31 #include <LightApp_SelectionMgr.h>
32
33 #include <SalomeApp_Application.h>
34 #include <SalomeApp_Study.h>
35 #include <SALOME_ListIO.hxx>
36
37 // Qt includes
38 #include <QGridLayout>
39 #include <QPushButton>
40 #include <QRadioButton>
41 #include <QHeaderView>
42 #include <QMessageBox>
43
44 GEOMToolsGUI_ReduceStudyDlg::GEOMToolsGUI_ReduceStudyDlg( QWidget* parent )
45 :QDialog( parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint )
46 {
47   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
48   myVisible = QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) );
49   myInvisible = QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) );
50
51   myApp = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
52   if ( !myApp ) return;
53
54   myDisplayer = GEOM_Displayer();
55
56   setWindowTitle( tr( "GEOM_REDUCE_STUDY_TITLE" ) );
57   setAttribute(Qt::WA_DeleteOnClose);
58
59   QGridLayout* topLayout = new QGridLayout( this );
60   topLayout->setMargin( 11 ); topLayout->setSpacing( 6 );
61
62   /**********************   Objects to be kept    **********************/
63   QGroupBox* groupKeptObjects = new QGroupBox( tr( "GEOM_REDUCE_STUDY_KEPT_OBJECTS" ) );
64   QGridLayout* layoutKeptObjects = new QGridLayout( groupKeptObjects );
65   createTreeWidget( myTreeKeptObjects = new QTreeWidget() );
66   layoutKeptObjects->addWidget( myTreeKeptObjects );
67
68   /**********************  Objects to be removed  **********************/
69   QGroupBox* groupRemoveObjects = new QGroupBox( tr( "GEOM_REDUCE_STUDY_REMOVE_OBJECTS" ) );
70   QGridLayout* layoutRemoveObjects = new QGridLayout( groupRemoveObjects );
71   createTreeWidget( myTreeRemoveObjects = new QTreeWidget() );
72   layoutRemoveObjects->addWidget( myTreeRemoveObjects );
73
74   /**********************         Options         **********************/
75   QGroupBox* groupOptions = new QGroupBox( tr( "GEOM_REDUCE_STUDY_OPTIONS" ) );
76   QVBoxLayout* layoutOptions = new QVBoxLayout( groupOptions );
77
78   // Intermediate objects
79   QGroupBox* groupIntermediates = createButtonGroup( myGroupIntermediates = new QButtonGroup() );
80   groupIntermediates->setTitle( tr( "GEOM_REDUCE_STUDY_INTERMEDIATES" ) );
81
82   // Sub-objects
83   QGroupBox* groupSubObjects = createButtonGroup( myGroupSubObjects = new QButtonGroup() );
84   groupSubObjects->setTitle( tr( "GEOM_REDUCE_STUDY_SUB_OBJECTS" ) );
85
86   // Others
87   myCBRemoveEmptyFolder = new QCheckBox( tr( "GEOM_REDUCE_STUDY_REMOVE_EMPTY_FOLDER" ) );
88   myCBRemoveEmptyFolder->setChecked( true );
89   myCBSoftRemoval = new QCheckBox( tr( "GEOM_REDUCE_STUDY_SOFT_REMOVAL" ) );
90
91   layoutOptions->addWidget( groupIntermediates );
92   layoutOptions->addWidget( groupSubObjects );
93   layoutOptions->addWidget( myCBRemoveEmptyFolder );
94   layoutOptions->addWidget( myCBSoftRemoval );
95
96   /**********************         Buttons         **********************/
97   QGroupBox* groupButtons = new QGroupBox();
98   QHBoxLayout* layoutButtons = new QHBoxLayout( groupButtons );
99
100   QPushButton* buttonOk = new QPushButton( tr( "GEOM_BUT_OK" ) );
101   QPushButton* buttonCancel = new QPushButton( tr( "GEOM_BUT_CANCEL" ) );
102   QPushButton* buttonHelp = new QPushButton( tr( "GEOM_BUT_HELP" ) );
103
104   layoutButtons->addWidget( buttonOk );
105   layoutButtons->addStretch();
106   layoutButtons->addWidget( buttonCancel );
107   layoutButtons->addWidget( buttonHelp );
108
109   topLayout->addWidget( groupKeptObjects, 0, 0 );
110   topLayout->addWidget( groupRemoveObjects, 0, 1 );
111   topLayout->addWidget( groupOptions, 1, 0, 1, 2 );
112   topLayout->addWidget( groupButtons, 2, 0, 1, 2 );
113
114   // Signals and slots connections
115
116   connect( myTreeKeptObjects, SIGNAL( itemClicked( QTreeWidgetItem*, int) ), this, SLOT( onItemClicked( QTreeWidgetItem*, int ) ) );
117   connect( myTreeRemoveObjects, SIGNAL( itemClicked( QTreeWidgetItem*, int) ), this, SLOT( onItemClicked( QTreeWidgetItem*, int ) ) );
118   connect( myTreeKeptObjects->header(), SIGNAL( sectionClicked ( int ) ), this, SLOT( onHeaderClicked( int ) ) );
119   connect( myTreeRemoveObjects->header(), SIGNAL( sectionClicked ( int ) ), this, SLOT( onHeaderClicked( int ) ) );
120
121   connect( myGroupIntermediates, SIGNAL( buttonClicked( int ) ), this, SLOT( update() ) );
122   connect( myGroupSubObjects, SIGNAL( buttonClicked( int ) ), this, SLOT( update() ) );
123
124   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( clickOnOk() ) );
125   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
126   connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( clickOnHelp() ) );
127
128   connect( myApp->selectionMgr(), SIGNAL( currentSelectionChanged() ),
129            this, SLOT( selectionChanged() ) );
130
131   init( getSelectedObjects() );
132 }
133
134 GEOMToolsGUI_ReduceStudyDlg::~GEOMToolsGUI_ReduceStudyDlg()
135 {
136   // no need to delete child widgets, Qt does it all for us
137 }
138
139 //=================================================================================
140 // function : init()
141 // purpose  : initialize dialog data
142 //=================================================================================
143 void GEOMToolsGUI_ReduceStudyDlg::init( const std::set<std::string>& theObjectEntries )
144 {
145   myMainEntries.clear();
146
147   myKeptObjects.clear();
148   myListParents.clear();
149   myListSubObjects.clear();
150   myRemovedObjects.clear();
151
152   myMainEntries = theObjectEntries;
153
154   GEOM::string_array_var keptObjects = new GEOM::string_array();
155   int It = 0;
156   keptObjects->length( theObjectEntries.size() );
157   std::set<std::string>::iterator iter;
158   for( iter=theObjectEntries.begin(); iter!=theObjectEntries.end(); ++iter, It++ )
159     keptObjects[ It ] = (*iter).c_str();
160
161   GEOM::string_array_var parentsObjects = new GEOM::string_array();
162   GEOM::string_array_var subObjects = new GEOM::string_array();
163   GEOM::string_array_var otherObjects = new GEOM::string_array();
164
165   GeometryGUI::GetGeomGen()->GetEntriesToReduceStudy( keptObjects, parentsObjects,
166                                                               subObjects, otherObjects );
167
168   for ( unsigned long i = 0; i < keptObjects->length(); i++ )
169     myKeptObjects.insert( keptObjects[i].in() );
170   for( unsigned long i = 0; i< otherObjects->length(); i++ )
171     myRemovedObjects.insert( otherObjects[i].in() );
172   for( unsigned long i = 0; i< parentsObjects->length(); i++ )
173     myListParents.insert( parentsObjects[i].in() );
174   for( unsigned long i = 0; i< subObjects->length(); i++ )
175     myListSubObjects.insert( subObjects[i].in() );
176
177   update();
178
179   checkVisibleIcon( myTreeKeptObjects );
180   checkVisibleIcon( myTreeRemoveObjects );
181 }
182
183 //=================================================================================
184 // function : getSelectedObjects()
185 // purpose  : get selected objects in object browser
186 //=================================================================================
187 std::set<std::string> GEOMToolsGUI_ReduceStudyDlg::getSelectedObjects() const
188 {
189   std::set<std::string> objects;
190
191   SALOME_ListIO selected;
192   myApp->selectionMgr()->selectedObjects( selected );
193
194   for( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
195     Handle( SALOME_InteractiveObject ) io = It.Value();
196     if( !io->hasEntry() )
197       continue;
198     GEOM::GEOM_Object_var geomObject = GEOM::GEOM_Object::_nil();
199     geomObject = GEOMBase::ConvertIOinGEOMObject( io );
200     if( geomObject->_is_nil() )
201       continue;
202     QString entry = geomObject->GetEntry();
203     objects.insert( entry.toStdString().c_str() );
204   }
205   return objects;
206 }
207
208 //=================================================================================
209 // function : createTreeWidget()
210 // purpose  : create tree widget for unpublished or removed objects
211 //=================================================================================
212 void GEOMToolsGUI_ReduceStudyDlg::createTreeWidget( QTreeWidget* theTreeWidget )
213 {
214   theTreeWidget->setColumnCount( 2 );
215   QStringList columnNames;
216   columnNames.append(tr( "GEOM_REDUCE_STUDY_NAME" ));
217   columnNames.append("");
218   theTreeWidget->setHeaderLabels( columnNames );
219   QTreeWidgetItem * headerItem = new QTreeWidgetItem( columnNames );
220   theTreeWidget->setHeaderItem ( headerItem );
221   theTreeWidget->header()->moveSection( 1, 0 );
222   theTreeWidget->header()->setSectionsClickable( true );
223   theTreeWidget->header()->setSectionsMovable( false );
224   theTreeWidget->header()->setSectionResizeMode( 1, QHeaderView::ResizeToContents );
225
226   theTreeWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
227 }
228
229 //=================================================================================
230 // function : createButtonGroup()
231 // purpose  : create button group for intermediate objects or sub-objects
232 //=================================================================================
233 QGroupBox* GEOMToolsGUI_ReduceStudyDlg::createButtonGroup( QButtonGroup* theButtonGroup )
234 {
235   QGroupBox* groupObjects = new QGroupBox();
236   QHBoxLayout* layoutObjects = new QHBoxLayout( groupObjects );
237
238   QRadioButton* buttonKeep = new QRadioButton( tr( "GEOM_REDUCE_STUDY_KEEP") );
239   theButtonGroup->addButton( buttonKeep, 0 );
240   QRadioButton* buttonUnpublish = new QRadioButton( tr( "GEOM_REDUCE_STUDY_UNPUBLISH") );
241   theButtonGroup->addButton( buttonUnpublish, 1 );
242   QRadioButton* buttonRemove = new QRadioButton( tr( "GEOM_REDUCE_STUDY_REMOVE") );
243   theButtonGroup->addButton( buttonRemove, 2 );
244
245   theButtonGroup->button( 0 )->setChecked( true );
246   theButtonGroup->setExclusive( true );
247
248   layoutObjects->addWidget( buttonKeep );
249   layoutObjects->addWidget( buttonUnpublish );
250   layoutObjects->addWidget( buttonRemove );
251
252   return groupObjects;
253 }
254
255 //=================================================================================
256 // function : addObjectsToTree()
257 // purpose  : add the list of objects to tree
258 //=================================================================================
259 void GEOMToolsGUI_ReduceStudyDlg::addObjectsToTree( QTreeWidget* theWidget, std::set<std::string>& theObjects )
260 {
261   std::set<std::string>::iterator it;
262   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
263     std::string objectEntry = *it;
264     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( objectEntry.c_str() );
265     GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( GeomBaseObject );
266     QString studyEntry = GeomBaseObject->GetStudyEntry();
267     if( GeomObject->_is_nil() || studyEntry.isEmpty() || !isObjectDrawable( studyEntry.toStdString() ) )
268       continue;
269     addSubObject( theWidget, theObjects, GeomObject );
270   }
271 }
272
273 //=================================================================================
274 // function : addSubObject()
275 // purpose  : add sub-object to parent object in the tree
276 //=================================================================================
277 GEOMToolsGUI_TreeWidgetItem* GEOMToolsGUI_ReduceStudyDlg::addSubObject( QTreeWidget* theWidget,
278                                                                         std::set<std::string>& theObjects,
279                                                                         GEOM::GEOM_Object_var theObject )
280 {
281   GEOMToolsGUI_TreeWidgetItem* item;
282   if( !theObject->IsMainShape() ) {
283     GEOM::GEOM_Object_var aMainShape = theObject->GetMainShape();
284     if ( CORBA::is_nil( aMainShape ) )
285       return NULL;
286     GEOMToolsGUI_TreeWidgetItem* parentItem = addSubObject( theWidget, theObjects, aMainShape );
287     item = findObjectInTree( theWidget, theObject );
288     if( !item )
289       item = new GEOMToolsGUI_TreeWidgetItem( parentItem, QStringList() << theObject->GetName(), theObject->GetStudyEntry() );
290   }
291   else {
292     item = findObjectInTree( theWidget, theObject );
293     if( !item )
294       item = new GEOMToolsGUI_TreeWidgetItem( theWidget, QStringList() << theObject->GetName(), theObject->GetStudyEntry() );
295   }
296
297   bool isDisplayed = false;
298   if( theObjects.find( theObject->GetEntry() ) != theObjects.end() ) {
299     isDisplayed = myDisplayer.IsDisplayed( theObject->GetStudyEntry() );
300     if ( isDisplayed )
301       item->setVisible( true, myVisible );
302     else
303       item->setVisible( false, myInvisible );
304
305     if( myMainEntries.find( theObject->GetEntry() ) != myMainEntries.end() ) {
306       QFont Textfont = item->font(0);
307       Textfont.setBold( true );
308       item->setFont( 0, Textfont );
309     }
310   }
311   else {
312     item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
313     item->setTextColor( 0, QColor( 150, 150, 150 ) );
314   }
315   return item;
316 }
317
318 //=================================================================================
319 // function : findObjectInTree()
320 // purpose  : find object in the tree
321 //=================================================================================
322 GEOMToolsGUI_TreeWidgetItem* GEOMToolsGUI_ReduceStudyDlg::findObjectInTree( QTreeWidget* theWidget, GEOM::GEOM_Object_var theObject )
323 {
324   QTreeWidgetItemIterator it( theWidget );
325   while(*it) {
326     GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
327     if( QString( item->getStudyEntry() ) == QString( theObject->GetStudyEntry() ) )
328       return item;
329     ++it;
330   }
331   return NULL;
332 }
333
334 //=================================================================================
335 // function : checkVisibleIcon()
336 // purpose  : set visible or invisible icon in the header of tree
337 //=================================================================================
338 void GEOMToolsGUI_ReduceStudyDlg::checkVisibleIcon( QTreeWidget* theWidget )
339 {
340   bool isInvisible = false;
341   QTreeWidgetItemIterator it( theWidget );
342   while(*it) {
343     GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
344     //const char* entry = item->getStudyEntry();
345     if( item->flags() & Qt::ItemIsSelectable )
346       if( !item->isVisible() )
347         isInvisible = true;
348     ++it;
349   }
350
351   if( isInvisible ) {
352     theWidget->headerItem()->setIcon( 1, myInvisible );
353     myMapTreeSelectAll[ theWidget ] = false;
354   }
355   else {
356     theWidget->headerItem()->setIcon( 1, myVisible );
357     myMapTreeSelectAll[ theWidget ] = true;
358   }
359 }
360
361 //=================================================================================
362 // function : isObjectDrawable()
363 // purpose  : return true if object is drawable, and false if object is hidden in the study
364 //=================================================================================
365 bool GEOMToolsGUI_ReduceStudyDlg::isObjectDrawable( std::string theStudyEntry )
366 {
367   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
368   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
369   //If object hasn't "AttributeDrawable" => it visible
370   bool isDrawable = true;
371   _PTR(SObject) SO ( aStudy->FindObjectID( theStudyEntry ) );
372   _PTR(GenericAttribute) anAttr;
373   if ( SO && SO->FindAttribute( anAttr, "AttributeDrawable" ) ) {
374     _PTR(AttributeDrawable) aDrw (anAttr);
375     isDrawable = aDrw->IsDrawable();
376   }
377   return isDrawable;
378 }
379
380 //=================================================================================
381 // function : unpublishObjects()
382 // purpose  : unpublish(hide) objects in the study
383 //=================================================================================
384 void GEOMToolsGUI_ReduceStudyDlg::unpublishObjects( std::set<std::string>& theObjects )
385 {
386   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
387   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
388   std::set<std::string>::iterator it;
389   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
390     std::string objectEntry = *it;
391     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( objectEntry.c_str() );
392     std::string studyEntry = GeomBaseObject->GetStudyEntry();
393     if ( studyEntry == "" || !isObjectDrawable( studyEntry ) )
394       continue;
395     _PTR(SObject) obj ( aStudy->FindObjectID( studyEntry.c_str() ) );
396     _PTR(GenericAttribute) anAttr;
397     if ( obj ) {
398       _PTR(AttributeDrawable) aDrw = aStudyBuilder->FindOrCreateAttribute( obj, "AttributeDrawable" );
399       aDrw->SetDrawable( false );
400       myDisplayer.EraseWithChildren( new SALOME_InteractiveObject( studyEntry.c_str(), "GEOM", "TEMP_IO" ) );
401       // hide references if any
402       std::vector< _PTR(SObject) > vso = aStudy->FindDependances(obj);
403       for ( int i = 0; i < (int)vso.size(); i++ ) {
404         _PTR(SObject) refObj = vso[i];
405         aDrw = aStudyBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
406         aDrw->SetDrawable( false );
407       }
408     }
409   }
410   myApp->updateObjectBrowser( false );
411   myApp->updateActions();
412 }
413
414 //=================================================================================
415 // function : removeObjects()
416 // purpose  : remove objects from the study
417 //=================================================================================
418 void GEOMToolsGUI_ReduceStudyDlg::removeObjects( std::set<std::string>& theObjects )
419 {
420   std::set<std::string>::iterator it;
421   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
422     std::string objectEntry = *it;
423     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( objectEntry.c_str() );
424     std::string studyEntry = GeomBaseObject->GetStudyEntry();
425     if ( studyEntry == "" )
426       GeometryGUI::GetGeomGen()->RemoveObject( GeomBaseObject );
427     else {
428       if( !isObjectDrawable( studyEntry ) )
429         continue;
430       removeObject( studyEntry );
431     }
432   }
433   myApp->updateObjectBrowser( false );
434   myApp->updateActions();
435 }
436
437 //=================================================================================
438 // function : removeObject()
439 // purpose  : remove object with given study entry
440 //=================================================================================
441 void GEOMToolsGUI_ReduceStudyDlg::removeObject( std::string& theStudyEntry )
442 {
443   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
444   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
445   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
446   _PTR(UseCaseBuilder) aUseCaseBuilder = aStudy->GetUseCaseBuilder();
447
448   _PTR(SObject) obj ( aStudy->FindObjectID( theStudyEntry.c_str() ) );
449   if ( obj ) {
450     // remove visual properties of the object
451     appStudy->removeObjectProperties(obj->GetID().c_str());
452     // remove references to this object
453     appStudy->deleteReferencesTo( obj );
454     // remove objects from study
455     aStudyBuilder->RemoveObjectWithChildren( obj );
456     // remove object from use case tree
457     aUseCaseBuilder->Remove( obj );
458     myDisplayer.EraseWithChildren( new SALOME_InteractiveObject( theStudyEntry.c_str(), "GEOM", "TEMP_IO" ) );
459   }
460 }
461
462 //=================================================================================
463 // function : removeEmptyFolders()
464 // purpose  : remove empty folders from the study
465 //=================================================================================
466 void GEOMToolsGUI_ReduceStudyDlg::removeEmptyFolders()
467 {
468   std::set<std::string> emptyFolders;
469
470   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
471   _PTR(SComponent) SC ( aStudy->FindComponent( "GEOM" ) );
472   if ( !SC )
473     return;
474   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( SC ) );
475   anIter->InitEx( true );
476   while( anIter->More() ) {
477     _PTR(SObject) valSO ( anIter->Value() );
478     _PTR(SObject) refSO;
479     if ( !valSO->ReferencedObject( refSO ) )
480       getEmptyFolders( valSO, emptyFolders );
481     anIter->Next();
482   }
483
484   std::set<std::string>::iterator iter;
485   for( iter = emptyFolders.begin(); iter != emptyFolders.end(); ++iter ) {
486     std::string studyEntry = *iter;
487     removeObject( studyEntry );
488   }
489   myApp->updateObjectBrowser( false );
490   myApp->updateActions();
491 }
492
493 //=================================================================================
494 // function : removeEmptyFolders()
495 // purpose  : remove empty folders from the study
496 //=================================================================================
497 void GEOMToolsGUI_ReduceStudyDlg::getEmptyFolders( _PTR(SObject) theSO, std::set<std::string>& theFolders )
498 {
499   _PTR(UseCaseBuilder) aUseCaseBuilder = SalomeApp_Application::getStudy()->GetUseCaseBuilder();
500
501   bool isFolder = false;
502   _PTR(GenericAttribute) anAttr;
503   if ( theSO->FindAttribute(anAttr, "AttributeLocalID") ) {
504     _PTR(AttributeLocalID) aLocalID( anAttr );
505     isFolder = aLocalID->Value() == 999;
506   }
507   QString studyEntry = theSO->GetID().c_str();
508   if ( isFolder ) {
509     if( !aUseCaseBuilder->HasChildren( theSO ) )
510       theFolders.insert( studyEntry.toStdString() );
511     else {
512       _PTR(UseCaseIterator) ucit ( aUseCaseBuilder->GetUseCaseIterator( theSO ) );
513       for ( ucit->Init( false ); ucit->More(); ucit->Next() )
514         getEmptyFolders( ucit->Value(), theFolders );
515     }
516   }
517 }
518
519 //=================================================================================
520 // function : onItemClicked()
521 // purpose  : called when tree item was clicked
522 //=================================================================================
523 void GEOMToolsGUI_ReduceStudyDlg::onItemClicked( QTreeWidgetItem* theItem, int theColumn )
524 {
525   if( theColumn != 1 || !( theItem->flags() & Qt::ItemIsSelectable ) )
526     return;
527
528   GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>( theItem );
529
530   const char* entry = item->getStudyEntry();
531   Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry, "GEOM", "TEMP_IO" );
532   if( myDisplayer.IsDisplayed( entry ) ) {
533     item->setVisible( false, myInvisible );
534     myDisplayer.Erase( io );
535   }
536   else {
537     item->setVisible( true, myVisible );
538     myDisplayer.Display( io );
539   }
540   myDisplayer.UpdateViewer();
541   checkVisibleIcon( item->treeWidget() );
542 }
543
544 //=================================================================================
545 // function : onHeaderClicked()
546 // purpose  : called when header item of tree was clicked
547 //=================================================================================
548 void GEOMToolsGUI_ReduceStudyDlg::onHeaderClicked( int theColumn )
549 {
550   if( theColumn != 1 )
551     return;
552
553   QTreeWidget* treeWidget = dynamic_cast<QTreeWidget*>(sender()->parent());
554   if( myMapTreeSelectAll[ treeWidget ] ) {
555     myMapTreeSelectAll[ treeWidget ] = false;
556     treeWidget->headerItem()->setIcon( 1, myInvisible );
557     QTreeWidgetItemIterator it( treeWidget );
558     while(*it) {
559       GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
560       if( ( item->flags() & Qt::ItemIsSelectable ) && item->isVisible() ) {
561         const char* entry = item->getStudyEntry();
562         item->setVisible( false, myInvisible );
563         myDisplayer.Erase( new SALOME_InteractiveObject( entry, "GEOM", "TEMP_IO" ) );
564       }
565       ++it;
566     }
567   }
568   else {
569     myMapTreeSelectAll[ treeWidget ] = true;
570     treeWidget->headerItem()->setIcon( 1, myVisible );
571     QTreeWidgetItemIterator it( treeWidget );
572     while(*it) {
573       GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
574       if( ( item->flags() & Qt::ItemIsSelectable ) && !item->isVisible() ) {
575         const char* entry = item->getStudyEntry();
576         item->setVisible( true, myVisible );
577         myDisplayer.Display( new SALOME_InteractiveObject( entry, "GEOM", "TEMP_IO" ) );
578       }
579       ++it;
580     }
581   }
582   myDisplayer.UpdateViewer();
583 }
584
585 //=================================================================================
586 // function : selectionChanged()
587 // purpose  : called when selection of object browser was changed
588 //=================================================================================
589 void GEOMToolsGUI_ReduceStudyDlg::selectionChanged()
590 {
591   init( getSelectedObjects() );
592 }
593
594 //=================================================================================
595 // function : update()
596 // purpose  : update tree data
597 //=================================================================================
598 void GEOMToolsGUI_ReduceStudyDlg::update()
599 {
600   myTreeKeptObjects->clear();
601   myTreeRemoveObjects->clear();
602
603   std::set<std::string> keptObjects( myKeptObjects );
604   std::set<std::string> removeObjects( myRemovedObjects );
605
606   // Intermediate objects
607   if( myGroupIntermediates->checkedId() == 2 ) { // remove
608     std::set<std::string>::iterator iter;
609     for( iter=myListParents.begin(); iter!=myListParents.end(); ++iter)
610       removeObjects.insert( *iter );
611   }
612   else { // keep or unpublish
613     std::set<std::string>::iterator iter;
614     for( iter=myListParents.begin(); iter!=myListParents.end(); ++iter)
615       keptObjects.insert( *iter );
616   }
617
618   // Sub-objects
619   if( myGroupSubObjects->checkedId() == 2 ) {
620     std::set<std::string>::iterator iter;
621     for( iter=myListSubObjects.begin(); iter!=myListSubObjects.end(); ++iter)
622       removeObjects.insert( *iter );
623   }
624   else {
625     std::set<std::string>::iterator iter;
626     for( iter=myListSubObjects.begin(); iter!=myListSubObjects.end(); ++iter)
627       keptObjects.insert( *iter );
628   }
629
630   addObjectsToTree( myTreeKeptObjects, keptObjects );
631   addObjectsToTree( myTreeRemoveObjects, removeObjects );
632
633   myTreeKeptObjects->collapseAll();
634   myTreeRemoveObjects->collapseAll();
635
636 }
637
638 //=================================================================================
639 // function : clickOnOk()
640 // purpose  : called when OK button was clicked
641 //=================================================================================
642 void GEOMToolsGUI_ReduceStudyDlg::clickOnOk()
643 {
644   std::set<std::string> objectsToBeRemoved = myRemovedObjects;
645   std::set<std::string> objectsToBeUnpublished;
646
647   // Create lists of intermediate objects to be removed or to be unpublished
648   std::set<std::string>::iterator iter;
649   if( myGroupIntermediates->checkedId() == 1 ) { // unpublish
650     for( iter = myListParents.begin(); iter != myListParents.end(); ++iter )
651     objectsToBeUnpublished.insert( *iter );
652   }
653   if( myGroupIntermediates->checkedId() == 2 ) { // remove
654     if( !myCBSoftRemoval->isChecked() && 
655         SUIT_MessageBox::question( this,
656                                    tr( "GEOM_WRN_WARNING" ),
657                                    tr( "GEOM_REDUCE_STUDY_WARNING_DELETE" ),
658                                    QMessageBox::Yes | QMessageBox::No,
659                                    QMessageBox::Yes ) == QMessageBox::No ) {
660       return;
661     }
662     for( iter = myListParents.begin(); iter != myListParents.end(); ++iter )
663       objectsToBeRemoved.insert( *iter );
664   }
665
666   // Create lists of sub-objects to be removed or to be unpublished
667   if( myGroupSubObjects->checkedId() == 1 ) { // unpublish
668     for( iter = myListSubObjects.begin(); iter != myListSubObjects.end(); ++iter )
669       objectsToBeUnpublished.insert( *iter );
670   }
671   else if( myGroupSubObjects->checkedId() == 2 ) { // remove
672     for( iter = myListSubObjects.begin(); iter != myListSubObjects.end(); ++iter )
673       objectsToBeRemoved.insert( *iter );
674   }
675
676   // if user chosen the soft removal
677   if( myCBSoftRemoval->isChecked() ) {
678     for( iter = objectsToBeRemoved.begin(); iter != objectsToBeRemoved.end(); ++iter )
679       objectsToBeUnpublished.insert( *iter );
680     unpublishObjects( objectsToBeUnpublished );
681   }
682   else {
683     unpublishObjects( objectsToBeUnpublished );
684     removeObjects( objectsToBeRemoved );
685   }
686
687   // if user want to delete the empty folders
688   if( myCBRemoveEmptyFolder->isChecked() )
689     removeEmptyFolders();
690
691   accept();
692 }
693
694 //=================================================================================
695 // function : clickOnHelp()
696 // purpose  : called when Help button was clicked to open a help page
697 //=================================================================================
698 void GEOMToolsGUI_ReduceStudyDlg::clickOnHelp()
699 {
700   myApp->onHelpContextModule( "GEOM", "reduce_study_page.html" );
701 }
702
703 GEOMToolsGUI_TreeWidgetItem::GEOMToolsGUI_TreeWidgetItem( QTreeWidget* view, const QStringList &strings,
704                                                           char* studyEntry, int type )
705 :QTreeWidgetItem( view, strings, type ),
706  myVisible( false ),
707  myStudyEntry( studyEntry )
708 {
709 }
710
711 GEOMToolsGUI_TreeWidgetItem::GEOMToolsGUI_TreeWidgetItem( QTreeWidgetItem* parent, const QStringList &strings,
712                                                           char* studyEntry, int type )
713 :QTreeWidgetItem( parent, strings, type ),
714  myVisible( false ),
715  myStudyEntry( studyEntry )
716 {
717 }
718
719 GEOMToolsGUI_TreeWidgetItem::~GEOMToolsGUI_TreeWidgetItem()
720 {
721 }
722
723 bool GEOMToolsGUI_TreeWidgetItem::isVisible()
724 {
725   return myVisible;
726 }
727
728 void GEOMToolsGUI_TreeWidgetItem::setVisible( bool theIsVisible, QIcon& theIcon )
729 {
730   myVisible = theIsVisible;
731   setIcon( 1, theIcon );
732 }
733
734 char* GEOMToolsGUI_TreeWidgetItem::getStudyEntry() const
735 {
736   return myStudyEntry;
737 }