Salome HOME
d6005a3504c95baf2eaebca5761f3a74bba5d144
[modules/geom.git] / src / BlocksGUI / BlocksGUI_QuadFaceDlg.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  CEA
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 //
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 // File   : BlocksGUI_QuadFaceDlg.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
23 //
24
25 #include "BlocksGUI_QuadFaceDlg.h"
26
27 #include <DlgRef.h>
28 #include <GeometryGUI.h>
29 #include <GEOMBase.h>
30 #include <GEOMImpl_Types.hxx>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <qlabel.h>
38
39 //=================================================================================
40 // class    : BlocksGUI_QuadFaceDlg()
41 // purpose  : Constructs a BlocksGUI_QuadFaceDlg which is a child of 'parent'.
42 //=================================================================================
43 BlocksGUI_QuadFaceDlg::BlocksGUI_QuadFaceDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
44   : GEOMBase_Skeleton( theGeometryGUI, parent )
45 {
46   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
47   QPixmap image1( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_QUAD_FACE_4_VERT" ) ) );
48   QPixmap image2( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_QUAD_FACE_2_EDGE" ) ) );
49   QPixmap image3( aResMgr->loadPixmap( "GEOM", tr( "ICON_DLG_QUAD_FACE_4_EDGE" ) ) );
50   QPixmap imageS( aResMgr->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
51
52   setWindowTitle( tr( "GEOM_QUAD_FACE_TITLE" ) );
53
54   /***************************************************************/
55   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_QUAD_FACE" ) );
56
57   mainFrame()->RadioButton1->setIcon( image1 );
58   mainFrame()->RadioButton2->setIcon( image2 );
59   mainFrame()->RadioButton3->setIcon( image3 );
60
61   // Create first group
62   myGrp1 = new QGroupBox( tr( "GEOM_ARGUMENTS" ), centralWidget() );
63
64   createSelWg( tr( "VERTEX_1" ), imageS, myGrp1, Vertex1 );
65   createSelWg( tr( "VERTEX_2" ), imageS, myGrp1, Vertex2 );
66   createSelWg( tr( "VERTEX_3" ), imageS, myGrp1, Vertex3 );
67   createSelWg( tr( "VERTEX_4" ), imageS, myGrp1, Vertex4 );
68
69   // Create second group
70   myGrp2 = new QGroupBox( tr( "GEOM_ARGUMENTS" ), centralWidget() );
71
72   createSelWg( tr( "EDGE_1" ), imageS, myGrp2, Edge12 );
73   createSelWg( tr( "EDGE_2" ), imageS, myGrp2, Edge22 );
74
75   // Create fird group
76   myGrp3 = new QGroupBox( tr( "GEOM_ARGUMENTS" ), centralWidget() );
77
78   createSelWg( tr( "EDGE_1" ), imageS, myGrp3, Edge14 );
79   createSelWg( tr( "EDGE_2" ), imageS, myGrp3, Edge24 );
80   createSelWg( tr( "EDGE_3" ), imageS, myGrp3, Edge34 );
81   createSelWg( tr( "EDGE_4" ), imageS, myGrp3, Edge44 );
82
83   // Add groups to layout
84   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
85   layout->setMargin( 0 ); layout->setSpacing( 6 );
86   layout->addWidget( myGrp1 );
87   layout->addWidget( myGrp2 );
88   layout->addWidget( myGrp3 );
89
90   /***************************************************************/
91
92   setHelpFileName( "build_by_blocks_page.html#quad_face_anchor" );
93
94   Init();
95 }
96
97 //=================================================================================
98 // function : ~BlocksGUI_QuadFaceDlg()
99 // purpose  : Destroys the object and frees any allocated resources
100 //=================================================================================
101 BlocksGUI_QuadFaceDlg::~BlocksGUI_QuadFaceDlg()
102 {
103   // no need to delete child widgets, Qt does it all for us
104 }
105
106 //=================================================================================
107 // function : Init()
108 // purpose  :
109 //=================================================================================
110 void BlocksGUI_QuadFaceDlg::Init()
111 {
112   // signals and slots connections
113   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
114   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
115
116   connect( this, SIGNAL( constructorsClicked( int ) ), 
117            this, SLOT( ConstructorsClicked( int ) ) );
118
119   QMap<int, QPushButton*>::iterator anIterBtn;
120   for ( anIterBtn = mySelBtn.begin(); anIterBtn != mySelBtn.end(); ++anIterBtn )
121     connect( anIterBtn.value(), SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
122
123   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
124            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
125
126   // init controls and fields
127   initName( tr( "GEOM_QUAD_FACE" ) );
128
129   myConstructorId = -1;
130   ConstructorsClicked( 0 );
131 }
132
133 //=================================================================================
134 // function : ConstructorsClicked()
135 // purpose  : Radio button management
136 //=================================================================================
137 void BlocksGUI_QuadFaceDlg::ConstructorsClicked( int constructorId )
138 {
139   if ( myConstructorId == constructorId )
140     return;
141
142   myConstructorId = constructorId;
143
144   switch ( constructorId ) {
145   case 0:
146     myGrp2->hide();
147     myGrp3->hide();
148     myGrp1->show();
149     myEditCurrentArgument = mySelName[Vertex1];
150     break;
151   case 1:
152     myGrp1->hide();
153     myGrp3->hide();
154     myGrp2->show();
155     myEditCurrentArgument = mySelName[Edge12];
156     break;
157   case 2:
158     myGrp1->hide();
159     myGrp2->hide();
160     myGrp3->show();
161     myEditCurrentArgument = mySelName[Edge14];
162     break;
163   default:
164     break;
165   }
166
167   // clear line edits
168   QMap<int, QLineEdit*>::iterator anIterLE;
169   for ( anIterLE = mySelName.begin(); anIterLE != mySelName.end(); ++anIterLE )
170     anIterLE.value()->setText( "" );
171
172   // init fields
173   myShape1 = myShape2 = GEOM::GEOM_Object::_nil();
174   myShape3 = myShape4 = myShape1;
175
176   qApp->processEvents();
177   updateGeometry();
178   resize( minimumSize() );
179
180   activateSelection();
181 }
182
183 //=================================================================================
184 // function : ClickOnOk()
185 // purpose  :
186 //=================================================================================
187 void BlocksGUI_QuadFaceDlg::ClickOnOk()
188 {
189   if ( ClickOnApply() )
190     ClickOnCancel();
191 }
192
193 //=================================================================================
194 // function : ClickOnApply()
195 // purpose  :
196 //=================================================================================
197 bool BlocksGUI_QuadFaceDlg::ClickOnApply()
198 {
199   if ( !onAccept() )
200     return false;
201
202   initName();
203   return true;
204 }
205
206 //=================================================================================
207 // function : SelectionIntoArgument()
208 // purpose  : Called when selection has changed
209 //=================================================================================
210 void BlocksGUI_QuadFaceDlg::SelectionIntoArgument()
211 {
212   erasePreview();
213   myEditCurrentArgument->setText( "" );
214
215   // Get index of current selection focus
216   int aCurrFocus = -1;
217   QMap<int, QLineEdit*>::iterator anIter;
218   for ( anIter = mySelName.begin(); anIter != mySelName.end(); ++anIter ) {
219     if ( myEditCurrentArgument == anIter.value() ) {
220       aCurrFocus = anIter.key();
221       break;
222     }
223   }
224
225   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
226   SALOME_ListIO aSelList;
227   aSelMgr->selectedObjects(aSelList);
228
229   GEOM::GEOM_Object_var anObj;
230   if (aSelList.Extent() == 1) {
231     Standard_Boolean aResult = Standard_False;
232     anObj = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), aResult);
233     if ( aResult ) {
234       if (anObj->_is_nil()) {
235         aResult = Standard_False;
236       } 
237       else {
238         mySelName[aCurrFocus]->setText( GEOMBase::GetName( anObj ) );
239       }
240     } 
241     else {
242       anObj = GEOM::GEOM_Object::_nil();
243     }
244   }
245
246   switch ( aCurrFocus ) {
247   case Vertex1:
248   case Edge12:
249   case Edge14:
250     myShape1 = anObj; break;
251   case Vertex2:
252   case Edge22:
253   case Edge24:
254     myShape2 = anObj; break;
255   case Vertex3:
256   case Edge34:
257     myShape3 = anObj; break;
258   case Vertex4:
259   case Edge44:
260     myShape4 = anObj; break;
261   default:
262     return;
263   }
264   displayPreview();
265 }
266
267 //=================================================================================
268 // function : SetEditCurrentArgument()
269 // purpose  :
270 //=================================================================================
271 void BlocksGUI_QuadFaceDlg::SetEditCurrentArgument()
272 {
273   QPushButton* aSender = (QPushButton*)sender();
274
275   QMap<int, QPushButton*>::iterator anIter;
276   for ( anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter ) {
277     if ( anIter.value() == aSender ) {
278       mySelName[anIter.key()]->setFocus();
279       myEditCurrentArgument = mySelName[anIter.key()];
280     }
281   }
282
283   activateSelection();
284 }
285
286 //=================================================================================
287 // function : ActivateThisDialog()
288 // purpose  :
289 //=================================================================================
290 void BlocksGUI_QuadFaceDlg::ActivateThisDialog()
291 {
292   GEOMBase_Skeleton::ActivateThisDialog();
293   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
294            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
295
296   activateSelection();
297   displayPreview();
298 }
299
300 //=================================================================================
301 // function : enterEvent()
302 // purpose  :
303 //=================================================================================
304 void BlocksGUI_QuadFaceDlg::enterEvent( QEvent* )
305 {
306   if ( !mainFrame()->GroupConstructors->isEnabled() )
307     this->ActivateThisDialog();
308 }
309
310 //=================================================================================
311 // function : DeactivateActiveDialog()
312 // purpose  :
313 //=================================================================================
314 //void BlocksGUI_QuadFaceDlg::DeactivateActiveDialog()
315 //{
316 //  // disconnect selection
317 //  GEOMBase_Skeleton::DeactivateActiveDialog();
318 //}
319
320 //=================================================================================
321 // function : createSelWg()
322 // purpose  :
323 //=================================================================================
324 void BlocksGUI_QuadFaceDlg::createSelWg( const QString& theLbl,
325                                          QPixmap&       thePix,
326                                          QWidget*       theParent,
327                                          const int      theId )
328 {
329   QLabel* lab = new QLabel( theLbl, theParent );
330   mySelBtn[theId] = new QPushButton( theParent );
331   mySelBtn[theId]->setIcon( thePix );
332   mySelBtn[theId]->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
333   mySelName[theId] = new QLineEdit( theParent );
334   mySelName[theId]->setReadOnly( true );
335   QGridLayout* l = 0;
336   if ( !theParent->layout() ) {
337     l = new QGridLayout( theParent );
338     l->setMargin( 9 ); l->setSpacing( 6 );
339   }
340   else {
341     l = qobject_cast<QGridLayout*>( theParent->layout() );
342   }
343   int row = l->rowCount();
344   l->addWidget( lab,              row, 0 );
345   l->addWidget( mySelBtn[theId],  row, 1 );
346   l->addWidget( mySelName[theId], row, 2 );
347 }
348
349 //=================================================================================
350 // function : activateSelection
351 // purpose  : Activate selection in accordance with myEditCurrentArgument
352 //=================================================================================
353 void BlocksGUI_QuadFaceDlg::activateSelection()
354 {
355   if ( myEditCurrentArgument == mySelName[Vertex1] ||
356        myEditCurrentArgument == mySelName[Vertex2] ||
357        myEditCurrentArgument == mySelName[Vertex3] ||
358        myEditCurrentArgument == mySelName[Vertex4] ) {
359     globalSelection( GEOM_POINT );
360   } 
361   else {
362     globalSelection( GEOM_EDGE );
363   }
364
365   SelectionIntoArgument();
366 }
367
368 //=================================================================================
369 // function : createOperation
370 // purpose  :
371 //=================================================================================
372 GEOM::GEOM_IOperations_ptr BlocksGUI_QuadFaceDlg::createOperation()
373 {
374   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
375 }
376
377 //=================================================================================
378 // function : isValid
379 // purpose  : Verify validity of input data
380 //=================================================================================
381 bool BlocksGUI_QuadFaceDlg::isValid( QString& )
382 {
383   bool ok = false;
384   switch ( getConstructorId() ) {
385   case 0:
386     ok = ( !myShape1->_is_nil() && !myShape2->_is_nil() &&
387            !myShape3->_is_nil() && !myShape4->_is_nil() ); 
388     break;
389   case 1:
390     ok = ( !myShape1->_is_nil() && !myShape2->_is_nil() );
391     break;
392   case 2:
393     ok = ( !myShape1->_is_nil() && !myShape2->_is_nil() &&
394            !myShape3->_is_nil() && !myShape4->_is_nil() );
395     break;
396   default:
397     break;
398   }
399   return ok;
400 }
401
402 //=================================================================================
403 // function : execute
404 // purpose  :
405 //=================================================================================
406 bool BlocksGUI_QuadFaceDlg::execute( ObjectList& objects )
407 {
408   bool res = false;
409
410   GEOM::GEOM_Object_var anObj;
411
412   switch ( getConstructorId() ) {
413   case 0:
414     anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->MakeQuad4Vertices
415       ( myShape1, myShape2, myShape3, myShape4 );
416     res = true;
417     break;
418   case 1:
419     anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->MakeQuad2Edges
420       ( myShape1, myShape2 );
421     res = true;
422     break;
423   case 2:
424     anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->MakeQuad
425       ( myShape1, myShape2, myShape3, myShape4 );
426     res = true;
427     break;
428   default:
429     break;
430   }
431
432   if ( !anObj->_is_nil() )
433     objects.push_back( anObj._retn() );
434   
435   return res;
436 }