1 // Copyright (C) 2007-2016 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 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
82 myTreeWidget->header()->setClickable( true );
84 myTreeWidget->header()->setSectionsClickable( true );
86 myTreeWidget->header()->setSortIndicatorShown( true );
87 myTreeWidget->header()->moveSection( 2, 0 );
88 myTreeWidget->setSortingEnabled( true );
89 myTreeWidget->setColumnWidth( 2, VISIBILITY_COLUMN_WIDTH );
92 QPushButton* selectAllButton = new QPushButton( tr("SELECT_ALL"),this );
94 //Unselect Select All button
95 QPushButton* unselectAllButton = new QPushButton( tr("UNSELECT_ALL"),this );
98 QPushButton* publishButton = new QPushButton( tr("GEOM_PUBLISH_BTN"),this );
100 //Publish and Close button
101 QPushButton* publishCloseButton = new QPushButton( tr("GEOM_PUBLISH_CLOSE_BTN"), this );
104 QPushButton* closeButton = new QPushButton( tr("GEOM_BUT_CLOSE"), this );
106 QGridLayout* gridLayout = new QGridLayout(this);
107 gridLayout->setMargin(DEFAULT_MARGIN);
108 gridLayout->setSpacing(DEFAULT_SPACING);
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);
119 //Connect signals and slots
120 connect( myTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(onItemClicked(QTreeWidgetItem*, int)) );
122 connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
123 connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
125 connect(publishCloseButton, SIGNAL(clicked()), this, SLOT(clickOnOk()));
126 connect(publishButton, SIGNAL(clicked()), this, SLOT(clickOnApply()));
127 connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
132 //=================================================================================
133 // function : ~GEOMToolsGUI_PublishDlg()
134 // purpose : Destroys the object and frees any allocated resources
135 //=================================================================================
136 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
138 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
140 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
142 _PTR(Study) aStudy = appStudy->studyDS();
144 GEOM_Displayer displayer ( appStudy );
145 QTreeWidgetItemIterator it( myTreeWidget );
147 QString entry = myEntryToItem.key( *it );
148 _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( entry ) ) );
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 );
159 displayer.UpdateViewer();
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() );
174 SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>(app->activeModule());
179 CAM_DataModel* dataModel = module->dataModel();
183 myGeomRoot = dynamic_cast<SalomeApp_DataObject*>(dataModel->root());
188 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
192 _PTR(Study) aStudy = appStudy->studyDS();
196 buildTree(aStudy, myGeomRoot);
198 myTreeWidget->resizeColumnToContents(0);
199 myTreeWidget->resizeColumnToContents(1);
200 myTreeWidget->sortByColumn(0, Qt::AscendingOrder);
203 //=================================================================================
204 // function : createItem()
206 //=================================================================================
207 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
209 aList<<theAttributes.second<<theAttributes.first<<"";
210 QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
213 anItem->setCheckState(0, Qt::Unchecked);
215 anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
219 theParent->addChild(anItem);
221 myTreeWidget->addTopLevelItem(anItem);
223 myEntryToItem.insert(theAttributes.first, anItem);
224 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
225 anItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
226 anItem->setExpanded(true);
230 //=================================================================================
231 // function : findParentItem()
233 //=================================================================================
234 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, SalomeApp_DataObject* theObject, BufferedList& theList ) {
236 QTreeWidgetItem* aResult = NULL;
237 SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
239 QString targetEntry = aParrent->entry();
240 if( !(aResult = myEntryToItem.value(targetEntry)) ) {
241 if( aParrent != myGeomRoot ) {
243 _PTR(SObject) aSO ( theStudy->FindObjectID(qPrintable(aParrent->entry())));
244 _PTR(GenericAttribute) anAttr;
245 if ( aSO->FindAttribute(anAttr, "AttributeName") ) {
246 _PTR(AttributeName) anAttrName (anAttr);
247 aName = anAttrName->Value().c_str();
249 theList.push_front(qMakePair(targetEntry, aName));
250 aResult = findParentItem(theStudy,aParrent,theList);
253 for(int i = 0; i < theList.size(); i++ ) {
254 aResult = createItem(aResult, theList[i], false);
260 for(int i = 0; i < theList.size(); i++ ) {
261 aResult = createItem(aResult, theList[i], false);
269 //=================================================================================
270 // function : buildTree()
272 //=================================================================================
273 void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObject* theItem) {
274 if(!theItem || theItem->isReference())
277 if(theItem != myGeomRoot) {
279 //If object hasn't "AttributeDrawable" => it visible
280 bool isDrawable = true;
281 _PTR(SObject) SO ( theStudy->FindObjectID(qPrintable(theItem->entry())));
282 _PTR(GenericAttribute) anAttr;
283 if ( SO && SO->FindAttribute(anAttr, "AttributeDrawable") ) {
284 _PTR(AttributeDrawable) aDrw (anAttr);
285 isDrawable = aDrw->IsDrawable();
290 if ( SO->FindAttribute(anAttr, "AttributeName") ) {
291 _PTR(AttributeName) aAttrName (anAttr);
292 aName = aAttrName->Value().c_str();
295 QTreeWidgetItem* parentItem = findParentItem(theStudy, theItem, aList);
296 createItem(parentItem,qMakePair(theItem->entry(),aName),true);
300 DataObjectList listObj = theItem->children( false );
301 DataObjectList::iterator itr = listObj.begin();
302 while( itr != listObj.end()) {
303 buildTree(theStudy, dynamic_cast<SalomeApp_DataObject*>(*itr));
308 //=================================================================================
309 // function : clickOnApply()
310 // purpose : Called then "Publish" button clicked
311 //=================================================================================
312 void GEOMToolsGUI_PublishDlg::clickOnApply() {
313 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
317 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
321 _PTR(Study) aStudy = appStudy->studyDS();
326 QList<QTreeWidgetItem*> toProcess;
327 getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
329 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
330 for( int i = 0; i < toProcess.count(); i++) {
331 QTreeWidgetItem* item = toProcess[i];
333 QString entry = myEntryToItem.key(item);
334 _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
336 _PTR(AttributeDrawable) aDrw = aBuilder->FindOrCreateAttribute( SO, "AttributeDrawable" );
337 aDrw->SetDrawable( true );
338 //Remove or change item
339 if( item != myTreeWidget->invisibleRootItem() ){
340 if( item->data(0,Qt::UserRole).toBool() ) {
343 item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
344 item->setData(0,Qt::CheckStateRole,QVariant());
347 // show references if any
348 std::vector< _PTR(SObject) > vso = aStudy->FindDependances(SO);
349 for ( int i = 0; i < vso.size(); i++ ) {
350 _PTR(SObject) refObj = vso[i];
351 aDrw = aBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
352 aDrw->SetDrawable( true );
357 getTails(myTreeWidget->invisibleRootItem(), toProcess);
358 for( int i = 0; i < toProcess.count(); i++) {
359 if( toProcess[i] != myTreeWidget->invisibleRootItem() )
363 app->updateObjectBrowser( false );
364 app->updateActions();
367 //=================================================================================
368 // function : clickOnOk()
369 // purpose : Called then "Publish And Close" button clicked
370 //=================================================================================
371 void GEOMToolsGUI_PublishDlg::clickOnOk() {
376 //=================================================================================
377 // function : clickOnSelectAll()
378 // purpose : Called then "Select All" button clicked
379 //=================================================================================
380 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
381 processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
384 //=================================================================================
385 // function : clickOnUnSelectAll()
386 // purpose : Called then "Unselect All" button clicked
387 //=================================================================================
388 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
389 processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
392 //=================================================================================
393 // function : processAll()
395 //=================================================================================
396 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
399 if((theItem->flags() & Qt::ItemIsUserCheckable))
400 theItem->setCheckState(0,state);
403 while(i < theItem->childCount()) {
404 processAll(theItem->child(i), state);
409 //=================================================================================
410 // function : processAll()
412 //=================================================================================
413 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
415 theItem->setData(0, Qt::UserRole, true);
417 if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
418 theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
422 while(i < theItem->childCount()) {
423 getDrawableList(theItem->child(i), theList);
424 theItem->setData( 0, Qt::UserRole, (theItem->data(0,Qt::UserRole).toBool() && theItem->child(i)->data(0,Qt::UserRole).toBool()) );
428 if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
429 theList.push_back(theItem);
433 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
435 while(i < theItem->childCount()) {
436 getTails(theItem->child(i),theList);
440 if(theItem->data(0,Qt::UserRole).toBool())
441 theList.push_back(theItem);
444 //=================================================================================
445 // function : onItemClicked()
446 // purpose : Called then treeItem clicked
447 //=================================================================================
448 void GEOMToolsGUI_PublishDlg::onItemClicked(QTreeWidgetItem* theItem, int theColumn) {
449 if ( theColumn == 2 ) {
450 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
452 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
453 if ( !appStudy ) return;
454 _PTR(Study) aStudy = appStudy->studyDS();
455 if ( !aStudy ) return;
456 QString entry = myEntryToItem.key( theItem );
457 _PTR(SObject) SO ( aStudy->FindObjectID( qPrintable( entry ) ) );
459 GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
460 if ( CORBA::is_nil( aGeomObject ) ) return;
461 GEOM_Displayer displayer ( appStudy );
462 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
463 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
464 if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
465 theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
466 displayer.Erase( io );
469 theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
470 displayer.Display( io );
472 displayer.UpdateViewer();