Salome HOME
*** empty log message ***
[modules/geom.git] / src / BlocksGUI / BlocksGUI_BlockDlg.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_BlockDlg.cxx
22 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
23 //
24
25 #include "BlocksGUI_BlockDlg.h"
26
27 #include <DlgRef.h>
28 #include <GeometryGUI.h>
29 #include <GEOMBase.h>
30
31 #include <SUIT_Session.h>
32 #include <SUIT_ResourceMgr.h>
33 #include <SalomeApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35
36 #include <GEOMImpl_Types.hxx>
37
38 //=================================================================================
39 // class    : BlocksGUI_BlockDlg()
40 // purpose  : Constructs a BlocksGUI_BlockDlg which is a child of 'parent'.
41 //=================================================================================
42 BlocksGUI_BlockDlg::BlocksGUI_BlockDlg( GeometryGUI* theGeometryGUI, QWidget* parent )
43   : GEOMBase_Skeleton( theGeometryGUI, parent )
44 {
45   QPixmap image0( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BLOCK_2F" ) ) );
46   QPixmap image1( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_DLG_BLOCK_6F" ) ) );
47   QPixmap imageS( SUIT_Session::session()->resourceMgr()->loadPixmap( "GEOM", tr( "ICON_SELECT" ) ) );
48
49   setWindowTitle( tr( "GEOM_BLOCK_TITLE" ) );
50
51   /***************************************************************/
52   mainFrame()->GroupConstructors->setTitle( tr( "GEOM_BLOCK" ) );
53
54   mainFrame()->RadioButton1->setIcon( image0 );
55   mainFrame()->RadioButton2->setIcon( image1 );
56   mainFrame()->RadioButton3->setAttribute( Qt::WA_DeleteOnClose );
57   mainFrame()->RadioButton3->close();
58
59   // Create first group
60   Group2F = new DlgRef_2Sel( centralWidget() );
61   Group2F->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
62   Group2F->TextLabel1->setText( tr( "FACE_1" ) );
63   Group2F->TextLabel2->setText( tr( "FACE_2" ) );
64   Group2F->PushButton1->setIcon( imageS );
65   Group2F->PushButton2->setIcon( imageS );
66
67   // Create second group
68   Group6F = new DlgRef_6Sel( centralWidget() );
69   Group6F->GroupBox1->setTitle( tr( "GEOM_ARGUMENTS" ) );
70   Group6F->TextLabel1->setText( tr( "FACE_1" ) );
71   Group6F->TextLabel2->setText( tr( "FACE_2" ) );
72   Group6F->TextLabel3->setText( tr( "FACE_3" ) );
73   Group6F->TextLabel4->setText( tr( "FACE_4" ) );
74   Group6F->TextLabel5->setText( tr( "FACE_5" ) );
75   Group6F->TextLabel6->setText( tr( "FACE_6" ) );
76   Group6F->PushButton1->setIcon( imageS );
77   Group6F->PushButton2->setIcon( imageS );
78   Group6F->PushButton3->setIcon( imageS );
79   Group6F->PushButton4->setIcon( imageS );
80   Group6F->PushButton5->setIcon( imageS );
81   Group6F->PushButton6->setIcon( imageS );
82
83   // Add groups to layout
84   QVBoxLayout* layout = new QVBoxLayout( centralWidget() );
85   layout->setMargin( 0 ); layout->setSpacing( 6 );
86   layout->addWidget( Group2F );
87   layout->addWidget( Group6F );
88   /***************************************************************/
89
90   setHelpFileName( "newentity_blocks.htm#HexahedralSolid" );
91
92   Init();
93 }
94
95 //=================================================================================
96 // function : ~BlocksGUI_BlockDlg()
97 // purpose  : Destroys the object and frees any allocated resources
98 //=================================================================================
99 BlocksGUI_BlockDlg::~BlocksGUI_BlockDlg()
100 {
101   // no need to delete child widgets, Qt does it all for us
102 }
103
104 //=================================================================================
105 // function : Init()
106 // purpose  :
107 //=================================================================================
108 void BlocksGUI_BlockDlg::Init()
109 {
110   // init variables
111   Group2F->LineEdit1->setReadOnly( true );
112   Group2F->LineEdit2->setReadOnly( true );
113
114   Group6F->LineEdit1->setReadOnly( true );
115   Group6F->LineEdit2->setReadOnly( true );
116   Group6F->LineEdit3->setReadOnly( true );
117   Group6F->LineEdit4->setReadOnly( true );
118   Group6F->LineEdit5->setReadOnly( true );
119   Group6F->LineEdit6->setReadOnly( true );
120
121   // signals and slots connections
122   connect( buttonOk(),    SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
123   connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
124
125   connect( this, SIGNAL( constructorsClicked( int ) ), 
126            this, SLOT( ConstructorsClicked( int ) ) );
127
128   connect( Group2F->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
129   connect( Group2F->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
130
131   connect( Group6F->PushButton1, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
132   connect( Group6F->PushButton2, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
133   connect( Group6F->PushButton3, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
134   connect( Group6F->PushButton4, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
135   connect( Group6F->PushButton5, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
136   connect( Group6F->PushButton6, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
137
138   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(),
139            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
140
141   // init controls and fields
142   initName( tr( "GEOM_BLOCK" ) );
143
144   myConstructorId = -1;
145   ConstructorsClicked( 0 );
146 }
147
148 //=================================================================================
149 // function : ConstructorsClicked()
150 // purpose  : Radio button management
151 //=================================================================================
152 void BlocksGUI_BlockDlg::ConstructorsClicked( int constructorId )
153 {
154   if ( myConstructorId == constructorId )
155     return;
156
157   myConstructorId = constructorId;
158
159   switch ( constructorId ) {
160   case 0:
161     Group6F->hide();
162     Group2F->show();
163
164     myEditCurrentArgument = Group2F->LineEdit1;
165     Group2F->LineEdit1->setText( tr( "" ) );
166     Group2F->LineEdit2->setText( tr( "" ) );
167     break;
168   case 1:
169     Group2F->hide();
170     Group6F->show();
171
172     myEditCurrentArgument = Group6F->LineEdit1;
173     Group6F->LineEdit1->setText( tr( "" ) );
174     Group6F->LineEdit2->setText( tr( "" ) );
175     Group6F->LineEdit3->setText( tr( "" ) );
176     Group6F->LineEdit4->setText( tr( "" ) );
177     Group6F->LineEdit5->setText( tr( "" ) );
178     Group6F->LineEdit6->setText( tr( "" ) );
179     break;
180   default:
181     break;
182   }
183
184   // init fields
185   myFace1 = myFace2 = GEOM::GEOM_Object::_nil();
186   myFace3 = myFace4 = myFace5 = myFace6 = myFace1;
187
188   qApp->processEvents();
189   updateGeometry();
190   resize( minimumSize() );
191
192   globalSelection( GEOM_FACE );
193   SelectionIntoArgument();
194   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
195           SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
196 }
197
198 //=================================================================================
199 // function : ClickOnOk()
200 // purpose  :
201 //=================================================================================
202 void BlocksGUI_BlockDlg::ClickOnOk()
203 {
204   if ( ClickOnApply() )
205     ClickOnCancel();
206 }
207
208 //=================================================================================
209 // function : ClickOnApply()
210 // purpose  :
211 //=================================================================================
212 bool BlocksGUI_BlockDlg::ClickOnApply()
213 {
214   if ( !onAccept() )
215     return false;
216
217   initName();
218   return true;
219 }
220
221 //=================================================================================
222 // function : SelectionIntoArgument()
223 // purpose  : Called when selection as changed or other case
224 //=================================================================================
225 void BlocksGUI_BlockDlg::SelectionIntoArgument()
226 {
227   erasePreview();
228   myEditCurrentArgument->setText( "" );
229
230   if ( IObjectCount() != 1 ) {
231     if ( myEditCurrentArgument == Group2F->LineEdit1 ||
232          myEditCurrentArgument == Group6F->LineEdit1 )
233       myFace1 = GEOM::GEOM_Object::_nil();
234     else if ( myEditCurrentArgument == Group2F->LineEdit2 ||
235               myEditCurrentArgument == Group6F->LineEdit2 )
236       myFace2 = GEOM::GEOM_Object::_nil();
237     else if ( myEditCurrentArgument == Group6F->LineEdit3 )
238       myFace3 = GEOM::GEOM_Object::_nil();
239     else if ( myEditCurrentArgument == Group6F->LineEdit4 )
240       myFace4 = GEOM::GEOM_Object::_nil();
241     else if ( myEditCurrentArgument == Group6F->LineEdit5 )
242       myFace5 = GEOM::GEOM_Object::_nil();
243     else if ( myEditCurrentArgument == Group6F->LineEdit6 )
244       myFace6 = GEOM::GEOM_Object::_nil();
245     return;
246   }
247
248   // nbSel == 1
249   Standard_Boolean testResult = Standard_False;
250   GEOM::GEOM_Object_var aSelectedObject =
251     GEOMBase::ConvertIOinGEOMObject( firstIObject(), testResult );
252
253   if ( !testResult || CORBA::is_nil( aSelectedObject ) )
254     return;
255
256   if ( myEditCurrentArgument == Group2F->LineEdit1 ||
257        myEditCurrentArgument == Group6F->LineEdit1 )
258     myFace1 = aSelectedObject;
259   else if ( myEditCurrentArgument == Group2F->LineEdit2 ||
260             myEditCurrentArgument == Group6F->LineEdit2 )
261     myFace2 = aSelectedObject;
262   else if ( myEditCurrentArgument == Group6F->LineEdit3 )
263     myFace3 = aSelectedObject;
264   else if ( myEditCurrentArgument == Group6F->LineEdit4 )
265     myFace4 = aSelectedObject;
266   else if ( myEditCurrentArgument == Group6F->LineEdit5 )
267     myFace5 = aSelectedObject;
268   else if ( myEditCurrentArgument == Group6F->LineEdit6 )
269     myFace6 = aSelectedObject;
270
271   myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) );
272
273   displayPreview();
274 }
275
276 //=================================================================================
277 // function : SetEditCurrentArgument()
278 // purpose  :
279 //=================================================================================
280 void BlocksGUI_BlockDlg::SetEditCurrentArgument()
281 {
282   QPushButton* aSender = (QPushButton*)sender();
283
284   if ( aSender == Group2F->PushButton1 ) {
285     myEditCurrentArgument = Group2F->LineEdit1;
286   } 
287   else if ( aSender == Group2F->PushButton2 ) {
288     myEditCurrentArgument = Group2F->LineEdit2;
289   }
290   else if ( aSender == Group6F->PushButton1 ) {
291     myEditCurrentArgument = Group6F->LineEdit1;
292   }
293   else if ( aSender == Group6F->PushButton2 ) {
294     myEditCurrentArgument = Group6F->LineEdit2;
295   } 
296   else if ( aSender == Group6F->PushButton3 ) {
297     myEditCurrentArgument = Group6F->LineEdit3;
298   } 
299   else if ( aSender == Group6F->PushButton4 ) {
300     myEditCurrentArgument = Group6F->LineEdit4;
301   } 
302   else if ( aSender == Group6F->PushButton5 ) {
303     myEditCurrentArgument = Group6F->LineEdit5;
304   } 
305   else if ( aSender == Group6F->PushButton6 ) {
306     myEditCurrentArgument = Group6F->LineEdit6;
307   }
308
309   globalSelection( GEOM_FACE );
310
311   myEditCurrentArgument->setFocus();
312   SelectionIntoArgument();
313 }
314
315 //=================================================================================
316 // function : ActivateThisDialog()
317 // purpose  :
318 //=================================================================================
319 void BlocksGUI_BlockDlg::ActivateThisDialog()
320 {
321   GEOMBase_Skeleton::ActivateThisDialog();
322   connect( ( (SalomeApp_Application*)( SUIT_Session::session()->activeApplication() ) )->selectionMgr(), 
323            SIGNAL( currentSelectionChanged() ), this, SLOT( SelectionIntoArgument() ) );
324
325   globalSelection( GEOM_FACE ); 
326
327   SelectionIntoArgument();
328 }
329
330 //=================================================================================
331 // function : enterEvent()
332 // purpose  :
333 //=================================================================================
334 void BlocksGUI_BlockDlg::enterEvent( QEvent* )
335 {
336   if ( !mainFrame()->GroupConstructors->isEnabled() )
337     ActivateThisDialog();
338 }
339
340 //=================================================================================
341 // function : createOperation
342 // purpose  :
343 //=================================================================================
344 GEOM::GEOM_IOperations_ptr BlocksGUI_BlockDlg::createOperation()
345 {
346   return getGeomEngine()->GetIBlocksOperations( getStudyId() );
347 }
348
349 //=================================================================================
350 // function : isValid
351 // purpose  :
352 //=================================================================================
353 bool BlocksGUI_BlockDlg::isValid (QString&)
354 {
355   bool ok = false;
356   switch ( getConstructorId() ) {
357   case 0:
358     ok = !( myFace1->_is_nil() || myFace2->_is_nil() );
359     break;
360   case 1:
361     ok =  !( myFace1->_is_nil() || myFace2->_is_nil() ||
362              myFace3->_is_nil() || myFace4->_is_nil() ||
363              myFace5->_is_nil() || myFace6->_is_nil() );
364     break;
365   default:
366     break;
367   }
368   return ok;
369 }
370
371 //=================================================================================
372 // function : execute
373 // purpose  :
374 //=================================================================================
375 bool BlocksGUI_BlockDlg::execute( ObjectList& objects )
376 {
377   bool res = false;
378
379   GEOM::GEOM_Object_var anObj;
380
381   switch ( getConstructorId() ) {
382   case 0:
383     if ( !CORBA::is_nil( myFace1 ) && !CORBA::is_nil( myFace2 ) ) {
384       anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->
385         MakeHexa2Faces( myFace1, myFace2 );
386       res = true;
387     }
388     break;
389   case 1:
390     if ( !CORBA::is_nil( myFace1 ) && !CORBA::is_nil( myFace2 ) &&
391          !CORBA::is_nil(myFace3 )  && !CORBA::is_nil( myFace4 ) &&
392          !CORBA::is_nil( myFace5 ) && !CORBA::is_nil( myFace6 ) ) {
393         anObj = GEOM::GEOM_IBlocksOperations::_narrow( getOperation() )->
394           MakeHexa( myFace1, myFace2, myFace3, myFace4, myFace5, myFace6 );
395         res = true;
396     }
397     break;
398   }
399
400   if ( !anObj->_is_nil() )
401     objects.push_back( anObj._retn() );
402
403   return res;
404 }