Back to index

salome-geom  6.5.0
GEOMToolsGUI_PublishDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : GEOMToolsGUI_PublishDlg.cxx
00025 // Author : Roman NIKOLAEV, Open CASCADE S.A.S.
00026 //
00027 
00028 #include "GEOMToolsGUI_PublishDlg.h"
00029 
00030 //SALOME GUI includes
00031 #include <SUIT_Session.h>
00032 #include <SUIT_DataObject.h>
00033 
00034 #include <CAM_DataModel.h>
00035 
00036 #include <SalomeApp_DataObject.h>
00037 
00038 #include <SalomeApp_Application.h>
00039 #include <SalomeApp_Module.h>
00040 #include <SalomeApp_Study.h>
00041 
00042 
00043 //QT includes
00044 #include <QGridLayout>
00045 #include <QTreeWidget>
00046 #include <QPushButton>
00047 
00048 #define  DEFAULT_MARGIN 11
00049 #define  DEFAULT_SPACING 6
00050 
00051 GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
00052   QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
00053 {
00054    setObjectName( "GEOMToolsGUI_PublishDlg" );
00055    setModal( true );
00056    setWindowTitle( tr( "GEOM_PUBLISH_OBJECTS_TLT" ) );
00057 
00058    setAttribute(Qt::WA_DeleteOnClose);
00059    
00060    //List of the objects
00061    myTreeWidget = new QTreeWidget(this);
00062    //myTreeWidget->setRootIsDecorated(false);
00063    //myTreeWidget->setUniformRowHeights(true);
00064    myTreeWidget->setAllColumnsShowFocus(true);
00065    myTreeWidget->setColumnCount(2);
00066 
00067    QStringList columnNames;
00068    columnNames.append(tr("OBJECT_NAME"));
00069    columnNames.append(tr("OBJECT_ENTRY"));
00070    QTreeWidgetItem * headerItem = new QTreeWidgetItem(columnNames);
00071    myTreeWidget->setHeaderItem ( headerItem );
00072 
00073    //Select All button
00074    QPushButton* selectAllButton = new  QPushButton( tr("SELECT_ALL"),this );
00075 
00076    //Unselect Select All button
00077    QPushButton* unselectAllButton = new  QPushButton( tr("UNSELECT_ALL"),this );
00078 
00079    //Publish button
00080    QPushButton* publishButton = new QPushButton( tr("GEOM_PUBLISH_BTN"),this );
00081 
00082    //Publish and Close button
00083    QPushButton* publishCloseButton = new QPushButton( tr("GEOM_PUBLISH_CLOSE_BTN"), this );
00084    
00085    //Close button
00086    QPushButton* closeButton = new QPushButton( tr("GEOM_BUT_CLOSE"), this );
00087    
00088    QGridLayout* gridLayout = new QGridLayout(this);
00089    gridLayout->setMargin(DEFAULT_MARGIN);
00090    gridLayout->setSpacing(DEFAULT_SPACING);
00091 
00092    gridLayout->addWidget(myTreeWidget, 0, 0, 3, 3);
00093    gridLayout->addWidget(selectAllButton, 0, 3, 1, 1);
00094    gridLayout->addWidget(unselectAllButton, 1, 3, 1, 1);
00095    gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 2, 3, 1, 1);
00096    gridLayout->addWidget(publishCloseButton, 3, 0, 1, 1); 
00097    gridLayout->addWidget(publishButton, 3, 1, 1, 1);
00098    gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 3, 2, 1, 1);
00099    gridLayout->addWidget(closeButton, 3, 3, 1, 1);
00100 
00101    //Connect signals and slots
00102    connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
00103    connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
00104    
00105    connect(publishCloseButton, SIGNAL(clicked()), this, SLOT(clickOnOk()));
00106    connect(publishButton, SIGNAL(clicked()), this, SLOT(clickOnApply()));
00107    connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
00108 
00109    initData();
00110 }
00111 
00112 //=================================================================================
00113 // function : ~GEOMToolsGUI_PublishDlg()
00114 // purpose  : Destroys the object and frees any allocated resources
00115 //=================================================================================
00116 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
00117 {
00118 }
00119 
00120 //=================================================================================
00121 // function : initData()
00122 // purpose  : Fill dialog after opening
00123 //=================================================================================
00124 void GEOMToolsGUI_PublishDlg::initData() {
00125   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
00126   if(!app)
00127     return;
00128   
00129   SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>(app->activeModule());
00130   
00131   if(!module)
00132     return;
00133 
00134   CAM_DataModel* dataModel = module->dataModel();
00135   if(!dataModel)
00136     return;
00137   
00138   myGeomRoot = dynamic_cast<SalomeApp_DataObject*>(dataModel->root());
00139 
00140   if(!myGeomRoot)
00141     return;
00142 
00143   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00144   if(!appStudy ) 
00145     return;
00146 
00147   _PTR(Study) aStudy = appStudy->studyDS();
00148   if(!aStudy)
00149     return;
00150 
00151   buildTree(aStudy, myGeomRoot);
00152 
00153 
00154   myTreeWidget->resizeColumnToContents(0);
00155   myTreeWidget->resizeColumnToContents(1);
00156 }
00157 
00158 //=================================================================================
00159 // function : buildTree()
00160 // purpose  :
00161 //=================================================================================
00162 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
00163   QStringList aList;
00164   aList<<theAttributes.second<<theAttributes.first;
00165   QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
00166   
00167   if(isCheckable)
00168     anItem->setCheckState(0, Qt::Unchecked);
00169   else {
00170     anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
00171   }
00172   
00173   if(theParent)
00174     theParent->addChild(anItem);
00175   else 
00176     myTreeWidget->addTopLevelItem(anItem);
00177 
00178   myEntryToItem.insert(theAttributes.first, anItem);
00179   
00180   anItem->setExpanded(true);
00181   return anItem;
00182 }
00183 
00184 //=================================================================================
00185 // function : buildTree()
00186 // purpose  :
00187 //=================================================================================
00188 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, SalomeApp_DataObject* theObject, BufferedList& theList ) {
00189   
00190   QTreeWidgetItem* aResult = NULL;
00191   SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
00192   if(aParrent) {
00193     QString targetEntry = aParrent->entry();
00194     if( !(aResult = myEntryToItem.value(targetEntry)) ) {
00195       if( aParrent != myGeomRoot ) {
00196        QString aName;
00197        _PTR(SObject) aSO ( theStudy->FindObjectID(qPrintable(aParrent->entry())));
00198        _PTR(GenericAttribute) anAttr;
00199        if ( aSO->FindAttribute(anAttr, "AttributeName") ) {
00200          _PTR(AttributeName) anAttrName (anAttr);
00201          aName = anAttrName->Value().c_str();
00202        }
00203        theList.push_front(qMakePair(targetEntry, aName));
00204        aResult = findParentItem(theStudy,aParrent,theList);
00205       } else {
00206        //Publish List
00207        for(int i = 0; i < theList.size(); i++ ) {
00208          aResult = createItem(aResult, theList[i], false);
00209        }
00210        theList.clear();
00211       }
00212     } else {
00213       //Publish List
00214       for(int i = 0; i < theList.size(); i++ ) {
00215        aResult = createItem(aResult, theList[i], false);
00216       }
00217       theList.clear();
00218     }
00219   }
00220   return aResult;
00221 }
00222 
00223 //=================================================================================
00224 // function : buildTree()
00225 // purpose  :
00226 //=================================================================================
00227 void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObject* theItem) {
00228   if(!theItem || theItem->isReference())
00229     return;
00230   
00231   if(theItem != myGeomRoot) {
00232 
00233     //If object hasn't "AttributeDrawable" => it visible
00234     bool isDrawable = true;
00235     _PTR(SObject) SO ( theStudy->FindObjectID(qPrintable(theItem->entry())));
00236     _PTR(GenericAttribute) anAttr;
00237     if ( SO && SO->FindAttribute(anAttr, "AttributeDrawable") ) {
00238       _PTR(AttributeDrawable) aDrw (anAttr);
00239       isDrawable = aDrw->IsDrawable();
00240     }
00241     
00242     if(!isDrawable) {
00243       QString aName;
00244       if ( SO->FindAttribute(anAttr, "AttributeName") ) {
00245        _PTR(AttributeName) aAttrName (anAttr);
00246        aName = aAttrName->Value().c_str();
00247       }
00248       BufferedList aList;
00249       QTreeWidgetItem* parentItem = findParentItem(theStudy, theItem, aList);
00250       createItem(parentItem,qMakePair(theItem->entry(),aName),true);
00251     }
00252   }
00253   
00254   DataObjectList listObj = theItem->children( false );
00255   DataObjectList::iterator itr = listObj.begin();
00256   while( itr != listObj.end()) {
00257     buildTree(theStudy, dynamic_cast<SalomeApp_DataObject*>(*itr));
00258     itr++;
00259   }
00260 }
00261 
00262 //=================================================================================
00263 // function : clickOnApply()
00264 // purpose  : Called then "Publish" button clicked
00265 //=================================================================================
00266 void GEOMToolsGUI_PublishDlg::clickOnApply() {
00267   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
00268   if(!app)
00269     return;
00270   
00271   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00272   if(!appStudy ) 
00273     return;
00274 
00275   _PTR(Study) aStudy = appStudy->studyDS();
00276   if(!aStudy)
00277     return;
00278 
00279 
00280   QList<QTreeWidgetItem*> toProcess;
00281   getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
00282 
00283   _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00284   for( int i = 0; i < toProcess.count(); i++) {
00285     QTreeWidgetItem* item = toProcess[i];
00286     if(item) {
00287       QString entry = myEntryToItem.key(item);
00288       _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
00289       if(!SO) continue;
00290       _PTR(AttributeDrawable) aDrw = aBuilder->FindOrCreateAttribute( SO, "AttributeDrawable" );
00291       aDrw->SetDrawable( true );
00292       //Remove or change item
00293       if( item != myTreeWidget->invisibleRootItem() ){
00294        if( item->data(0,Qt::UserRole).toBool() ) {
00295          delete item;
00296        } else {
00297          item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
00298          item->setData(0,Qt::CheckStateRole,QVariant());
00299        }
00300       }
00301     }
00302   }
00303   toProcess.clear();
00304   getTails(myTreeWidget->invisibleRootItem(), toProcess);
00305   for( int i = 0; i < toProcess.count(); i++) {
00306     if( toProcess[i] != myTreeWidget->invisibleRootItem() )
00307       delete toProcess[i];
00308   }
00309       
00310   app->updateObjectBrowser( false );
00311   app->updateActions();
00312 }
00313 
00314 //=================================================================================
00315 // function : clickOnOk()
00316 // purpose  : Called then "Publish And Close" button clicked
00317 //=================================================================================
00318 void GEOMToolsGUI_PublishDlg::clickOnOk() {
00319   clickOnApply();
00320   accept();
00321 }
00322 
00323 //=================================================================================
00324 // function : clickOnSelectAll()
00325 // purpose  : Called then "Select All" button clicked
00326 //=================================================================================
00327 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
00328   processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
00329 }
00330 
00331 //=================================================================================
00332 // function : clickOnUnSelectAll()
00333 // purpose  : Called then "Unselect All" button clicked
00334 //=================================================================================
00335 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
00336   processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
00337 }
00338 
00339 //=================================================================================
00340 // function : processAll()
00341 // purpose  : 
00342 //=================================================================================
00343 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
00344   
00345     
00346   if((theItem->flags() & Qt::ItemIsUserCheckable))
00347     theItem->setCheckState(0,state);
00348   
00349   int i = 0;
00350   while(i < theItem->childCount()) {
00351     processAll(theItem->child(i), state);
00352     i++;
00353   }
00354 }
00355 
00356 //=================================================================================
00357 // function : processAll()
00358 // purpose  : 
00359 //=================================================================================
00360 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
00361   
00362   theItem->setData(0, Qt::UserRole, true);
00363 
00364   if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
00365     theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
00366   }
00367   
00368   int i = 0;
00369   while(i < theItem->childCount()) {
00370     getDrawableList(theItem->child(i), theList);
00371     theItem->setData( 0, Qt::UserRole, (theItem->data(0,Qt::UserRole).toBool() && theItem->child(i)->data(0,Qt::UserRole).toBool()) );
00372     i++;
00373   } 
00374 
00375   if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
00376     theList.push_back(theItem);
00377 }
00378 
00379 
00380 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
00381   int i = 0;
00382   while(i < theItem->childCount()) {
00383     getTails(theItem->child(i),theList);
00384     i++;
00385   }
00386 
00387   if(theItem->data(0,Qt::UserRole).toBool())
00388     theList.push_back(theItem);
00389 }