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