Salome HOME
The selections of elements corrected.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupOpDlg.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_GroupOpDlg.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_GroupOpDlg.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_VTKUtils.h"
33
34 #include "SMESH_TypeFilter.hxx"
35
36 #include "SUIT_ResourceMgr.h"
37 #include "SUIT_Desktop.h"
38
39 #include "SalomeApp_SelectionMgr.h"
40 #include "SVTK_Selection.h"
41 #include "SVTK_ViewWindow.h"
42 #include "SVTK_Selector.h"
43 #include "SALOME_ListIO.hxx"
44
45 // QT Includes
46 #include <qframe.h>
47 #include <qlayout.h>
48 #include <qpushbutton.h>
49 #include <qgroupbox.h>
50 #include <qlabel.h>
51 #include <qlistbox.h>
52 #include <qlineedit.h>
53 #include <qmessagebox.h>
54
55 #define SPACING 5
56 #define MARGIN  10
57
58 /*!
59  *  Class       : SMESHGUI_GroupOpDlg
60  *  Description : Perform boolean operations on groups
61  */
62
63 //=======================================================================
64 // name    : SMESHGUI_GroupOpDlg::SMESHGUI_GroupOpDlg
65 // Purpose : Constructor
66 //=======================================================================
67 SMESHGUI_GroupOpDlg::SMESHGUI_GroupOpDlg( SMESHGUI* theModule, const int theMode )
68      : QDialog( SMESH::GetDesktop( theModule ), "SMESHGUI_GroupOpDlg", false,
69                 WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
70      mySMESHGUI( theModule ),
71      mySelectionMgr( SMESH::GetSelectionMgr( theModule ) ),
72      myViewWindow( SMESH::GetViewWindow( theModule ) ),
73      mySelector( myViewWindow->GetSelector() )
74 {
75   myMode = theMode;
76
77   if (myMode == UNION) setCaption(tr("UNION_OF_TWO_GROUPS"));
78   else if (myMode == INTERSECT) setCaption(tr("INTERSECTION_OF_TWO_GROUPS"));
79   else setCaption(tr("CUT_OF_TWO_GROUPS"));
80
81   QVBoxLayout* aDlgLay = new QVBoxLayout (this, MARGIN, SPACING);
82
83   QFrame* aMainFrame = createMainFrame  (this);
84   QFrame* aBtnFrame  = createButtonFrame(this);
85
86   aDlgLay->addWidget(aMainFrame);
87   aDlgLay->addWidget(aBtnFrame);
88
89   aDlgLay->setStretchFactor(aMainFrame, 1);
90
91   Init();
92 }
93
94 //=======================================================================
95 // name    : SMESHGUI_GroupOpDlg::createMainFrame
96 // Purpose : Create frame containing dialog's input fields
97 //=======================================================================
98 QFrame* SMESHGUI_GroupOpDlg::createMainFrame (QWidget* theParent)
99 {
100   QGroupBox* aMainGrp = new QGroupBox(1, Qt::Horizontal, theParent);
101   aMainGrp->setFrameStyle(QFrame::NoFrame);
102   aMainGrp->setInsideMargin(0);
103
104   QGroupBox* aNameGrp = new QGroupBox(1, Qt::Vertical, tr("NAME"), aMainGrp);
105   new QLabel(tr("RESULT_NAME"), aNameGrp);
106   myNameEdit = new QLineEdit(aNameGrp);
107
108   QGroupBox* anArgGrp = new QGroupBox(3, Qt::Horizontal, tr("ARGUMENTS"), aMainGrp);
109
110   new QLabel(myMode == CUT ? tr("MAIN_OBJECT") :tr("OBJECT_1"), anArgGrp);
111   myBtn1 = new QPushButton(anArgGrp);
112   myEdit1 = new QLineEdit(anArgGrp);
113   myEdit1->setAlignment( Qt::AlignLeft );
114
115   new QLabel(myMode == CUT ? tr("TOOL_OBJECT") :tr("OBJECT_2"), anArgGrp);
116   myBtn2 = new QPushButton(anArgGrp);
117   myEdit2 = new QLineEdit(anArgGrp);
118   myEdit2->setAlignment( Qt::AlignLeft );
119
120   myEdit1->setReadOnly(true);
121   myEdit2->setReadOnly(true);
122
123   QPixmap aPix (SMESH::GetResourceMgr( mySMESHGUI )->loadPixmap("SMESH", tr("ICON_SELECT")));
124   myBtn1->setPixmap(aPix);
125   myBtn2->setPixmap(aPix);
126
127   return aMainGrp;
128 }
129
130 //=======================================================================
131 // name    : SMESHGUI_GroupOpDlg::createButtonFrame
132 // Purpose : Create frame containing buttons
133 //=======================================================================
134 QFrame* SMESHGUI_GroupOpDlg::createButtonFrame (QWidget* theParent)
135 {
136   QFrame* aFrame = new QFrame(theParent);
137   aFrame->setFrameStyle(QFrame::Box | QFrame::Sunken);
138
139   myOkBtn     = new QPushButton(tr("SMESH_BUT_OK"   ), aFrame);
140   myApplyBtn  = new QPushButton(tr("SMESH_BUT_APPLY"), aFrame);
141   myCloseBtn  = new QPushButton(tr("SMESH_BUT_CLOSE"), aFrame);
142
143   QSpacerItem* aSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
144
145   QHBoxLayout* aLay = new QHBoxLayout(aFrame, MARGIN, SPACING);
146
147   aLay->addWidget(myOkBtn);
148   aLay->addWidget(myApplyBtn);
149   aLay->addItem(aSpacer);
150   aLay->addWidget(myCloseBtn);
151
152   // connect signals and slots
153   connect(myOkBtn,    SIGNAL(clicked()), SLOT(onOk()));
154   connect(myCloseBtn, SIGNAL(clicked()), SLOT(onClose()));
155   connect(myApplyBtn, SIGNAL(clicked()), SLOT(onApply()));
156
157   return aFrame;
158 }
159
160 //=======================================================================
161 // name    : SMESHGUI_GroupOpDlg::~SMESHGUI_GroupOpDlg
162 // Purpose : Destructor
163 //=======================================================================
164 SMESHGUI_GroupOpDlg::~SMESHGUI_GroupOpDlg()
165 {
166 }
167
168 //=======================================================================
169 // name    : SMESHGUI_GroupOpDlg::Init
170 // Purpose : Init dialog fields, connect signals and slots, show dialog
171 //=======================================================================
172 void SMESHGUI_GroupOpDlg::Init()
173 {
174   mySMESHGUI->SetActiveDialogBox((QDialog*)this);
175   myFocusWg = myEdit1;
176
177   myGroup1 = SMESH::SMESH_GroupBase::_nil();
178   myGroup2 = SMESH::SMESH_GroupBase::_nil();
179
180   // selection and SMESHGUI
181   connect(mySelectionMgr, SIGNAL(currentSelectionChanged()), SLOT(onSelectionDone()));
182   connect(mySMESHGUI, SIGNAL(SignalDeactivateActiveDialog()), SLOT(onDeactivate()));
183   connect(mySMESHGUI, SIGNAL(SignalCloseAllDialogs()), SLOT(ClickOnClose()));
184
185   connect(myBtn1, SIGNAL(clicked()), this, SLOT(onFocusChanged()));
186   connect(myBtn2, SIGNAL(clicked()), this, SLOT(onFocusChanged()));
187
188   int x, y;
189   mySMESHGUI->DefineDlgPosition(this, x, y);
190   this->move(x, y);
191   this->show();
192
193   // set selection mode
194   myViewWindow->SetSelectionMode(ActorSelection);
195   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
196
197   return;
198 }
199
200 //=======================================================================
201 // name    : SMESHGUI_GroupOpDlg::isValid
202 // Purpose : Verify validity of input data
203 //=======================================================================
204 bool SMESHGUI_GroupOpDlg::isValid()
205 {
206   // Verify validity of group name
207   if (myNameEdit->text() == "") {
208     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
209                              tr("EMPTY_NAME"), QMessageBox::Ok);
210     return false;
211   }
212
213   // Verufy wheter arguments speciffiyed
214   if (myGroup1->_is_nil() || myGroup2->_is_nil()) {
215     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
216                              tr("INCORRECT_ARGUMENTS"), QMessageBox::Ok);
217     return false;
218   }
219
220   // Verify whether arguments belongs to same mesh
221   SMESH::SMESH_Mesh_ptr aMesh1 = myGroup1->GetMesh();
222   SMESH::SMESH_Mesh_ptr aMesh2 = myGroup2->GetMesh();
223
224   int aMeshId1 = !aMesh1->_is_nil() ? aMesh1->GetId() : -1;
225   int aMeshId2 = !aMesh2->_is_nil() ? aMesh2->GetId() : -1;
226
227   if (aMeshId1 != aMeshId2 || aMeshId1 == -1) {
228     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
229                              tr("DIFF_MESHES"), QMessageBox::Ok);
230     return false;
231   }
232
233   // Verify whether groups have same types of entities
234   if (myGroup1->GetType() != myGroup2->GetType()) {
235     QMessageBox::information(SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
236                              tr("DIFF_TYPES"), QMessageBox::Ok);
237     return false;
238   }
239
240   return true;
241 }
242
243 //=======================================================================
244 // name    : SMESHGUI_GroupOpDlg::onApply
245 // Purpose : SLOT called when "Apply" button pressed.
246 //=======================================================================
247 bool SMESHGUI_GroupOpDlg::onApply()
248 {
249   if (!isValid() || mySMESHGUI->isActiveStudyLocked())
250     return false;
251
252   SMESH::SMESH_Mesh_ptr aMesh = myGroup1->GetMesh();
253   QString aName = myNameEdit->text();
254   SMESH::SMESH_Group_ptr aNewGrp = SMESH::SMESH_Group::_nil();
255
256   if (myMode == UNION) aNewGrp = aMesh->UnionGroups(myGroup1, myGroup2, aName.latin1());
257   else if (myMode == INTERSECT) aNewGrp = aMesh->IntersectGroups(myGroup1, myGroup2, aName.latin1());
258   else aNewGrp = aMesh->CutGroups(myGroup1, myGroup2, aName.latin1());
259
260   if (!aNewGrp->_is_nil()) {
261     mySMESHGUI->updateObjBrowser(true);
262     reset();
263     return true;
264   } else {
265     QMessageBox::critical(SMESHGUI::desktop(), tr("SMESH_ERROR"),
266                           tr("SMESH_OPERATION_FAILED"), "OK");
267     return false;
268   }
269 }
270
271 //=======================================================================
272 // name    : SMESHGUI_GroupOpDlg::onOk
273 // Purpose : SLOT called when "Ok" button pressed.
274 //=======================================================================
275 void SMESHGUI_GroupOpDlg::onOk()
276 {
277   if (onApply())
278     onClose();
279 }
280
281 //=======================================================================
282 // name    : SMESHGUI_GroupOpDlg::onClose
283 // Purpose : SLOT called when "Close" button pressed. Close dialog
284 //=======================================================================
285 void SMESHGUI_GroupOpDlg::onClose()
286 {
287   myViewWindow->SetSelectionMode(ActorSelection);
288   disconnect(mySelectionMgr, 0, this, 0);
289   disconnect(mySMESHGUI, 0, this, 0);
290   mySMESHGUI->ResetState();
291   mySelectionMgr->clearFilters();
292   reject();
293 }
294
295 //=======================================================================
296 // name    : SMESHGUI_GroupOpDlg::onSelectionDone
297 // Purpose : SLOT called when selection changed
298 //=======================================================================
299 void SMESHGUI_GroupOpDlg::onSelectionDone()
300 {
301   if (myFocusWg == myEdit1)
302     myGroup1 = SMESH::SMESH_GroupBase::_nil();
303   else
304     myGroup2 = SMESH::SMESH_GroupBase::_nil();
305
306   myFocusWg->setText("");
307
308   SALOME_ListIO aList;
309   mySelectionMgr->selectedObjects(aList);
310
311   if (aList.Extent() == 1) {
312     SMESH::SMESH_GroupBase_var aGroup =
313       SMESH::IObjectToInterface<SMESH::SMESH_GroupBase>(aList.First());
314
315     if (!aGroup->_is_nil())
316     {
317       myFocusWg->setText(aGroup->GetName());
318       myFocusWg->setCursorPosition( 0 );
319
320       if (myFocusWg == myEdit1)
321         myGroup1 = aGroup;
322       else
323         myGroup2 = aGroup;
324     }
325   }
326 }
327
328 //=======================================================================
329 // name    : SMESHGUI_GroupOpDlg::onDeactivate
330 // Purpose : SLOT called when dialog must be deativated
331 //=======================================================================
332 void SMESHGUI_GroupOpDlg::onDeactivate()
333 {
334   setEnabled(false);
335   mySelectionMgr->clearFilters();
336 }
337
338 //=======================================================================
339 // name    : SMESHGUI_GroupOpDlg::enterEvent
340 // Purpose : Event filter
341 //=======================================================================
342 void SMESHGUI_GroupOpDlg::enterEvent (QEvent*)
343 {
344   mySMESHGUI->EmitSignalDeactivateDialog();
345   setEnabled(true);
346   myViewWindow->SetSelectionMode(ActorSelection);
347   mySelectionMgr->installFilter(new SMESH_TypeFilter (GROUP));
348 }
349
350 //=======================================================================
351 // name    : SMESHGUI_GroupOpDlg::closeEvent
352 // purpose :
353 //=======================================================================
354 void SMESHGUI_GroupOpDlg::closeEvent (QCloseEvent*)
355 {
356   onClose();
357 }
358
359 //=======================================================================
360 // name    : SMESHGUI_GroupOpDlg::onFocusChanged
361 // Purpose : SLOT. Called when "Select" button pressed.
362 //=======================================================================
363 void SMESHGUI_GroupOpDlg::onFocusChanged()
364 {
365   const QObject* aSender = sender();
366   myFocusWg = aSender == myBtn1 ? myEdit1 : myEdit2;
367   onSelectionDone();
368 }
369
370 //=======================================================================
371 // name    : SMESHGUI_GroupOpDlg::reset
372 // Purpose : Rest state of dialog
373 //=======================================================================
374 void SMESHGUI_GroupOpDlg::reset()
375 {
376   myNameEdit->setText("");
377   myEdit1->setText("");
378   myEdit2->setText("");
379   myFocusWg = myEdit1;
380   myNameEdit->setFocus();
381 }