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