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