Salome HOME
SALOME PAL V1_4_1
[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 using namespace std;
30 #include "SMESHGUI_GroupDlg.h"
31 #include "SMESHGUI_FilterDlg.h"
32
33 #include "SMESHGUI.h"
34 #include "SALOMEGUI_QtCatchCorbaException.hxx"
35 #include "SALOME_ListIteratorOfListIO.hxx"
36 #include "VTKViewer_InteractorStyleSALOME.h"
37 #include "VTKViewer_ViewFrame.h"
38 #include "QAD_Application.h"
39 #include "QAD_Desktop.h"
40 #include "QAD_MessageBox.h"
41 #include "QAD_RightFrame.h"
42 #include "utilities.h"
43
44 #include "SMESH_Actor.h"
45
46 // QT Includes
47 #include <qbuttongroup.h>
48 #include <qgroupbox.h>
49 #include <qhbox.h>
50 #include <qlabel.h>
51 #include <qlineedit.h>
52 #include <qpushbutton.h>
53 #include <qradiobutton.h>
54 #include <qcheckbox.h>
55 #include <qlayout.h>
56 #include <qlistbox.h>
57 #include <qimage.h>
58 #include <qpixmap.h>
59 #include <qmemarray.h>
60
61 // STL includes
62 #include <vector>
63 #include <algorithm>
64
65 //=================================================================================
66 // class    : SMESHGUI_GroupDlg()
67 // purpose  : 
68 //=================================================================================
69 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( QWidget* parent, const char* name, SALOME_Selection* theSel,
70                                       SMESH::SMESH_Mesh_ptr theMesh, bool modal, WFlags fl )
71   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
72 {
73   if ( !name ) setName( "SMESHGUI_GroupDlg" );
74   initDialog(theSel, true);
75   init(theMesh);
76
77   /* Move widget on the botton right corner of main widget */
78   int x, y ;
79   mySMESHGUI->DefineDlgPosition(this, x, y);
80   this->move(x, y);
81 }
82
83 SMESHGUI_GroupDlg::SMESHGUI_GroupDlg( QWidget* parent, const char* name, SALOME_Selection* theSel,
84                                       SMESH::SMESH_Group_ptr theGroup, bool modal, WFlags fl )
85   : QDialog( parent, name, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu | WDestructiveClose )
86 {
87   if ( !name ) setName( "SMESHGUI_GroupDlg" );
88   initDialog(theSel, false);
89   init(theGroup);
90
91   /* Move widget on the botton right corner of main widget */
92   int x, y ;
93   mySMESHGUI->DefineDlgPosition(this, x, y);
94   this->move(x, y);
95 }
96
97 void SMESHGUI_GroupDlg::initDialog(SALOME_Selection* theSel, bool create)
98 {
99   myFilterDlg = 0;
100   
101   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap( "SMESH",tr("ICON_SELECT")));
102
103   if (create)
104     setCaption( tr( "SMESH_CREATE_GROUP_TITLE"  ) );
105   else 
106     setCaption( tr( "SMESH_EDIT_GROUP_TITLE"  ) );
107   setSizeGripEnabled( TRUE );
108
109   QVBoxLayout* aMainLayout = new QVBoxLayout(this, 11, 6);
110   
111   /***************************************************************/
112   myTypeGroup = new QButtonGroup(1, Qt::Vertical, this, "Group types");
113   myTypeGroup->setTitle(tr("SMESH_ELEMENTS_TYPE"));
114   myTypeGroup->setExclusive(true);
115
116   QStringList types;
117   types.append(tr("MESH_NODE"));
118   types.append(tr("SMESH_EDGE"));
119   types.append(tr("SMESH_FACE"));
120   types.append(tr("SMESH_VOLUME"));
121   QRadioButton* rb;
122   for (int i = 0; i < types.count(); i++) {
123     rb = new QRadioButton(types[i], myTypeGroup);
124   }
125   myTypeGroup->setEnabled(create);
126   myTypeId = -1;
127     
128   /***************************************************************/
129   QHBox* aNameBox = new QHBox(this, "name box");
130   QLabel* aName = new QLabel(aNameBox, "name label");
131   aName->setText(tr("SMESH_NAME"));
132   aName->setMinimumSize(50,0);
133   myName = new QLineEdit(aNameBox, "name");
134     
135   /***************************************************************/
136   QGroupBox* aContentBox = new QGroupBox(1, Qt::Horizontal, this, "content box");
137   aContentBox->setTitle(tr("SMESH_CONTENT"));
138   QFrame* aContent = new QFrame(aContentBox, "content");
139   QGridLayout* aLayout = new QGridLayout(aContent, 7, 4);
140   aLayout->setSpacing(6);
141   aLayout->setAutoAdd(false);
142
143   QLabel* aLabel = new QLabel(aContent, "elements label");
144   aLabel->setText(tr("SMESH_ID_ELEMENTS"));
145   myElements = new QListBox(aContent, "elements list");
146   myElements->setSelectionMode(QListBox::Extended);
147   //  myElements->setMinimumHeight(150);
148
149   myFilter = new QPushButton(aContent, "filter");
150   myFilter->setText(tr("SMESH_BUT_FILTER"));
151   QPushButton* aAddBtn = new QPushButton(aContent, "add");
152   aAddBtn->setText(tr("SMESH_BUT_ADD"));
153   QPushButton* aRemoveBtn = new QPushButton(aContent, "remove");
154   aRemoveBtn->setText(tr("SMESH_BUT_REMOVE"));
155   QPushButton* aSortBtn = new QPushButton(aContent, "sort");
156   aSortBtn->setText(tr("SMESH_BUT_SORT"));
157
158   aLayout->addWidget(aLabel, 0, 0);
159   aLayout->addMultiCellWidget(myElements, 1, 6, 0, 0);
160   aLayout->addWidget(myFilter, 1, 2);
161   aLayout->addWidget(aAddBtn, 3, 2);
162   aLayout->addWidget(aRemoveBtn, 4, 2);
163   aLayout->addWidget(aSortBtn, 6, 2);
164
165   aLayout->setColStretch(0, 1);
166   aLayout->addColSpacing(1, 20);
167   aLayout->addColSpacing(3, 20);
168   aLayout->setRowStretch(2, 1);
169   aLayout->setRowStretch(5, 1);
170
171   aContentBox->setMinimumHeight(aContent->sizeHint().height() + 
172                                 aContentBox->sizeHint().height());
173
174   /***************************************************************/
175   QGroupBox* aSelectBox = new QGroupBox(3, Qt::Horizontal, this, "select box");
176   aSelectBox->setTitle(tr("SMESH_SELECT_FROM"));
177
178   mySelectSubMesh = new QCheckBox(aSelectBox, "submesh checkbox");
179   mySelectSubMesh->setText(tr("SMESH_SUBMESH"));
180   mySelectSubMesh->setMinimumSize(50, 0);
181   mySubMeshBtn = new QPushButton(aSelectBox, "submesh button");
182   mySubMeshBtn->setText("");
183   mySubMeshBtn->setPixmap(image0);
184   mySubMeshLine = new QLineEdit(aSelectBox, "submesh line");
185   mySubMeshLine->setReadOnly(true);
186   onSelectSubMesh(false);
187
188   mySelectGroup = new QCheckBox(aSelectBox, "group checkbox");
189   mySelectGroup->setText(tr("SMESH_GROUP"));
190   mySelectGroup->setMinimumSize(50, 0);
191   myGroupBtn = new QPushButton(aSelectBox, "group button");
192   myGroupBtn->setText("");
193   myGroupBtn->setPixmap(image0);
194   myGroupLine = new QLineEdit(aSelectBox, "group line");
195   myGroupLine->setReadOnly(true);
196   onSelectGroup(false);
197
198   aSelectBox->setMinimumHeight(mySubMeshBtn->sizeHint().height() + 
199                                myGroupBtn->sizeHint().height() +
200                                aSelectBox->sizeHint().height());
201
202   /***************************************************************/
203   QFrame* aButtons = new QFrame(this, "button box");
204   aButtons->setFrameStyle(QFrame::Box | QFrame::Sunken);
205   QHBoxLayout* aBtnLayout = new QHBoxLayout(aButtons, 11, 6);
206   aBtnLayout->setAutoAdd(false);
207
208   QPushButton* aOKBtn = new QPushButton(aButtons, "ok");
209   aOKBtn->setText(tr("SMESH_BUT_OK"));
210   aOKBtn->setAutoDefault(true);
211   aOKBtn->setDefault(true);
212   QPushButton* aApplyBtn = new QPushButton(aButtons, "apply");
213   aApplyBtn->setText(tr("SMESH_BUT_APPLY"));
214   aApplyBtn->setAutoDefault(true);
215   QPushButton* aCloseBtn = new QPushButton(aButtons, "close");
216   aCloseBtn->setText(tr("SMESH_BUT_CLOSE"));
217   aCloseBtn->setAutoDefault(true);
218
219   aBtnLayout->addWidget(aOKBtn);
220   aBtnLayout->addWidget(aApplyBtn);
221   aBtnLayout->addStretch();
222   aBtnLayout->addWidget(aCloseBtn);
223
224   /***************************************************************/
225   aMainLayout->addWidget(myTypeGroup);
226   aMainLayout->addWidget(aNameBox);
227   aMainLayout->addWidget(aContentBox);
228   aMainLayout->addWidget(aSelectBox);
229   aMainLayout->addWidget(aButtons);
230
231   /* signals and slots connections */
232   connect(myTypeGroup, SIGNAL(clicked(int)), this, SLOT(onTypeChanged(int)));
233
234   connect(myName, SIGNAL(textChanged(const QString&)), this, SLOT(onNameChanged(const QString&)));
235   connect(myElements, SIGNAL(selectionChanged()), this, SLOT(onListSelectionChanged()));
236
237   connect(myFilter, SIGNAL(clicked()), this, SLOT(setFilters()));
238   connect(aAddBtn, SIGNAL(clicked()), this, SLOT(onAdd()));
239   connect(aRemoveBtn, SIGNAL(clicked()), this, SLOT(onRemove()));
240   connect(aSortBtn, SIGNAL(clicked()), this, SLOT(onSort()));
241
242   connect(mySelectSubMesh, SIGNAL(toggled(bool)), this, SLOT(onSelectSubMesh(bool)));
243   connect(mySelectGroup, SIGNAL(toggled(bool)), this, SLOT(onSelectGroup(bool)));
244   connect(mySubMeshBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
245   connect(myGroupBtn, SIGNAL(clicked()), this, SLOT(setCurrentSelection()));
246
247   connect(aOKBtn, SIGNAL(clicked()), this, SLOT(onOK()));
248   connect(aApplyBtn, SIGNAL(clicked()), this, SLOT(onApply()));
249   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(onClose()));
250
251   /* Init selection */
252   mySelection = theSel;  
253   mySMESHGUI = SMESHGUI::GetSMESHGUI();
254   mySMESHGUI->SetActiveDialogBox(this);
255   mySMESHGUI->SetState(800);
256
257   mySelectionMode = -1;
258   mySubMeshFilter = new SMESH_TypeFilter(SUBMESH);
259   myGroupFilter = new SMESH_TypeFilter(GROUP);
260
261   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(onClose()));
262   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(onClose()));
263   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(onObjectSelectionChanged()));
264
265   updateButtons();
266 }
267
268 //=================================================================================
269 // function : ~SMESHGUI_GroupDlg()
270 // purpose  : Destroys the object and frees any allocated resources
271 //=================================================================================
272 SMESHGUI_GroupDlg::~SMESHGUI_GroupDlg()
273 {
274     // no need to delete child widgets, Qt does it all for us
275 }
276
277
278 //=================================================================================
279 // function : Init()
280 // purpose  :
281 //=================================================================================
282 void SMESHGUI_GroupDlg::init(SMESH::SMESH_Mesh_ptr theMesh)
283 {
284   /* init data from current selection */
285   myMesh = SMESH::SMESH_Mesh::_duplicate(theMesh);
286   myGroup = SMESH::SMESH_Group::_nil();
287
288   Standard_Boolean aResult;
289   myActor = mySMESHGUI->FindActor(myMesh, aResult, true);
290   mySMESHGUI->SetPickable(myActor);
291
292   myTypeGroup->setButton(0);
293   onTypeChanged(0);
294 }
295
296 //=================================================================================
297 // function : Init()
298 // purpose  :
299 //=================================================================================
300 void SMESHGUI_GroupDlg::init(SMESH::SMESH_Group_ptr theGroup)
301 {
302
303   myMesh = theGroup->GetMesh();
304   myGroup = SMESH::SMESH_Group::_duplicate(theGroup);
305
306   Standard_Boolean aResult;
307   myActor = mySMESHGUI->FindActor(myMesh, aResult, true);
308   if ( !myActor )
309     myActor = mySMESHGUI->FindActor(myGroup, aResult, true);
310   mySMESHGUI->SetPickable(myActor);
311
312   int aType = 0;
313   switch(theGroup->GetType()) {
314   case SMESH::NODE: aType= 0; break;
315   case SMESH::EDGE: aType = 1; break;
316   case SMESH::FACE: aType = 2; break;
317   case SMESH::VOLUME: aType = 3; break;
318   } 
319   myTypeGroup->setButton(aType);
320   onTypeChanged(aType);
321
322   myName->setText(myGroup->GetName());
323   myName->home(false);
324
325   if (!theGroup->IsEmpty()) {
326     SMESH::long_array_var anElements = myGroup->GetListOfID();
327     int k = anElements->length();
328     for (int i = 0; i < k; i++) {
329       myIdList.append(anElements[i]);
330       myElements->insertItem(QString::number(anElements[i]));
331     }
332     myElements->selectAll(true);
333   }
334 }
335
336
337 //=================================================================================
338 // function : updateButtons()
339 // purpose  : 
340 //=================================================================================
341 void SMESHGUI_GroupDlg::updateButtons()
342 {
343   bool enable = !myName->text().stripWhiteSpace().isEmpty() && myElements->count() > 0;
344   QPushButton* aBtn;
345   aBtn = (QPushButton*) child("ok", "QPushButton");
346   if (aBtn) aBtn->setEnabled(enable);
347   aBtn = (QPushButton*) child("apply", "QPushButton");
348   if (aBtn) aBtn->setEnabled(enable);
349 }
350
351 //=================================================================================
352 // function : onNameChanged()
353 // purpose  : 
354 //=================================================================================
355 void SMESHGUI_GroupDlg::onNameChanged(const QString& text)
356 {
357   updateButtons();
358 }
359
360 //=================================================================================
361 // function : onTypeChanged()
362 // purpose  : Radio button management
363 //=================================================================================
364 void SMESHGUI_GroupDlg::onTypeChanged(int id)
365 {
366   if (myTypeId != id) {
367     myElements->clear();
368     if (myCurrentLineEdit == 0)
369       setSelectionMode(id);
370     myFilter->setEnabled(id == 1 || id == 2);
371   }
372   myTypeId = id;
373 }
374
375 //=================================================================================
376 // function : setSelectionMode()
377 // purpose  : Radio button management
378 //=================================================================================
379 void SMESHGUI_GroupDlg::setSelectionMode(int theMode)
380 {
381   if (mySelectionMode != theMode) {
382     mySelection->ClearIObjects();
383     mySelection->ClearFilters();
384     if (mySelectionMode == 0)
385       mySMESHGUI->EraseSimulationActors();
386     if (theMode < 4) {
387       if (theMode == 0) {
388         mySMESHGUI->ViewNodes();
389         QAD_Application::getDesktop()->SetSelectionMode(NodeSelection, true);
390       }
391       else if (theMode == 1) {
392         QAD_Application::getDesktop()->SetSelectionMode(EdgeSelection, true);
393       }
394       else if (theMode == 2) {
395         QAD_Application::getDesktop()->SetSelectionMode(FaceSelection, true);
396       }
397       else {
398         QAD_Application::getDesktop()->SetSelectionMode(VolumeSelection, true);
399       }
400     }
401     else {
402       QAD_Application::getDesktop()->SetSelectionMode(ActorSelection, true);
403       if (theMode == 4)
404         mySelection->AddFilter(mySubMeshFilter);
405       else if (theMode == 5)
406         mySelection->AddFilter(myGroupFilter);
407     }
408     mySelectionMode = theMode;
409   }
410
411
412 //=================================================================================
413 // function : onApply()
414 // purpose  :
415 //=================================================================================
416 bool SMESHGUI_GroupDlg::onApply()
417 {
418   if (!myName->text().stripWhiteSpace().isEmpty() && myElements->count() > 0) {
419     mySelection->ClearIObjects();
420     if (myGroup->_is_nil()) {
421       SMESH::ElementType aType = SMESH::ALL;
422       switch(myTypeId) {
423       case 0: aType = SMESH::NODE; break;
424       case 1: aType = SMESH::EDGE; break;
425       case 2: aType = SMESH::FACE; break;
426       case 3: aType = SMESH::VOLUME; break;
427       }
428       SMESH::long_array_var anIdList = new SMESH::long_array;
429       int i, k = myElements->count();
430       anIdList->length(k);
431       QListBoxItem* anItem;
432       for (i = 0, anItem = myElements->firstItem(); anItem != 0; i++, anItem = anItem->next()) {
433         anIdList[i] = anItem->text().toInt();
434       }
435
436       myGroup = mySMESHGUI->AddGroup(myMesh, aType, myName->text());
437       myGroup->Add(anIdList);
438
439       /* init for next operation */
440       myName->setText("");
441       myElements->clear();
442       myGroup = SMESH::SMESH_Group::_nil();
443     }
444     else {
445       myGroup->SetName(myName->text());
446
447       QValueList<int> aAddList;
448       QValueList<int>::iterator anIt;
449       QListBoxItem* anItem;
450       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
451         int anId = anItem->text().toInt();
452         if ((anIt = myIdList.find(anId)) == myIdList.end())
453           aAddList.append(anId);
454         else
455           myIdList.remove(anIt);
456       }
457       if (!aAddList.empty()) {
458         SMESH::long_array_var anIdList = new SMESH::long_array;
459         anIdList->length(aAddList.count());
460         int i;
461         for (i = 0, anIt = aAddList.begin(); anIt != aAddList.end(); anIt++, i++)
462           anIdList[i] = *anIt;
463         myGroup->Add(anIdList);
464       }
465       if (!myIdList.empty()) {
466         SMESH::long_array_var anIdList = new SMESH::long_array;
467         anIdList->length(myIdList.count());
468         int i;
469         for (i = 0, anIt = myIdList.begin(); anIt != myIdList.end(); anIt++, i++)
470           anIdList[i] = *anIt;
471         myGroup->Remove(anIdList);
472       }
473       /* init for next operation */
474       myIdList.clear();
475       for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next())
476         myIdList.append(anItem->text().toInt());
477     }
478
479     mySMESHGUI->GetActiveStudy()->updateObjBrowser(true);
480     mySelection->ClearIObjects();
481     return true;
482   }
483   return false;
484 }
485
486 //=================================================================================
487 // function : onOK()
488 // purpose  :
489 //=================================================================================
490 void SMESHGUI_GroupDlg::onOK()
491 {
492   if ( onApply() )
493     onClose();
494 }
495
496 //=================================================================================
497 // function : onClose()
498 // purpose  :
499 //=================================================================================
500 void SMESHGUI_GroupDlg::onClose()
501 {
502   close();
503 }
504
505
506 static bool busy = false;
507 //=================================================================================
508 // function : onListSelectionChanged()
509 // purpose  : Called when selection in element list is changed
510 //=================================================================================
511 void SMESHGUI_GroupDlg::onListSelectionChanged()
512 {
513   //  MESSAGE("SMESHGUI_GroupDlg::onListSelectionChanged(); myActor = " << myActor);
514   if (busy || !myActor) return;
515   busy = true;
516
517   if (myCurrentLineEdit == 0) {
518     mySelection->ClearIObjects();
519     TColStd_MapOfInteger aIndexes;
520     QListBoxItem* anItem;
521     for (anItem = myElements->firstItem(); anItem != 0; anItem = anItem->next()) {
522       if (anItem->isSelected()) {
523         std::vector<int> aVtkList;
524         if (myTypeId == 0)
525           aVtkList = myActor->GetNodeVtkId(anItem->text().toInt());
526         else
527           aVtkList = myActor->GetElemVtkId(anItem->text().toInt());
528
529         if (aVtkList.size() > 0) {
530           std::vector<int>::iterator it;
531           for (it = aVtkList.begin(); it != aVtkList.end(); ++it) {
532             aIndexes.Add(*it);
533           }
534         }
535       }
536     }
537     mySelection->AddOrRemoveIndex(myActor->getIO(), aIndexes, false, false);
538     mySelection->AddIObject(myActor->getIO());
539   }
540   busy = false;
541 }
542
543 //=================================================================================
544 // function : onObjectSelectionChanged()
545 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
546 //=================================================================================
547 void SMESHGUI_GroupDlg::onObjectSelectionChanged()
548 {
549   if (busy) return;
550   busy = true;
551
552   int aNbSel = mySelection->IObjectCount();
553   myElements->clearSelection();
554
555   if (myCurrentLineEdit) {
556     myCurrentLineEdit->setText("") ;
557     QString aString = "";
558     if (aNbSel >= 1) {
559       if (aNbSel > 1) {
560         if (myCurrentLineEdit = mySubMeshLine)
561           aString = tr("SMESH_SUBMESH_SELECTED").arg(aNbSel);
562         else if (myCurrentLineEdit = myGroupLine)
563           aString = tr("SMESH_GROUP_SELECTED").arg(aNbSel);
564       }
565       else {
566         aString = mySelection->firstIObject()->getName();
567       }
568     }
569
570     myCurrentLineEdit->setText(aString) ;
571     myCurrentLineEdit->home( false );
572   }
573   else {
574     if (aNbSel == 1) {
575       QString aListStr = "";
576       int aNbItems = 0;
577       if (myTypeId == 0) {
578         aNbItems = mySMESHGUI->GetNameOfSelectedNodes(mySelection, aListStr);
579       }
580       else {
581         aNbItems = mySMESHGUI->GetNameOfSelectedElements(mySelection, aListStr);
582       }
583       if (aNbItems > 0) {
584         QStringList anElements = QStringList::split(" ", aListStr);
585         QListBoxItem* anItem = 0;
586         for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
587           anItem = myElements->findItem(*it, Qt::ExactMatch);
588           if (anItem) myElements->setSelected(anItem, true);
589         }
590       }
591     }
592   }
593   busy = false;
594 }
595
596
597 //=================================================================================
598 // function : onSelectSubMesh()
599 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
600 //=================================================================================
601 void SMESHGUI_GroupDlg::onSelectSubMesh(bool on)
602 {
603   if (on) {
604     if (mySelectGroup->isChecked()) {
605       mySelectGroup->setChecked(false);
606     }
607     myCurrentLineEdit = mySubMeshLine;
608     setSelectionMode(4);
609   }
610   else {
611     mySubMeshLine->setText("");
612     myCurrentLineEdit = 0;
613     if (myTypeId != -1)
614       setSelectionMode(myTypeId);
615   }
616   mySubMeshBtn->setEnabled(on);
617   mySubMeshLine->setEnabled(on);
618 }
619
620 //=================================================================================
621 // function : (onSelectGroup)
622 // purpose  : Called when selection in 3D view or ObjectBrowser is changed
623 //=================================================================================
624 void SMESHGUI_GroupDlg::onSelectGroup(bool on)
625 {
626   if (on) {
627     if (mySelectSubMesh->isChecked()) {
628       mySelectSubMesh->setChecked(false);
629     }
630     myCurrentLineEdit = myGroupLine;
631     setSelectionMode(5);
632   }
633   else {
634     myGroupLine->setText("");
635     myCurrentLineEdit = 0;
636     if (myTypeId != -1)
637       setSelectionMode(myTypeId);
638   }
639   myGroupBtn->setEnabled(on);
640   myGroupLine->setEnabled(on);
641 }
642
643 //=================================================================================
644 // function : setCurrentSelection()
645 // purpose  :
646 //=================================================================================
647 void SMESHGUI_GroupDlg::setCurrentSelection()
648 {
649   QPushButton* send = (QPushButton*)sender();
650   myCurrentLineEdit = 0;
651   if (send == mySubMeshBtn) {
652     myCurrentLineEdit = mySubMeshLine;
653     onObjectSelectionChanged();
654   }
655   else if (send == myGroupBtn) {
656     myCurrentLineEdit = myGroupLine;
657     onObjectSelectionChanged();
658   }
659 }
660
661
662 //=================================================================================
663 // function : setFilters()
664 // purpose  : SLOT. Called when "Filter" button pressed. 
665 //=================================================================================
666 void SMESHGUI_GroupDlg::setFilters()
667 {
668   SMESH::ElementType aType = SMESH::ALL;
669   switch ( myTypeId )
670   {
671     case 0 : aType = SMESH::NODE; break;
672     case 1 : aType = SMESH::EDGE; break;
673     case 2 : aType = SMESH::FACE; break;
674     case 3 : aType = SMESH::VOLUME; break;
675     default: return;
676   }
677  
678   if ( myFilterDlg == 0 )
679     myFilterDlg = new SMESHGUI_FilterDlg( this, aType, true );
680   else
681     myFilterDlg->Init( aType );
682
683   myFilterDlg->SetSelection( mySelection );
684   myFilterDlg->SetMesh( myMesh );
685   myFilterDlg->SetSourceWg( myElements );
686
687   if ( myFilterDlg->exec() != QDialog::Accepted )
688     return;
689
690   if ( mySelectSubMesh->isChecked() || mySelectGroup->isChecked() )
691   {
692     mySelectionMode = myTypeId;
693     mySelectSubMesh->setChecked( false );
694     mySelectGroup->setChecked( false );
695   }
696 }
697
698 //=================================================================================
699 // function : onAdd()
700 // purpose  :
701 //=================================================================================
702 void SMESHGUI_GroupDlg::onAdd()
703 {
704   int aNbSel = mySelection->IObjectCount();
705   if (aNbSel == 0) return;
706
707   busy = true;
708
709   SMESH::ElementType aType = SMESH::ALL;
710   switch(myTypeId) {
711   case 0: aType = SMESH::NODE; break;
712   case 1: aType = SMESH::EDGE; break;
713   case 2: aType = SMESH::FACE; break;
714   case 3: aType = SMESH::VOLUME; break;
715   }
716
717   if (myCurrentLineEdit == 0) {
718     if (aNbSel != 1) { busy = false; return; }
719     QString aListStr = "";
720     int aNbItems = 0;
721     if (myTypeId == 0) {
722       aNbItems = mySMESHGUI->GetNameOfSelectedNodes(mySelection, aListStr);
723     }
724     else {
725       aNbItems = mySMESHGUI->GetNameOfSelectedElements(mySelection, aListStr);
726     }
727     if (aNbItems > 0) {
728       QStringList anElements = QStringList::split(" ", aListStr);
729       QListBoxItem* anItem = 0;
730       for (QStringList::iterator it = anElements.begin(); it != anElements.end(); ++it) {
731         anItem = myElements->findItem(*it, Qt::ExactMatch);
732         if (!anItem) {
733           anItem = new QListBoxText(*it);
734           myElements->insertItem(anItem);
735         }
736         myElements->setSelected(anItem, true);
737       }
738     }
739   }
740   else if (myCurrentLineEdit == mySubMeshLine) {
741     Standard_Boolean aRes;
742     SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
743     for (; anIt.More(); anIt.Next()) {
744       SMESH::SMESH_subMesh_var aSubMesh = mySMESHGUI->ConvertIOinSubMesh(anIt.Value(), aRes);
745       if (aRes && !aSubMesh->_is_nil()) {
746         // check if mesh is the same
747         if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
748           if (aType == SMESH::NODE) {
749             try {
750               SMESH::long_array_var anElements = aSubMesh->GetNodesId();
751               int k = anElements->length();
752               QListBoxItem* anItem = 0;
753               for (int i = 0; i < k; i++) {
754                 QString aText = QString::number(anElements[i]);
755                 anItem = myElements->findItem(aText, Qt::ExactMatch);
756                 if (!anItem) {
757                   anItem = new QListBoxText(aText);
758                   myElements->insertItem(anItem);
759                 }
760                 myElements->setSelected(anItem, true);
761               }
762             }
763             catch (const SALOME::SALOME_Exception& ex) {
764               QtCatchCorbaException(ex);
765             }
766           }
767           else {
768             try {
769               SMESH::long_array_var anElements = aSubMesh->GetElementsId();
770               int k = anElements->length();
771               QListBoxItem* anItem = 0;
772               for (int i = 0; i < k; i++) {
773                 QString aText = QString::number(anElements[i]);
774                 anItem = myElements->findItem(aText, Qt::ExactMatch);
775                 if (!anItem) {
776                   anItem = new QListBoxText(aText);
777                   myElements->insertItem(anItem);
778                 }
779                 myElements->setSelected(anItem, true);
780               }
781             }
782             catch (const SALOME::SALOME_Exception& ex) {
783               QtCatchCorbaException(ex);
784             }
785           }
786         }
787       }
788     }
789     mySelectSubMesh->setChecked(false);
790     busy = false;
791     onListSelectionChanged();
792   }
793   else if (myCurrentLineEdit == myGroupLine) {
794     Standard_Boolean aRes;
795     SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
796     for (; anIt.More(); anIt.Next()) {
797       SMESH::SMESH_Group_var aGroup = mySMESHGUI->ConvertIOinSMESHGroup(anIt.Value(), aRes);
798       if (aRes && !aGroup->_is_nil()) {
799         // check if mesh is the same
800         if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
801           SMESH::long_array_var anElements = aGroup->GetListOfID();
802           int k = anElements->length();
803           QListBoxItem* anItem = 0;
804           for (int i = 0; i < k; i++) {
805             QString aText = QString::number(anElements[i]);
806             anItem = myElements->findItem(aText, Qt::ExactMatch);
807             if (!anItem) {
808               anItem = new QListBoxText(aText);
809               myElements->insertItem(anItem);
810             }
811             myElements->setSelected(anItem, true);
812           }
813         }
814       }
815     }
816     mySelectGroup->setChecked(false);
817     busy = false;
818     onListSelectionChanged();
819   }
820   busy = false;
821   //  mySelection->ClearIObjects();
822   updateButtons();
823 }
824
825 //=================================================================================
826 // function : onRemove()
827 // purpose  :
828 //=================================================================================
829 void SMESHGUI_GroupDlg::onRemove()
830 {
831   busy = true;
832   if (myCurrentLineEdit == 0) {
833     for (int i = myElements->count(); i > 0; i--) {
834       if (myElements->isSelected(i-1)) {
835         myElements->removeItem(i-1);
836       }
837     }
838   }
839   else {
840     int aNbSel = mySelection->IObjectCount();
841     if (aNbSel == 0) { busy = false; return; }
842     
843     SMESH::ElementType aType = SMESH::ALL;
844     switch(myTypeId) {
845     case 0: aType = SMESH::NODE; break;
846     case 1: aType = SMESH::EDGE; break;
847     case 2: aType = SMESH::FACE; break;
848     case 3: aType = SMESH::VOLUME; break;
849     }
850
851     if (myCurrentLineEdit == mySubMeshLine) {
852       Standard_Boolean aRes;
853       SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
854       for (; anIt.More(); anIt.Next()) {
855         SMESH::SMESH_subMesh_var aSubMesh = mySMESHGUI->ConvertIOinSubMesh(anIt.Value(), aRes);
856         if (aRes && !aSubMesh->_is_nil()) {
857           // check if mesh is the same
858           if (aSubMesh->GetFather()->GetId() == myMesh->GetId()) {
859             if (aType == SMESH::NODE) {
860               try {
861                 SMESH::long_array_var anElements = aSubMesh->GetNodesId();
862                 int k = anElements->length();
863                 QListBoxItem* anItem = 0;
864                 for (int i = 0; i < k; i++) {
865                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
866                   if (anItem) delete anItem;
867                 }
868               }
869               catch (const SALOME::SALOME_Exception& ex) {
870                 QtCatchCorbaException(ex);
871               }
872             }
873             else {
874               try {
875                 SMESH::long_array_var anElements = aSubMesh->GetElementsId();
876                 int k = anElements->length();
877                 QListBoxItem* anItem = 0;
878                 for (int i = 0; i < k; i++) {
879                   anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
880                   if (anItem) delete anItem;
881                 }
882               }
883               catch (const SALOME::SALOME_Exception& ex) {
884                 QtCatchCorbaException(ex);
885               }
886             }
887           }
888         }
889       }
890     }
891     else if (myCurrentLineEdit == myGroupLine) {
892       Standard_Boolean aRes;
893       SALOME_ListIteratorOfListIO anIt(mySelection->StoredIObjects());
894       for (; anIt.More(); anIt.Next()) {
895         SMESH::SMESH_Group_var aGroup = mySMESHGUI->ConvertIOinSMESHGroup(anIt.Value(), aRes);
896         if (aRes && !aGroup->_is_nil()) {
897           // check if mesh is the same
898           if (aGroup->GetType() == aType && aGroup->GetMesh()->GetId() == myMesh->GetId()) {
899             SMESH::long_array_var anElements = aGroup->GetListOfID();
900             int k = anElements->length();
901             QListBoxItem* anItem = 0;
902             for (int i = 0; i < k; i++) {
903               anItem = myElements->findItem(QString::number(anElements[i]), Qt::ExactMatch);
904               if (anItem) delete anItem;
905             }
906           }
907         }
908       }
909     }
910   }
911   busy = false;
912   updateButtons();
913 }
914
915 //=================================================================================
916 // function : onSort()
917 // purpose  :
918 //=================================================================================
919 void SMESHGUI_GroupDlg::onSort()
920 {
921   // PAL5412: sorts items in ascending by "string" value
922   // myElements->sort(true);
923   // myElements->update();
924   int i, k = myElements->count();
925   if (k > 0) {
926     busy = true;
927     QStringList aSelected;
928     std::vector<int> anArray(k);
929     //    QMemArray<int> anArray(k);
930     QListBoxItem* anItem;
931     // fill the array
932     for (anItem = myElements->firstItem(), i = 0; anItem != 0; anItem = anItem->next(), i++) {
933       anArray[i] = anItem->text().toInt();
934       if (anItem->isSelected()) 
935         aSelected.append(anItem->text());
936     }
937     // sort & update list
938     std::sort(anArray.begin(), anArray.end());
939     //    anArray.sort();
940     myElements->clear();
941     for (i = 0; i < k; i++) {
942       myElements->insertItem(QString::number(anArray[i]));
943     }
944     for (QStringList::iterator it = aSelected.begin(); it != aSelected.end(); ++it) {
945       anItem = myElements->findItem(*it, Qt::ExactMatch);
946       if (anItem) myElements->setSelected(anItem, true);
947     }
948     busy = false;
949   }
950 }
951
952 //=================================================================================
953 // function : closeEvent()
954 // purpose  :
955 //=================================================================================
956 void SMESHGUI_GroupDlg::closeEvent( QCloseEvent* e )
957 {
958   QAD_StudyFrame* aStudyFrame = mySMESHGUI->GetActiveStudy()->getActiveStudyFrame();
959   if (aStudyFrame->getTypeView() == VIEW_VTK) {
960     mySMESHGUI->SetPickable();
961     if (mySelectionMode == 0)
962       mySMESHGUI->EraseSimulationActors();
963
964     // remove filters from viewer
965     VTKViewer_InteractorStyleSALOME* aStyle = ((VTKViewer_ViewFrame*)aStudyFrame->getRightFrame()->getViewFrame())->getRWInteractor()->GetInteractorStyleSALOME();
966     aStyle->RemoveEdgeFilter();
967     aStyle->RemoveFaceFilter();
968
969   }
970   
971   mySelection->ClearIObjects();
972   QAD_Application::getDesktop()->SetSelectionMode(ActorSelection);
973   mySelection->ClearFilters();
974   mySMESHGUI->ResetState();
975
976   QDialog::closeEvent( e );
977 }