Salome HOME
sources v1.2
[modules/kernel.git] / src / SALOMEGUI / QAD_NameBrowser.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_NameBrowser.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "QAD_NameBrowser.h"
31
32 #include "QAD_Study.h"
33 #include "QAD_Desktop.h"
34 #include "QAD_Application.h"
35 #include "QAD_Selection.h"
36 #include "QAD_InteractiveObject.h"
37
38 #include "utilities.h"
39
40 // IDL headers
41 #include <SALOMEconfig.h>
42 #include CORBA_SERVER_HEADER(SALOMEDS)
43
44 #include <qcombobox.h>
45 #include <qgroupbox.h>
46 #include <qlabel.h>
47 #include <qlistview.h>
48 #include <qpushbutton.h>
49 #include <qtoolbutton.h>
50 #include <qlayout.h>
51 #include <qvariant.h>
52 #include <qtooltip.h>
53 #include <qwhatsthis.h>
54
55 /*! 
56   Constructs a QAD_NameBrowser which is a child of 'parent', with the 
57   name 'name'
58 */
59 QAD_NameBrowser::QAD_NameBrowser( QWidget* parent,  const char* name)
60   : QMainWindow( parent, name, WStyle_NormalBorder | 
61                  WStyle_MinMax | WStyle_SysMenu | WDestructiveClose  )
62 {
63     if ( !name )
64         setName( "QAD_NameBrowser" );
65
66     resize( 321, 280 ); 
67     setCaption( name );
68
69     GroupBox1 = new QGroupBox( this, "GroupBox1" );
70     GroupBox1->setGeometry( QRect( 10, 10, 300, 50 ) ); 
71     GroupBox1->setTitle( tr( "Filters"  ) );
72
73     ComboBox1 = new QComboBox( TRUE, GroupBox1 );
74     ComboBox1->setGeometry( QRect( 10, 20, 120, 21 ) ); 
75     ComboBox1->insertItem( tr( "Type"  ) );
76
77     connect( ComboBox1, SIGNAL( textChanged(const QString &) ), this, SLOT( updateFilters() ) );
78
79
80     PushButton5 = new QPushButton( tr( " = "  ), GroupBox1);
81     PushButton5->setGeometry( QRect( 140, 20, 30, 21 ) ); 
82
83     ComboBox2 = new QComboBox( TRUE, GroupBox1);
84     ComboBox2->setGeometry( QRect( 180, 20, 111, 21 ) ); 
85     ComboBox2->insertItem( tr( "All"  ) );
86     connect( ComboBox2, SIGNAL( textChanged(const QString &) ), this, SLOT( updateFilters() ) );
87
88     GroupBox2 = new QGroupBox( this, "GroupBox2" );
89     GroupBox2->setGeometry( QRect( 10, 60, 300, 200 ) ); 
90     GroupBox2->setTitle( tr( "Objects"  ) );
91
92     PushButton1 = new QPushButton( tr( "All"  ), GroupBox2 );
93     PushButton1->setGeometry( QRect( 10, 170, 71, 21 ) ); 
94     connect( PushButton1, SIGNAL( clicked() ), this, SLOT( selectAllFilteredObjects() ) );
95
96     ListView1 = new QListView( GroupBox2 );
97     ListView1->setGeometry( QRect( 10, 20, 280, 140 ) ); 
98     ListView1->addColumn("");
99     ListView1->addColumn("");
100     ListView1->addColumn("");
101     ListView1->setColumnText( 0, tr( "Filtered"  ) );
102     ListView1->setColumnWidth(0, 130);
103     ListView1->setRootIsDecorated(TRUE);
104     ListView1->setMultiSelection (TRUE);
105     ListView1->setSelectionMode ( QListView::Extended );
106
107     connect (ListView1,
108              SIGNAL( selectionChanged() ),
109              this, SLOT(onFilteredObjects()) );
110
111     initFilters();
112 }
113
114 /*!
115   Returns reference to QListView
116 */
117 QListView* QAD_NameBrowser::getListView() const
118 {
119   return ListView1;
120 }
121
122 /*!  
123   Destroys the object and frees any allocated resources
124 */
125 QAD_NameBrowser::~QAD_NameBrowser()
126 {
127   // no need to delete child widgets, Qt does it all for us
128 }
129
130 void QAD_NameBrowser::selectAllFilteredObjects()
131 {
132   ListView1->selectAll( true );
133 }
134
135 void QAD_NameBrowser::updateFilters()
136 {
137   initFilters();
138 }
139
140 void QAD_NameBrowser::initFilters()
141 {
142   QAD_Desktop*     myDesktop = QAD_Application::getDesktop();
143   QAD_Study*   myActiveStudy = myDesktop->getActiveStudy();
144   SALOMEDS::Study_var aStudy = myActiveStudy->getStudyDocument();
145
146   Update( aStudy );
147
148
149 //    QAD_Selection* Sel = QAD_Selection::Selection( myActiveStudy->getSelection() );
150 //    const QList<QAD_InteractiveObject>& ListIO = Sel->getInteractiveObjects();
151 //    int NbSelected = ListIO.count();
152 //    if ( NbSelected == 0 ) {
153 //      // setFilter "All"
154 //      Update( aStudy );
155
156 //    } else if ( NbSelected == 1 ) {
157 //      // setFilter "type of selected object"
158 //      QAD_InteractiveObject* IObject = Sel->getFisrtInteractiveObject();
159 //      SALOMEDS::SComponent_var SC = SALOMEDS::SComponent::_narrow ( myActiveStudy->FindObjectIOR( IObject->getFatherIOR() ));
160 //      if ( !SC->_is_nil() ) {
161 //        char* ObjectComp = "";
162 //        if ( SC->GetAttribute(SALOMEDS::Name, ObjectComp)) {
163 //      Update( aStudy, SC );
164 //        }
165 //      }
166
167 //    } else {
168 //  //      const QList<QAD_InteractiveObject>& ListIO = Sel->getInteractiveObjects();
169 //  //      QListIterator<QAD_InteractiveObject> it( Sel->getInteractiveObjects() );
170 //  //      for ( ; it.current(); ++it ) {
171 //  //        QAD_InteractiveObject* IObject = it.current();
172 //  //        QString IOR = IObject->getFatherIOR() ));
173
174 //  //      }
175
176 //      // heterogeneous selection -> setFilter "All"
177 //      Update( aStudy );
178
179 //      // homogeneous selection -> setFilter "type of selected objects"
180 //    }
181 }
182
183 void QAD_NameBrowser::onFilteredObjects()
184 {
185   QAD_Study*      myActiveStudy  = QAD_Application::getDesktop()->getActiveStudy();
186   SALOMEDS::Study_var    aStudy  = myActiveStudy->getStudyDocument();
187   QAD_Selection*            Sel  = QAD_Selection::Selection( myActiveStudy->getSelection() );
188   QAD_StudyFrame* myActiveSFrame = myActiveStudy->getActiveStudyFrame();
189
190   QString             ActiveComp = QAD_Application::getDesktop()->getActiveComponent();
191
192   MESSAGE ( "QAD_NAMEBROWSER - NB SELECTED INTERACTIVE OBJECT : " << Sel->getInteractiveObjectCount() )
193
194   QList<QAD_InteractiveObject> DeltaPos;
195   DeltaPos.clear();
196   QList<QAD_InteractiveObject> DeltaNeg;
197   DeltaNeg.clear();
198
199   QListViewItemIterator it( ListView1 );
200   for ( ; it.current(); ++it ) {
201     QListViewItem* item = it.current();
202     QString theIOR = item->text(2);
203     QString theEntry = item->text(1);
204
205     if ( ActiveComp.isEmpty() ) {/* Neutral point = no component loaded */
206       if ( !theEntry.isEmpty() && theIOR.isEmpty() ) { /* A component may be selected */
207         if ( item->isSelected() ) {
208           bool itemAlreadySelected = false;
209           
210           int nbSel = Sel->getInteractiveObjectCount();
211           for ( int i = 0; i < nbSel; i++ ) {
212             QAD_InteractiveObject* SO = Sel->getInteractiveObject(i);
213             if ( SO->getIOR().compare(item->text(1)) == 0 ) {
214               itemAlreadySelected = true;
215               break;
216             }
217           }
218           if (!itemAlreadySelected) {
219             QAD_InteractiveObject* SO = new QAD_InteractiveObject( item->text(1), "" ); 
220             DeltaPos.append( SO );
221           }
222         } else {
223           int nbSel = Sel->getInteractiveObjectCount();
224           for ( int i = 0; i < nbSel; i++ ) {
225             QAD_InteractiveObject* SO = Sel->getInteractiveObject(i);
226             if ( SO->getIOR().compare(item->text(1)) == 0 ) {
227               DeltaNeg.append( SO );
228               break;
229             }
230           }
231         }
232         
233       } else { /* An object is selected */
234         if ( item->isSelected() ) {
235           item->setSelected( false );
236           item->repaint();
237         }
238       }
239        
240     } else {
241       if ( !theIOR.isEmpty() ) {
242         if ( item->isSelected() ) {
243           bool itemAllreadySelected = false;
244           
245           int nbSel = Sel->getInteractiveObjectCount();
246           for ( int i = 0; i < nbSel; i++ ) {
247             QAD_InteractiveObject* SO = Sel->getInteractiveObject(i);
248             if ( SO->getIOR().compare(theIOR) == 0 ) {
249               itemAllreadySelected = true;
250               break;
251             }
252           }
253           
254           if (!itemAllreadySelected) {
255             SALOMEDS::SObject_var obj = aStudy->FindObjectID(theEntry.latin1());
256             SALOMEDS::SComponent_var theComponent = obj->GetFatherComponent();
257             SALOMEDS::GenericAttribute_var anAttr;
258             SALOMEDS::AttributeIOR_var     anIOR;
259             Standard_CString ior  =" ";
260             Standard_CString iorFather  =" ";
261             if (obj->FindAttribute(anAttr, "AttributeIOR")) {
262               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
263               ior = anIOR->Value();
264             }
265             theComponent->ComponentIOR(iorFather);
266             QAD_InteractiveObject* SO = new QAD_InteractiveObject( QString(ior), 
267                                                                    QString(iorFather),
268                                                                    QString(theEntry) );
269             DeltaPos.append( SO );
270           }
271         } else {
272           int nbSel = Sel->getInteractiveObjectCount();
273           for ( int i = 0; i < nbSel; i++ ) {
274             QAD_InteractiveObject* SO = Sel->getInteractiveObject(i);
275             if ( SO->getIOR().compare(theIOR) == 0 ) {
276               DeltaNeg.append( SO );
277               break;
278             }
279           }
280           
281         }
282       } else if ( !theEntry.isEmpty() && theIOR.isEmpty() ) {
283         if ( item->isSelected() ) {
284           bool itemAllreadySelected = false;
285           
286           int nbSel = Sel->getInteractiveObjectCount();
287           for ( int i = 0; i < nbSel; i++ ) {
288             QAD_InteractiveObject* SO = Sel->getInteractiveObject(i);
289             if ( SO->getEntry().compare(theEntry) == 0 ) {
290               itemAllreadySelected = true;
291               break;
292             }
293           }
294           if (!itemAllreadySelected) {
295             SALOMEDS::SObject_var obj = aStudy->FindObjectID(theEntry.latin1());
296             SALOMEDS::SComponent_var theComponent = obj->GetFatherComponent();
297             SALOMEDS::GenericAttribute_var anAttr;
298             SALOMEDS::AttributeIOR_var     anIOR;
299             Standard_CString ior  =" ";
300             Standard_CString iorFather  =" ";
301             if (obj->FindAttribute(anAttr, "AttributeIOR")) {
302               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
303               ior = anIOR->Value();
304             }
305             theComponent->ComponentIOR(iorFather);
306             QAD_InteractiveObject* SO = new QAD_InteractiveObject( QString(ior), 
307                                                                    QString(iorFather),
308                                                                    QString(theEntry) );
309             DeltaPos.append( SO );
310           }
311         } else {
312           int nbSel = Sel->getInteractiveObjectCount();
313           for ( int i = 0; i < nbSel; i++ ) {
314             QAD_InteractiveObject* SO = Sel->getInteractiveObject(i);
315             if ( SO->getEntry().compare(theEntry) == 0 ) {
316               DeltaNeg.append( SO );
317               break;
318             }
319           }
320           
321         }
322       } else {
323         item->setSelected( false );
324         item->repaint();
325       }
326     }
327   }
328
329   MESSAGE ( "NAMEBROWSER DeltaNeg.count() == " << DeltaNeg.count() )
330   for ( QAD_InteractiveObject* SO = DeltaNeg.first(); SO;  SO = DeltaNeg.next() ) {
331     MESSAGE ( "------" << SO->getIOR() )
332     Sel->RemoveInteractiveObject(SO);
333   }
334   MESSAGE ( "NAMEBROWSER DeltaPos.count() == " << DeltaPos.count() )
335   for ( QAD_InteractiveObject* SO = DeltaPos.first(); SO;  SO = DeltaPos.next() ) {
336     MESSAGE ( "++++++" << SO->getIOR() )
337     Sel->AddInteractiveObject(SO);
338   }
339 }
340
341 /*!
342   Update
343 */
344 void QAD_NameBrowser::Update(SALOMEDS::Study_ptr Study,
345                              SALOMEDS::SObject_ptr SO)
346 {
347   SALOMEDS::SObject_var RefSO;
348   SALOMEDS::ChildIterator_var it = Study->NewChildIterator(SO);
349
350   for (; it->More();it->Next()) {
351     SALOMEDS::SObject_var CSO= it->Value();
352     SALOMEDS::GenericAttribute_var anAttr;
353     SALOMEDS::AttributeName_var    aName;
354     SALOMEDS::AttributeIOR_var     anIOR;
355     QListViewItem* Item;
356
357     if (CSO->FindAttribute(anAttr, "AttributeName")) {
358       aName = SALOMEDS::AttributeName::_narrow(anAttr);
359       if (CSO->FindAttribute(anAttr, "AttributeIOR")) {
360         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
361         Item = AddItem ( aName->Value(), CSO->GetID(), anIOR->Value());
362       }
363     }
364
365     Update(Study,CSO);
366   }
367 }
368
369 /*!
370   Update
371 */
372 void QAD_NameBrowser::Update(SALOMEDS::Study_ptr Study,
373                              SALOMEDS::SComponent_ptr SC)
374 {
375   ListView1->clear();
376
377   if (Study->_is_nil()) {
378     return;
379   }
380
381 //  Standard_CString name;
382   Standard_CString dataType;
383   Standard_CString ior="";
384
385   QListViewItem* Item;
386
387   SALOMEDS::SComponentIterator_var itcomp;
388   SALOMEDS::GenericAttribute_var anAttr;
389   SALOMEDS::AttributeName_var    aName;
390   SALOMEDS::AttributeIOR_var     anIOR;
391
392   if ( !SC->_is_nil() ) {
393     if (SC->FindAttribute(anAttr, "AttributeName")) {
394       aName = SALOMEDS::AttributeName::_narrow(anAttr);
395       Item = AddItem (aName->Value(),SC->GetID(),ior);
396     } else {
397       Item = AddItem (dataType,SC->GetID(),ior);
398     }
399     Update ( Study, SALOMEDS::SObject::_narrow(SC) );
400     
401   } else {
402     itcomp = Study->NewComponentIterator();
403     for (; itcomp->More(); itcomp->Next()) {
404       SALOMEDS::SComponent_var SC1 = itcomp->Value();
405       dataType = SC1->ComponentDataType();
406       
407       if (SC1->FindAttribute(anAttr, "AttributeIOR")) {
408         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
409         ior = anIOR->Value();
410       }
411       
412       if ( QString(dataType).compare("Interface Applicative") != 0 ) {
413         if (SC1->FindAttribute(anAttr, "AttributeName")) {
414           aName = SALOMEDS::AttributeName::_narrow(anAttr);
415           Item = AddItem (aName->Value(), SC1->GetID(), ior);
416         } else {
417           Item = AddItem (dataType, SC1->GetID(), ior);
418         }
419         
420         Update ( Study, SALOMEDS::SObject::_narrow(SC1) );
421       }
422     }
423   }
424 }
425
426 QListViewItem* QAD_NameBrowser::AddItem(const QString& theText,
427                                         const QString& theEntry,
428                                         const QString& theIOR)
429
430 {
431   QListViewItem* anItem = new QListViewItem( ListView1 );
432   anItem->setText(0, theText);
433   anItem->setText(1, theEntry);
434   anItem->setText(2, theIOR);
435
436   return anItem;
437 }
438
439 /*!
440   Called when NameBrowser is about to close
441 */
442 void QAD_NameBrowser::closeEvent(QCloseEvent* e)
443 {
444   QAD_Application::getDesktop()->closeNameBrowser();
445   e->accept();
446 }