Salome HOME
Merge branch 'master' into rnc/t_shape_plugin
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_ReduceStudyDlg.cxx
1 // Copyright (C) 2014-2015  CEA/DEN, EDF R&D, 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 myDisplayer(NULL)
47 {
48   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
49   myVisible = QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) );
50   myInvisible = QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) );
51
52   myApp = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
53   if ( !myApp ) return;
54   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
55   myStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() )->studyDS();
56   myDisplayer = GEOM_Displayer( study );
57
58   setWindowTitle( tr( "GEOM_REDUCE_STUDY_TITLE" ) );
59   setAttribute(Qt::WA_DeleteOnClose);
60
61   QGridLayout* topLayout = new QGridLayout( this );
62   topLayout->setMargin( 11 ); topLayout->setSpacing( 6 );
63
64   /**********************   Objects to be kept    **********************/
65   QGroupBox* groupKeptObjects = new QGroupBox( tr( "GEOM_REDUCE_STUDY_KEPT_OBJECTS" ) );
66   QGridLayout* layoutKeptObjects = new QGridLayout( groupKeptObjects );
67   createTreeWidget( myTreeKeptObjects = new QTreeWidget() );
68   layoutKeptObjects->addWidget( myTreeKeptObjects );
69
70   /**********************  Objects to be removed  **********************/
71   QGroupBox* groupRemoveObjects = new QGroupBox( tr( "GEOM_REDUCE_STUDY_REMOVE_OBJECTS" ) );
72   QGridLayout* layoutRemoveObjects = new QGridLayout( groupRemoveObjects );
73   createTreeWidget( myTreeRemoveObjects = new QTreeWidget() );
74   layoutRemoveObjects->addWidget( myTreeRemoveObjects );
75
76   /**********************         Options         **********************/
77   QGroupBox* groupOptions = new QGroupBox( tr( "GEOM_REDUCE_STUDY_OPTIONS" ) );
78   QVBoxLayout* layoutOptions = new QVBoxLayout( groupOptions );
79
80   // Intermediate objects
81   QGroupBox* groupIntermediates = createButtonGroup( myGroupIntermediates = new QButtonGroup() );
82   groupIntermediates->setTitle( tr( "GEOM_REDUCE_STUDY_INTERMEDIATES" ) );
83
84   // Sub-objects
85   QGroupBox* groupSubObjects = createButtonGroup( myGroupSubObjects = new QButtonGroup() );
86   groupSubObjects->setTitle( tr( "GEOM_REDUCE_STUDY_SUB_OBJECTS" ) );
87
88   // Others
89   myCBRemoveEmptyFolder = new QCheckBox( tr( "GEOM_REDUCE_STUDY_REMOVE_EMPTY_FOLDER" ) );
90   myCBRemoveEmptyFolder->setChecked( true );
91   myCBSoftRemoval = new QCheckBox( tr( "GEOM_REDUCE_STUDY_SOFT_REMOVAL" ) );
92
93   layoutOptions->addWidget( groupIntermediates );
94   layoutOptions->addWidget( groupSubObjects );
95   layoutOptions->addWidget( myCBRemoveEmptyFolder );
96   layoutOptions->addWidget( myCBSoftRemoval );
97
98   /**********************         Buttons         **********************/
99   QGroupBox* groupButtons = new QGroupBox();
100   QHBoxLayout* layoutButtons = new QHBoxLayout( groupButtons );
101
102   QPushButton* buttonOk = new QPushButton( tr( "GEOM_BUT_OK" ) );
103   QPushButton* buttonCancel = new QPushButton( tr( "GEOM_BUT_CANCEL" ) );
104   QPushButton* buttonHelp = new QPushButton( tr( "GEOM_BUT_HELP" ) );
105
106   layoutButtons->addWidget( buttonOk );
107   layoutButtons->addStretch();
108   layoutButtons->addWidget( buttonCancel );
109   layoutButtons->addWidget( buttonHelp );
110
111   topLayout->addWidget( groupKeptObjects, 0, 0 );
112   topLayout->addWidget( groupRemoveObjects, 0, 1 );
113   topLayout->addWidget( groupOptions, 1, 0, 1, 2 );
114   topLayout->addWidget( groupButtons, 2, 0, 1, 2 );
115
116   // Signals and slots connections
117
118   connect( myTreeKeptObjects, SIGNAL( itemClicked( QTreeWidgetItem*, int) ), this, SLOT( onItemClicked( QTreeWidgetItem*, int ) ) );
119   connect( myTreeRemoveObjects, SIGNAL( itemClicked( QTreeWidgetItem*, int) ), this, SLOT( onItemClicked( QTreeWidgetItem*, int ) ) );
120   connect( myTreeKeptObjects->header(), SIGNAL( sectionClicked ( int ) ), this, SLOT( onHeaderClicked( int ) ) );
121   connect( myTreeRemoveObjects->header(), SIGNAL( sectionClicked ( int ) ), this, SLOT( onHeaderClicked( int ) ) );
122
123   connect( myGroupIntermediates, SIGNAL( buttonClicked( int ) ), this, SLOT( update() ) );
124   connect( myGroupSubObjects, SIGNAL( buttonClicked( int ) ), this, SLOT( update() ) );
125
126   connect( buttonOk, SIGNAL( clicked() ), this, SLOT( clickOnOk() ) );
127   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
128   connect( buttonHelp, SIGNAL( clicked() ), this, SLOT( clickOnHelp() ) );
129
130   connect( myApp->selectionMgr(), SIGNAL( currentSelectionChanged() ),
131            this, SLOT( selectionChanged() ) );
132
133   init( getSelectedObjects() );
134 }
135
136 GEOMToolsGUI_ReduceStudyDlg::~GEOMToolsGUI_ReduceStudyDlg()
137 {
138   // no need to delete child widgets, Qt does it all for us
139 }
140
141 //=================================================================================
142 // function : init()
143 // purpose  : initialize dialog data
144 //=================================================================================
145 void GEOMToolsGUI_ReduceStudyDlg::init( const std::set<std::string>& theObjectEntries )
146 {
147   myMainEntries.clear();
148
149   myKeptObjects.clear();
150   myListParents.clear();
151   myListSubObjects.clear();
152   myRemovedObjects.clear();
153
154   myMainEntries = theObjectEntries;
155
156   GEOM::string_array_var keptObjects = new GEOM::string_array();
157   int It = 0;
158   keptObjects->length( theObjectEntries.size() );
159   std::set<std::string>::iterator iter;
160   for( iter=theObjectEntries.begin(); iter!=theObjectEntries.end(); ++iter, It++ )
161     keptObjects[ It ] = (*iter).c_str();
162
163   GEOM::string_array_var parentsObjects = new GEOM::string_array();
164   GEOM::string_array_var subObjects = new GEOM::string_array();
165   GEOM::string_array_var otherObjects = new GEOM::string_array();
166
167   GeometryGUI::GetGeomGen()->GetEntriesToReduceStudy( GeometryGUI::ClientStudyToStudy( myStudy ),
168                                                              keptObjects, parentsObjects,
169                                                              subObjects, otherObjects );
170
171   for ( int i = 0; i < keptObjects->length(); i++ )
172     myKeptObjects.insert( keptObjects[i].in() );
173   for( int i = 0; i< otherObjects->length(); i++ )
174     myRemovedObjects.insert( otherObjects[i].in() );
175   for( int i = 0; i< parentsObjects->length(); i++ )
176     myListParents.insert( parentsObjects[i].in() );
177   for( int i = 0; i< subObjects->length(); i++ )
178     myListSubObjects.insert( subObjects[i].in() );
179
180   update();
181
182   checkVisibleIcon( myTreeKeptObjects );
183   checkVisibleIcon( myTreeRemoveObjects );
184 }
185
186 //=================================================================================
187 // function : getSelectedObjects()
188 // purpose  : get selected objects in object browser
189 //=================================================================================
190 std::set<std::string> GEOMToolsGUI_ReduceStudyDlg::getSelectedObjects() const
191 {
192   std::set<std::string> objects;
193
194   SALOME_ListIO selected;
195   myApp->selectionMgr()->selectedObjects( selected );
196
197   for( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
198     Handle( SALOME_InteractiveObject ) io = It.Value();
199     if( !io->hasEntry() )
200       continue;
201     GEOM::GEOM_Object_var geomObject = GEOM::GEOM_Object::_nil();
202     geomObject = GEOMBase::ConvertIOinGEOMObject( io );
203     if( geomObject->_is_nil() )
204       continue;
205     QString entry = geomObject->GetEntry();
206     objects.insert( entry.toStdString().c_str() );
207   }
208   return objects;
209 }
210
211 //=================================================================================
212 // function : createTreeWidget()
213 // purpose  : create tree widget for unpublished or removed objects
214 //=================================================================================
215 void GEOMToolsGUI_ReduceStudyDlg::createTreeWidget( QTreeWidget* theTreeWidget )
216 {
217   theTreeWidget->setColumnCount( 2 );
218   QStringList columnNames;
219   columnNames.append(tr( "GEOM_REDUCE_STUDY_NAME" ));
220   columnNames.append("");
221   theTreeWidget->setHeaderLabels( columnNames );
222   QTreeWidgetItem * headerItem = new QTreeWidgetItem( columnNames );
223   theTreeWidget->setHeaderItem ( headerItem );
224   theTreeWidget->header()->moveSection( 1, 0 );
225   theTreeWidget->header()->setClickable( true );
226   theTreeWidget->header()->setMovable( false );
227   theTreeWidget->header()->setResizeMode( 1, QHeaderView::ResizeToContents );
228   theTreeWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
229 }
230
231 //=================================================================================
232 // function : createButtonGroup()
233 // purpose  : create button group for intermediate objects or sub-objects
234 //=================================================================================
235 QGroupBox* GEOMToolsGUI_ReduceStudyDlg::createButtonGroup( QButtonGroup* theButtonGroup )
236 {
237   QGroupBox* groupObjects = new QGroupBox();
238   QHBoxLayout* layoutObjects = new QHBoxLayout( groupObjects );
239
240   QRadioButton* buttonKeep = new QRadioButton( tr( "GEOM_REDUCE_STUDY_KEEP") );
241   theButtonGroup->addButton( buttonKeep, 0 );
242   QRadioButton* buttonUnpublish = new QRadioButton( tr( "GEOM_REDUCE_STUDY_UNPUBLISH") );
243   theButtonGroup->addButton( buttonUnpublish, 1 );
244   QRadioButton* buttonRemove = new QRadioButton( tr( "GEOM_REDUCE_STUDY_REMOVE") );
245   theButtonGroup->addButton( buttonRemove, 2 );
246
247   theButtonGroup->button( 0 )->setChecked( true );
248   theButtonGroup->setExclusive( true );
249
250   layoutObjects->addWidget( buttonKeep );
251   layoutObjects->addWidget( buttonUnpublish );
252   layoutObjects->addWidget( buttonRemove );
253
254   return groupObjects;
255 }
256
257 //=================================================================================
258 // function : addObjectsToTree()
259 // purpose  : add the list of objects to tree
260 //=================================================================================
261 void GEOMToolsGUI_ReduceStudyDlg::addObjectsToTree( QTreeWidget* theWidget, std::set<std::string>& theObjects )
262 {
263   std::set<std::string>::iterator it;
264   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
265     std::string objectEntry = *it;
266     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( myStudy->StudyId(), objectEntry.c_str() );
267     GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( GeomBaseObject );
268     QString studyEntry = GeomBaseObject->GetStudyEntry();
269     if( GeomObject->_is_nil() || studyEntry.isEmpty() || !isObjectDrawable( studyEntry.toStdString() ) )
270       continue;
271     addSubObject( theWidget, theObjects, GeomObject );
272   }
273 }
274
275 //=================================================================================
276 // function : addSubObject()
277 // purpose  : add sub-object to parent object in the tree
278 //=================================================================================
279 GEOMToolsGUI_TreeWidgetItem* GEOMToolsGUI_ReduceStudyDlg::addSubObject( QTreeWidget* theWidget,
280                                                                         std::set<std::string>& theObjects,
281                                                                         GEOM::GEOM_Object_var theObject )
282 {
283   GEOMToolsGUI_TreeWidgetItem* item;
284   if( !theObject->IsMainShape() ) {
285     GEOM::GEOM_Object_var aMainShape = theObject->GetMainShape();
286     if ( CORBA::is_nil( aMainShape ) )
287       return NULL;
288     GEOMToolsGUI_TreeWidgetItem* parentItem = addSubObject( theWidget, theObjects, aMainShape );
289     item = findObjectInTree( theWidget, theObject );
290     if( !item )
291       item = new GEOMToolsGUI_TreeWidgetItem( parentItem, QStringList() << theObject->GetName(), theObject->GetStudyEntry() );
292   }
293   else {
294     item = findObjectInTree( theWidget, theObject );
295     if( !item )
296       item = new GEOMToolsGUI_TreeWidgetItem( theWidget, QStringList() << theObject->GetName(), theObject->GetStudyEntry() );
297   }
298
299   bool isDisplayed = false;
300   if( theObjects.find( theObject->GetEntry() ) != theObjects.end() ) {
301     isDisplayed = myDisplayer.IsDisplayed( theObject->GetStudyEntry() );
302     if ( isDisplayed )
303       item->setVisible( true, myVisible );
304     else
305       item->setVisible( false, myInvisible );
306
307     if( myMainEntries.find( theObject->GetEntry() ) != myMainEntries.end() ) {
308       QFont Textfont = item->font(0);
309       Textfont.setBold( true );
310       item->setFont( 0, Textfont );
311     }
312   }
313   else {
314     item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
315     item->setTextColor( 0, QColor( 150, 150, 150 ) );
316   }
317   return item;
318 }
319
320 //=================================================================================
321 // function : findObjectInTree()
322 // purpose  : find object in the tree
323 //=================================================================================
324 GEOMToolsGUI_TreeWidgetItem* GEOMToolsGUI_ReduceStudyDlg::findObjectInTree( QTreeWidget* theWidget, GEOM::GEOM_Object_var theObject )
325 {
326   QTreeWidgetItemIterator it( theWidget );
327   while(*it) {
328     GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
329     if( QString( item->getStudyEntry() ) == QString( theObject->GetStudyEntry() ) )
330       return item;
331     ++it;
332   }
333   return NULL;
334 }
335
336 //=================================================================================
337 // function : checkVisibleIcon()
338 // purpose  : set visible or invisible icon in the header of tree
339 //=================================================================================
340 void GEOMToolsGUI_ReduceStudyDlg::checkVisibleIcon( QTreeWidget* theWidget )
341 {
342   bool isInvisible = false;
343   QTreeWidgetItemIterator it( theWidget );
344   while(*it) {
345     GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
346     const char* entry = item->getStudyEntry();
347     if( item->flags() & Qt::ItemIsSelectable )
348       if( !item->isVisible() )
349         isInvisible = true;
350     ++it;
351   }
352
353   if( isInvisible ) {
354     theWidget->headerItem()->setIcon( 1, myInvisible );
355     myMapTreeSelectAll[ theWidget ] = false;
356   }
357   else {
358     theWidget->headerItem()->setIcon( 1, myVisible );
359     myMapTreeSelectAll[ theWidget ] = true;
360   }
361 }
362
363 //=================================================================================
364 // function : isObjectDrawable()
365 // purpose  : return true if object is drawable, and false if object is hidden in the study
366 //=================================================================================
367 bool GEOMToolsGUI_ReduceStudyDlg::isObjectDrawable( std::string theStudyEntry )
368 {
369   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
370   //If object hasn't "AttributeDrawable" => it visible
371   bool isDrawable = true;
372   _PTR(SObject) SO ( myStudy->FindObjectID( theStudyEntry ) );
373   _PTR(GenericAttribute) anAttr;
374   if ( SO && SO->FindAttribute( anAttr, "AttributeDrawable" ) ) {
375     _PTR(AttributeDrawable) aDrw (anAttr);
376     isDrawable = aDrw->IsDrawable();
377   }
378   return isDrawable;
379 }
380
381 //=================================================================================
382 // function : unpublishObjects()
383 // purpose  : unpublish(hide) objects in the study
384 //=================================================================================
385 void GEOMToolsGUI_ReduceStudyDlg::unpublishObjects( std::set<std::string>& theObjects )
386 {
387   _PTR(StudyBuilder) aStudyBuilder = myStudy->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( myStudy->StudyId(),
392                                                                                      objectEntry.c_str() );
393     std::string studyEntry = GeomBaseObject->GetStudyEntry();
394     if ( studyEntry == "" || !isObjectDrawable( studyEntry ) )
395       continue;
396     _PTR(SObject) obj ( myStudy->FindObjectID( studyEntry.c_str() ) );
397     _PTR(GenericAttribute) anAttr;
398     if ( obj ) {
399       _PTR(AttributeDrawable) aDrw = aStudyBuilder->FindOrCreateAttribute( obj, "AttributeDrawable" );
400       aDrw->SetDrawable( false );
401       myDisplayer.EraseWithChildren( new SALOME_InteractiveObject( studyEntry.c_str(), "GEOM", "TEMP_IO" ) );
402       // hide references if any
403       std::vector< _PTR(SObject) > vso = myStudy->FindDependances(obj);
404       for ( int i = 0; i < vso.size(); i++ ) {
405         _PTR(SObject) refObj = vso[i];
406         aDrw = aStudyBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
407         aDrw->SetDrawable( false );
408       }
409     }
410   }
411   myApp->updateObjectBrowser( false );
412   myApp->updateActions();
413 }
414
415 //=================================================================================
416 // function : removeObjects()
417 // purpose  : remove objects from the study
418 //=================================================================================
419 void GEOMToolsGUI_ReduceStudyDlg::removeObjects( std::set<std::string>& theObjects )
420 {
421   std::set<std::string>::iterator it;
422   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
423     std::string objectEntry = *it;
424     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( myStudy->StudyId(),
425                                                                                      objectEntry.c_str() );
426     std::string studyEntry = GeomBaseObject->GetStudyEntry();
427     if ( studyEntry == "" )
428       GeometryGUI::GetGeomGen()->RemoveObject( GeomBaseObject );
429     else {
430       if( !isObjectDrawable( studyEntry ) )
431         continue;
432       removeObject( studyEntry );
433     }
434   }
435   myApp->updateObjectBrowser( false );
436   myApp->updateActions();
437 }
438
439 //=================================================================================
440 // function : removeObject()
441 // purpose  : remove object with given study entry
442 //=================================================================================
443 void GEOMToolsGUI_ReduceStudyDlg::removeObject( std::string& theStudyEntry )
444 {
445   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
446   _PTR(StudyBuilder) aStudyBuilder = myStudy->NewBuilder();
447   _PTR(UseCaseBuilder) aUseCaseBuilder = myStudy->GetUseCaseBuilder();
448
449   _PTR(SObject) obj ( myStudy->FindObjectID( theStudyEntry.c_str() ) );
450   if ( obj ) {
451     // remove visual properties of the object
452     appStudy->removeObjectProperties(obj->GetID().c_str());
453     // remove references to this object
454     appStudy->deleteReferencesTo( obj );
455     // remove objects from study
456     aStudyBuilder->RemoveObjectWithChildren( obj );
457     // remove object from use case tree
458     aUseCaseBuilder->Remove( obj );
459     myDisplayer.EraseWithChildren( new SALOME_InteractiveObject( theStudyEntry.c_str(), "GEOM", "TEMP_IO" ) );
460   }
461 }
462
463 //=================================================================================
464 // function : removeEmptyFolders()
465 // purpose  : remove empty folders from the study
466 //=================================================================================
467 void GEOMToolsGUI_ReduceStudyDlg::removeEmptyFolders()
468 {
469   std::set<std::string> emptyFolders;
470
471   _PTR(SComponent) SC ( myStudy->FindComponent( "GEOM" ) );
472   if ( !SC )
473     return;
474   _PTR(ChildIterator) anIter ( myStudy->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 = myStudy->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  myStudyEntry( studyEntry ),
707  myVisible( false )
708 {
709 }
710
711 GEOMToolsGUI_TreeWidgetItem::GEOMToolsGUI_TreeWidgetItem( QTreeWidgetItem* parent, const QStringList &strings,
712                                                           char* studyEntry, int type )
713 :QTreeWidgetItem( parent, strings, type ),
714  myStudyEntry( studyEntry ),
715  myVisible( false )
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 }