Salome HOME
1765f9d0600f26f83a3d212f107196b35e48d481
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_PublishDlg.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOMToolsGUI_PublishDlg.cxx
25 // Author : Roman NIKOLAEV, Open CASCADE S.A.S.
26 //
27
28 #include "GEOMToolsGUI_PublishDlg.h"
29 #include "GEOM_Displayer.h"
30 #include "GeometryGUI.h"
31
32 //SALOME GUI includes
33 #include <SUIT_Session.h>
34 #include <SUIT_DataObject.h>
35 #include <SUIT_ResourceMgr.h>
36
37 #include <CAM_DataModel.h>
38
39 #include <SalomeApp_DataObject.h>
40
41 #include <SalomeApp_Application.h>
42 #include <SalomeApp_Module.h>
43 #include <SalomeApp_Study.h>
44
45
46 //QT includes
47 #include <QHeaderView>
48 #include <QGridLayout>
49 #include <QTreeWidget>
50 #include <QPushButton>
51
52 #define  DEFAULT_MARGIN 11
53 #define  DEFAULT_SPACING 6
54
55 #define VISIBILITY_COLUMN_WIDTH 25
56
57 GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
58   QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
59 {
60    setObjectName( "GEOMToolsGUI_PublishDlg" );
61    setModal( false );
62    setWindowTitle( tr( "GEOM_PUBLISH_OBJECTS_TLT" ) );
63
64    setAttribute(Qt::WA_DeleteOnClose);
65    
66    //List of the objects
67    myTreeWidget = new QTreeWidget(this);
68    //myTreeWidget->setRootIsDecorated(false);
69    //myTreeWidget->setUniformRowHeights(true);
70    myTreeWidget->setAllColumnsShowFocus(true);
71    myTreeWidget->setColumnCount(3);
72
73    QStringList columnNames;
74    columnNames.append(tr("OBJECT_NAME"));
75    columnNames.append(tr("OBJECT_ENTRY"));
76    columnNames.append("");
77    QTreeWidgetItem * headerItem = new QTreeWidgetItem(columnNames);
78    SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
79    headerItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
80    myTreeWidget->setHeaderItem ( headerItem );
81    myTreeWidget->header()->setSectionsClickable( true );
82    myTreeWidget->header()->setSortIndicatorShown( true );
83    myTreeWidget->header()->moveSection( 2, 0 );
84    myTreeWidget->setSortingEnabled( true );
85    myTreeWidget->setColumnWidth( 2, VISIBILITY_COLUMN_WIDTH );
86
87    //Select All button
88    QPushButton* selectAllButton = new  QPushButton( tr("SELECT_ALL"),this );
89
90    //Unselect Select All button
91    QPushButton* unselectAllButton = new  QPushButton( tr("UNSELECT_ALL"),this );
92
93    //Publish button
94    QPushButton* publishButton = new QPushButton( tr("GEOM_PUBLISH_BTN"),this );
95
96    //Publish and Close button
97    QPushButton* publishCloseButton = new QPushButton( tr("GEOM_PUBLISH_CLOSE_BTN"), this );
98    
99    //Close button
100    QPushButton* closeButton = new QPushButton( tr("GEOM_BUT_CLOSE"), this );
101    
102    QGridLayout* gridLayout = new QGridLayout(this);
103    gridLayout->setMargin(DEFAULT_MARGIN);
104    gridLayout->setSpacing(DEFAULT_SPACING);
105
106    gridLayout->addWidget(myTreeWidget, 0, 0, 3, 3);
107    gridLayout->addWidget(selectAllButton, 0, 3, 1, 1);
108    gridLayout->addWidget(unselectAllButton, 1, 3, 1, 1);
109    gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 2, 3, 1, 1);
110    gridLayout->addWidget(publishCloseButton, 3, 0, 1, 1); 
111    gridLayout->addWidget(publishButton, 3, 1, 1, 1);
112    gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 3, 2, 1, 1);
113    gridLayout->addWidget(closeButton, 3, 3, 1, 1);
114
115    //Connect signals and slots
116    connect( myTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(onItemClicked(QTreeWidgetItem*, int)) );
117
118    connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
119    connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
120    
121    connect(publishCloseButton, SIGNAL(clicked()), this, SLOT(clickOnOk()));
122    connect(publishButton, SIGNAL(clicked()), this, SLOT(clickOnApply()));
123    connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
124
125    initData();
126 }
127
128 //=================================================================================
129 // function : ~GEOMToolsGUI_PublishDlg()
130 // purpose  : Destroys the object and frees any allocated resources
131 //=================================================================================
132 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
133 {
134   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
135   if ( app ) {
136     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
137     if ( appStudy ) {
138       _PTR(Study) aStudy = appStudy->studyDS();
139       if ( aStudy ) {
140         GEOM_Displayer displayer;
141             QTreeWidgetItemIterator it( myTreeWidget );
142             while ( *it ) {
143               QString entry = myEntryToItem.key( *it );
144               _PTR(SObject) SO ( aStudy->FindObjectID( qUtf8Printable( entry ) ) );
145               if ( SO ) {
146                 GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
147                 if ( CORBA::is_nil( aGeomObject ) ) continue;
148                 if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
149                   Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
150                   displayer.Erase( io );
151                 }
152               }
153               ++it;
154             }
155             displayer.UpdateViewer();
156       }
157     }
158   }
159 }
160
161 //=================================================================================
162 // function : initData()
163 // purpose  : Fill dialog after opening
164 //=================================================================================
165 void GEOMToolsGUI_PublishDlg::initData() {
166   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
167   if(!app)
168     return;
169   
170   SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>(app->activeModule());
171   
172   if(!module)
173     return;
174
175   CAM_DataModel* dataModel = module->dataModel();
176   if(!dataModel)
177     return;
178   
179   myGeomRoot = dynamic_cast<SalomeApp_DataObject*>(dataModel->root());
180
181   if(!myGeomRoot)
182     return;
183
184   buildTree(myGeomRoot);
185
186   myTreeWidget->resizeColumnToContents(0);
187   myTreeWidget->resizeColumnToContents(1);
188   myTreeWidget->sortByColumn(0, Qt::AscendingOrder);
189 }
190
191 //=================================================================================
192 // function : createItem()
193 // purpose  :
194 //=================================================================================
195 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
196   QStringList aList;
197   aList<<theAttributes.second<<theAttributes.first<<"";
198   QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
199   
200   if(isCheckable)
201     anItem->setCheckState(0, Qt::Unchecked);
202   else {
203     anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
204   }
205   
206   if(theParent)
207     theParent->addChild(anItem);
208   else 
209     myTreeWidget->addTopLevelItem(anItem);
210
211   myEntryToItem.insert(theAttributes.first, anItem);
212   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
213   anItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
214   anItem->setExpanded(true);
215   return anItem;
216 }
217
218 //=================================================================================
219 // function : findParentItem()
220 // purpose  :
221 //=================================================================================
222 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(SalomeApp_DataObject* theObject, BufferedList& theList ) {
223   
224   QTreeWidgetItem* aResult = NULL;
225   SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
226   if(aParrent) {
227     QString targetEntry = aParrent->entry();
228     if( !(aResult = myEntryToItem.value(targetEntry)) ) {
229       if( aParrent != myGeomRoot ) {
230         QString aName;
231         _PTR(SObject) aSO ( SalomeApp_Application::getStudy()->FindObjectID(qUtf8Printable(aParrent->entry())));
232         _PTR(GenericAttribute) anAttr;
233         if ( aSO->FindAttribute(anAttr, "AttributeName") ) {
234           _PTR(AttributeName) anAttrName (anAttr);
235           aName = anAttrName->Value().c_str();
236         }
237         theList.push_front(qMakePair(targetEntry, aName));
238         aResult = findParentItem(aParrent,theList);
239       } else {
240         //Publish List
241         for(int i = 0; i < theList.size(); i++ ) {
242           aResult = createItem(aResult, theList[i], false);
243         }
244         theList.clear();
245       }
246     } else {
247       //Publish List
248       for(int i = 0; i < theList.size(); i++ ) {
249         aResult = createItem(aResult, theList[i], false);
250       }
251       theList.clear();
252     }
253   }
254   return aResult;
255 }
256
257 //=================================================================================
258 // function : buildTree()
259 // purpose  :
260 //=================================================================================
261 void GEOMToolsGUI_PublishDlg::buildTree(SalomeApp_DataObject* theItem) {
262   if(!theItem || theItem->isReference())
263     return;
264   
265   if(theItem != myGeomRoot) {
266
267     //If object hasn't "AttributeDrawable" => it visible
268     bool isDrawable = true;
269     _PTR(SObject) SO ( SalomeApp_Application::getStudy()->FindObjectID(qUtf8Printable(theItem->entry())));
270     _PTR(GenericAttribute) anAttr;
271     if ( SO && SO->FindAttribute(anAttr, "AttributeDrawable") ) {
272       _PTR(AttributeDrawable) aDrw (anAttr);
273       isDrawable = aDrw->IsDrawable();
274     }
275     
276     if(!isDrawable) {
277       QString aName;
278       if ( SO->FindAttribute(anAttr, "AttributeName") ) {
279         _PTR(AttributeName) aAttrName (anAttr);
280         aName = aAttrName->Value().c_str();
281       }
282       BufferedList aList;
283       QTreeWidgetItem* parentItem = findParentItem(theItem, aList);
284       createItem(parentItem,qMakePair(theItem->entry(),aName),true);
285     }
286   }
287   
288   DataObjectList listObj = theItem->children( false );
289   DataObjectList::iterator itr = listObj.begin();
290   while( itr != listObj.end()) {
291     buildTree(dynamic_cast<SalomeApp_DataObject*>(*itr));
292     itr++;
293   }
294 }
295
296 //=================================================================================
297 // function : clickOnApply()
298 // purpose  : Called then "Publish" button clicked
299 //=================================================================================
300 void GEOMToolsGUI_PublishDlg::clickOnApply() {
301   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
302   if(!app)
303     return;
304   
305   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
306   if(!appStudy ) 
307     return;
308
309   _PTR(Study) aStudy = appStudy->studyDS();
310   if(!aStudy)
311     return;
312
313
314   QList<QTreeWidgetItem*> toProcess;
315   getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
316
317   _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
318   for( int i = 0; i < toProcess.count(); i++) {
319     QTreeWidgetItem* item = toProcess[i];
320     if(item) {
321       QString entry = myEntryToItem.key(item);
322       _PTR(SObject) SO ( aStudy->FindObjectID(qUtf8Printable(entry)) );
323       if(!SO) continue;
324       _PTR(AttributeDrawable) aDrw = aBuilder->FindOrCreateAttribute( SO, "AttributeDrawable" );
325       aDrw->SetDrawable( true );
326       //Remove or change item
327       if( item != myTreeWidget->invisibleRootItem() ){
328         if( item->data(0,Qt::UserRole).toBool() ) {
329           delete item;
330         } else {
331           item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
332           item->setData(0,Qt::CheckStateRole,QVariant());
333         }
334       }
335       // show references if any
336       std::vector< _PTR(SObject) > vso = aStudy->FindDependances(SO);
337       for ( size_t i = 0; i < vso.size(); i++ ) {
338         _PTR(SObject) refObj = vso[i];
339         aDrw = aBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
340         aDrw->SetDrawable( true );
341       }
342     }
343   }
344   toProcess.clear();
345   getTails(myTreeWidget->invisibleRootItem(), toProcess);
346   for( int i = 0; i < toProcess.count(); i++) {
347     if( toProcess[i] != myTreeWidget->invisibleRootItem() )
348       delete toProcess[i];
349   }
350       
351   app->updateObjectBrowser( false );
352   app->updateActions();
353 }
354
355 //=================================================================================
356 // function : clickOnOk()
357 // purpose  : Called then "Publish And Close" button clicked
358 //=================================================================================
359 void GEOMToolsGUI_PublishDlg::clickOnOk() {
360   clickOnApply();
361   accept();
362 }
363
364 //=================================================================================
365 // function : clickOnSelectAll()
366 // purpose  : Called then "Select All" button clicked
367 //=================================================================================
368 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
369   processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
370 }
371
372 //=================================================================================
373 // function : clickOnUnSelectAll()
374 // purpose  : Called then "Unselect All" button clicked
375 //=================================================================================
376 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
377   processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
378 }
379
380 //=================================================================================
381 // function : processAll()
382 // purpose  : 
383 //=================================================================================
384 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
385   
386     
387   if((theItem->flags() & Qt::ItemIsUserCheckable))
388     theItem->setCheckState(0,state);
389   
390   int i = 0;
391   while(i < theItem->childCount()) {
392     processAll(theItem->child(i), state);
393     i++;
394   }
395 }
396
397 //=================================================================================
398 // function : processAll()
399 // purpose  : 
400 //=================================================================================
401 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
402   
403   theItem->setData(0, Qt::UserRole, true);
404
405   if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
406     theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
407   }
408   
409   int i = 0;
410   while(i < theItem->childCount()) {
411     getDrawableList(theItem->child(i), theList);
412     theItem->setData( 0, Qt::UserRole, (theItem->data(0,Qt::UserRole).toBool() && theItem->child(i)->data(0,Qt::UserRole).toBool()) );
413     i++;
414   } 
415
416   if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
417     theList.push_back(theItem);
418 }
419
420
421 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
422   int i = 0;
423   while(i < theItem->childCount()) {
424     getTails(theItem->child(i),theList);
425     i++;
426   }
427
428   if(theItem->data(0,Qt::UserRole).toBool())
429     theList.push_back(theItem);
430 }
431
432 //=================================================================================
433 // function : onItemClicked()
434 // purpose  : Called then treeItem clicked
435 //=================================================================================
436 void GEOMToolsGUI_PublishDlg::onItemClicked(QTreeWidgetItem* theItem, int theColumn) {
437   if ( theColumn == 2 ) {
438     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
439     if ( !app ) return;
440     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
441     if ( !appStudy ) return;
442     _PTR(Study) aStudy = appStudy->studyDS();
443     if ( !aStudy ) return;
444     QString entry = myEntryToItem.key( theItem );
445     _PTR(SObject) SO ( aStudy->FindObjectID( qUtf8Printable( entry ) ) );
446     if ( !SO ) return;
447     GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
448     if ( CORBA::is_nil( aGeomObject ) ) return;
449     GEOM_Displayer displayer;
450     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
451     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
452     if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
453       theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
454       displayer.Erase( io );
455     }
456     else {
457       theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
458       displayer.Display( io );
459     }
460     displayer.UpdateViewer();
461   }
462 }
463