]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMToolsGUI/GEOMToolsGUI_PublishDlg.cxx
Salome HOME
Merge multi-study removal branch.
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_PublishDlg.cxx
1 // Copyright (C) 2007-2016  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 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
82    myTreeWidget->header()->setClickable( true );
83 #else
84    myTreeWidget->header()->setSectionsClickable( true );
85 #endif
86    myTreeWidget->header()->setSortIndicatorShown( true );
87    myTreeWidget->header()->moveSection( 2, 0 );
88    myTreeWidget->setSortingEnabled( true );
89    myTreeWidget->setColumnWidth( 2, VISIBILITY_COLUMN_WIDTH );
90
91    //Select All button
92    QPushButton* selectAllButton = new  QPushButton( tr("SELECT_ALL"),this );
93
94    //Unselect Select All button
95    QPushButton* unselectAllButton = new  QPushButton( tr("UNSELECT_ALL"),this );
96
97    //Publish button
98    QPushButton* publishButton = new QPushButton( tr("GEOM_PUBLISH_BTN"),this );
99
100    //Publish and Close button
101    QPushButton* publishCloseButton = new QPushButton( tr("GEOM_PUBLISH_CLOSE_BTN"), this );
102    
103    //Close button
104    QPushButton* closeButton = new QPushButton( tr("GEOM_BUT_CLOSE"), this );
105    
106    QGridLayout* gridLayout = new QGridLayout(this);
107    gridLayout->setMargin(DEFAULT_MARGIN);
108    gridLayout->setSpacing(DEFAULT_SPACING);
109
110    gridLayout->addWidget(myTreeWidget, 0, 0, 3, 3);
111    gridLayout->addWidget(selectAllButton, 0, 3, 1, 1);
112    gridLayout->addWidget(unselectAllButton, 1, 3, 1, 1);
113    gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 2, 3, 1, 1);
114    gridLayout->addWidget(publishCloseButton, 3, 0, 1, 1); 
115    gridLayout->addWidget(publishButton, 3, 1, 1, 1);
116    gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 3, 2, 1, 1);
117    gridLayout->addWidget(closeButton, 3, 3, 1, 1);
118
119    //Connect signals and slots
120    connect( myTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(onItemClicked(QTreeWidgetItem*, int)) );
121
122    connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
123    connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
124    
125    connect(publishCloseButton, SIGNAL(clicked()), this, SLOT(clickOnOk()));
126    connect(publishButton, SIGNAL(clicked()), this, SLOT(clickOnApply()));
127    connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
128
129    initData();
130 }
131
132 //=================================================================================
133 // function : ~GEOMToolsGUI_PublishDlg()
134 // purpose  : Destroys the object and frees any allocated resources
135 //=================================================================================
136 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
137 {
138   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
139   if ( app ) {
140     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
141     if ( appStudy ) {
142       _PTR(Study) aStudy = appStudy->studyDS();
143       if ( aStudy ) {
144         GEOM_Displayer displayer;
145             QTreeWidgetItemIterator it( myTreeWidget );
146             while ( *it ) {
147               QString entry = myEntryToItem.key( *it );
148               _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( entry ) ) );
149               if ( SO ) {
150                 GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
151                 if ( CORBA::is_nil( aGeomObject ) ) continue;
152                 if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
153                   Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
154                   displayer.Erase( io );
155                 }
156               }
157               ++it;
158             }
159             displayer.UpdateViewer();
160       }
161     }
162   }
163 }
164
165 //=================================================================================
166 // function : initData()
167 // purpose  : Fill dialog after opening
168 //=================================================================================
169 void GEOMToolsGUI_PublishDlg::initData() {
170   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
171   if(!app)
172     return;
173   
174   SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>(app->activeModule());
175   
176   if(!module)
177     return;
178
179   CAM_DataModel* dataModel = module->dataModel();
180   if(!dataModel)
181     return;
182   
183   myGeomRoot = dynamic_cast<SalomeApp_DataObject*>(dataModel->root());
184
185   if(!myGeomRoot)
186     return;
187
188   buildTree(myGeomRoot);
189
190   myTreeWidget->resizeColumnToContents(0);
191   myTreeWidget->resizeColumnToContents(1);
192   myTreeWidget->sortByColumn(0, Qt::AscendingOrder);
193 }
194
195 //=================================================================================
196 // function : createItem()
197 // purpose  :
198 //=================================================================================
199 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
200   QStringList aList;
201   aList<<theAttributes.second<<theAttributes.first<<"";
202   QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
203   
204   if(isCheckable)
205     anItem->setCheckState(0, Qt::Unchecked);
206   else {
207     anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
208   }
209   
210   if(theParent)
211     theParent->addChild(anItem);
212   else 
213     myTreeWidget->addTopLevelItem(anItem);
214
215   myEntryToItem.insert(theAttributes.first, anItem);
216   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
217   anItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
218   anItem->setExpanded(true);
219   return anItem;
220 }
221
222 //=================================================================================
223 // function : findParentItem()
224 // purpose  :
225 //=================================================================================
226 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(SalomeApp_DataObject* theObject, BufferedList& theList ) {
227   
228   QTreeWidgetItem* aResult = NULL;
229   SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
230   if(aParrent) {
231     QString targetEntry = aParrent->entry();
232     if( !(aResult = myEntryToItem.value(targetEntry)) ) {
233       if( aParrent != myGeomRoot ) {
234         QString aName;
235         _PTR(SObject) aSO ( SalomeApp_Application::getStudy()->FindObjectID(qPrintable(aParrent->entry())));
236         _PTR(GenericAttribute) anAttr;
237         if ( aSO->FindAttribute(anAttr, "AttributeName") ) {
238           _PTR(AttributeName) anAttrName (anAttr);
239           aName = anAttrName->Value().c_str();
240         }
241         theList.push_front(qMakePair(targetEntry, aName));
242         aResult = findParentItem(aParrent,theList);
243       } else {
244         //Publish List
245         for(int i = 0; i < theList.size(); i++ ) {
246           aResult = createItem(aResult, theList[i], false);
247         }
248         theList.clear();
249       }
250     } else {
251       //Publish List
252       for(int i = 0; i < theList.size(); i++ ) {
253         aResult = createItem(aResult, theList[i], false);
254       }
255       theList.clear();
256     }
257   }
258   return aResult;
259 }
260
261 //=================================================================================
262 // function : buildTree()
263 // purpose  :
264 //=================================================================================
265 void GEOMToolsGUI_PublishDlg::buildTree(SalomeApp_DataObject* theItem) {
266   if(!theItem || theItem->isReference())
267     return;
268   
269   if(theItem != myGeomRoot) {
270
271     //If object hasn't "AttributeDrawable" => it visible
272     bool isDrawable = true;
273     _PTR(SObject) SO ( SalomeApp_Application::getStudy()->FindObjectID(qPrintable(theItem->entry())));
274     _PTR(GenericAttribute) anAttr;
275     if ( SO && SO->FindAttribute(anAttr, "AttributeDrawable") ) {
276       _PTR(AttributeDrawable) aDrw (anAttr);
277       isDrawable = aDrw->IsDrawable();
278     }
279     
280     if(!isDrawable) {
281       QString aName;
282       if ( SO->FindAttribute(anAttr, "AttributeName") ) {
283         _PTR(AttributeName) aAttrName (anAttr);
284         aName = aAttrName->Value().c_str();
285       }
286       BufferedList aList;
287       QTreeWidgetItem* parentItem = findParentItem(theItem, aList);
288       createItem(parentItem,qMakePair(theItem->entry(),aName),true);
289     }
290   }
291   
292   DataObjectList listObj = theItem->children( false );
293   DataObjectList::iterator itr = listObj.begin();
294   while( itr != listObj.end()) {
295     buildTree(dynamic_cast<SalomeApp_DataObject*>(*itr));
296     itr++;
297   }
298 }
299
300 //=================================================================================
301 // function : clickOnApply()
302 // purpose  : Called then "Publish" button clicked
303 //=================================================================================
304 void GEOMToolsGUI_PublishDlg::clickOnApply() {
305   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
306   if(!app)
307     return;
308   
309   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
310   if(!appStudy ) 
311     return;
312
313   _PTR(Study) aStudy = appStudy->studyDS();
314   if(!aStudy)
315     return;
316
317
318   QList<QTreeWidgetItem*> toProcess;
319   getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
320
321   _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
322   for( int i = 0; i < toProcess.count(); i++) {
323     QTreeWidgetItem* item = toProcess[i];
324     if(item) {
325       QString entry = myEntryToItem.key(item);
326       _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
327       if(!SO) continue;
328       _PTR(AttributeDrawable) aDrw = aBuilder->FindOrCreateAttribute( SO, "AttributeDrawable" );
329       aDrw->SetDrawable( true );
330       //Remove or change item
331       if( item != myTreeWidget->invisibleRootItem() ){
332         if( item->data(0,Qt::UserRole).toBool() ) {
333           delete item;
334         } else {
335           item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
336           item->setData(0,Qt::CheckStateRole,QVariant());
337         }
338       }
339       // show references if any
340       std::vector< _PTR(SObject) > vso = aStudy->FindDependances(SO);
341       for ( size_t i = 0; i < vso.size(); i++ ) {
342         _PTR(SObject) refObj = vso[i];
343         aDrw = aBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
344         aDrw->SetDrawable( true );
345       }
346     }
347   }
348   toProcess.clear();
349   getTails(myTreeWidget->invisibleRootItem(), toProcess);
350   for( int i = 0; i < toProcess.count(); i++) {
351     if( toProcess[i] != myTreeWidget->invisibleRootItem() )
352       delete toProcess[i];
353   }
354       
355   app->updateObjectBrowser( false );
356   app->updateActions();
357 }
358
359 //=================================================================================
360 // function : clickOnOk()
361 // purpose  : Called then "Publish And Close" button clicked
362 //=================================================================================
363 void GEOMToolsGUI_PublishDlg::clickOnOk() {
364   clickOnApply();
365   accept();
366 }
367
368 //=================================================================================
369 // function : clickOnSelectAll()
370 // purpose  : Called then "Select All" button clicked
371 //=================================================================================
372 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
373   processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
374 }
375
376 //=================================================================================
377 // function : clickOnUnSelectAll()
378 // purpose  : Called then "Unselect All" button clicked
379 //=================================================================================
380 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
381   processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
382 }
383
384 //=================================================================================
385 // function : processAll()
386 // purpose  : 
387 //=================================================================================
388 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
389   
390     
391   if((theItem->flags() & Qt::ItemIsUserCheckable))
392     theItem->setCheckState(0,state);
393   
394   int i = 0;
395   while(i < theItem->childCount()) {
396     processAll(theItem->child(i), state);
397     i++;
398   }
399 }
400
401 //=================================================================================
402 // function : processAll()
403 // purpose  : 
404 //=================================================================================
405 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
406   
407   theItem->setData(0, Qt::UserRole, true);
408
409   if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
410     theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
411   }
412   
413   int i = 0;
414   while(i < theItem->childCount()) {
415     getDrawableList(theItem->child(i), theList);
416     theItem->setData( 0, Qt::UserRole, (theItem->data(0,Qt::UserRole).toBool() && theItem->child(i)->data(0,Qt::UserRole).toBool()) );
417     i++;
418   } 
419
420   if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
421     theList.push_back(theItem);
422 }
423
424
425 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
426   int i = 0;
427   while(i < theItem->childCount()) {
428     getTails(theItem->child(i),theList);
429     i++;
430   }
431
432   if(theItem->data(0,Qt::UserRole).toBool())
433     theList.push_back(theItem);
434 }
435
436 //=================================================================================
437 // function : onItemClicked()
438 // purpose  : Called then treeItem clicked
439 //=================================================================================
440 void GEOMToolsGUI_PublishDlg::onItemClicked(QTreeWidgetItem* theItem, int theColumn) {
441   if ( theColumn == 2 ) {
442     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
443     if ( !app ) return;
444     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
445     if ( !appStudy ) return;
446     _PTR(Study) aStudy = appStudy->studyDS();
447     if ( !aStudy ) return;
448     QString entry = myEntryToItem.key( theItem );
449     _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( entry ) ) );
450     if ( !SO ) return;
451     GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
452     if ( CORBA::is_nil( aGeomObject ) ) return;
453     GEOM_Displayer displayer;
454     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
455     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
456     if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
457       theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
458       displayer.Erase( io );
459     }
460     else {
461       theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
462       displayer.Display( io );
463     }
464     displayer.UpdateViewer();
465   }
466 }
467