Salome HOME
6f2a9860cb6bae21574bf84deb12d5ae37d30ecc
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupDlg.cxx
1 //  SMESH SMESHGUI : GUI for SMESH component
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.salome-platform.org or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESHGUI_GroupDlg.cxx
25 //  Author : Natalia KOPNOVA
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESHGUI_GroupDlg.h"
30 #include "SMESHGUI_FilterDlg.h"
31 #include "SMESHGUI_Filter.h"
32
33 #include "SMESHGUI.h"
34 #include "SMESHGUI_Utils.h"
35 #include "SMESHGUI_VTKUtils.h"
36 #include "SMESHGUI_GroupUtils.h"
37 #include "SMESHGUI_FilterUtils.h"
38 #include "SMESHGUI_GEOMGenUtils.h"
39
40 #include "SALOMEGUI_QtCatchCorbaException.hxx"
41 #include "SALOME_ListIteratorOfListIO.hxx"
42 #include "VTKViewer_ViewFrame.h"
43 #include "QAD_Application.h"
44 #include "QAD_Desktop.h"
45 #include "QAD_MessageBox.h"
46 #include "QAD_RightFrame.h"
47 #include "utilities.h"
48
49 #include "SMESH_Actor.h"
50
51 #include "GEOMBase.h"
52
53 // QT Includes
54 #include <qbuttongroup.h>
55 #include <qgroupbox.h>
56 #include <qhbox.h>
57 #include <qlabel.h>
58 #include <qlineedit.h>
59 #include <qpushbutton.h>
60 #include <qradiobutton.h>
61 #include <qcheckbox.h>
62 #include <qlayout.h>
63 #include <qlistbox.h>
64 #include <qimage.h>
65 #include <qpixmap.h>
66 #include <qmemarray.h>
67
68 // STL includes
69 #include <vector>
70 #include <algorithm>
71
72 using namespace std;
73
74 //=================================================================================
75 // class    : SMESHGUI_GroupDlg()
76 // purpose  : 
77 //=================================================================================
78 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( QWidget* parent, const char* name, SALOME_Selection* theSel,
79                                       SMESH::SMESH_Mesh_ptr theMesh, bool modal, WFlags fl )
80   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
81 {
82   if ( !name ) setName( "SMESHGUI_GroupDlg" );
83   initDialog(theSel, true);
84   init(theMesh);
85
86   /* Move widget on the botton right corner of main widget */
87   int x, y ;
88   mySMESHGUI->DefineDlgPosition(this, x, y);
89   this->move(x, y);
90 }
91
92 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( QWidget* parent, const char* name, SALOME_Selection* theSel,
93                                       SMESH::SMESH_Group_ptr theGroup, bool modal, WFlags fl )
94   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
95 {
96   if ( !name ) setName( "SMESHGUI_GroupDlg" );
97   initDialog(theSel, false);
98   init(theGroup);
99
100   /* Move widget on the botton right corner of main widget */
101   int x, y ;
102   mySMESHGUI->DefineDlgPosition(this, x, y);
103   this->move(x, y);
104 }
105
106 void SMESHGUI_GroupDlg::initDialog(SALOME_Selection* theSel, bool create)
107 {
108   myFilterDlg = 0;
109   
110   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
111
112   if (create)
113     setCaption( tr( "SMESH_CREATE_GROUP_TITLE"  ) );
114   else 
115     setCaption( tr( "SMESH_EDIT_GROUP_TITLE"  ) );
116   setSizeGripEnabled( TRUE );
117
118   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 11, 6);
119   
120   /***************************************************************/
121   myTypeGroup = new QButtonGroup(1, Qt::Vertical, this, "Group types");
122   myTypeGroup->setTitle(tr("SMESH_ELEMENTS_TYPE"));
123   myTypeGroup->setExclusive(true);
124
125   QStringList types;
126   types.append(tr("MESH_NODE"));
127   types.append(tr("SMESH_EDGE"));
128   types.append(tr("SMESH_FACE"));
129   types.append(tr("SMESH_VOLUME"));
130   QRadioButton* rb;
131   for (int i = 0; i < types.count(); i++) {
132     rb = new QRadioButton(types[i], myTypeGroup);
133   }
134   myTypeGroup->setEnabled(create);
135   myTypeId = -1;
136     
137   /***************************************************************/
138   QHBox* aNameBox = new QHBox(this, "name box");
139   QLabel* aName = new QLabel(aNameBox, "name label");
140   aName->setText(tr("SMESH_NAME"));
141   aName->setMinimumSize(50,0);
142   myName = new QLineEdit(aNameBox, "name");
143     
144   /***************************************************************/
145   QGroupBox* aContentBox = new QGroupBox(1, Qt::Horizontal, this, "content box");
146   aContentBox->setTitle(tr("SMESH_CONTENT"));
147   QFrame* aContent = new QFrame(aContentBox, "content");
148   QGridLayout* aLayout = new QGridLayout(aContent, 7, 4);
149   aLayout->setSpacing(6);
150   aLayout->setAutoAdd(false);
151
152   QLabel* aLabel = new QLabel(aContent, "elements label");
153   aLabel->setText(tr("SMESH_ID_ELEMENTS"));
154   myElements = new QListBox(aContent, "elements list");
155   myElements->setSelectionMode(QListBox::Extended);
156   //  myElements->setMinimumHeight(150);
157
158   myFilter = new QPushButton(aContent, "filter");
159   myFilter->setText(tr("SMESH_BUT_FILTER"));
160   QPushButton* aAddBtn = new QPushButton(aContent, "add");
161   aAddBtn->setText(tr("SMESH_BUT_ADD"));
162   QPushButton* aRemoveBtn = new QPushButton(aContent, "remove");
163   aRemoveBtn->setText(tr("SMESH_BUT_REMOVE"));
164   QPushButton* aSortBtn = new QPushButton(aContent, "sort");
165   aSortBtn->setText(tr("SMESH_BUT_SORT"));
166
167   aLayout->addWidget(aLabel, 0, 0);
168   aLayout->addMultiCellWidget(myElements, 1, 6, 0, 0);
169   aLayout->addWidget(myFilter, 1, 2);
170   aLayout->addWidget(aAddBtn, 3, 2);
171   aLayout->addWidget(aRemoveBtn, 4, 2);
172   aLayout->addWidget(aSortBtn, 6, 2);
173
174   aLayout->setColStretch(0, 1);
175   aLayout->addColSpacing(1, 20);
176   aLayout->addColSpacing(3, 20);
177   aLayout->setRowStretch(2, 1);
178   aLayout->setRowStretch(5, 1);
179
180   aContentBox->setMinimumHeight(aContent->sizeHint().height() + 
181                                 aContentBox->sizeHint().height());
182
183   /***************************************************************/
184   QGroupBox* aSelectBox = new QGroupBox(3, Qt::Horizontal, this, "select box");
185   aSelectBox->setTitle(tr("SMESH_SELECT_FROM"));
186   
187   mySelectSubMesh = new QCheckBox(aSelectBox, "submesh checkbox");
188   mySelectSubMesh->setText(tr("SMESH_SUBMESH"));
189   mySelectSubMesh->setMinimumSize(50, 0);
190   mySubMeshBtn = new QPushButton(aSelectBox, "submesh button");
191   mySubMeshBtn->setText("");
192   mySubMeshBtn->setPixmap(image0);
193   mySubMeshLine = new QLineEdit(aSelectBox, "submesh line");
194   mySubMeshLine->setReadOnly(true);
195   onSelectSubMesh(false);
196   
197   mySelectGroup = new QCheckBox(aSelectBox, "group checkbox");
198   mySelectGroup->setText(tr("SMESH_GROUP"));
199   mySelectGroup->setMinimumSize(50, 0);
200   myGroupBtn = new QPushButton(aSelectBox, "group button");
201   myGroupBtn->setText("");
202   myGroupBtn->setPixmap(image0);
203   myGroupLine = new QLineEdit(aSelectBox, "group line");
204   myGroupLine->setReadOnly(true);
205   onSelectGroup(false);
206   
207   mySelectGeomGroup = new QCheckBox(aSelectBox, "geometry group checkbox");
208   mySelectGeomGroup->setText(tr("SMESH_GEOM_GROUP"));
209   mySelectGeomGroup->setMinimumSize(50, 0);
210   mySelectGeomGroup->setEnabled(create);
211   myGeomGroupBtn = new QPushButton(aSelectBox, "geometry group button");
212   myGeomGroupBtn->setText("");
213   myGeomGroupBtn->setPixmap(image0);
214   myGeomGroupLine = new QLineEdit(aSelectBox, "geometry group line");
215   myGeomGroupLine->setReadOnly(true);
216   onSelectGeomGroup(false);
217   
218   aSelectBox->setMinimumHeight(137);
219   aSelectBox->setMinimumWidth(305);
220
221   /***************************************************************/
222   QFrame* aButtons = new QFrame(this, "button box");
223   aButtons->setFrameStyle(QFrame::Box | QFrame::Sunken);
224   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons, 11, 6);
225   aBtnLayout->setAutoAdd(false);
226
227   QPushButton* aOKBtn = new QPushButton(aButtons, "ok");
228   aOKBtn->setText(tr("SMESH_BUT_OK"));
229   aOKBtn->setAutoDefault(true);
230   aOKBtn->setDefault(true);
231   QPushButton* aApplyBtn = new QPushButton(aButtons, "apply");
232   aApplyBtn->setText(tr("SMESH_BUT_APPLY"));
233   aApplyBtn->setAutoDefault(true);
234   QPushButton* aCloseBtn = new QPushButton(aButtons, "close");
235   aCloseBtn->setText(tr("SMESH_BUT_CLOSE"));
236   aCloseBtn->setAutoDefault(true);
237
238   aBtnLayout->addWidget(aOKBtn);
239   aBtnLayout->addWidget(aApplyBtn);
240   aBtnLayout->addStretch();
241   aBtnLayout->addWidget(aCloseBtn);
242
243   /***************************************************************/
244   aMainLayout->addWidget(myTypeGroup);
245   aMainLayout->addWidget(aNameBox);
246   aMainLayout->addWidget(aContentBox);
247   aMainLayout->addWidget(aSelectBox);
248   aMainLayout->addWidget(aButtons);
249
250   /* signals and slots connections */
251   connect(myTypeGroup, SIGNAL(clicked(int)), this, SLOT(onTypeChanged(int)));
252
253   connect(myName, SIGNAL(textChanged(const QString&)), this, SLOT(onNameChanged(const QString&)));
254   connect(myElements, SIGNAL(selectionChanged()), this, SLOT(onListSelectionChanged()));
255
256   connect(myFilter, SIGNAL(clicked()), this, SLOT(setFilters()));
257   connect(aAddBtn, SIGNAL(clicked()), this, SLOT(onAdd()));
258   connect(aRemoveBtn, SIGNAL(clicked()), this, SLOT(onRemove()));
259   connect(aSortBtn, SIGNAL(clicked()), this, SLOT(onSort()));
260
261   connect(mySelectSubMesh, SIGNAL(toggled(bool)), this, SLOT(onSelectSubMesh(bool)));
262   connect(mySelectGroup, SIGNAL(toggled(bool)), this, SLOT(onSelectGroup(bool)));
263   connect(mySelectGeomGroup, SIGNAL(toggled(bool)), this, SLOT(onSelectGeomGroup(bool)));
264   connect(mySubMeshBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
265   connect(myGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
266   connect(myGeomGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
267
268
269   connect(aOKBtn, SIGNAL(clicked()), this, SLOT(onOK()));
270   connect(aApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
271   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(onClose()));
272
273   /* Init selection */
274   mySelection = theSel;  
275   mySMESHGUI = SMESHGUI::GetSMESHGUI();
276   mySMESHGUI->SetActiveDialogBox(this);
277   mySMESHGUI->SetState(800);
278
279   mySelectionMode = -1;
280   mySubMeshFilter = new SMESH_TypeFilter(SUBMESH);
281   myGroupFilter = new SMESH_TypeFilter(GROUP);
282
283   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(onDeactivate()));
284   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(onClose()));
285   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(onObjectSelectionChanged()));
286
287   updateButtons();
288 }
289
290 //=================================================================================
291 // function : ~SMESHGUI_GroupDlg()
292 // purpose  : Destroys the object and frees any allocated resources
293 //=================================================================================
294 SMESHGUI_GroupDlg::~SMESHGUI_GroupDlg()
295 {
296     // no need to delete child widgets, Qt does it all for us
297   if ( myFilterDlg != 0 )
298   {
299     myFilterDlg->reparent( 0, QPoint() );
300     delete myFilterDlg;
301   }
302 }
303
304
305 //=================================================================================
306 // function : Init()
307 // purpose  :
308 //=================================================================================
309 void SMESHGUI_GroupDlg::init(SMESH::SMESH_Mesh_ptr theMesh)
310 {
311   /* init data from current selection */
312   myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
313   myGroup = SMESH::SMESH_Group::_nil();
314
315   myActor = SMESH::FindActorByObject(myMesh);
316   SMESH::SetPickable(myActor);
317
318   myTypeGroup->setButton(0);
319   onTypeChanged(0);
320 }
321
322 //=================================================================================
323 // function : Init()
324 // purpose  :
325 //=================================================================================
326 void SMESHGUI_GroupDlg::init(SMESH::SMESH_Group_ptr theGroup)
327 {
328   myMesh = theGroup->GetMesh();
329   myGroup = SMESH::SMESH_Group::_duplicate(theGroup);
330   
331   myActor = SMESH::FindActorByObject(myMesh);
332   if ( !myActor )
333     myActor = SMESH::FindActorByObject(myGroup);
334   SMESH::SetPickable(myActor);
335
336   int aType = 0;
337   switch(theGroup->GetType()) {
338   case SMESH::NODE: aType= 0; break;
339   case SMESH::EDGE: aType = 1; break;
340   case SMESH::FACE: aType = 2; break;
341   case SMESH::VOLUME: aType = 3; break;
342   } 
343   myTypeGroup->setButton(aType);
344   onTypeChanged(aType);
345
346   myName->setText(myGroup->GetName());
347   myName->home(false);
348
349   if (!theGroup->IsEmpty()) {
350     SMESH::long_array_var anElements = myGroup->GetListOfID();
351     int k = anElements->length();
352     for (int i = 0; i < k; i++) {
353       myIdList.append(anElements[i]);
354       myElements->insertItem(QString::number(anElements[i]));
355     }
356     myElements->selectAll(true);
357   }
358 }
359
360
361 //=================================================================================
362 // function : updateButtons()
363 // purpose  : 
364 //=================================================================================
365 void SMESHGUI_GroupDlg::updateButtons()
366 {
367   bool enable = !myName->text().stripWhiteSpace().isEmpty() && myElements->count() > 0;
368   QPushButton* aBtn;
369   aBtn = (QPushButton*) child("ok", "QPushButton");
370   if (aBtn) aBtn->setEnabled(enable);
371   aBtn = (QPushButton*) child("apply", "QPushButton");
372   if (aBtn) aBtn->setEnabled(enable);
373 }
374
375 //=================================================================================
376 // function : onNameChanged()
377 // purpose  : 
378 //=================================================================================
379 void SMESHGUI_GroupDlg::onNameChanged(const QString& text)
380 {
381   updateButtons();
382 }
383
384 //=================================================================================
385 // function : onTypeChanged()
386 // purpose  : Radio button management
387 //=================================================================================
388 void SMESHGUI_GroupDlg::onTypeChanged(int id)
389 {
390   if (myTypeId != id) {
391     myElements->clear();
392     if (myCurrentLineEdit == 0)
393       setSelectionMode(id);
394   }
395   myTypeId = id;
396 }
397
398 //=================================================================================
399 // function : setSelectionMode()
400 // purpose  : Radio button management
401 //=================================================================================
402 void SMESHGUI_GroupDlg::setSelectionMode(int theMode)
403 {
404   if (mySelectionMode != theMode) {
405     mySelection->ClearIObjects();
406     mySelection->ClearFilters();
407     SMESH::SetPointRepresentation(false);
408     if (theMode < 4) {
409       switch(theMode){
410       case 0:
411         if ( myActor )
412           myActor->SetPointRepresentation(true);
413         else
414           SMESH::SetPointRepresentation(true);
415         QAD_Application::getDesktop()->SetSelectionMode(NodeSelection, true);
416         break;
417       case 1:
418         QAD_Application::getDesktop()->SetSelectionMode(EdgeSelection, true);
419         break;
420       case 2:
421         QAD_Application::getDesktop()->SetSelectionMode(FaceSelection, true);
422         break;
423       default:
424         QAD_Application::getDesktop()->SetSelectionMode(VolumeSelection, true);
425       }
426     }
427     else {
428       QAD_Application::getDesktop()->SetSelectionMode(ActorSelection, true);
429       if (theMode == 4)
430         mySelection->AddFilter(mySubMeshFilter);
431       else if (theMode == 5)
432         mySelection->AddFilter(myGroupFilter);
433     }
434     mySelectionMode = theMode;
435   }
436
437
438 //=================================================================================
439 // function : onApply()
440 // purpose  :
441 //=================================================================================
442 bool SMESHGUI_GroupDlg::onApply()
443 {
444   if (mySMESHGUI->ActiveStudyLocked())
445     return false;
446   if (!myName->text().stripWhiteSpace().isEmpty() && myElements->count() > 0) {
447     mySelection->ClearIObjects();
448     if (myGroup->_is_nil()) {
449       SMESH::ElementType aType = SMESH::ALL;
450       switch(myTypeId) {
451       case 0: aType = SMESH::NODE; break;
452       case 1: aType = SMESH::EDGE; break;
453       case 2: aType = SMESH::FACE; break;
454       case 3: aType = SMESH::VOLUME; break;
455       }
456       SMESH::long_array_var anIdList = new SMESH::long_array;
457       int i, k = myElements->count();
458       anIdList->length(k);
459       QListBoxItem* anItem;
460       for (i = 0, anItem = myElements->firstItem(); anItem != 0; i++, anItem = anItem->next()) {
461         anIdList[i] = anItem->text().toInt();
462       }
463
464       myGroup = SMESH::AddGroup(myMesh, aType, myName->text());
465       myGroup->Add(anIdList.inout());
466
467       //Add reference to geometry group if it is neccessary
468       if (!CORBA::is_nil( myGeomGroup ))
469         {
470           SALOMEDS::Study_var aStudy = SMESH::GetActiveStudyDocument();
471           SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
472           SALOMEDS::SObject_var aGeomGroupSO = aStudy->FindObjectIOR( aStudy->ConvertObjectToIOR(myGeomGroup) );
473           SALOMEDS::SObject_var aMeshGroupSO = aStudy->FindObjectIOR( aStudy->ConvertObjectToIOR(myGroup) );
474           SALOMEDS::SObject_var aReference = aStudyBuilder->NewObject(aMeshGroupSO);
475           aStudyBuilder->Addreference(aReference, aGeomGroupSO);
476         }
477       
478       /* init for next operation */
479       myName->setText("");
480       myElements->clear();
481       myGroup = SMESH::SMESH_Group::_nil();
482     }
483     else {
484       myGroup->SetName(myName->text());
485
486       QValueList<int> aAddList;
487       QValueList<int>::iterator anIt;
488       QListBoxItem* anItem;
489       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
490         int anId = anItem->text().toInt();
491         if ((anIt = myIdList.find(anId)) == myIdList.end())
492           aAddList.append(anId);
493         else
494           myIdList.remove(anIt);
495       }
496       if (!aAddList.empty()) {
497         SMESH::long_array_var anIdList = new SMESH::long_array;
498         anIdList->length(aAddList.count());
499         int i;
500         for (i = 0, anIt = aAddList.begin(); anIt != aAddList.end(); anIt++, i++)
501           anIdList[i] = *anIt;
502         myGroup->Add(anIdList.inout());
503       }
504       if (!myIdList.empty()) {
505         SMESH::long_array_var anIdList = new SMESH::long_array;
506         anIdList->length(myIdList.count());
507         int i;
508         for (i = 0, anIt = myIdList.begin(); anIt != myIdList.end(); anIt++, i++)
509           anIdList[i] = *anIt;
510         myGroup->Remove(anIdList.inout());
511       }
512       /* init for next operation */
513       myIdList.clear();
514       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next())
515         myIdList.append(anItem->text().toInt());
516     }
517
518     mySMESHGUI->GetActiveStudy()->updateObjBrowser(true);
519     SMESH::UpdateView(); // asv: fix of BUG PAL5515
520     mySelection->ClearIObjects();
521     return true;
522   }
523   return false;
524 }
525
526 //=================================================================================
527 // function : onOK()
528 // purpose  :
529 //=================================================================================
530 void SMESHGUI_GroupDlg::onOK()
531 {
532   if ( onApply() )
533     onClose();
534 }
535
536 static bool busy = false;
537 //=================================================================================
538 // function : onListSelectionChanged()
539 // purpose  : Called when selection in element list is changed
540 //=================================================================================
541 void SMESHGUI_GroupDlg::onListSelectionChanged()
542 {
543   //  MESSAGE("SMESHGUI_GroupDlg::onListSelectionChanged(); myActor = " << myActor);
544   if (busy || !myActor) return;
545   busy = true;
546
547   if (myCurrentLineEdit == 0) {
548     mySelection->ClearIObjects();
549     TColStd_MapOfInteger aIndexes;
550     QListBoxItem* anItem;
551     for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
552       if (anItem->isSelected()) {
553         int anId = anItem->text().toInt();
554         aIndexes.Add(anId);
555       }
556     }
557     mySelection->AddOrRemoveIndex(myActor->getIO(), aIndexes, false, false);
558     mySelection->AddIObject(myActor->getIO());
559   }
560   busy = false;
561 }
562
563 //=================================================================================
564 // function : onObjectSelectionChanged()
565 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
566 //=================================================================================
567 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
568 {
569   if (busy || !isEnabled()) return;
570   busy = true;
571
572   int aNbSel = mySelection->IObjectCount();
573   myElements->clearSelection();
574  
575   if (myCurrentLineEdit) {
576     myCurrentLineEdit->setText("") ;
577     QString aString = "";
578     
579     if (myCurrentLineEdit == myGeomGroupLine)
580       {
581         if(aNbSel != 1)
582           {
583             myGeomGroup = GEOM::GEOM_Object::_nil();
584             busy = false;
585             return;
586           }
587         Standard_Boolean testResult = Standard_False;
588         myGeomGroup = GEOMBase::ConvertIOinGEOMObject(mySelection->firstIObject(), testResult );
589         
590         // Check if the object is a geometry group
591         if(!testResult || CORBA::is_nil( myGeomGroup ) || myGeomGroup->GetType() != 37)
592           {
593             myGeomGroup = GEOM::GEOM_Object::_nil();
594             busy = false;
595             return;
596           }
597         // Check if group constructed on the same shape as a mesh or on its child
598         SALOMEDS::Study_var aStudy = SMESH::GetActiveStudyDocument();
599         GEOM::GEOM_IGroupOperations_var anOp = SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
600         // The main shape of the group 
601         GEOM::GEOM_Object_var aGroupMainShape = anOp->GetMainShape( myGeomGroup );
602         SALOMEDS::SObject_var aGroupMainShapeSO = aStudy->FindObjectIOR( aStudy->ConvertObjectToIOR(aGroupMainShape) );
603         // The mesh SObject
604         SALOMEDS::SObject_var aMeshSO = aStudy->FindObjectIOR( aStudy->ConvertObjectToIOR(myMesh) );
605         
606         SALOMEDS::SObject_var anObj, aRef;
607         bool isRefOrSubShape = false;
608         
609         if ( aMeshSO->FindSubObject( 1, anObj ) &&  anObj->ReferencedObject( aRef )) {
610           if ( strcmp( aRef->GetID(), aGroupMainShapeSO->GetID() ) == 0 )
611             isRefOrSubShape = true;
612           else
613             {
614               SALOMEDS::SObject_var aFather = aGroupMainShapeSO->GetFather();
615               SALOMEDS::SComponent_var aComponent = aGroupMainShapeSO->GetFatherComponent();
616               while ( !isRefOrSubShape && strcmp( aFather->GetID(), aComponent->GetID() ) != 0 )
617                 {
618                   if (strcmp( aRef->GetID(), aFather->GetID() ) == 0)
619                     isRefOrSubShape = true;
620                   else
621                     aFather = aFather->GetFather();
622                 }
623             }
624           if ( !isRefOrSubShape ) 
625             {
626               myGeomGroup = GEOM::GEOM_Object::_nil();
627               busy = false;
628               return;
629             }
630         }
631       }
632     
633     if (aNbSel >= 1) {
634       if (aNbSel > 1) {
635         if (myCurrentLineEdit == mySubMeshLine)
636           aString = tr("SMESH_SUBMESH_SELECTED").arg(aNbSel);
637         else if (myCurrentLineEdit == myGroupLine || myCurrentLineEdit == myGeomGroupLine)
638           aString = tr("SMESH_GROUP_SELECTED").arg(aNbSel);
639       }
640       else {
641         aString = mySelection->firstIObject()->getName();
642       }
643     }
644     
645     myCurrentLineEdit->setText(aString) ;
646     myCurrentLineEdit->home( false );
647   }
648   else {
649     if (aNbSel == 1) {
650       QString aListStr = "";
651       int aNbItems = 0;
652       if (myTypeId == 0) {
653         aNbItems = SMESH::GetNameOfSelectedNodes(mySelection, aListStr);
654       }
655       else {
656         aNbItems = SMESH::GetNameOfSelectedElements(mySelection, aListStr);
657       }
658       if (aNbItems > 0) {
659         QStringList anElements = QStringList::split(" ", aListStr);
660         QListBoxItem* anItem = 0;
661         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
662           anItem = myElements->findItem(*it, Qt::ExactMatch);
663           if (anItem) myElements->setSelected(anItem, true);
664         }
665       }
666     }
667   }
668   
669   if ( !myActor ) {
670     if ( !myGroup->_is_nil() )
671       myActor = SMESH::FindActorByObject(myGroup);
672     else
673       myActor = SMESH::FindActorByObject(myMesh);
674   }
675   
676   busy = false;
677 }
678
679
680 //=================================================================================
681 // function : onSelectSubMesh()
682 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
683 //=================================================================================
684 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
685 {
686   if (on) {
687     if (mySelectGroup->isChecked()) {
688       mySelectGroup->setChecked(false);
689     } 
690     else if (mySelectGeomGroup->isChecked()) {
691       mySelectGeomGroup->setChecked(false);
692     }
693     myCurrentLineEdit = mySubMeshLine;
694     setSelectionMode(4);
695   }
696   else {
697     mySubMeshLine->setText("");
698     myCurrentLineEdit = 0;
699     if (myTypeId != -1)
700       setSelectionMode(myTypeId);
701   }
702   mySubMeshBtn->setEnabled(on);
703   mySubMeshLine->setEnabled(on);
704 }
705
706
707 //=================================================================================
708 // function : (onSelectGroup)
709 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
710 //=================================================================================
711 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
712 {
713   if (on) {
714     if (mySelectSubMesh->isChecked()) {
715       mySelectSubMesh->setChecked(false);
716     }
717     else if (mySelectGeomGroup->isChecked()) {
718       mySelectGeomGroup->setChecked(false);
719     }
720     myCurrentLineEdit = myGroupLine;
721     setSelectionMode(5);
722   }
723   else {
724     myGroupLine->setText("");
725     myCurrentLineEdit = 0;
726     if (myTypeId != -1)
727       setSelectionMode(myTypeId);
728   }
729   myGroupBtn->setEnabled(on);
730   myGroupLine->setEnabled(on);
731 }
732
733
734 //=================================================================================
735 // function : (onSelectGeomGroup)
736 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
737 //=================================================================================
738 void SMESHGUI_GroupDlg::onSelectGeomGroup(bool on)
739 {
740   if (on) {
741     if (mySelectSubMesh->isChecked()) {
742       mySelectSubMesh->setChecked(false);
743     }
744     else if (mySelectGroup->isChecked()) {
745       mySelectGroup->setChecked(false);
746     }
747     myCurrentLineEdit = myGeomGroupLine;
748     setSelectionMode(6);
749   }
750   else {
751     myGeomGroupLine->setText("");
752     myCurrentLineEdit = 0;
753     if (myTypeId != -1)
754       setSelectionMode(myTypeId);
755   }
756   myGeomGroupBtn->setEnabled(on);
757   myGeomGroupLine->setEnabled(on);
758 }
759
760
761 //=================================================================================
762 // function : setCurrentSelection()
763 // purpose  :
764 //=================================================================================
765 void SMESHGUI_GroupDlg::setCurrentSelection()
766 {
767   QPushButton* send = (QPushButton*)sender();
768   myCurrentLineEdit = 0;
769   if (send == mySubMeshBtn) {
770     myCurrentLineEdit = mySubMeshLine;
771     onObjectSelectionChanged();
772   }
773   else if (send == myGroupBtn) {
774     myCurrentLineEdit = myGroupLine;
775     onObjectSelectionChanged();
776   }
777   else if (send == myGeomGroupBtn) {
778     myCurrentLineEdit = myGeomGroupLine;
779     onObjectSelectionChanged();
780   }
781 }
782
783
784 //=================================================================================
785 // function : setFilters()
786 // purpose  : SLOT. Called when "Filter" button pressed. 
787 //=================================================================================
788 void SMESHGUI_GroupDlg::setFilters()
789 {
790   SMESH::ElementType aType = SMESH::ALL;
791   switch ( myTypeId )
792   {
793     case 0 : aType = SMESH::NODE; break;
794     case 1 : aType = SMESH::EDGE; break;
795     case 2 : aType = SMESH::FACE; break;
796     case 3 : aType = SMESH::VOLUME; break;
797     default: return;
798   }
799
800   if ( myFilterDlg == 0 )
801   {
802     myFilterDlg = new SMESHGUI_FilterDlg( (QWidget*)parent(), aType );
803     connect( myFilterDlg, SIGNAL( Accepted() ), SLOT( onFilterAccepted() ) );
804   }
805   else
806     myFilterDlg->Init( aType );
807
808   myFilterDlg->SetSelection( mySelection );
809   myFilterDlg->SetMesh( myMesh );
810   myFilterDlg->SetSourceWg( myElements );
811
812   myFilterDlg->show();
813 }
814
815 //=================================================================================
816 // function : onFilterAccepted()
817 // purpose  : SLOT. Called when Filter dlg closed with OK button.
818 //            Uncheck "Select submesh" and "Select group" checkboxes
819 //=================================================================================
820 void SMESHGUI_GroupDlg::onFilterAccepted()
821 {
822   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
823   {
824     mySelectionMode = myTypeId;
825     mySelectSubMesh->setChecked( false );
826     mySelectGroup->setChecked( false );
827   }
828 }
829
830 //=================================================================================
831 // function : onAdd()
832 // purpose  :
833 //=================================================================================
834 void SMESHGUI_GroupDlg::onAdd()
835 {
836   int aNbSel = mySelection->IObjectCount();
837   if (aNbSel == 0 || !myActor) return;
838
839   busy = true;
840
841   SMESH::ElementType aType = SMESH::ALL;
842   switch(myTypeId) {
843   case 0: aType = SMESH::NODE; break;
844   case 1: aType = SMESH::EDGE; break;
845   case 2: aType = SMESH::FACE; break;
846   case 3: aType = SMESH::VOLUME; break;
847   }
848
849   if (myCurrentLineEdit == 0) {
850     //if (aNbSel != 1) { busy = false; return; }
851     QString aListStr = "";
852     int aNbItems = 0;
853     if (myTypeId == 0) {
854       aNbItems = SMESH::GetNameOfSelectedNodes(mySelection, myActor->getIO(), aListStr);
855     }
856     else {
857       aNbItems = SMESH::GetNameOfSelectedElements(mySelection, myActor->getIO(), aListStr);
858     }
859     if (aNbItems > 0) {
860       QStringList anElements = QStringList::split(" ", aListStr);
861       QListBoxItem* anItem = 0;
862       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
863         anItem = myElements->findItem(*it, Qt::ExactMatch);
864         if (!anItem) {
865           anItem = new QListBoxText(*it);
866           myElements->insertItem(anItem);
867         }
868         myElements->setSelected(anItem, true);
869       }
870     }
871   }
872   else if (myCurrentLineEdit == mySubMeshLine) {
873     SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
874     for (; anIt.More(); anIt.Next()) {
875       SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
876       if (!aSubMesh->_is_nil()) {
877         // check if mesh is the same
878         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
879           try {
880             SMESH::long_array_var anElements = aSubMesh->GetElementsByType ( aType );
881             int k = anElements->length();
882             QListBoxItem* anItem = 0;
883             for (int i = 0; i < k; i++) {
884               QString aText = QString::number(anElements[i]);
885               anItem = myElements->findItem(aText, Qt::ExactMatch);
886               if (!anItem) {
887                 anItem = new QListBoxText(aText);
888                 myElements->insertItem(anItem);
889               }
890               myElements->setSelected(anItem, true);
891             }
892           }
893           catch (const SALOME::SALOME_Exception& ex) {
894             QtCatchCorbaException(ex);
895           }
896         }
897       }
898     }
899     mySelectSubMesh->setChecked(false);
900     busy = false;
901     onListSelectionChanged();
902   }
903   else if (myCurrentLineEdit == myGroupLine) {
904     SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
905     for (; anIt.More(); anIt.Next()) {
906       SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
907       if (!aGroup->_is_nil()) {
908         // check if mesh is the same
909         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
910           SMESH::long_array_var anElements = aGroup->GetListOfID();
911           int k = anElements->length();
912           QListBoxItem* anItem = 0;
913           for (int i = 0; i < k; i++) {
914             QString aText = QString::number(anElements[i]);
915             anItem = myElements->findItem(aText, Qt::ExactMatch);
916             if (!anItem) {
917               anItem = new QListBoxText(aText);
918               myElements->insertItem(anItem);
919             }
920             myElements->setSelected(anItem, true);
921           }
922         }
923       }
924     }
925     mySelectGroup->setChecked(false);
926     busy = false;
927     onListSelectionChanged();
928   }
929   else if (myCurrentLineEdit == myGeomGroupLine && !CORBA::is_nil(myGeomGroup)) {
930     
931     SALOMEDS::Study_var aStudy = SMESH::GetActiveStudyDocument();
932     GEOM::GEOM_IGroupOperations_var aGroupOp = SMESH::GetGEOMGen()->GetIGroupOperations(aStudy->StudyId());
933     
934     SMESH::ElementType aGroupType = SMESH::ALL;
935     switch(aGroupOp->GetType(myGeomGroup)) {
936     case 7: aGroupType = SMESH::NODE; break;
937     case 6: aGroupType = SMESH::EDGE; break;
938     case 4: aGroupType = SMESH::FACE; break;
939     case 2: aGroupType = SMESH::VOLUME; break;
940     default: return;
941     }
942     
943     if (aGroupType == aType) {
944       SALOMEDS::SObject_var aGroupSO = aStudy->FindObjectIOR( aStudy->ConvertObjectToIOR(myGeomGroup) );
945       // Construct filter
946       SMESH::FilterManager_var aFilterMgr = SMESH::GetFilterManager();
947       SMESH::Filter_var aFilter = aFilterMgr->CreateFilter();
948       SMESH::BelongToGeom_var aBelongToGeom = aFilterMgr->CreateBelongToGeom();;
949       aBelongToGeom->SetGeom(myGeomGroup);
950       aBelongToGeom->SetShapeName(aGroupSO->GetName());
951       aBelongToGeom->SetElementType(aType);
952       aFilter->SetPredicate( aBelongToGeom );
953       
954       SMESH::long_array_var anElements = aFilter->GetElementsId( myMesh );
955       
956       int k = anElements->length();
957       QListBoxItem* anItem = 0;
958       for (int i = 0; i < k; i++) {
959         QString aText = QString::number(anElements[i]);
960         anItem = myElements->findItem(aText, Qt::ExactMatch);
961         if (!anItem) {
962           anItem = new QListBoxText(aText);
963           myElements->insertItem(anItem);
964         }
965         myElements->setSelected(anItem, true);
966       }
967     }
968     
969     mySelectGeomGroup->setChecked(false);
970     busy = false;
971     onListSelectionChanged();
972   }
973   busy = false;
974   //  mySelection->ClearIObjects();
975   updateButtons();
976 }
977
978 //=================================================================================
979 // function : onRemove()
980 // purpose  :
981 //=================================================================================
982 void SMESHGUI_GroupDlg::onRemove()
983 {
984   busy = true;
985   if (myCurrentLineEdit == 0) {
986     for (int i = myElements->count(); i > 0; i--) {
987       if (myElements->isSelected(i-1)) {
988         myElements->removeItem(i-1);
989       }
990     }
991   }
992   else {
993     int aNbSel = mySelection->IObjectCount();
994     if (aNbSel == 0) { busy = false; return; }
995     
996     SMESH::ElementType aType = SMESH::ALL;
997     switch(myTypeId) {
998     case 0: aType = SMESH::NODE; break;
999     case 1: aType = SMESH::EDGE; break;
1000     case 2: aType = SMESH::FACE; break;
1001     case 3: aType = SMESH::VOLUME; break;
1002     }
1003
1004     if (myCurrentLineEdit == mySubMeshLine) {
1005       SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
1006       for (; anIt.More(); anIt.Next()) {
1007         SMESH::SMESH_subMesh_var aSubMesh = SMESH::IObjectToInterface<SMESH::SMESH_subMesh>(anIt.Value());
1008         if (!aSubMesh->_is_nil()) {
1009           // check if mesh is the same
1010           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
1011             if (aType == SMESH::NODE) {
1012               try {
1013                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
1014                 int k = anElements->length();
1015                 QListBoxItem* anItem = 0;
1016                 for (int i = 0; i < k; i++) {
1017                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1018                   if (anItem) delete anItem;
1019                 }
1020               }
1021               catch (const SALOME::SALOME_Exception& ex) {
1022                 QtCatchCorbaException(ex);
1023               }
1024             }
1025             else {
1026               try {
1027                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
1028                 int k = anElements->length();
1029                 QListBoxItem* anItem = 0;
1030                 for (int i = 0; i < k; i++) {
1031                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1032                   if (anItem) delete anItem;
1033                 }
1034               }
1035               catch (const SALOME::SALOME_Exception& ex) {
1036                 QtCatchCorbaException(ex);
1037               }
1038             }
1039           }
1040         }
1041       }
1042     }
1043     else if (myCurrentLineEdit == myGroupLine) {
1044       Standard_Boolean aRes;
1045       SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
1046       for (; anIt.More(); anIt.Next()) {
1047         SMESH::SMESH_Group_var aGroup = SMESH::IObjectToInterface<SMESH::SMESH_Group>(anIt.Value());
1048         if (aRes && !aGroup->_is_nil()) {
1049           // check if mesh is the same
1050           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
1051             SMESH::long_array_var anElements = aGroup->GetListOfID();
1052             int k = anElements->length();
1053             QListBoxItem* anItem = 0;
1054             for (int i = 0; i < k; i++) {
1055               anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
1056               if (anItem) delete anItem;
1057             }
1058           }
1059         }
1060       }
1061     }
1062   }
1063   busy = false;
1064   updateButtons();
1065 }
1066
1067 //=================================================================================
1068 // function : onSort()
1069 // purpose  :
1070 //=================================================================================
1071 void SMESHGUI_GroupDlg::onSort()
1072 {
1073   // PAL5412: sorts items in ascending by "string" value
1074   // myElements->sort(true);
1075   // myElements->update();
1076   int i, k = myElements->count();
1077   if (k > 0) {
1078     busy = true;
1079     QStringList aSelected;
1080     std::vector<int> anArray(k);
1081     //    QMemArray<int> anArray(k);
1082     QListBoxItem* anItem;
1083     // fill the array
1084     for (anItem = myElements->firstItem(), i = 0; anItem != 0; anItem = anItem->next(), i++) {
1085       anArray[i] = anItem->text().toInt();
1086       if (anItem->isSelected()) 
1087         aSelected.append(anItem->text());
1088     }
1089     // sort & update list
1090     std::sort(anArray.begin(), anArray.end());
1091     //    anArray.sort();
1092     myElements->clear();
1093     for (i = 0; i < k; i++) {
1094       myElements->insertItem(QString::number(anArray[i]));
1095     }
1096     for (QStringList::iterator it = aSelected.begin(); it != aSelected.end(); ++it) {
1097       anItem = myElements->findItem(*it, Qt::ExactMatch);
1098       if (anItem) myElements->setSelected(anItem, true);
1099     }
1100     busy = false;
1101   }
1102 }
1103
1104 //=================================================================================
1105 // function : closeEvent()
1106 // purpose  :
1107 //=================================================================================
1108 void SMESHGUI_GroupDlg::closeEvent( QCloseEvent* e )
1109 {
1110   onClose();
1111 }
1112
1113 //=======================================================================
1114 // name    : SMESHGUI_GroupDlg::onClose
1115 // Purpose : SLOT called when "Close" button pressed. Close dialog
1116 //=======================================================================
1117 void SMESHGUI_GroupDlg::onClose()
1118 {
1119   QAD_StudyFrame* aStudyFrame = mySMESHGUI->GetActiveStudy()->getActiveStudyFrame();
1120   if (aStudyFrame->getTypeView() == VIEW_VTK) {
1121     SMESH::SetPointRepresentation(false);
1122     SMESH::SetPickable();
1123
1124     // remove filters from viewer
1125     if(VTKViewer_InteractorStyleSALOME* aStyle = SMESH::GetInteractorStyle()){
1126       SMESH::RemoveFilter(SMESHGUI_EdgeFilter,aStyle);
1127       SMESH::RemoveFilter(SMESHGUI_FaceFilter,aStyle);
1128     }
1129   }
1130   
1131   mySelection->ClearIObjects();
1132   QAD_Application::getDesktop()->SetSelectionMode(ActorSelection);
1133   mySelection->ClearFilters();
1134   mySMESHGUI->ResetState();
1135
1136   reject();
1137 }
1138
1139 //=======================================================================
1140 // name    : SMESHGUI_GroupDlg::onDeactivate
1141 // Purpose : SLOT called when dialog must be deativated
1142 //=======================================================================
1143 void SMESHGUI_GroupDlg::onDeactivate()
1144 {
1145   setEnabled( false );
1146 }
1147
1148 //=======================================================================
1149 // name    : SMESHGUI_GroupDlg::enterEvent
1150 // Purpose : Event filter
1151 //=======================================================================
1152 void SMESHGUI_GroupDlg::enterEvent( QEvent* )
1153 {
1154   if ( !isEnabled() ) {
1155     SMESHGUI::GetSMESHGUI()->EmitSignalDeactivateDialog();
1156     setEnabled( true );
1157     mySelectionMode = -1;
1158     setSelectionMode( myTypeId );
1159   }
1160 }
1161
1162 //=======================================================================
1163 //function : hideEvent
1164 //purpose  : caused by ESC key
1165 //=======================================================================
1166
1167 void SMESHGUI_GroupDlg::hideEvent ( QHideEvent * e )
1168 {
1169   if ( !isMinimized() )
1170     onClose();
1171 }