Salome HOME
Copyright update 2022
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_PublishDlg.cxx
index a8a70fc3f57e7f77ee5c9cce4e841116b17076b0..d31ecf834790a9c773c76ca4e6d5574166c53f1b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 //
 
 #include "GEOMToolsGUI_PublishDlg.h"
+#include "GEOM_Displayer.h"
+#include "GeometryGUI.h"
 
 //SALOME GUI includes
 #include <SUIT_Session.h>
 #include <SUIT_DataObject.h>
+#include <SUIT_ResourceMgr.h>
 
 #include <CAM_DataModel.h>
 
@@ -41,6 +44,7 @@
 
 
 //QT includes
+#include <QHeaderView>
 #include <QGridLayout>
 #include <QTreeWidget>
 #include <QPushButton>
 #define  DEFAULT_MARGIN 11
 #define  DEFAULT_SPACING 6
 
+#define VISIBILITY_COLUMN_WIDTH 25
+
 GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
   QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
 {
    setObjectName( "GEOMToolsGUI_PublishDlg" );
-   setModal( true );
+   setModal( false );
    setWindowTitle( tr( "GEOM_PUBLISH_OBJECTS_TLT" ) );
 
    setAttribute(Qt::WA_DeleteOnClose);
@@ -62,13 +68,21 @@ GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
    //myTreeWidget->setRootIsDecorated(false);
    //myTreeWidget->setUniformRowHeights(true);
    myTreeWidget->setAllColumnsShowFocus(true);
-   myTreeWidget->setColumnCount(2);
+   myTreeWidget->setColumnCount(3);
 
    QStringList columnNames;
    columnNames.append(tr("OBJECT_NAME"));
    columnNames.append(tr("OBJECT_ENTRY"));
+   columnNames.append("");
    QTreeWidgetItem * headerItem = new QTreeWidgetItem(columnNames);
+   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
+   headerItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
    myTreeWidget->setHeaderItem ( headerItem );
+   myTreeWidget->header()->setSectionsClickable( true );
+   myTreeWidget->header()->setSortIndicatorShown( true );
+   myTreeWidget->header()->moveSection( 2, 0 );
+   myTreeWidget->setSortingEnabled( true );
+   myTreeWidget->setColumnWidth( 2, VISIBILITY_COLUMN_WIDTH );
 
    //Select All button
    QPushButton* selectAllButton = new  QPushButton( tr("SELECT_ALL"),this );
@@ -99,6 +113,8 @@ GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
    gridLayout->addWidget(closeButton, 3, 3, 1, 1);
 
    //Connect signals and slots
+   connect( myTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(onItemClicked(QTreeWidgetItem*, int)) );
+
    connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
    connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
    
@@ -115,6 +131,31 @@ GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
 //=================================================================================
 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
 {
+  SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
+  if ( app ) {
+    SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
+    if ( appStudy ) {
+      _PTR(Study) aStudy = appStudy->studyDS();
+      if ( aStudy ) {
+        GEOM_Displayer displayer;
+           QTreeWidgetItemIterator it( myTreeWidget );
+           while ( *it ) {
+             QString entry = myEntryToItem.key( *it );
+             _PTR(SObject) SO ( aStudy->FindObjectID( qUtf8Printable( entry ) ) );
+             if ( SO ) {
+               GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
+               if ( CORBA::is_nil( aGeomObject ) ) continue;
+               if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
+                 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
+                 displayer.Erase( io );
+               }
+             }
+             ++it;
+           }
+           displayer.UpdateViewer();
+      }
+    }
+  }
 }
 
 //=================================================================================
@@ -140,28 +181,20 @@ void GEOMToolsGUI_PublishDlg::initData() {
   if(!myGeomRoot)
     return;
 
-  SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
-  if(!appStudy ) 
-    return;
-
-  _PTR(Study) aStudy = appStudy->studyDS();
-  if(!aStudy)
-    return;
-
-  buildTree(aStudy, myGeomRoot);
-
+  buildTree(myGeomRoot);
 
   myTreeWidget->resizeColumnToContents(0);
   myTreeWidget->resizeColumnToContents(1);
+  myTreeWidget->sortByColumn(0, Qt::AscendingOrder);
 }
 
 //=================================================================================
-// function : buildTree()
+// function : createItem()
 // purpose  :
 //=================================================================================
 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
   QStringList aList;
-  aList<<theAttributes.second<<theAttributes.first;
+  aList<<theAttributes.second<<theAttributes.first<<"";
   QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
   
   if(isCheckable)
@@ -176,16 +209,17 @@ QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent,
     myTreeWidget->addTopLevelItem(anItem);
 
   myEntryToItem.insert(theAttributes.first, anItem);
-  
+  SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
+  anItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
   anItem->setExpanded(true);
   return anItem;
 }
 
 //=================================================================================
-// function : buildTree()
+// function : findParentItem()
 // purpose  :
 //=================================================================================
-QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, SalomeApp_DataObject* theObject, BufferedList& theList ) {
+QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(SalomeApp_DataObject* theObject, BufferedList& theList ) {
   
   QTreeWidgetItem* aResult = NULL;
   SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
@@ -194,14 +228,14 @@ QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, S
     if( !(aResult = myEntryToItem.value(targetEntry)) ) {
       if( aParrent != myGeomRoot ) {
         QString aName;
-        _PTR(SObject) aSO ( theStudy->FindObjectID(qPrintable(aParrent->entry())));
+        _PTR(SObject) aSO ( SalomeApp_Application::getStudy()->FindObjectID(qUtf8Printable(aParrent->entry())));
         _PTR(GenericAttribute) anAttr;
         if ( aSO->FindAttribute(anAttr, "AttributeName") ) {
           _PTR(AttributeName) anAttrName (anAttr);
           aName = anAttrName->Value().c_str();
         }
         theList.push_front(qMakePair(targetEntry, aName));
-        aResult = findParentItem(theStudy,aParrent,theList);
+        aResult = findParentItem(aParrent,theList);
       } else {
         //Publish List
         for(int i = 0; i < theList.size(); i++ ) {
@@ -224,7 +258,7 @@ QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, S
 // function : buildTree()
 // purpose  :
 //=================================================================================
-void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObject* theItem) {
+void GEOMToolsGUI_PublishDlg::buildTree(SalomeApp_DataObject* theItem) {
   if(!theItem || theItem->isReference())
     return;
   
@@ -232,7 +266,7 @@ void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObje
 
     //If object hasn't "AttributeDrawable" => it visible
     bool isDrawable = true;
-    _PTR(SObject) SO ( theStudy->FindObjectID(qPrintable(theItem->entry())));
+    _PTR(SObject) SO ( SalomeApp_Application::getStudy()->FindObjectID(qUtf8Printable(theItem->entry())));
     _PTR(GenericAttribute) anAttr;
     if ( SO && SO->FindAttribute(anAttr, "AttributeDrawable") ) {
       _PTR(AttributeDrawable) aDrw (anAttr);
@@ -246,7 +280,7 @@ void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObje
         aName = aAttrName->Value().c_str();
       }
       BufferedList aList;
-      QTreeWidgetItem* parentItem = findParentItem(theStudy, theItem, aList);
+      QTreeWidgetItem* parentItem = findParentItem(theItem, aList);
       createItem(parentItem,qMakePair(theItem->entry(),aName),true);
     }
   }
@@ -254,7 +288,7 @@ void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObje
   DataObjectList listObj = theItem->children( false );
   DataObjectList::iterator itr = listObj.begin();
   while( itr != listObj.end()) {
-    buildTree(theStudy, dynamic_cast<SalomeApp_DataObject*>(*itr));
+    buildTree(dynamic_cast<SalomeApp_DataObject*>(*itr));
     itr++;
   }
 }
@@ -285,7 +319,7 @@ void GEOMToolsGUI_PublishDlg::clickOnApply() {
     QTreeWidgetItem* item = toProcess[i];
     if(item) {
       QString entry = myEntryToItem.key(item);
-      _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
+      _PTR(SObject) SO ( aStudy->FindObjectID(qUtf8Printable(entry)) );
       if(!SO) continue;
       _PTR(AttributeDrawable) aDrw = aBuilder->FindOrCreateAttribute( SO, "AttributeDrawable" );
       aDrw->SetDrawable( true );
@@ -298,6 +332,13 @@ void GEOMToolsGUI_PublishDlg::clickOnApply() {
           item->setData(0,Qt::CheckStateRole,QVariant());
         }
       }
+      // show references if any
+      std::vector< _PTR(SObject) > vso = aStudy->FindDependances(SO);
+      for ( size_t i = 0; i < vso.size(); i++ ) {
+        _PTR(SObject) refObj = vso[i];
+        aDrw = aBuilder->FindOrCreateAttribute( refObj, "AttributeDrawable" );
+        aDrw->SetDrawable( true );
+      }
     }
   }
   toProcess.clear();
@@ -387,3 +428,36 @@ void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidg
   if(theItem->data(0,Qt::UserRole).toBool())
     theList.push_back(theItem);
 }
+
+//=================================================================================
+// function : onItemClicked()
+// purpose  : Called then treeItem clicked
+//=================================================================================
+void GEOMToolsGUI_PublishDlg::onItemClicked(QTreeWidgetItem* theItem, int theColumn) {
+  if ( theColumn == 2 ) {
+    SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
+    if ( !app ) return;
+    SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
+    if ( !appStudy ) return;
+    _PTR(Study) aStudy = appStudy->studyDS();
+    if ( !aStudy ) return;
+    QString entry = myEntryToItem.key( theItem );
+    _PTR(SObject) SO ( aStudy->FindObjectID( qUtf8Printable( entry ) ) );
+    if ( !SO ) return;
+    GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( SO ) );
+    if ( CORBA::is_nil( aGeomObject ) ) return;
+    GEOM_Displayer displayer;
+    SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
+    Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( aGeomObject->GetStudyEntry(), "GEOM", "" );
+    if ( displayer.IsDisplayed( aGeomObject->GetStudyEntry() ) ) {
+      theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_INVISIBLE" ) ) ) );
+      displayer.Erase( io );
+    }
+    else {
+      theItem->setIcon( 2, QIcon( resMgr->loadPixmap( "SUIT", tr( "ICON_DATAOBJ_VISIBLE" ) ) ) );
+      displayer.Display( io );
+    }
+    displayer.UpdateViewer();
+  }
+}
+