1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : GEOMToolsGUI_PublishDlg.cxx
25 // Author : Roman NIKOLAEV, Open CASCADE S.A.S.
28 #include "GEOMToolsGUI_PublishDlg.h"
29 #include "GEOM_Displayer.h"
30 #include "GeometryGUI.h"
33 #include <SUIT_Session.h>
34 #include <SUIT_DataObject.h>
35 #include <SUIT_ResourceMgr.h>
37 #include <CAM_DataModel.h>
39 #include <SalomeApp_DataObject.h>
41 #include <SalomeApp_Application.h>
42 #include <SalomeApp_Module.h>
43 #include <SalomeApp_Study.h>
47 #include <QHeaderView>
48 #include <QGridLayout>
49 #include <QTreeWidget>
50 #include <QPushButton>
52 #define DEFAULT_MARGIN 11
53 #define DEFAULT_SPACING 6
55 #define VISIBILITY_COLUMN_WIDTH 25
57 GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
58 QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
60 setObjectName( "GEOMToolsGUI_PublishDlg" );
62 setWindowTitle( tr( "GEOM_PUBLISH_OBJECTS_TLT" ) );
64 setAttribute(Qt::WA_DeleteOnClose);
67 myTreeWidget = new QTreeWidget(this);
68 //myTreeWidget->setRootIsDecorated(false);
69 //myTreeWidget->setUniformRowHeights(true);
70 myTreeWidget->setAllColumnsShowFocus(true);
71 myTreeWidget->setColumnCount(3);
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 );
88 QPushButton* selectAllButton = new QPushButton( tr("SELECT_ALL"),this );
90 //Unselect Select All button
91 QPushButton* unselectAllButton = new QPushButton( tr("UNSELECT_ALL"),this );
94 QPushButton* publishButton = new QPushButton( tr("GEOM_PUBLISH_BTN"),this );
96 //Publish and Close button
97 QPushButton* publishCloseButton = new QPushButton( tr("GEOM_PUBLISH_CLOSE_BTN"), this );
100 QPushButton* closeButton = new QPushButton( tr("GEOM_BUT_CLOSE"), this );
102 QGridLayout* gridLayout = new QGridLayout(this);
103 gridLayout->setMargin(DEFAULT_MARGIN);
104 gridLayout->setSpacing(DEFAULT_SPACING);
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);
115 //Connect signals and slots
116 connect( myTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(onItemClicked(QTreeWidgetItem*, int)) );
118 connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
119 connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
121 connect(publishCloseButton, SIGNAL(clicked()), this, SLOT(clickOnOk()));
122 connect(publishButton, SIGNAL(clicked()), this, SLOT(clickOnApply()));
123 connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
128 //=================================================================================
129 // function : ~GEOMToolsGUI_PublishDlg()
130 // purpose : Destroys the object and frees any allocated resources
131 //=================================================================================
132 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
134 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
136 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
138 _PTR(Study) aStudy = appStudy->studyDS();
140 GEOM_Displayer displayer ( appStudy );
141 QTreeWidgetItemIterator it( myTreeWidget );
143 QString entry = myEntryToItem.key( *it );
144 _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( entry ) ) );
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 );
155 displayer.UpdateViewer();
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() );
170 SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>(app->activeModule());
175 CAM_DataModel* dataModel = module->dataModel();
179 myGeomRoot = dynamic_cast<SalomeApp_DataObject*>(dataModel->root());
184 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
188 _PTR(Study) aStudy = appStudy->studyDS();
192 buildTree(aStudy, myGeomRoot);
194 myTreeWidget->resizeColumnToContents(0);
195 myTreeWidget->resizeColumnToContents(1);
196 myTreeWidget->sortByColumn(0, Qt::AscendingOrder);
199 //=================================================================================
200 // function : createItem()
202 //=================================================================================
203 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
205 aList<<theAttributes.second<<theAttributes.first<<"";
206 QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
209 anItem->setCheckState(0, Qt::Unchecked);
211 anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
215 theParent->addChild(anItem);
217 myTreeWidget->addTopLevelItem(anItem);
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);
226 //=================================================================================
227 // function : findParentItem()
229 //=================================================================================
230 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, SalomeApp_DataObject* theObject, BufferedList& theList ) {
232 QTreeWidgetItem* aResult = NULL;
233 SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
235 QString targetEntry = aParrent->entry();
236 if( !(aResult = myEntryToItem.value(targetEntry)) ) {
237 if( aParrent != myGeomRoot ) {
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();
245 theList.push_front(qMakePair(targetEntry, aName));
246 aResult = findParentItem(theStudy,aParrent,theList);
249 for(int i = 0; i < theList.size(); i++ ) {
250 aResult = createItem(aResult, theList[i], false);
256 for(int i = 0; i < theList.size(); i++ ) {
257 aResult = createItem(aResult, theList[i], false);
265 //=================================================================================
266 // function : buildTree()
268 //=================================================================================
269 void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObject* theItem) {
270 if(!theItem || theItem->isReference())
273 if(theItem != myGeomRoot) {
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();
286 if ( SO->FindAttribute(anAttr, "AttributeName") ) {
287 _PTR(AttributeName) aAttrName (anAttr);
288 aName = aAttrName->Value().c_str();
291 QTreeWidgetItem* parentItem = findParentItem(theStudy, theItem, aList);
292 createItem(parentItem,qMakePair(theItem->entry(),aName),true);
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));
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() );
313 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
317 _PTR(Study) aStudy = appStudy->studyDS();
322 QList<QTreeWidgetItem*> toProcess;
323 getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
325 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
326 for( int i = 0; i < toProcess.count(); i++) {
327 QTreeWidgetItem* item = toProcess[i];
329 QString entry = myEntryToItem.key(item);
330 _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
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() ) {
339 item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
340 item->setData(0,Qt::CheckStateRole,QVariant());
343 // show references if any
344 std::vector< _PTR(SObject) > vso = aStudy->FindDependances(SO);
345 for ( int i = 0; i < vso.size(); i++ ) {
346 _PTR(SObject) refObj = vso[i];
347 aDrw = aBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
348 aDrw->SetDrawable( true );
353 getTails(myTreeWidget->invisibleRootItem(), toProcess);
354 for( int i = 0; i < toProcess.count(); i++) {
355 if( toProcess[i] != myTreeWidget->invisibleRootItem() )
359 app->updateObjectBrowser( false );
360 app->updateActions();
363 //=================================================================================
364 // function : clickOnOk()
365 // purpose : Called then "Publish And Close" button clicked
366 //=================================================================================
367 void GEOMToolsGUI_PublishDlg::clickOnOk() {
372 //=================================================================================
373 // function : clickOnSelectAll()
374 // purpose : Called then "Select All" button clicked
375 //=================================================================================
376 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
377 processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
380 //=================================================================================
381 // function : clickOnUnSelectAll()
382 // purpose : Called then "Unselect All" button clicked
383 //=================================================================================
384 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
385 processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
388 //=================================================================================
389 // function : processAll()
391 //=================================================================================
392 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
395 if((theItem->flags() & Qt::ItemIsUserCheckable))
396 theItem->setCheckState(0,state);
399 while(i < theItem->childCount()) {
400 processAll(theItem->child(i), state);
405 //=================================================================================
406 // function : processAll()
408 //=================================================================================
409 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
411 theItem->setData(0, Qt::UserRole, true);
413 if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
414 theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
418 while(i < theItem->childCount()) {
419 getDrawableList(theItem->child(i), theList);
420 theItem->setData( 0, Qt::UserRole, (theItem->data(0,Qt::UserRole).toBool() && theItem->child(i)->data(0,Qt::UserRole).toBool()) );
424 if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
425 theList.push_back(theItem);
429 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
431 while(i < theItem->childCount()) {
432 getTails(theItem->child(i),theList);
436 if(theItem->data(0,Qt::UserRole).toBool())
437 theList.push_back(theItem);
440 //=================================================================================
441 // function : onItemClicked()
442 // purpose : Called then treeItem clicked
443 //=================================================================================
444 void GEOMToolsGUI_PublishDlg::onItemClicked(QTreeWidgetItem* theItem, int theColumn) {
445 if ( theColumn == 2 ) {
446 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
448 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
449 if ( !appStudy ) return;
450 _PTR(Study) aStudy = appStudy->studyDS();
451 if ( !aStudy ) return;
452 QString entry = myEntryToItem.key( theItem );
453 _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( entry ) ) );
455 GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
456 if ( CORBA::is_nil( aGeomObject ) ) return;
457 GEOM_Displayer displayer ( appStudy );
458 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
459 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
460 if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
461 theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
462 displayer.Erase( io );
465 theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
466 displayer.Display( io );
468 displayer.UpdateViewer();