Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/geom.git] / src / BlocksGUI / BlocksGUI_TrsfDlg.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 //
22 //
23 //  File   : BlocksGUI_TrsfDlg.cxx
24 //  Author : Julia DOROVSKIKH
25 //  Module : GEOM
26 //  $Header$
27
28 #include "BlocksGUI_TrsfDlg.h"
29
30 #include "DlgRef_SpinBox.h"
31
32 #include "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "LightApp_SelectionMgr.h"
35 #include "OCCViewer_ViewModel.h"
36
37 #include <TColStd_IndexedMapOfInteger.hxx>
38
39 #include <qlabel.h>
40
41 //=================================================================================
42 // class    : BlocksGUI_TrsfDlg()
43 // purpose  : Constructs a BlocksGUI_TrsfDlg which is a child of 'parent'.
44 //=================================================================================
45 BlocksGUI_TrsfDlg::BlocksGUI_TrsfDlg (GeometryGUI* theGeometryGUI, QWidget* parent, bool modal)
46      : GEOMBase_Skeleton(theGeometryGUI, parent, "TrsfDlg", modal,
47                          WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
48 {
49   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
50   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_BLOCK_MULTITRSF_SIMPLE")));
51   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_BLOCK_MULTITRSF_DOUBLE")));
52   QPixmap imageS (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
53
54   setCaption(tr("GEOM_BLOCK_MULTITRSF_TITLE"));
55
56   /***************************************************************/
57   GroupConstructors->setTitle(tr("GEOM_BLOCK_MULTITRSF"));
58
59   RadioButton1->setPixmap(image1);
60   RadioButton2->setPixmap(image2);
61   RadioButton3->close(TRUE);
62
63   // Create first group
64   myGrp1 = new QGroupBox(1, Qt::Horizontal, tr("GEOM_BLOCK_MULTITRSF_SIMPLE"), this);
65
66   QGroupBox* aSelGrp1 = new QGroupBox(3, Qt::Horizontal, myGrp1);
67   aSelGrp1->setFrameStyle(QFrame::NoFrame);
68   aSelGrp1->setInsideMargin(0);
69
70   createSelWg(tr("GEOM_MAIN_OBJECT"), imageS, aSelGrp1, MainObj1);
71   createSelWg(tr("FACE_1"), imageS, aSelGrp1, Face1);
72   createSelWg(tr("FACE_2"), imageS, aSelGrp1, Face2);
73
74   QGroupBox* aSpinGrp1 = new QGroupBox(1, Qt::Vertical, myGrp1);
75   aSpinGrp1->setFrameStyle(QFrame::NoFrame);
76   aSpinGrp1->setInsideMargin(0);
77
78   new QLabel(tr("GEOM_NB_TIMES"), aSpinGrp1);
79   mySpinBox[SpinBox1] = new DlgRef_SpinBox(aSpinGrp1);
80
81   // Create second group
82   myGrp2 = new QGroupBox(1, Qt::Horizontal, tr("GEOM_BLOCK_MULTITRSF_DOUBLE"), this);
83
84   // U trsf
85   QGroupBox* aSelGrp2U = new QGroupBox(3, Qt::Horizontal, myGrp2);
86   aSelGrp2U->setFrameStyle(QFrame::NoFrame);
87   aSelGrp2U->setInsideMargin(0);
88
89   createSelWg(tr("GEOM_MAIN_OBJECT"), imageS, aSelGrp2U, MainObj2);
90   createSelWg(tr("FACE_1U"), imageS, aSelGrp2U, Face1U);
91   createSelWg(tr("FACE_2U"), imageS, aSelGrp2U, Face2U);
92
93   QGroupBox* aSpinGrp2U = new QGroupBox(1, Qt::Vertical, myGrp2);
94   aSpinGrp2U->setFrameStyle(QFrame::NoFrame);
95   aSpinGrp2U->setInsideMargin(0);
96
97   new QLabel(tr("GEOM_NB_TIMES_U"), aSpinGrp2U);
98   mySpinBox[SpinBox2U] = new DlgRef_SpinBox(aSpinGrp2U);
99
100   // V trsf
101   QGroupBox* aSelGrp2V = new QGroupBox(3, Qt::Horizontal, myGrp2);
102   aSelGrp2V->setFrameStyle(QFrame::NoFrame);
103   aSelGrp2V->setInsideMargin(0);
104
105   createSelWg(tr("FACE_1V"), imageS, aSelGrp2V, Face1V);
106   createSelWg(tr("FACE_2V"), imageS, aSelGrp2V, Face2V);
107
108   QGroupBox* aSpinGrp2V = new QGroupBox(1, Qt::Vertical, myGrp2);
109   aSpinGrp2V->setFrameStyle(QFrame::NoFrame);
110   aSpinGrp2V->setInsideMargin(0);
111
112   new QLabel(tr("GEOM_NB_TIMES_V"), aSpinGrp2V);
113   mySpinBox[SpinBox2V] = new DlgRef_SpinBox(aSpinGrp2V);
114
115   (new QLabel(myGrp2))->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
116
117   // Add groups to layout
118   Layout1->addWidget( myGrp1, 2, 0 );
119   Layout1->addWidget( myGrp2, 2, 0 );
120   /***************************************************************/
121
122   setHelpFileName("multi_transformation.htm");
123
124   Init();
125 }
126
127 //=================================================================================
128 // function : ~BlocksGUI_TrsfDlg()
129 // purpose  : Destroys the object and frees any allocated resources
130 //=================================================================================
131 BlocksGUI_TrsfDlg::~BlocksGUI_TrsfDlg()
132 {
133   // no need to delete child widgets, Qt does it all for us
134 }
135
136 //=================================================================================
137 // function : Init()
138 // purpose  :
139 //=================================================================================
140 void BlocksGUI_TrsfDlg::Init()
141 {
142   // Set range of spinboxes
143   double SpecificStep = 1.0;
144   QMap<int, DlgRef_SpinBox*>::iterator anIter;
145   for (anIter = mySpinBox.begin(); anIter != mySpinBox.end(); ++anIter) {
146     anIter.data()->RangeStepAndValidator(1.0, 999.999, SpecificStep, 3);
147   }
148
149   // signals and slots connections
150   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
151   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
152   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
153
154   QMap<int, QPushButton*>::iterator anIterBtn;
155   for (anIterBtn = mySelBtn.begin(); anIterBtn != mySelBtn.end(); ++anIterBtn)
156     connect(anIterBtn.data(), SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
157
158   QMap<int, DlgRef_SpinBox*>::iterator anIterSpin;
159   for (anIterSpin = mySpinBox.begin(); anIterSpin != mySpinBox.end(); ++anIterSpin)
160     connect(anIterSpin.data(), SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
161
162   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
163           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
164
165   // init controls and fields
166   initName(tr("GEOM_BLOCK_MULTITRSF"));
167
168   myConstructorId = -1;
169   ConstructorsClicked(0);
170 }
171
172 //=================================================================================
173 // function : ConstructorsClicked()
174 // purpose  : Radio button management
175 //=================================================================================
176 void BlocksGUI_TrsfDlg::ConstructorsClicked (int constructorId)
177 {
178   if (myConstructorId == constructorId)
179     return;
180
181   myConstructorId = constructorId;
182
183   switch (constructorId) {
184   case 0:
185     myGrp2->hide();
186     myGrp1->show();
187     mySpinBox[SpinBox1]->SetValue(2.0);
188     myEditCurrentArgument = mySelName[MainObj1];
189     myFaces[Face1] = -1;
190     myFaces[Face2] = -1;
191     break;
192   case 1:
193     myGrp1->hide();
194     myGrp2->show();
195     mySpinBox[SpinBox2U]->SetValue(2.0);
196     mySpinBox[SpinBox2V]->SetValue(2.0);
197     myEditCurrentArgument = mySelName[MainObj2];
198     myFaces[Face1U] = -1;
199     myFaces[Face2U] = -1;
200     myFaces[Face1V] = -1;
201     myFaces[Face2V] = -1;
202     break;
203   default:
204     break;
205   }
206
207   // clear line edits
208   QMap<int, QLineEdit*>::iterator anIterLE;
209   for (anIterLE = mySelName.begin(); anIterLE != mySelName.end(); ++anIterLE)
210     anIterLE.data()->setText("");
211
212   // init fields
213   myShape = GEOM::GEOM_Object::_nil();
214
215   activateSelection();
216 //  enableWidgets();
217 //  displayPreview();
218 }
219
220 //=================================================================================
221 // function : ClickOnOk()
222 // purpose  :
223 //=================================================================================
224 void BlocksGUI_TrsfDlg::ClickOnOk()
225 {
226   if (ClickOnApply())
227     ClickOnCancel();
228 }
229
230 //=================================================================================
231 // function : ClickOnApply()
232 // purpose  :
233 //=================================================================================
234 bool BlocksGUI_TrsfDlg::ClickOnApply()
235 {
236   if (!onAccept())
237     return false;
238
239   initName();
240   return true;
241 }
242
243 //=================================================================================
244 // function : SelectionIntoArgument()
245 // purpose  : Called when selection has changed
246 //=================================================================================
247 void BlocksGUI_TrsfDlg::SelectionIntoArgument()
248 {
249   erasePreview();
250   myEditCurrentArgument->setText("");
251
252   // Get index of current selection focus
253   int aCurrFocus = -1;
254   QMap<int, QLineEdit*>::iterator anIter;
255   for (anIter = mySelName.begin(); anIter != mySelName.end(); ++anIter) {
256     if (myEditCurrentArgument == anIter.data()) {
257       aCurrFocus = anIter.key();
258       break;
259     }
260   }
261
262   // If selection of main object is activated
263   if (aCurrFocus == MainObj1 || aCurrFocus == MainObj2) {
264     if (IObjectCount() == 1) {
265       Standard_Boolean aResult = Standard_False;
266       GEOM::GEOM_Object_var anObj =
267         GEOMBase::ConvertIOinGEOMObject(firstIObject(), aResult);
268
269       if (aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
270         myShape = anObj;
271         mySelName[aCurrFocus]->setText(GEOMBase::GetName(anObj));
272         enableWidgets();
273         return;
274       }
275     }
276
277     myShape = GEOM::GEOM_Object::_nil();
278     enableWidgets();
279   }
280   // If face selection is activated
281   else if (aCurrFocus == Face1  || aCurrFocus == Face2  ||
282            aCurrFocus == Face1U || aCurrFocus == Face2U ||
283            aCurrFocus == Face1V || aCurrFocus == Face2V) {
284     if (IObjectCount() == 1) {
285       Standard_Boolean aResult = Standard_False;
286       GEOM::GEOM_Object_var anObj =
287         GEOMBase::ConvertIOinGEOMObject(firstIObject(), aResult);
288
289       if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
290         TColStd_IndexedMapOfInteger anIndexes;
291         ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( firstIObject(), anIndexes );
292
293         if (anIndexes.Extent() == 1) {
294           int anIndex = anIndexes(1);
295           QString aFaceName = QString(GEOMBase::GetName(anObj)) + ":%1";
296           myEditCurrentArgument->setText(aFaceName.arg(anIndex));
297           myFaces[aCurrFocus] = anIndex;
298           displayPreview();
299           return;
300         }
301       }
302     }
303
304     myFaces[aCurrFocus] = -1;
305   }
306 }
307
308 //=================================================================================
309 // function : SetEditCurrentArgument()
310 // purpose  :
311 //=================================================================================
312 void BlocksGUI_TrsfDlg::SetEditCurrentArgument()
313 {
314   QPushButton* aSender = (QPushButton*)sender();
315
316   QMap<int, QPushButton*>::iterator anIter;
317   for (anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter) {
318     if (anIter.data() == aSender) {
319       mySelName[anIter.key()]->setFocus();
320       myEditCurrentArgument = mySelName[anIter.key()];
321       break;
322     }
323   }
324
325   activateSelection();
326 }
327
328 //=================================================================================
329 // function : ActivateThisDialog()
330 // purpose  :
331 //=================================================================================
332 void BlocksGUI_TrsfDlg::ActivateThisDialog()
333 {
334   GEOMBase_Skeleton::ActivateThisDialog();
335   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
336           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
337
338   activateSelection();
339   displayPreview();
340 }
341
342 //=================================================================================
343 // function : enterEvent()
344 // purpose  :
345 //=================================================================================
346 void BlocksGUI_TrsfDlg::enterEvent (QEvent* e)
347 {
348   if (!GroupConstructors->isEnabled())
349     this->ActivateThisDialog();
350 }
351
352 //=================================================================================
353 // function : ValueChangedInSpinBox()
354 // purpose  :
355 //=================================================================================
356 void BlocksGUI_TrsfDlg::ValueChangedInSpinBox (double newValue)
357 {
358   displayPreview();
359 }
360
361 //=================================================================================
362 // function : createSelWg()
363 // purpose  :
364 //=================================================================================
365 void BlocksGUI_TrsfDlg::createSelWg (const QString& theLbl,
366                                      QPixmap&       thePix,
367                                      QWidget*       theParent,
368                                      const int      theId)
369 {
370   new QLabel(theLbl, theParent);
371   mySelBtn[theId] = new QPushButton(theParent);
372   mySelBtn[theId]->setPixmap(thePix);
373   mySelName[theId] = new QLineEdit(theParent);
374   mySelName[theId]->setReadOnly(true);
375 }
376
377 //=================================================================================
378 // function : activateSelection
379 // purpose  : Activate selection in accordance with myEditCurrentArgument
380 //=================================================================================
381 void BlocksGUI_TrsfDlg::activateSelection()
382 {
383   if (!myShape->_is_nil() &&
384       (myEditCurrentArgument == mySelName[ Face1  ] ||
385        myEditCurrentArgument == mySelName[ Face2  ] ||
386        myEditCurrentArgument == mySelName[ Face1U ] ||
387        myEditCurrentArgument == mySelName[ Face2U ] ||
388        myEditCurrentArgument == mySelName[ Face1V ] ||
389        myEditCurrentArgument == mySelName[ Face2V ])) {
390
391     // Local selection is available only in the OCC Viewer
392     if (SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
393         == OCCViewer_Viewer::Type()) {
394       localSelection(myShape, TopAbs_FACE);
395     } else {
396       return;
397     }
398   } else {
399     globalSelection( GEOM_ALLSHAPES );
400   }
401
402   SelectionIntoArgument();
403 }
404
405 //=================================================================================
406 // function : enableWidgets
407 // purpose  : Enable widgets of faces in accordance with value of main object
408 //=================================================================================
409 void BlocksGUI_TrsfDlg::enableWidgets()
410 {
411   int anId = getConstructorId();
412
413   bool toEnable = !myShape->_is_nil();
414
415   if (anId == 0) {
416     mySelName[Face1]->setEnabled(toEnable);
417     mySelName[Face2]->setEnabled(toEnable);
418     mySelBtn[Face1]->setEnabled(toEnable);
419     mySelBtn[Face2]->setEnabled(toEnable);
420
421     if (!toEnable) {
422       mySelName[Face1]->setText("");
423       mySelName[Face2]->setText("");
424       myFaces[Face1] = -1;
425       myFaces[Face2] = -1;
426     }
427   } else if (anId == 1) {
428     mySelName[Face1U]->setEnabled(toEnable);
429     mySelName[Face2U]->setEnabled(toEnable);
430     mySelName[Face1V]->setEnabled(toEnable);
431     mySelName[Face2V]->setEnabled(toEnable);
432     mySelBtn[Face1U]->setEnabled(toEnable);
433     mySelBtn[Face2U]->setEnabled(toEnable);
434     mySelBtn[Face1V]->setEnabled(toEnable);
435     mySelBtn[Face2V]->setEnabled(toEnable);
436
437     if (!toEnable) {
438       mySelName[Face1U]->setText("");
439       mySelName[Face2U]->setText("");
440       mySelName[Face1V]->setText("");
441       mySelName[Face2V]->setText("");
442       myFaces[Face1U] = -1;
443       myFaces[Face2U] = -1;
444       myFaces[Face1V] = -1;
445       myFaces[Face2V] = -1;
446     }
447   }
448 }
449
450 //=================================================================================
451 // function : createOperation
452 // purpose  :
453 //=================================================================================
454 GEOM::GEOM_IOperations_ptr BlocksGUI_TrsfDlg::createOperation()
455 {
456   return getGeomEngine()->GetIBlocksOperations(getStudyId());
457 }
458
459 //=================================================================================
460 // function : ClickOnApply()
461 // purpose  : Verify validity of input data
462 //=================================================================================
463 bool BlocksGUI_TrsfDlg::isValid (QString&)
464 {
465   switch (getConstructorId()) {
466     case 0:
467       return !myShape->_is_nil() && myFaces[Face1] > 0;
468     case 1:
469       return !myShape->_is_nil() && myFaces[Face1U] > 0 && myFaces[Face1V] > 0;
470     default:
471       return false;
472   }
473   return false;
474 }
475
476 //=================================================================================
477 // function : execute
478 // purpose  :
479 //=================================================================================
480 bool BlocksGUI_TrsfDlg::execute (ObjectList& objects)
481 {
482   bool res = false;
483
484   GEOM::GEOM_Object_var anObj;
485
486   switch (getConstructorId()) {
487     case 0:
488       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeMultiTransformation1D
489         (myShape,
490          myFaces[Face1],
491          myFaces[Face2],
492          (int)mySpinBox[SpinBox1]->GetValue());
493       res = true;
494       break;
495     case 1:
496       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeMultiTransformation2D
497         (myShape,
498          myFaces[Face1U],
499          myFaces[Face2U],
500          (int)mySpinBox[SpinBox2U]->GetValue(),
501          myFaces[Face1V],
502          myFaces[Face2V],
503          (int)mySpinBox[SpinBox2V]->GetValue());
504       res = true;
505       break;
506   }
507
508   if (!anObj->_is_nil())
509     objects.push_back(anObj._retn());
510
511   return res;
512 }