1 // Copyright (C) 2007-2022 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()->setSectionsClickable( 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;
141 QTreeWidgetItemIterator it( myTreeWidget );
143 QString entry = myEntryToItem.key( *it );
144 _PTR(SObject) SO ( aStudy->FindObjectID( qUtf8Printable( 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 buildTree(myGeomRoot);
186 myTreeWidget->resizeColumnToContents(0);
187 myTreeWidget->resizeColumnToContents(1);
188 myTreeWidget->sortByColumn(0, Qt::AscendingOrder);
191 //=================================================================================
192 // function : createItem()
194 //=================================================================================
195 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
197 aList<<theAttributes.second<<theAttributes.first<<"";
198 QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
201 anItem->setCheckState(0, Qt::Unchecked);
203 anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
207 theParent->addChild(anItem);
209 myTreeWidget->addTopLevelItem(anItem);
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);
218 //=================================================================================
219 // function : findParentItem()
221 //=================================================================================
222 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(SalomeApp_DataObject* theObject, BufferedList& theList ) {
224 QTreeWidgetItem* aResult = NULL;
225 SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
227 QString targetEntry = aParrent->entry();
228 if( !(aResult = myEntryToItem.value(targetEntry)) ) {
229 if( aParrent != myGeomRoot ) {
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();
237 theList.push_front(qMakePair(targetEntry, aName));
238 aResult = findParentItem(aParrent,theList);
241 for(int i = 0; i < theList.size(); i++ ) {
242 aResult = createItem(aResult, theList[i], false);
248 for(int i = 0; i < theList.size(); i++ ) {
249 aResult = createItem(aResult, theList[i], false);
257 //=================================================================================
258 // function : buildTree()
260 //=================================================================================
261 void GEOMToolsGUI_PublishDlg::buildTree(SalomeApp_DataObject* theItem) {
262 if(!theItem || theItem->isReference())
265 if(theItem != myGeomRoot) {
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();
278 if ( SO->FindAttribute(anAttr, "AttributeName") ) {
279 _PTR(AttributeName) aAttrName (anAttr);
280 aName = aAttrName->Value().c_str();
283 QTreeWidgetItem* parentItem = findParentItem(theItem, aList);
284 createItem(parentItem,qMakePair(theItem->entry(),aName),true);
288 DataObjectList listObj = theItem->children( false );
289 DataObjectList::iterator itr = listObj.begin();
290 while( itr != listObj.end()) {
291 buildTree(dynamic_cast<SalomeApp_DataObject*>(*itr));
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() );
305 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
309 _PTR(Study) aStudy = appStudy->studyDS();
314 QList<QTreeWidgetItem*> toProcess;
315 getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
317 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
318 for( int i = 0; i < toProcess.count(); i++) {
319 QTreeWidgetItem* item = toProcess[i];
321 QString entry = myEntryToItem.key(item);
322 _PTR(SObject) SO ( aStudy->FindObjectID(qUtf8Printable(entry)) );
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() ) {
331 item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
332 item->setData(0,Qt::CheckStateRole,QVariant());
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 );
345 getTails(myTreeWidget->invisibleRootItem(), toProcess);
346 for( int i = 0; i < toProcess.count(); i++) {
347 if( toProcess[i] != myTreeWidget->invisibleRootItem() )
351 app->updateObjectBrowser( false );
352 app->updateActions();
355 //=================================================================================
356 // function : clickOnOk()
357 // purpose : Called then "Publish And Close" button clicked
358 //=================================================================================
359 void GEOMToolsGUI_PublishDlg::clickOnOk() {
364 //=================================================================================
365 // function : clickOnSelectAll()
366 // purpose : Called then "Select All" button clicked
367 //=================================================================================
368 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
369 processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
372 //=================================================================================
373 // function : clickOnUnSelectAll()
374 // purpose : Called then "Unselect All" button clicked
375 //=================================================================================
376 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
377 processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
380 //=================================================================================
381 // function : processAll()
383 //=================================================================================
384 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
387 if((theItem->flags() & Qt::ItemIsUserCheckable))
388 theItem->setCheckState(0,state);
391 while(i < theItem->childCount()) {
392 processAll(theItem->child(i), state);
397 //=================================================================================
398 // function : processAll()
400 //=================================================================================
401 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
403 theItem->setData(0, Qt::UserRole, true);
405 if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
406 theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
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()) );
416 if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
417 theList.push_back(theItem);
421 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
423 while(i < theItem->childCount()) {
424 getTails(theItem->child(i),theList);
428 if(theItem->data(0,Qt::UserRole).toBool())
429 theList.push_back(theItem);
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() );
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 ) ) );
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 );
457 theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
458 displayer.Display( io );
460 displayer.UpdateViewer();