1 // Copyright (C) 2007-2010 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.
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"
31 #include <SUIT_Session.h>
32 #include <SUIT_DataObject.h>
34 #include <CAM_DataModel.h>
36 #include <SalomeApp_DataObject.h>
38 #include <SalomeApp_Application.h>
39 #include <SalomeApp_Module.h>
40 #include <SalomeApp_Study.h>
44 #include <QGridLayout>
45 #include <QTreeWidget>
46 #include <QPushButton>
48 #define DEFAULT_MARGIN 11
49 #define DEFAULT_SPACING 6
51 GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
52 QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
54 setObjectName( "GEOMToolsGUI_PublishDlg" );
56 setWindowTitle( tr( "GEOM_PUBLISH_OBJECTS_TLT" ) );
58 setAttribute(Qt::WA_DeleteOnClose);
61 myTreeWidget = new QTreeWidget(this);
62 //myTreeWidget->setRootIsDecorated(false);
63 //myTreeWidget->setUniformRowHeights(true);
64 myTreeWidget->setAllColumnsShowFocus(true);
65 myTreeWidget->setColumnCount(2);
67 QStringList columnNames;
68 columnNames.append(tr("OBJECT_NAME"));
69 columnNames.append(tr("OBJECT_ENTRY"));
70 QTreeWidgetItem * headerItem = new QTreeWidgetItem(columnNames);
71 myTreeWidget->setHeaderItem ( headerItem );
74 QPushButton* selectAllButton = new QPushButton( tr("SELECT_ALL"),this );
76 //Unselect Select All button
77 QPushButton* unselectAllButton = new QPushButton( tr("UNSELECT_ALL"),this );
80 QPushButton* publishButton = new QPushButton( tr("GEOM_PUBLISH_BTN"),this );
82 //Publish and Close button
83 QPushButton* publishCloseButton = new QPushButton( tr("GEOM_PUBLISH_CLOSE_BTN"), this );
86 QPushButton* closeButton = new QPushButton( tr("GEOM_BUT_CLOSE"), this );
88 QGridLayout* gridLayout = new QGridLayout(this);
89 gridLayout->setMargin(DEFAULT_MARGIN);
90 gridLayout->setSpacing(DEFAULT_SPACING);
92 gridLayout->addWidget(myTreeWidget, 0, 0, 3, 3);
93 gridLayout->addWidget(selectAllButton, 0, 3, 1, 1);
94 gridLayout->addWidget(unselectAllButton, 1, 3, 1, 1);
95 gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 2, 3, 1, 1);
96 gridLayout->addWidget(publishCloseButton, 3, 0, 1, 1);
97 gridLayout->addWidget(publishButton, 3, 1, 1, 1);
98 gridLayout->addItem( new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum), 3, 2, 1, 1);
99 gridLayout->addWidget(closeButton, 3, 3, 1, 1);
101 //Connect signals and slots
102 connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
103 connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
105 connect(publishCloseButton, SIGNAL(clicked()), this, SLOT(clickOnOk()));
106 connect(publishButton, SIGNAL(clicked()), this, SLOT(clickOnApply()));
107 connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
112 //=================================================================================
113 // function : ~GEOMToolsGUI_PublishDlg()
114 // purpose : Destroys the object and frees any allocated resources
115 //=================================================================================
116 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
120 //=================================================================================
121 // function : initData()
122 // purpose : Fill dialog after opening
123 //=================================================================================
124 void GEOMToolsGUI_PublishDlg::initData() {
125 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
129 SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>(app->activeModule());
134 CAM_DataModel* dataModel = module->dataModel();
138 myGeomRoot = dynamic_cast<SalomeApp_DataObject*>(dataModel->root());
143 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
147 _PTR(Study) aStudy = appStudy->studyDS();
151 buildTree(aStudy, myGeomRoot);
154 myTreeWidget->resizeColumnToContents(0);
155 myTreeWidget->resizeColumnToContents(1);
158 //=================================================================================
159 // function : buildTree()
161 //=================================================================================
162 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
164 aList<<theAttributes.second<<theAttributes.first;
165 QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
168 anItem->setCheckState(0, Qt::Unchecked);
170 anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
174 theParent->addChild(anItem);
176 myTreeWidget->addTopLevelItem(anItem);
178 myEntryToItem.insert(theAttributes.first, anItem);
180 anItem->setExpanded(true);
184 //=================================================================================
185 // function : buildTree()
187 //=================================================================================
188 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, SalomeApp_DataObject* theObject, BufferedList& theList ) {
190 QTreeWidgetItem* aResult = NULL;
191 SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
193 QString targetEntry = aParrent->entry();
194 if( !(aResult = myEntryToItem.value(targetEntry)) ) {
195 if( aParrent != myGeomRoot ) {
197 _PTR(SObject) aSO ( theStudy->FindObjectID(qPrintable(aParrent->entry())));
198 _PTR(GenericAttribute) anAttr;
199 if ( aSO->FindAttribute(anAttr, "AttributeName") ) {
200 _PTR(AttributeName) anAttrName (anAttr);
201 aName = anAttrName->Value().c_str();
203 theList.push_front(qMakePair(targetEntry, aName));
204 aResult = findParentItem(theStudy,aParrent,theList);
207 for(int i = 0; i < theList.size(); i++ ) {
208 aResult = createItem(aResult, theList[i], false);
214 for(int i = 0; i < theList.size(); i++ ) {
215 aResult = createItem(aResult, theList[i], false);
223 //=================================================================================
224 // function : buildTree()
226 //=================================================================================
227 void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObject* theItem) {
228 if(!theItem || theItem->isReference())
231 if(theItem != myGeomRoot) {
233 //If object hasn't "AttributeDrawable" => it visible
234 bool isDrawable = true;
235 _PTR(SObject) SO ( theStudy->FindObjectID(qPrintable(theItem->entry())));
236 _PTR(GenericAttribute) anAttr;
237 if ( SO && SO->FindAttribute(anAttr, "AttributeDrawable") ) {
238 _PTR(AttributeDrawable) aDrw (anAttr);
239 isDrawable = aDrw->IsDrawable();
244 if ( SO->FindAttribute(anAttr, "AttributeName") ) {
245 _PTR(AttributeName) aAttrName (anAttr);
246 aName = aAttrName->Value().c_str();
249 QTreeWidgetItem* parentItem = findParentItem(theStudy, theItem, aList);
250 createItem(parentItem,qMakePair(theItem->entry(),aName),true);
254 DataObjectList listObj = theItem->children( false );
255 DataObjectList::iterator itr = listObj.begin();
256 while( itr != listObj.end()) {
257 buildTree(theStudy, dynamic_cast<SalomeApp_DataObject*>(*itr));
262 //=================================================================================
263 // function : clickOnApply()
264 // purpose : Called then "Publish" button clicked
265 //=================================================================================
266 void GEOMToolsGUI_PublishDlg::clickOnApply() {
267 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
271 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
275 _PTR(Study) aStudy = appStudy->studyDS();
280 QList<QTreeWidgetItem*> toProcess;
281 getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
283 _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
284 for( int i = 0; i < toProcess.count(); i++) {
285 QTreeWidgetItem* item = toProcess[i];
287 QString entry = myEntryToItem.key(item);
288 _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
290 _PTR(AttributeDrawable) aDrw = aBuilder->FindOrCreateAttribute( SO, "AttributeDrawable" );
291 aDrw->SetDrawable( true );
292 //Remove or change item
293 if( item != myTreeWidget->invisibleRootItem() ){
294 if( item->data(0,Qt::UserRole).toBool() ) {
297 item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
298 item->setData(0,Qt::CheckStateRole,QVariant());
304 getTails(myTreeWidget->invisibleRootItem(), toProcess);
305 for( int i = 0; i < toProcess.count(); i++) {
306 if( toProcess[i] != myTreeWidget->invisibleRootItem() )
310 app->updateObjectBrowser( false );
311 app->updateActions();
314 //=================================================================================
315 // function : clickOnOk()
316 // purpose : Called then "Publish And Close" button clicked
317 //=================================================================================
318 void GEOMToolsGUI_PublishDlg::clickOnOk() {
323 //=================================================================================
324 // function : clickOnSelectAll()
325 // purpose : Called then "Select All" button clicked
326 //=================================================================================
327 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
328 processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
331 //=================================================================================
332 // function : clickOnUnSelectAll()
333 // purpose : Called then "Unselect All" button clicked
334 //=================================================================================
335 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
336 processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
339 //=================================================================================
340 // function : processAll()
342 //=================================================================================
343 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
346 if((theItem->flags() & Qt::ItemIsUserCheckable))
347 theItem->setCheckState(0,state);
350 while(i < theItem->childCount()) {
351 processAll(theItem->child(i), state);
356 //=================================================================================
357 // function : processAll()
359 //=================================================================================
360 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
362 theItem->setData(0, Qt::UserRole, true);
364 if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
365 theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
369 while(i < theItem->childCount()) {
370 getDrawableList(theItem->child(i), theList);
371 theItem->setData( 0, Qt::UserRole, (theItem->data(0,Qt::UserRole).toBool() && theItem->child(i)->data(0,Qt::UserRole).toBool()) );
375 if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
376 theList.push_back(theItem);
380 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
382 while(i < theItem->childCount()) {
383 getTails(theItem->child(i),theList);
387 if(theItem->data(0,Qt::UserRole).toBool())
388 theList.push_back(theItem);