Salome HOME
Implementation of the "0021239: EDF 1829 OCC: Bring to front selected objects" issue.
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_PublishDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOMToolsGUI_PublishDlg.cxx
25 // Author : Roman NIKOLAEV, Open CASCADE S.A.S.
26 //
27
28 #include "GEOMToolsGUI_PublishDlg.h"
29
30 //SALOME GUI includes
31 #include <SUIT_Session.h>
32 #include <SUIT_DataObject.h>
33
34 #include <CAM_DataModel.h>
35
36 #include <SalomeApp_DataObject.h>
37
38 #include <SalomeApp_Application.h>
39 #include <SalomeApp_Module.h>
40 #include <SalomeApp_Study.h>
41
42
43 //QT includes
44 #include <QGridLayout>
45 #include <QTreeWidget>
46 #include <QPushButton>
47
48 #define  DEFAULT_MARGIN 11
49 #define  DEFAULT_SPACING 6
50
51 GEOMToolsGUI_PublishDlg::GEOMToolsGUI_PublishDlg(QWidget* parent):
52   QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
53 {
54    setObjectName( "GEOMToolsGUI_PublishDlg" );
55    setModal( true );
56    setWindowTitle( tr( "GEOM_PUBLISH_OBJECTS_TLT" ) );
57
58    setAttribute(Qt::WA_DeleteOnClose);
59    
60    //List of the objects
61    myTreeWidget = new QTreeWidget(this);
62    //myTreeWidget->setRootIsDecorated(false);
63    //myTreeWidget->setUniformRowHeights(true);
64    myTreeWidget->setAllColumnsShowFocus(true);
65    myTreeWidget->setColumnCount(2);
66
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 );
72
73    //Select All button
74    QPushButton* selectAllButton = new  QPushButton( tr("SELECT_ALL"),this );
75
76    //Unselect Select All button
77    QPushButton* unselectAllButton = new  QPushButton( tr("UNSELECT_ALL"),this );
78
79    //Publish button
80    QPushButton* publishButton = new QPushButton( tr("GEOM_PUBLISH_BTN"),this );
81
82    //Publish and Close button
83    QPushButton* publishCloseButton = new QPushButton( tr("GEOM_PUBLISH_CLOSE_BTN"), this );
84    
85    //Close button
86    QPushButton* closeButton = new QPushButton( tr("GEOM_BUT_CLOSE"), this );
87    
88    QGridLayout* gridLayout = new QGridLayout(this);
89    gridLayout->setMargin(DEFAULT_MARGIN);
90    gridLayout->setSpacing(DEFAULT_SPACING);
91
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);
100
101    //Connect signals and slots
102    connect( selectAllButton, SIGNAL(clicked()), this, SLOT(clickOnSelectAll()) );
103    connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(clickOnUnSelectAll()) );
104    
105    connect(publishCloseButton, SIGNAL(clicked()), this, SLOT(clickOnOk()));
106    connect(publishButton, SIGNAL(clicked()), this, SLOT(clickOnApply()));
107    connect(closeButton, SIGNAL(clicked()), this, SLOT(reject()));
108
109    initData();
110 }
111
112 //=================================================================================
113 // function : ~GEOMToolsGUI_PublishDlg()
114 // purpose  : Destroys the object and frees any allocated resources
115 //=================================================================================
116 GEOMToolsGUI_PublishDlg::~GEOMToolsGUI_PublishDlg()
117 {
118 }
119
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() );
126   if(!app)
127     return;
128   
129   SalomeApp_Module* module = dynamic_cast<SalomeApp_Module*>(app->activeModule());
130   
131   if(!module)
132     return;
133
134   CAM_DataModel* dataModel = module->dataModel();
135   if(!dataModel)
136     return;
137   
138   myGeomRoot = dynamic_cast<SalomeApp_DataObject*>(dataModel->root());
139
140   if(!myGeomRoot)
141     return;
142
143   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
144   if(!appStudy ) 
145     return;
146
147   _PTR(Study) aStudy = appStudy->studyDS();
148   if(!aStudy)
149     return;
150
151   buildTree(aStudy, myGeomRoot);
152
153
154   myTreeWidget->resizeColumnToContents(0);
155   myTreeWidget->resizeColumnToContents(1);
156 }
157
158 //=================================================================================
159 // function : buildTree()
160 // purpose  :
161 //=================================================================================
162 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::createItem(QTreeWidgetItem* theParent, Pair theAttributes, bool isCheckable) {
163   QStringList aList;
164   aList<<theAttributes.second<<theAttributes.first;
165   QTreeWidgetItem* anItem = new QTreeWidgetItem(aList);
166   
167   if(isCheckable)
168     anItem->setCheckState(0, Qt::Unchecked);
169   else {
170     anItem->setFlags(anItem->flags() & ~Qt::ItemIsUserCheckable);
171   }
172   
173   if(theParent)
174     theParent->addChild(anItem);
175   else 
176     myTreeWidget->addTopLevelItem(anItem);
177
178   myEntryToItem.insert(theAttributes.first, anItem);
179   
180   anItem->setExpanded(true);
181   return anItem;
182 }
183
184 //=================================================================================
185 // function : buildTree()
186 // purpose  :
187 //=================================================================================
188 QTreeWidgetItem* GEOMToolsGUI_PublishDlg::findParentItem(_PTR(Study) theStudy, SalomeApp_DataObject* theObject, BufferedList& theList ) {
189   
190   QTreeWidgetItem* aResult = NULL;
191   SalomeApp_DataObject* aParrent = dynamic_cast<SalomeApp_DataObject*>(theObject->parent());
192   if(aParrent) {
193     QString targetEntry = aParrent->entry();
194     if( !(aResult = myEntryToItem.value(targetEntry)) ) {
195       if( aParrent != myGeomRoot ) {
196         QString aName;
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();
202         }
203         theList.push_front(qMakePair(targetEntry, aName));
204         aResult = findParentItem(theStudy,aParrent,theList);
205       } else {
206         //Publish List
207         for(int i = 0; i < theList.size(); i++ ) {
208           aResult = createItem(aResult, theList[i], false);
209         }
210         theList.clear();
211       }
212     } else {
213       //Publish List
214       for(int i = 0; i < theList.size(); i++ ) {
215         aResult = createItem(aResult, theList[i], false);
216       }
217       theList.clear();
218     }
219   }
220   return aResult;
221 }
222
223 //=================================================================================
224 // function : buildTree()
225 // purpose  :
226 //=================================================================================
227 void GEOMToolsGUI_PublishDlg::buildTree(_PTR(Study) theStudy, SalomeApp_DataObject* theItem) {
228   if(!theItem || theItem->isReference())
229     return;
230   
231   if(theItem != myGeomRoot) {
232
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();
240     }
241     
242     if(!isDrawable) {
243       QString aName;
244       if ( SO->FindAttribute(anAttr, "AttributeName") ) {
245         _PTR(AttributeName) aAttrName (anAttr);
246         aName = aAttrName->Value().c_str();
247       }
248       BufferedList aList;
249       QTreeWidgetItem* parentItem = findParentItem(theStudy, theItem, aList);
250       createItem(parentItem,qMakePair(theItem->entry(),aName),true);
251     }
252   }
253   
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));
258     itr++;
259   }
260 }
261
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() );
268   if(!app)
269     return;
270   
271   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
272   if(!appStudy ) 
273     return;
274
275   _PTR(Study) aStudy = appStudy->studyDS();
276   if(!aStudy)
277     return;
278
279
280   QList<QTreeWidgetItem*> toProcess;
281   getDrawableList(myTreeWidget->invisibleRootItem(), toProcess);
282
283   _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
284   for( int i = 0; i < toProcess.count(); i++) {
285     QTreeWidgetItem* item = toProcess[i];
286     if(item) {
287       QString entry = myEntryToItem.key(item);
288       _PTR(SObject) SO ( aStudy->FindObjectID(qPrintable(entry)) );
289       if(!SO) continue;
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() ) {
295           delete item;
296         } else {
297           item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
298           item->setData(0,Qt::CheckStateRole,QVariant());
299         }
300       }
301     }
302   }
303   toProcess.clear();
304   getTails(myTreeWidget->invisibleRootItem(), toProcess);
305   for( int i = 0; i < toProcess.count(); i++) {
306     if( toProcess[i] != myTreeWidget->invisibleRootItem() )
307       delete toProcess[i];
308   }
309       
310   app->updateObjectBrowser( false );
311   app->updateActions();
312 }
313
314 //=================================================================================
315 // function : clickOnOk()
316 // purpose  : Called then "Publish And Close" button clicked
317 //=================================================================================
318 void GEOMToolsGUI_PublishDlg::clickOnOk() {
319   clickOnApply();
320   accept();
321 }
322
323 //=================================================================================
324 // function : clickOnSelectAll()
325 // purpose  : Called then "Select All" button clicked
326 //=================================================================================
327 void GEOMToolsGUI_PublishDlg::clickOnSelectAll() {
328   processAll(myTreeWidget->invisibleRootItem(),Qt::Checked);
329 }
330
331 //=================================================================================
332 // function : clickOnUnSelectAll()
333 // purpose  : Called then "Unselect All" button clicked
334 //=================================================================================
335 void GEOMToolsGUI_PublishDlg::clickOnUnSelectAll() {
336   processAll(myTreeWidget->invisibleRootItem(),Qt::Unchecked);
337 }
338
339 //=================================================================================
340 // function : processAll()
341 // purpose  : 
342 //=================================================================================
343 void GEOMToolsGUI_PublishDlg::processAll(QTreeWidgetItem* theItem, Qt::CheckState state) {
344   
345     
346   if((theItem->flags() & Qt::ItemIsUserCheckable))
347     theItem->setCheckState(0,state);
348   
349   int i = 0;
350   while(i < theItem->childCount()) {
351     processAll(theItem->child(i), state);
352     i++;
353   }
354 }
355
356 //=================================================================================
357 // function : processAll()
358 // purpose  : 
359 //=================================================================================
360 void GEOMToolsGUI_PublishDlg::getDrawableList(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
361   
362   theItem->setData(0, Qt::UserRole, true);
363
364   if((theItem->flags() & Qt::ItemIsUserCheckable) ) {
365     theItem->setData(0, Qt::UserRole,theItem->checkState(0) == Qt::Checked);
366   }
367   
368   int i = 0;
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()) );
372     i++;
373   } 
374
375   if( (theItem->flags() & Qt::ItemIsUserCheckable) && (theItem->checkState(0) == Qt::Checked))
376     theList.push_back(theItem);
377 }
378
379
380 void GEOMToolsGUI_PublishDlg::getTails(QTreeWidgetItem* theItem, QList<QTreeWidgetItem*>& theList) {
381   int i = 0;
382   while(i < theItem->childCount()) {
383     getTails(theItem->child(i),theList);
384     i++;
385   }
386
387   if(theItem->data(0,Qt::UserRole).toBool())
388     theList.push_back(theItem);
389 }