]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMToolsGUI/GEOMToolsGUI_PublishDlg.cxx
Salome HOME
0022311: EDF 2687 GEOM: Management of Publish Objects
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_PublishDlg.cxx
1 // Copyright (C) 2007-2013  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.
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()->setClickable( 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 ( appStudy );
141         QTreeWidgetItemIterator it( myTreeWidget );
142         while ( *it ) {
143           QString entry = myEntryToItem.key( *it );
144           _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( 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   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
185   if(!appStudy ) 
186     return;
187
188   _PTR(Study) aStudy = appStudy->studyDS();
189   if(!aStudy)
190     return;
191
192   buildTree(aStudy, myGeomRoot);
193
194   myTreeWidget->resizeColumnToContents(0);
195   myTreeWidget->resizeColumnToContents(1);
196   myTreeWidget->sortByColumn(0, Qt::AscendingOrder);
197 }
198
199 //=================================================================================
200 // function : createItem()
201 // purpose  :
202 //=================================================================================
203 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
204   QStringList aList;
205   aList<<theAttributes.second<<theAttributes.first<<"";
206   QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
207   
208   if(isCheckable)
209     anItem->setCheckState(0, Qt::Unchecked);
210   else {
211     anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
212   }
213   
214   if(theParent)
215     theParent->addChild(anItem);
216   else 
217     myTreeWidget->addTopLevelItem(anItem);
218
219   myEntryToItem.insert(theAttributes.first, anItem);
220   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
221   anItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
222   anItem->setExpanded(true);
223   return anItem;
224 }
225
226 //=================================================================================
227 // function : findParentItem()
228 // purpose  :
229 //=================================================================================
230 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, SalomeApp_DataObject* theObject, BufferedList& theList ) {
231   
232   QTreeWidgetItem* aResult = NULL;
233   SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
234   if(aParrent) {
235     QString targetEntry = aParrent->entry();
236     if( !(aResult = myEntryToItem.value(targetEntry)) ) {
237       if( aParrent != myGeomRoot ) {
238         QString aName;
239         _PTR(SObject) aSO ( theStudy->FindObjectID(qPrintable(aParrent->entry())));
240         _PTR(GenericAttribute) anAttr;
241         if ( aSO->FindAttribute(anAttr, "AttributeName") ) {
242           _PTR(AttributeName) anAttrName (anAttr);
243           aName = anAttrName->Value().c_str();
244         }
245         theList.push_front(qMakePair(targetEntry, aName));
246         aResult = findParentItem(theStudy,aParrent,theList);
247       } else {
248         //Publish List
249         for(int i = 0; i < theList.size(); i++ ) {
250           aResult = createItem(aResult, theList[i], false);
251         }
252         theList.clear();
253       }
254     } else {
255       //Publish List
256       for(int i = 0; i < theList.size(); i++ ) {
257         aResult = createItem(aResult, theList[i], false);
258       }
259       theList.clear();
260     }
261   }
262   return aResult;
263 }
264
265 //=================================================================================
266 // function : buildTree()
267 // purpose  :
268 //=================================================================================
269 void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObject* theItem) {
270   if(!theItem || theItem->isReference())
271     return;
272   
273   if(theItem != myGeomRoot) {
274
275     //If object hasn't "AttributeDrawable" => it visible
276     bool isDrawable = true;
277     _PTR(SObject) SO ( theStudy->FindObjectID(qPrintable(theItem->entry())));
278     _PTR(GenericAttribute) anAttr;
279     if ( SO && SO->FindAttribute(anAttr, "AttributeDrawable") ) {
280       _PTR(AttributeDrawable) aDrw (anAttr);
281       isDrawable = aDrw->IsDrawable();
282     }
283     
284     if(!isDrawable) {
285       QString aName;
286       if ( SO->FindAttribute(anAttr, "AttributeName") ) {
287         _PTR(AttributeName) aAttrName (anAttr);
288         aName = aAttrName->Value().c_str();
289       }
290       BufferedList aList;
291       QTreeWidgetItem* parentItem = findParentItem(theStudy, theItem, aList);
292       createItem(parentItem,qMakePair(theItem->entry(),aName),true);
293     }
294   }
295   
296   DataObjectList listObj = theItem->children( false );
297   DataObjectList::iterator itr = listObj.begin();
298   while( itr != listObj.end()) {
299     buildTree(theStudy, dynamic_cast<SalomeApp_DataObject*>(*itr));
300     itr++;
301   }
302 }
303
304 //=================================================================================
305 // function : clickOnApply()
306 // purpose  : Called then "Publish" button clicked
307 //=================================================================================
308 void GEOMToolsGUI_PublishDlg::clickOnApply() {
309   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
310   if(!app)
311     return;
312   
313   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
314   if(!appStudy ) 
315     return;
316
317   _PTR(Study) aStudy = appStudy->studyDS();
318   if(!aStudy)
319     return;
320
321
322   QList<QTreeWidgetItem*> toProcess;
323   getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
324
325   _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
326   for( int i = 0; i < toProcess.count(); i++) {
327     QTreeWidgetItem* item = toProcess[i];
328     if(item) {
329       QString entry = myEntryToItem.key(item);
330       _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
331       if(!SO) continue;
332       _PTR(AttributeDrawable) aDrw = aBuilder->FindOrCreateAttribute( SO, "AttributeDrawable" );
333       aDrw->SetDrawable( true );
334       //Remove or change item
335       if( item != myTreeWidget->invisibleRootItem() ){
336         if( item->data(0,Qt::UserRole).toBool() ) {
337           delete item;
338         } else {
339           item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
340           item->setData(0,Qt::CheckStateRole,QVariant());
341         }
342       }
343     }
344   }
345   toProcess.clear();
346   getTails(myTreeWidget->invisibleRootItem(), toProcess);
347   for( int i = 0; i < toProcess.count(); i++) {
348     if( toProcess[i] != myTreeWidget->invisibleRootItem() )
349       delete toProcess[i];
350   }
351       
352   app->updateObjectBrowser( false );
353   app->updateActions();
354 }
355
356 //=================================================================================
357 // function : clickOnOk()
358 // purpose  : Called then "Publish And Close" button clicked
359 //=================================================================================
360 void GEOMToolsGUI_PublishDlg::clickOnOk() {
361   clickOnApply();
362   accept();
363 }
364
365 //=================================================================================
366 // function : clickOnSelectAll()
367 // purpose  : Called then "Select All" button clicked
368 //=================================================================================
369 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
370   processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
371 }
372
373 //=================================================================================
374 // function : clickOnUnSelectAll()
375 // purpose  : Called then "Unselect All" button clicked
376 //=================================================================================
377 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
378   processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
379 }
380
381 //=================================================================================
382 // function : processAll()
383 // purpose  : 
384 //=================================================================================
385 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
386   
387     
388   if((theItem->flags() & Qt::ItemIsUserCheckable))
389     theItem->setCheckState(0,state);
390   
391   int i = 0;
392   while(i < theItem->childCount()) {
393     processAll(theItem->child(i), state);
394     i++;
395   }
396 }
397
398 //=================================================================================
399 // function : processAll()
400 // purpose  : 
401 //=================================================================================
402 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
403   
404   theItem->setData(0, Qt::UserRole, true);
405
406   if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
407     theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
408   }
409   
410   int i = 0;
411   while(i < theItem->childCount()) {
412     getDrawableList(theItem->child(i), theList);
413     theItem->setData( 0, Qt::UserRole, (theItem->data(0,Qt::UserRole).toBool() && theItem->child(i)->data(0,Qt::UserRole).toBool()) );
414     i++;
415   } 
416
417   if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
418     theList.push_back(theItem);
419 }
420
421
422 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
423   int i = 0;
424   while(i < theItem->childCount()) {
425     getTails(theItem->child(i),theList);
426     i++;
427   }
428
429   if(theItem->data(0,Qt::UserRole).toBool())
430     theList.push_back(theItem);
431 }
432
433 //=================================================================================
434 // function : onItemClicked()
435 // purpose  : Called then treeItem clicked
436 //=================================================================================
437 void GEOMToolsGUI_PublishDlg::onItemClicked(QTreeWidgetItem* theItem, int theColumn) {
438   if ( theColumn == 2 ) {
439     SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
440     if ( !app ) return;
441     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
442     if ( !appStudy ) return;
443     _PTR(Study) aStudy = appStudy->studyDS();
444     if ( !aStudy ) return;
445     QString entry = myEntryToItem.key( theItem );
446     _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( entry ) ) );
447     if ( !SO ) return;
448     GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
449     if ( CORBA::is_nil( aGeomObject ) ) return;
450     GEOM_Displayer displayer ( appStudy );
451     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
452     Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
453     if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
454       theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
455       displayer.Erase( io );
456     }
457     else {
458       theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
459       displayer.Display( io );
460     }
461     displayer.UpdateViewer();
462   }
463 }
464