]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMToolsGUI/GEOMToolsGUI_ReduceStudyDlg.cxx
Salome HOME
Merge multi-study removal branch.
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_ReduceStudyDlg.cxx
1 // Copyright (C) 2014-2016  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 {
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 ( int i = 0; i < keptObjects->length(); i++ )
169     myKeptObjects.insert( keptObjects[i].in() );
170   for( int i = 0; i< otherObjects->length(); i++ )
171     myRemovedObjects.insert( otherObjects[i].in() );
172   for( int i = 0; i< parentsObjects->length(); i++ )
173     myListParents.insert( parentsObjects[i].in() );
174   for( int 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 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
223   theTreeWidget->header()->setClickable( true );
224   theTreeWidget->header()->setMovable( false );
225   theTreeWidget->header()->setResizeMode( 1, QHeaderView::ResizeToContents );
226 #else
227   theTreeWidget->header()->setSectionsClickable( true );
228   theTreeWidget->header()->setSectionsMovable( false );
229   theTreeWidget->header()->setSectionResizeMode( 1, QHeaderView::ResizeToContents );
230 #endif
231   theTreeWidget->setSelectionMode( QAbstractItemView::ExtendedSelection );
232 }
233
234 //=================================================================================
235 // function : createButtonGroup()
236 // purpose  : create button group for intermediate objects or sub-objects
237 //=================================================================================
238 QGroupBox* GEOMToolsGUI_ReduceStudyDlg::createButtonGroup( QButtonGroup* theButtonGroup )
239 {
240   QGroupBox* groupObjects = new QGroupBox();
241   QHBoxLayout* layoutObjects = new QHBoxLayout( groupObjects );
242
243   QRadioButton* buttonKeep = new QRadioButton( tr( "GEOM_REDUCE_STUDY_KEEP") );
244   theButtonGroup->addButton( buttonKeep, 0 );
245   QRadioButton* buttonUnpublish = new QRadioButton( tr( "GEOM_REDUCE_STUDY_UNPUBLISH") );
246   theButtonGroup->addButton( buttonUnpublish, 1 );
247   QRadioButton* buttonRemove = new QRadioButton( tr( "GEOM_REDUCE_STUDY_REMOVE") );
248   theButtonGroup->addButton( buttonRemove, 2 );
249
250   theButtonGroup->button( 0 )->setChecked( true );
251   theButtonGroup->setExclusive( true );
252
253   layoutObjects->addWidget( buttonKeep );
254   layoutObjects->addWidget( buttonUnpublish );
255   layoutObjects->addWidget( buttonRemove );
256
257   return groupObjects;
258 }
259
260 //=================================================================================
261 // function : addObjectsToTree()
262 // purpose  : add the list of objects to tree
263 //=================================================================================
264 void GEOMToolsGUI_ReduceStudyDlg::addObjectsToTree( QTreeWidget* theWidget, std::set<std::string>& theObjects )
265 {
266   std::set<std::string>::iterator it;
267   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
268     std::string objectEntry = *it;
269     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( objectEntry.c_str() );
270     GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( GeomBaseObject );
271     QString studyEntry = GeomBaseObject->GetStudyEntry();
272     if( GeomObject->_is_nil() || studyEntry.isEmpty() || !isObjectDrawable( studyEntry.toStdString() ) )
273       continue;
274     addSubObject( theWidget, theObjects, GeomObject );
275   }
276 }
277
278 //=================================================================================
279 // function : addSubObject()
280 // purpose  : add sub-object to parent object in the tree
281 //=================================================================================
282 GEOMToolsGUI_TreeWidgetItem* GEOMToolsGUI_ReduceStudyDlg::addSubObject( QTreeWidget* theWidget,
283                                                                         std::set<std::string>& theObjects,
284                                                                         GEOM::GEOM_Object_var theObject )
285 {
286   GEOMToolsGUI_TreeWidgetItem* item;
287   if( !theObject->IsMainShape() ) {
288     GEOM::GEOM_Object_var aMainShape = theObject->GetMainShape();
289     if ( CORBA::is_nil( aMainShape ) )
290       return NULL;
291     GEOMToolsGUI_TreeWidgetItem* parentItem = addSubObject( theWidget, theObjects, aMainShape );
292     item = findObjectInTree( theWidget, theObject );
293     if( !item )
294       item = new GEOMToolsGUI_TreeWidgetItem( parentItem, QStringList() << theObject->GetName(), theObject->GetStudyEntry() );
295   }
296   else {
297     item = findObjectInTree( theWidget, theObject );
298     if( !item )
299       item = new GEOMToolsGUI_TreeWidgetItem( theWidget, QStringList() << theObject->GetName(), theObject->GetStudyEntry() );
300   }
301
302   bool isDisplayed = false;
303   if( theObjects.find( theObject->GetEntry() ) != theObjects.end() ) {
304     isDisplayed = myDisplayer.IsDisplayed( theObject->GetStudyEntry() );
305     if ( isDisplayed )
306       item->setVisible( true, myVisible );
307     else
308       item->setVisible( false, myInvisible );
309
310     if( myMainEntries.find( theObject->GetEntry() ) != myMainEntries.end() ) {
311       QFont Textfont = item->font(0);
312       Textfont.setBold( true );
313       item->setFont( 0, Textfont );
314     }
315   }
316   else {
317     item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
318     item->setTextColor( 0, QColor( 150, 150, 150 ) );
319   }
320   return item;
321 }
322
323 //=================================================================================
324 // function : findObjectInTree()
325 // purpose  : find object in the tree
326 //=================================================================================
327 GEOMToolsGUI_TreeWidgetItem* GEOMToolsGUI_ReduceStudyDlg::findObjectInTree( QTreeWidget* theWidget, GEOM::GEOM_Object_var theObject )
328 {
329   QTreeWidgetItemIterator it( theWidget );
330   while(*it) {
331     GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
332     if( QString( item->getStudyEntry() ) == QString( theObject->GetStudyEntry() ) )
333       return item;
334     ++it;
335   }
336   return NULL;
337 }
338
339 //=================================================================================
340 // function : checkVisibleIcon()
341 // purpose  : set visible or invisible icon in the header of tree
342 //=================================================================================
343 void GEOMToolsGUI_ReduceStudyDlg::checkVisibleIcon( QTreeWidget* theWidget )
344 {
345   bool isInvisible = false;
346   QTreeWidgetItemIterator it( theWidget );
347   while(*it) {
348     GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
349     //const char* entry = item->getStudyEntry();
350     if( item->flags() & Qt::ItemIsSelectable )
351       if( !item->isVisible() )
352         isInvisible = true;
353     ++it;
354   }
355
356   if( isInvisible ) {
357     theWidget->headerItem()->setIcon( 1, myInvisible );
358     myMapTreeSelectAll[ theWidget ] = false;
359   }
360   else {
361     theWidget->headerItem()->setIcon( 1, myVisible );
362     myMapTreeSelectAll[ theWidget ] = true;
363   }
364 }
365
366 //=================================================================================
367 // function : isObjectDrawable()
368 // purpose  : return true if object is drawable, and false if object is hidden in the study
369 //=================================================================================
370 bool GEOMToolsGUI_ReduceStudyDlg::isObjectDrawable( std::string theStudyEntry )
371 {
372   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
373   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
374   //If object hasn't "AttributeDrawable" => it visible
375   bool isDrawable = true;
376   _PTR(SObject) SO ( aStudy->FindObjectID( theStudyEntry ) );
377   _PTR(GenericAttribute) anAttr;
378   if ( SO && SO->FindAttribute( anAttr, "AttributeDrawable" ) ) {
379     _PTR(AttributeDrawable) aDrw (anAttr);
380     isDrawable = aDrw->IsDrawable();
381   }
382   return isDrawable;
383 }
384
385 //=================================================================================
386 // function : unpublishObjects()
387 // purpose  : unpublish(hide) objects in the study
388 //=================================================================================
389 void GEOMToolsGUI_ReduceStudyDlg::unpublishObjects( std::set<std::string>& theObjects )
390 {
391   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
392   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
393   std::set<std::string>::iterator it;
394   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
395     std::string objectEntry = *it;
396     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( objectEntry.c_str() );
397     std::string studyEntry = GeomBaseObject->GetStudyEntry();
398     if ( studyEntry == "" || !isObjectDrawable( studyEntry ) )
399       continue;
400     _PTR(SObject) obj ( aStudy->FindObjectID( studyEntry.c_str() ) );
401     _PTR(GenericAttribute) anAttr;
402     if ( obj ) {
403       _PTR(AttributeDrawable) aDrw = aStudyBuilder->FindOrCreateAttribute( obj, "AttributeDrawable" );
404       aDrw->SetDrawable( false );
405       myDisplayer.EraseWithChildren( new SALOME_InteractiveObject( studyEntry.c_str(), "GEOM", "TEMP_IO" ) );
406       // hide references if any
407       std::vector< _PTR(SObject) > vso = aStudy->FindDependances(obj);
408       for ( int i = 0; i < vso.size(); i++ ) {
409         _PTR(SObject) refObj = vso[i];
410         aDrw = aStudyBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
411         aDrw->SetDrawable( false );
412       }
413     }
414   }
415   myApp->updateObjectBrowser( false );
416   myApp->updateActions();
417 }
418
419 //=================================================================================
420 // function : removeObjects()
421 // purpose  : remove objects from the study
422 //=================================================================================
423 void GEOMToolsGUI_ReduceStudyDlg::removeObjects( std::set<std::string>& theObjects )
424 {
425   std::set<std::string>::iterator it;
426   for( it = theObjects.begin(); it != theObjects.end(); ++it ) {
427     std::string objectEntry = *it;
428     GEOM::GEOM_BaseObject_var GeomBaseObject = GeometryGUI::GetGeomGen()->GetObject( objectEntry.c_str() );
429     std::string studyEntry = GeomBaseObject->GetStudyEntry();
430     if ( studyEntry == "" )
431       GeometryGUI::GetGeomGen()->RemoveObject( GeomBaseObject );
432     else {
433       if( !isObjectDrawable( studyEntry ) )
434         continue;
435       removeObject( studyEntry );
436     }
437   }
438   myApp->updateObjectBrowser( false );
439   myApp->updateActions();
440 }
441
442 //=================================================================================
443 // function : removeObject()
444 // purpose  : remove object with given study entry
445 //=================================================================================
446 void GEOMToolsGUI_ReduceStudyDlg::removeObject( std::string& theStudyEntry )
447 {
448   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
449   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
450   _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
451   _PTR(UseCaseBuilder) aUseCaseBuilder = aStudy->GetUseCaseBuilder();
452
453   _PTR(SObject) obj ( aStudy->FindObjectID( theStudyEntry.c_str() ) );
454   if ( obj ) {
455     // remove visual properties of the object
456     appStudy->removeObjectProperties(obj->GetID().c_str());
457     // remove references to this object
458     appStudy->deleteReferencesTo( obj );
459     // remove objects from study
460     aStudyBuilder->RemoveObjectWithChildren( obj );
461     // remove object from use case tree
462     aUseCaseBuilder->Remove( obj );
463     myDisplayer.EraseWithChildren( new SALOME_InteractiveObject( theStudyEntry.c_str(), "GEOM", "TEMP_IO" ) );
464   }
465 }
466
467 //=================================================================================
468 // function : removeEmptyFolders()
469 // purpose  : remove empty folders from the study
470 //=================================================================================
471 void GEOMToolsGUI_ReduceStudyDlg::removeEmptyFolders()
472 {
473   std::set<std::string> emptyFolders;
474
475   _PTR(Study) aStudy = SalomeApp_Application::getStudy();
476   _PTR(SComponent) SC ( aStudy->FindComponent( "GEOM" ) );
477   if ( !SC )
478     return;
479   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( SC ) );
480   anIter->InitEx( true );
481   while( anIter->More() ) {
482     _PTR(SObject) valSO ( anIter->Value() );
483     _PTR(SObject) refSO;
484     if ( !valSO->ReferencedObject( refSO ) )
485       getEmptyFolders( valSO, emptyFolders );
486     anIter->Next();
487   }
488
489   std::set<std::string>::iterator iter;
490   for( iter = emptyFolders.begin(); iter != emptyFolders.end(); ++iter ) {
491     std::string studyEntry = *iter;
492     removeObject( studyEntry );
493   }
494   myApp->updateObjectBrowser( false );
495   myApp->updateActions();
496 }
497
498 //=================================================================================
499 // function : removeEmptyFolders()
500 // purpose  : remove empty folders from the study
501 //=================================================================================
502 void GEOMToolsGUI_ReduceStudyDlg::getEmptyFolders( _PTR(SObject) theSO, std::set<std::string>& theFolders )
503 {
504   _PTR(UseCaseBuilder) aUseCaseBuilder = SalomeApp_Application::getStudy()->GetUseCaseBuilder();
505
506   bool isFolder = false;
507   _PTR(GenericAttribute) anAttr;
508   if ( theSO->FindAttribute(anAttr, "AttributeLocalID") ) {
509     _PTR(AttributeLocalID) aLocalID( anAttr );
510     isFolder = aLocalID->Value() == 999;
511   }
512   QString studyEntry = theSO->GetID().c_str();
513   if ( isFolder ) {
514     if( !aUseCaseBuilder->HasChildren( theSO ) )
515       theFolders.insert( studyEntry.toStdString() );
516     else {
517       _PTR(UseCaseIterator) ucit ( aUseCaseBuilder->GetUseCaseIterator( theSO ) );
518       for ( ucit->Init( false ); ucit->More(); ucit->Next() )
519         getEmptyFolders( ucit->Value(), theFolders );
520     }
521   }
522 }
523
524 //=================================================================================
525 // function : onItemClicked()
526 // purpose  : called when tree item was clicked
527 //=================================================================================
528 void GEOMToolsGUI_ReduceStudyDlg::onItemClicked( QTreeWidgetItem* theItem, int theColumn )
529 {
530   if( theColumn != 1 || !( theItem->flags() & Qt::ItemIsSelectable ) )
531     return;
532
533   GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>( theItem );
534
535   const char* entry = item->getStudyEntry();
536   Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry, "GEOM", "TEMP_IO" );
537   if( myDisplayer.IsDisplayed( entry ) ) {
538     item->setVisible( false, myInvisible );
539     myDisplayer.Erase( io );
540   }
541   else {
542     item->setVisible( true, myVisible );
543     myDisplayer.Display( io );
544   }
545   myDisplayer.UpdateViewer();
546   checkVisibleIcon( item->treeWidget() );
547 }
548
549 //=================================================================================
550 // function : onHeaderClicked()
551 // purpose  : called when header item of tree was clicked
552 //=================================================================================
553 void GEOMToolsGUI_ReduceStudyDlg::onHeaderClicked( int theColumn )
554 {
555   if( theColumn != 1 )
556     return;
557
558   QTreeWidget* treeWidget = dynamic_cast<QTreeWidget*>(sender()->parent());
559   if( myMapTreeSelectAll[ treeWidget ] ) {
560     myMapTreeSelectAll[ treeWidget ] = false;
561     treeWidget->headerItem()->setIcon( 1, myInvisible );
562     QTreeWidgetItemIterator it( treeWidget );
563     while(*it) {
564       GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
565       if( ( item->flags() & Qt::ItemIsSelectable ) && item->isVisible() ) {
566         const char* entry = item->getStudyEntry();
567         item->setVisible( false, myInvisible );
568         myDisplayer.Erase( new SALOME_InteractiveObject( entry, "GEOM", "TEMP_IO" ) );
569       }
570       ++it;
571     }
572   }
573   else {
574     myMapTreeSelectAll[ treeWidget ] = true;
575     treeWidget->headerItem()->setIcon( 1, myVisible );
576     QTreeWidgetItemIterator it( treeWidget );
577     while(*it) {
578       GEOMToolsGUI_TreeWidgetItem* item = dynamic_cast<GEOMToolsGUI_TreeWidgetItem*>(*it);
579       if( ( item->flags() & Qt::ItemIsSelectable ) && !item->isVisible() ) {
580         const char* entry = item->getStudyEntry();
581         item->setVisible( true, myVisible );
582         myDisplayer.Display( new SALOME_InteractiveObject( entry, "GEOM", "TEMP_IO" ) );
583       }
584       ++it;
585     }
586   }
587   myDisplayer.UpdateViewer();
588 }
589
590 //=================================================================================
591 // function : selectionChanged()
592 // purpose  : called when selection of object browser was changed
593 //=================================================================================
594 void GEOMToolsGUI_ReduceStudyDlg::selectionChanged()
595 {
596   init( getSelectedObjects() );
597 }
598
599 //=================================================================================
600 // function : update()
601 // purpose  : update tree data
602 //=================================================================================
603 void GEOMToolsGUI_ReduceStudyDlg::update()
604 {
605   myTreeKeptObjects->clear();
606   myTreeRemoveObjects->clear();
607
608   std::set<std::string> keptObjects( myKeptObjects );
609   std::set<std::string> removeObjects( myRemovedObjects );
610
611   // Intermediate objects
612   if( myGroupIntermediates->checkedId() == 2 ) { // remove
613     std::set<std::string>::iterator iter;
614     for( iter=myListParents.begin(); iter!=myListParents.end(); ++iter)
615       removeObjects.insert( *iter );
616   }
617   else { // keep or unpublish
618     std::set<std::string>::iterator iter;
619     for( iter=myListParents.begin(); iter!=myListParents.end(); ++iter)
620       keptObjects.insert( *iter );
621   }
622
623   // Sub-objects
624   if( myGroupSubObjects->checkedId() == 2 ) {
625     std::set<std::string>::iterator iter;
626     for( iter=myListSubObjects.begin(); iter!=myListSubObjects.end(); ++iter)
627       removeObjects.insert( *iter );
628   }
629   else {
630     std::set<std::string>::iterator iter;
631     for( iter=myListSubObjects.begin(); iter!=myListSubObjects.end(); ++iter)
632       keptObjects.insert( *iter );
633   }
634
635   addObjectsToTree( myTreeKeptObjects, keptObjects );
636   addObjectsToTree( myTreeRemoveObjects, removeObjects );
637
638   myTreeKeptObjects->collapseAll();
639   myTreeRemoveObjects->collapseAll();
640
641 }
642
643 //=================================================================================
644 // function : clickOnOk()
645 // purpose  : called when OK button was clicked
646 //=================================================================================
647 void GEOMToolsGUI_ReduceStudyDlg::clickOnOk()
648 {
649   std::set<std::string> objectsToBeRemoved = myRemovedObjects;
650   std::set<std::string> objectsToBeUnpublished;
651
652   // Create lists of intermediate objects to be removed or to be unpublished
653   std::set<std::string>::iterator iter;
654   if( myGroupIntermediates->checkedId() == 1 ) { // unpublish
655     for( iter = myListParents.begin(); iter != myListParents.end(); ++iter )
656     objectsToBeUnpublished.insert( *iter );
657   }
658   if( myGroupIntermediates->checkedId() == 2 ) { // remove
659     if( !myCBSoftRemoval->isChecked() && 
660         SUIT_MessageBox::question( this,
661                                    tr( "GEOM_WRN_WARNING" ),
662                                    tr( "GEOM_REDUCE_STUDY_WARNING_DELETE" ),
663                                    QMessageBox::Yes | QMessageBox::No,
664                                    QMessageBox::Yes ) == QMessageBox::No ) {
665       return;
666     }
667     for( iter = myListParents.begin(); iter != myListParents.end(); ++iter )
668       objectsToBeRemoved.insert( *iter );
669   }
670
671   // Create lists of sub-objects to be removed or to be unpublished
672   if( myGroupSubObjects->checkedId() == 1 ) { // unpublish
673     for( iter = myListSubObjects.begin(); iter != myListSubObjects.end(); ++iter )
674       objectsToBeUnpublished.insert( *iter );
675   }
676   else if( myGroupSubObjects->checkedId() == 2 ) { // remove
677     for( iter = myListSubObjects.begin(); iter != myListSubObjects.end(); ++iter )
678       objectsToBeRemoved.insert( *iter );
679   }
680
681   // if user chosen the soft removal
682   if( myCBSoftRemoval->isChecked() ) {
683     for( iter = objectsToBeRemoved.begin(); iter != objectsToBeRemoved.end(); ++iter )
684       objectsToBeUnpublished.insert( *iter );
685     unpublishObjects( objectsToBeUnpublished );
686   }
687   else {
688     unpublishObjects( objectsToBeUnpublished );
689     removeObjects( objectsToBeRemoved );
690   }
691
692   // if user want to delete the empty folders
693   if( myCBRemoveEmptyFolder->isChecked() )
694     removeEmptyFolders();
695
696   accept();
697 }
698
699 //=================================================================================
700 // function : clickOnHelp()
701 // purpose  : called when Help button was clicked to open a help page
702 //=================================================================================
703 void GEOMToolsGUI_ReduceStudyDlg::clickOnHelp()
704 {
705   myApp->onHelpContextModule( "GEOM", "reduce_study_page.html" );
706 }
707
708 GEOMToolsGUI_TreeWidgetItem::GEOMToolsGUI_TreeWidgetItem( QTreeWidget* view, const QStringList &strings,
709                                                           char* studyEntry, int type )
710 :QTreeWidgetItem( view, strings, type ),
711  myStudyEntry( studyEntry ),
712  myVisible( false )
713 {
714 }
715
716 GEOMToolsGUI_TreeWidgetItem::GEOMToolsGUI_TreeWidgetItem( QTreeWidgetItem* parent, const QStringList &strings,
717                                                           char* studyEntry, int type )
718 :QTreeWidgetItem( parent, strings, type ),
719  myStudyEntry( studyEntry ),
720  myVisible( false )
721 {
722 }
723
724 GEOMToolsGUI_TreeWidgetItem::~GEOMToolsGUI_TreeWidgetItem()
725 {
726 }
727
728 bool GEOMToolsGUI_TreeWidgetItem::isVisible()
729 {
730   return myVisible;
731 }
732
733 void GEOMToolsGUI_TreeWidgetItem::setVisible( bool theIsVisible, QIcon& theIcon )
734 {
735   myVisible = theIsVisible;
736   setIcon( 1, theIcon );
737 }
738
739 char* GEOMToolsGUI_TreeWidgetItem::getStudyEntry() const
740 {
741   return myStudyEntry;
742 }