Salome HOME
*** empty log message ***
[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 <GEOM_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( "newentity_blocks.htm#QuadrangleFace" );
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   GEOM::GEOM_Object_var anObj;
226   Standard_Boolean aResult = Standard_False;
227   if ( IObjectCount() == 1 ) {
228     anObj = GEOMBase::ConvertIOinGEOMObject( firstIObject(), aResult );
229     if ( aResult ) {
230       if (anObj->_is_nil()) {
231         aResult = Standard_False;
232       } 
233       else {
234         mySelName[aCurrFocus]->setText( GEOMBase::GetName( anObj ) );
235       }
236     } 
237     else {
238       anObj = GEOM::GEOM_Object::_nil();
239     }
240   }
241
242   switch ( aCurrFocus ) {
243   case Vertex1:
244   case Edge12:
245   case Edge14:
246     myShape1 = anObj; break;
247   case Vertex2:
248   case Edge22:
249   case Edge24:
250     myShape2 = anObj; break;
251   case Vertex3:
252   case Edge34:
253     myShape3 = anObj; break;
254   case Vertex4:
255   case Edge44:
256     myShape4 = anObj; break;
257   default:
258     return;
259   }
260   displayPreview();
261 }
262
263 //=================================================================================
264 // function : SetEditCurrentArgument()
265 // purpose  :
266 //=================================================================================
267 void BlocksGUI_QuadFaceDlg::SetEditCurrentArgument()
268 {
269   QPushButton* aSender = (QPushButton*)sender();
270
271   QMap<int, QPushButton*>::iterator anIter;
272   for ( anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter ) {
273     if ( anIter.value() == aSender ) {
274       mySelName[anIter.key()]->setFocus();
275       myEditCurrentArgument = mySelName[anIter.key()];
276     }
277   }
278
279   activateSelection();
280 }
281
282 //=================================================================================
283 // function : ActivateThisDialog()
284 // purpose  :
285 //=================================================================================
286 void BlocksGUI_QuadFaceDlg::ActivateThisDialog()
287 {
288   GEOMBase_Skeleton::ActivateThisDialog();
289   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
290            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
291
292   activateSelection();
293   displayPreview();
294 }
295
296 //=================================================================================
297 // function : enterEvent()
298 // purpose  :
299 //=================================================================================
300 void BlocksGUI_QuadFaceDlg::enterEvent( QEvent* )
301 {
302   if ( !mainFrame()->GroupConstructors->isEnabled() )
303     this->ActivateThisDialog();
304 }
305
306 //=================================================================================
307 // function : DeactivateActiveDialog()
308 // purpose  :
309 //=================================================================================
310 //void BlocksGUI_QuadFaceDlg::DeactivateActiveDialog()
311 //{
312 //  // disconnect selection
313 //  GEOMBase_Skeleton::DeactivateActiveDialog();
314 //}
315
316 //=================================================================================
317 // function : createSelWg()
318 // purpose  :
319 //=================================================================================
320 void BlocksGUI_QuadFaceDlg::createSelWg( const QString& theLbl,
321                                          QPixmap&       thePix,
322                                          QWidget*       theParent,
323                                          const int      theId )
324 {
325   QLabel* lab = new QLabel( theLbl, theParent );
326   mySelBtn[theId] = new QPushButton( theParent );
327   mySelBtn[theId]->setIcon( thePix );
328   mySelBtn[theId]->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
329   mySelName[theId] = new QLineEdit( theParent );
330   mySelName[theId]->setReadOnly( true );
331   QGridLayout* l = 0;
332   if ( !theParent->layout() ) {
333     l = new QGridLayout( theParent );
334     l->setMargin( 9 ); l->setSpacing( 6 );
335   }
336   else {
337     l = qobject_cast<QGridLayout*>( theParent->layout() );
338   }
339   int row = l->rowCount();
340   l->addWidget( lab,              row, 0 );
341   l->addWidget( mySelBtn[theId],  row, 1 );
342   l->addWidget( mySelName[theId], row, 2 );
343 }
344
345 //=================================================================================
346 // function : activateSelection
347 // purpose  : Activate selection in accordance with myEditCurrentArgument
348 //=================================================================================
349 void BlocksGUI_QuadFaceDlg::activateSelection()
350 {
351   if ( myEditCurrentArgument == mySelName[Vertex1] ||
352        myEditCurrentArgument == mySelName[Vertex2] ||
353        myEditCurrentArgument == mySelName[Vertex3] ||
354        myEditCurrentArgument == mySelName[Vertex4] ) {
355     globalSelection( GEOM_POINT );
356   } 
357   else {
358     globalSelection( GEOM_EDGE );
359   }
360
361   SelectionIntoArgument();
362 }
363
364 //=================================================================================
365 // function : createOperation
366 // purpose  :
367 //=================================================================================
368 GEOM::GEOM_IOperations_ptr BlocksGUI_QuadFaceDlg::createOperation()
369 {
370   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
371 }
372
373 //=================================================================================
374 // function : isValid
375 // purpose  : Verify validity of input data
376 //=================================================================================
377 bool BlocksGUI_QuadFaceDlg::isValid( QString& )
378 {
379   bool ok = false;
380   switch ( getConstructorId() ) {
381   case 0:
382     ok = ( !myShape1->_is_nil() && !myShape2->_is_nil() &&
383            !myShape3->_is_nil() && !myShape4->_is_nil() ); 
384     break;
385   case 1:
386     ok = ( !myShape1->_is_nil() && !myShape2->_is_nil() );
387     break;
388   case 2:
389     ok = ( !myShape1->_is_nil() && !myShape2->_is_nil() &&
390            !myShape3->_is_nil() && !myShape4->_is_nil() );
391     break;
392   default:
393     break;
394   }
395   return ok;
396 }
397
398 //=================================================================================
399 // function : execute
400 // purpose  :
401 //=================================================================================
402 bool BlocksGUI_QuadFaceDlg::execute( ObjectList& objects )
403 {
404   bool res = false;
405
406   GEOM::GEOM_Object_var anObj;
407
408   switch ( getConstructorId() ) {
409   case 0:
410     anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->MakeQuad4Vertices
411       ( myShape1, myShape2, myShape3, myShape4 );
412     res = true;
413     break;
414   case 1:
415     anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->MakeQuad2Edges
416       ( myShape1, myShape2 );
417     res = true;
418     break;
419   case 2:
420     anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->MakeQuad
421       ( myShape1, myShape2, myShape3, myShape4 );
422     res = true;
423     break;
424   default:
425     break;
426   }
427
428   if ( !anObj->_is_nil() )
429     objects.push_back( anObj._retn() );
430   
431   return res;
432 }