Salome HOME
Portation on new based dialog
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GroupOp.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 //
20 //
21 //
22 //  File   : SMESHGUI_GroupOp.cxx
23 //  Author : Sergey LITONIN
24 //  Module : SMESH
25
26 #include <SMESHGUI_GroupOp.h>
27 #include <SMESHGUI_GroupOpDlg.h>
28 #include <SMESH_TypeFilter.hxx>
29 #include <SMESHGUI.h>
30
31 #include <SalomeApp_SelectionMgr.h>
32 #include <SalomeApp_UpdateFlags.h>
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_Desktop.h>
35
36 #include <SALOMEDS_SObject.hxx>
37
38 #include <SALOMEconfig.h>
39 #include CORBA_SERVER_HEADER(SMESH_Group)
40
41
42 /*
43   Class       : SMESHGUI_GroupOp
44   Description : Perform boolean operations on groups
45 */
46
47 //=======================================================================
48 // name    : SMESHGUI_GroupOp
49 // Purpose : 
50 //=======================================================================
51 SMESHGUI_GroupOp::SMESHGUI_GroupOp( const int mode )
52 : SMESHGUI_SelectionOp(),
53   myMode( mode ),
54   myDlg( 0 )
55 {
56 }
57
58 //=======================================================================
59 // name    : ~SMESHGUI_GroupOp
60 // Purpose :
61 //=======================================================================
62 SMESHGUI_GroupOp::~SMESHGUI_GroupOp()
63 {
64   if( myDlg )
65     delete myDlg;
66 }
67
68 //=======================================================================
69 // name    : dlg
70 // Purpose :
71 //=======================================================================
72 SalomeApp_Dialog* SMESHGUI_GroupOp::dlg() const
73 {
74   return myDlg;
75 }
76
77 //=======================================================================
78 // name    : onApply
79 // Purpose :
80 //=======================================================================
81 bool SMESHGUI_GroupOp::onApply()
82 {
83   if( !myDlg || !isValid() || isStudyLocked() )
84     return false;
85
86   QStringList selGroup[2];
87   for( int i=1; i<=2; i++ )
88     myDlg->selectedObject( i, selGroup[i-1] );
89     
90   _PTR(SObject) pGroup1 = studyDS()->FindObjectID( selGroup[0].first() ),
91                 pGroup2 = studyDS()->FindObjectID( selGroup[1].first() );
92   SMESH::SMESH_GroupBase_var aGroup1 = SMESH::SMESH_GroupBase::_narrow( _CAST(SObject,pGroup1)->GetObject() ),
93                              aGroup2 = SMESH::SMESH_GroupBase::_narrow( _CAST(SObject,pGroup2)->GetObject() );
94     
95   SMESH::SMESH_Mesh_ptr aMesh = aGroup1->GetMesh();
96   QString aName = myDlg->name();
97   SMESH::SMESH_Group_ptr aNewGrp = SMESH::SMESH_Group::_nil();
98
99   if( myMode == UNION )
100     aNewGrp = aMesh->UnionGroups( aGroup1, aGroup2, aName.latin1() );
101     
102   else if( myMode == INTERSECT )
103     aNewGrp = aMesh->IntersectGroups( aGroup1, aGroup2, aName.latin1() );
104     
105   else
106     aNewGrp = aMesh->CutGroups( aGroup1, aGroup2, aName.latin1() );
107
108   if (!aNewGrp->_is_nil())
109   {
110     update( UF_Model | UF_ObjBrowser );
111     initDialog();
112     return true;
113   }
114   else
115   {
116     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr("SMESH_ERROR"),
117                             tr("SMESH_OPERATION_FAILED"), tr( "SMESH_BUT_OK" ) );
118     return false;
119   }
120
121
122 //=======================================================================
123 // name    : startOperation
124 // Purpose :
125 //=======================================================================
126 void SMESHGUI_GroupOp::startOperation()
127 {
128   if( !myDlg )
129   {
130     myDlg = new SMESHGUI_GroupOpDlg( myMode );
131     connect( myDlg, SIGNAL( nameChanged( const QString& ) ), this, SLOT( onNameChanged( const QString& ) ) );
132   }
133
134   SMESHGUI_SelectionOp::startOperation();
135
136   myDlg->show();
137 }
138
139 //=======================================================================
140 // name    : startOperation
141 // Purpose :
142 //=======================================================================
143 SUIT_SelectionFilter* SMESHGUI_GroupOp::createFilter( const int ) const
144 {
145   return new SMESH_TypeFilter( GROUP );
146 }
147
148 //=======================================================================
149 // name    : startOperation
150 // Purpose :
151 //=======================================================================
152 void SMESHGUI_GroupOp::initDialog()
153 {
154   SMESHGUI_SelectionOp::initDialog();
155   if( myDlg )
156   {
157     myDlg->setName( QString::null );
158     updateDialog();
159   }
160 }
161
162 //=======================================================================
163 // name    : updateDialog
164 // Purpose :
165 //=======================================================================
166 void SMESHGUI_GroupOp::selectionDone()
167 {
168   SMESHGUI_SelectionOp::selectionDone();
169   updateDialog();
170 }
171
172 //=======================================================================
173 // name    : updateDialog
174 // Purpose :
175 //=======================================================================
176 void SMESHGUI_GroupOp::updateDialog()
177 {
178   if( !myDlg )
179     return;
180     
181   bool isEnabled = !myDlg->name().isEmpty() && myDlg->hasSelection( 1 ) && myDlg->hasSelection( 2 );
182   myDlg->setButtonEnabled( isEnabled, QtxDialog::OK | QtxDialog::Apply );
183 }
184
185 //=======================================================================
186 // name    : isValid
187 // Purpose : Verify validity of input data
188 //=======================================================================
189 bool SMESHGUI_GroupOp::isValid() const
190 {
191   if( !myDlg )
192     return false;
193     
194   // Verify validity of group name
195   if( myDlg->name().isEmpty() )
196   {
197     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
198                             tr("EMPTY_NAME"), tr( "SMESH_BUT_OK" ) );
199     return false;
200   }
201
202   QStringList selGroup[2];
203   for( int i=1; i<=2; i++ )
204     myDlg->selectedObject( i, selGroup[i-1] );
205
206   _PTR(SObject) pGroup1 = studyDS()->FindObjectID( selGroup[0].first() ),
207                 pGroup2 = studyDS()->FindObjectID( selGroup[1].first() );
208   SMESH::SMESH_GroupBase_var aGroup1 = SMESH::SMESH_GroupBase::_narrow( _CAST(SObject,pGroup1)->GetObject() ),
209                              aGroup2 = SMESH::SMESH_GroupBase::_narrow( _CAST(SObject,pGroup2)->GetObject() );
210   
211   // Verufy wheter arguments speciffiyed
212   if( aGroup1->_is_nil() || aGroup2->_is_nil())
213   {
214     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
215                             tr("INCORRECT_ARGUMENTS"), tr( "SMESH_BUT_OK" ) );
216     return false;
217   }
218
219   // Verify whether arguments belongs to same mesh
220   SMESH::SMESH_Mesh_ptr aMesh1 = aGroup1->GetMesh();
221   SMESH::SMESH_Mesh_ptr aMesh2 = aGroup2->GetMesh();
222
223   int aMeshId1 = !aMesh1->_is_nil() ? aMesh1->GetId() : -1;
224   int aMeshId2 = !aMesh2->_is_nil() ? aMesh2->GetId() : -1;
225
226   if (aMeshId1 != aMeshId2 || aMeshId1 == -1) {
227     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
228                             tr("DIFF_MESHES"), tr( "SMESH_BUT_OK" ) );
229     return false;
230   }
231
232   // Verify whether groups have same types of entities
233   if( aGroup1->GetType() != aGroup2->GetType() ) {
234     SUIT_MessageBox::warn1( SMESHGUI::desktop(), tr("SMESH_INSUFFICIENT_DATA"),
235                             tr("DIFF_TYPES"), tr( "SMESH_BUT_OK" ) );
236     return false;
237   }
238
239   return true;
240 }
241
242 //=======================================================================
243 // name    : onNameChanged
244 // Purpose :
245 //=======================================================================
246 void SMESHGUI_GroupOp::onNameChanged( const QString& )
247 {
248   updateDialog();
249 }