Salome HOME
0820f30d6c82e310dd81a4f88388f9db369856e6
[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 "SUIT_Session.h"
33 #include "SalomeApp_Application.h"
34 #include "SalomeApp_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 (QWidget* parent,
46                                       bool modal)
47      : GEOMBase_Skeleton(parent, "TrsfDlg", modal,
48                          WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
49 {
50   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_BLOCK_MULTITRSF_SIMPLE")));
51   QPixmap image2 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM",tr("ICON_DLG_BLOCK_MULTITRSF_DOUBLE")));
52   QPixmap imageS (SUIT_Session::session()->resourceMgr()->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   Init();
123 }
124
125 //=================================================================================
126 // function : ~BlocksGUI_TrsfDlg()
127 // purpose  : Destroys the object and frees any allocated resources
128 //=================================================================================
129 BlocksGUI_TrsfDlg::~BlocksGUI_TrsfDlg()
130 {
131   // no need to delete child widgets, Qt does it all for us
132 }
133
134 //=================================================================================
135 // function : Init()
136 // purpose  :
137 //=================================================================================
138 void BlocksGUI_TrsfDlg::Init()
139 {
140   // Set range of spinboxes
141   double SpecificStep = 1.0;
142   QMap<int, DlgRef_SpinBox*>::iterator anIter;
143   for (anIter = mySpinBox.begin(); anIter != mySpinBox.end(); ++anIter) {
144     anIter.data()->RangeStepAndValidator(1.0, 999.999, SpecificStep, 3);
145   }
146
147   // signals and slots connections
148   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
149   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
150   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
151
152   QMap<int, QPushButton*>::iterator anIterBtn;
153   for (anIterBtn = mySelBtn.begin(); anIterBtn != mySelBtn.end(); ++anIterBtn)
154     connect(anIterBtn.data(), SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
155
156   QMap<int, DlgRef_SpinBox*>::iterator anIterSpin;
157   for (anIterSpin = mySpinBox.begin(); anIterSpin != mySpinBox.end(); ++anIterSpin)
158     connect(anIterSpin.data(), SIGNAL(valueChanged(double)), this, SLOT(ValueChangedInSpinBox(double)));
159
160   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
161           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
162
163   // init controls and fields
164   initName(tr("GEOM_BLOCK_MULTITRSF"));
165
166   myConstructorId = -1;
167   ConstructorsClicked(0);
168 }
169
170 //=================================================================================
171 // function : ConstructorsClicked()
172 // purpose  : Radio button management
173 //=================================================================================
174 void BlocksGUI_TrsfDlg::ConstructorsClicked (int constructorId)
175 {
176   if (myConstructorId == constructorId)
177     return;
178
179   myConstructorId = constructorId;
180
181   switch (constructorId) {
182   case 0:
183     myGrp2->hide();
184     myGrp1->show();
185     mySpinBox[SpinBox1]->SetValue(2.0);
186     myEditCurrentArgument = mySelName[MainObj1];
187     myFaces[Face1] = -1;
188     myFaces[Face2] = -1;
189     break;
190   case 1:
191     myGrp1->hide();
192     myGrp2->show();
193     mySpinBox[SpinBox2U]->SetValue(2.0);
194     mySpinBox[SpinBox2V]->SetValue(2.0);
195     myEditCurrentArgument = mySelName[MainObj2];
196     myFaces[Face1U] = -1;
197     myFaces[Face2U] = -1;
198     myFaces[Face1V] = -1;
199     myFaces[Face2V] = -1;
200     break;
201   default:
202     break;
203   }
204
205   // clear line edits
206   QMap<int, QLineEdit*>::iterator anIterLE;
207   for (anIterLE = mySelName.begin(); anIterLE != mySelName.end(); ++anIterLE)
208     anIterLE.data()->setText("");
209
210   // init fields
211   myShape = GEOM::GEOM_Object::_nil();
212
213   activateSelection();
214 //  enableWidgets();
215 //  displayPreview();
216 }
217
218 //=================================================================================
219 // function : ClickOnOk()
220 // purpose  :
221 //=================================================================================
222 void BlocksGUI_TrsfDlg::ClickOnOk()
223 {
224   if (ClickOnApply())
225     ClickOnCancel();
226 }
227
228 //=================================================================================
229 // function : ClickOnApply()
230 // purpose  :
231 //=================================================================================
232 bool BlocksGUI_TrsfDlg::ClickOnApply()
233 {
234   if (!onAccept())
235     return false;
236
237   initName();
238   return true;
239 }
240
241 //=================================================================================
242 // function : SelectionIntoArgument()
243 // purpose  : Called when selection has changed
244 //=================================================================================
245 void BlocksGUI_TrsfDlg::SelectionIntoArgument()
246 {
247   erasePreview();
248   myEditCurrentArgument->setText("");
249
250   // Get index of current selection focus
251   int aCurrFocus = -1;
252   QMap<int, QLineEdit*>::iterator anIter;
253   for (anIter = mySelName.begin(); anIter != mySelName.end(); ++anIter) {
254     if (myEditCurrentArgument == anIter.data()) {
255       aCurrFocus = anIter.key();
256       break;
257     }
258   }
259
260   // If selection of main object is activated
261   if (aCurrFocus == MainObj1 || aCurrFocus == MainObj2) {
262     if (IObjectCount() == 1) {
263       Standard_Boolean aResult = Standard_False;
264       GEOM::GEOM_Object_var anObj =
265         GEOMBase::ConvertIOinGEOMObject(firstIObject(), aResult);
266
267       if (aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
268         myShape = anObj;
269         mySelName[aCurrFocus]->setText(GEOMBase::GetName(anObj));
270         enableWidgets();
271         return;
272       }
273     }
274
275     myShape = GEOM::GEOM_Object::_nil();
276     enableWidgets();
277   }
278   // If face selection is activated
279   else if (aCurrFocus == Face1  || aCurrFocus == Face2  ||
280            aCurrFocus == Face1U || aCurrFocus == Face2U ||
281            aCurrFocus == Face1V || aCurrFocus == Face2V) {
282     if (IObjectCount() == 1) {
283       Standard_Boolean aResult = Standard_False;
284       GEOM::GEOM_Object_var anObj =
285         GEOMBase::ConvertIOinGEOMObject(firstIObject(), aResult);
286
287       if ( aResult && !anObj->_is_nil() && GEOMBase::IsShape( anObj ) ) {
288         TColStd_IndexedMapOfInteger anIndexes;
289         ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->GetIndexes( firstIObject(), anIndexes );
290
291         if (anIndexes.Extent() == 1) {
292           int anIndex = anIndexes(1);
293           QString aFaceName = QString(GEOMBase::GetName(anObj)) + ":%1";
294           myEditCurrentArgument->setText(aFaceName.arg(anIndex));
295           myFaces[aCurrFocus] = anIndex;
296           displayPreview();
297           return;
298         }
299       }
300     }
301
302     myFaces[aCurrFocus] = -1;
303   }
304 }
305
306 //=================================================================================
307 // function : SetEditCurrentArgument()
308 // purpose  :
309 //=================================================================================
310 void BlocksGUI_TrsfDlg::SetEditCurrentArgument()
311 {
312   QPushButton* aSender = (QPushButton*)sender();
313
314   QMap<int, QPushButton*>::iterator anIter;
315   for (anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter) {
316     if (anIter.data() == aSender) {
317       mySelName[anIter.key()]->setFocus();
318       myEditCurrentArgument = mySelName[anIter.key()];
319       break;
320     }
321   }
322
323   activateSelection();
324 }
325
326 //=================================================================================
327 // function : ActivateThisDialog()
328 // purpose  :
329 //=================================================================================
330 void BlocksGUI_TrsfDlg::ActivateThisDialog()
331 {
332   GEOMBase_Skeleton::ActivateThisDialog();
333   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
334           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
335
336   activateSelection();
337   displayPreview();
338 }
339
340 //=================================================================================
341 // function : enterEvent()
342 // purpose  :
343 //=================================================================================
344 void BlocksGUI_TrsfDlg::enterEvent (QEvent* e)
345 {
346   if (!GroupConstructors->isEnabled())
347     this->ActivateThisDialog();
348 }
349
350 //=================================================================================
351 // function : DeactivateActiveDialog()
352 // purpose  :
353 //=================================================================================
354 //void BlocksGUI_TrsfDlg::DeactivateActiveDialog()
355 //{
356 //  // disconnect selection
357 //  GEOMBase_Skeleton::DeactivateActiveDialog();
358 //}
359
360 //=================================================================================
361 // function : ValueChangedInSpinBox()
362 // purpose  :
363 //=================================================================================
364 void BlocksGUI_TrsfDlg::ValueChangedInSpinBox (double newValue)
365 {
366   displayPreview();
367 }
368
369 //=================================================================================
370 // function : createSelWg()
371 // purpose  :
372 //=================================================================================
373 void BlocksGUI_TrsfDlg::createSelWg (const QString& theLbl,
374                                      QPixmap&       thePix,
375                                      QWidget*       theParent,
376                                      const int      theId)
377 {
378   new QLabel(theLbl, theParent);
379   mySelBtn[theId] = new QPushButton(theParent);
380   mySelBtn[theId]->setPixmap(thePix);
381   mySelName[theId] = new QLineEdit(theParent);
382   mySelName[theId]->setReadOnly(true);
383 }
384
385 //=================================================================================
386 // function : activateSelection
387 // purpose  : Activate selection in accordance with myEditCurrentArgument
388 //=================================================================================
389 void BlocksGUI_TrsfDlg::activateSelection()
390 {
391   if (!myShape->_is_nil() &&
392       (myEditCurrentArgument == mySelName[ Face1  ] ||
393        myEditCurrentArgument == mySelName[ Face2  ] ||
394        myEditCurrentArgument == mySelName[ Face1U ] ||
395        myEditCurrentArgument == mySelName[ Face2U ] ||
396        myEditCurrentArgument == mySelName[ Face1V ] ||
397        myEditCurrentArgument == mySelName[ Face2V ])) {
398
399     // Local selection is available only in the OCC Viewer
400     if (SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType() 
401         != OCCViewer_Viewer::Type()) {
402       localSelection(myShape, TopAbs_FACE);
403     } else {
404       return;
405     }
406   } else {
407     globalSelection( GEOM_ALLSHAPES );
408   }
409
410   SelectionIntoArgument();
411 }
412
413 //=================================================================================
414 // function : enableWidgets
415 // purpose  : Enable widgets of faces in accordance with value of main object
416 //=================================================================================
417 void BlocksGUI_TrsfDlg::enableWidgets()
418 {
419   int anId = getConstructorId();
420
421   bool toEnable = !myShape->_is_nil();
422
423   if (anId == 0) {
424     mySelName[Face1]->setEnabled(toEnable);
425     mySelName[Face2]->setEnabled(toEnable);
426     mySelBtn[Face1]->setEnabled(toEnable);
427     mySelBtn[Face2]->setEnabled(toEnable);
428
429     if (!toEnable) {
430       mySelName[Face1]->setText("");
431       mySelName[Face2]->setText("");
432       myFaces[Face1] = -1;
433       myFaces[Face2] = -1;
434     }
435   } else if (anId == 1) {
436     mySelName[Face1U]->setEnabled(toEnable);
437     mySelName[Face2U]->setEnabled(toEnable);
438     mySelName[Face1V]->setEnabled(toEnable);
439     mySelName[Face2V]->setEnabled(toEnable);
440     mySelBtn[Face1U]->setEnabled(toEnable);
441     mySelBtn[Face2U]->setEnabled(toEnable);
442     mySelBtn[Face1V]->setEnabled(toEnable);
443     mySelBtn[Face2V]->setEnabled(toEnable);
444
445     if (!toEnable) {
446       mySelName[Face1U]->setText("");
447       mySelName[Face2U]->setText("");
448       mySelName[Face1V]->setText("");
449       mySelName[Face2V]->setText("");
450       myFaces[Face1U] = -1;
451       myFaces[Face2U] = -1;
452       myFaces[Face1V] = -1;
453       myFaces[Face2V] = -1;
454     }
455   }
456 }
457
458 //=================================================================================
459 // function : createOperation
460 // purpose  :
461 //=================================================================================
462 GEOM::GEOM_IOperations_ptr BlocksGUI_TrsfDlg::createOperation()
463 {
464   return getGeomEngine()->GetIBlocksOperations(getStudyId());
465 }
466
467 //=================================================================================
468 // function : ClickOnApply()
469 // purpose  : Verify validity of input data
470 //=================================================================================
471 bool BlocksGUI_TrsfDlg::isValid (QString&)
472 {
473   switch (getConstructorId()) {
474     case 0:
475       return !myShape->_is_nil() && myFaces[Face1] > 0;
476     case 1:
477       return !myShape->_is_nil() && myFaces[Face1U] > 0 && myFaces[Face1V] > 0;
478     default:
479       return false;
480   }
481   return false;
482 }
483
484 //=================================================================================
485 // function : execute
486 // purpose  :
487 //=================================================================================
488 bool BlocksGUI_TrsfDlg::execute (ObjectList& objects)
489 {
490   bool res = false;
491
492   GEOM::GEOM_Object_var anObj;
493
494   switch (getConstructorId()) {
495     case 0:
496       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeMultiTransformation1D
497         (myShape,
498          myFaces[Face1],
499          myFaces[Face2],
500          (int)mySpinBox[SpinBox1]->GetValue());
501       res = true;
502       break;
503     case 1:
504       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeMultiTransformation2D
505         (myShape,
506          myFaces[Face1U],
507          myFaces[Face2U],
508          (int)mySpinBox[SpinBox2U]->GetValue(),
509          myFaces[Face1V],
510          myFaces[Face2V],
511          (int)mySpinBox[SpinBox2V]->GetValue());
512       res = true;
513       break;
514   }
515
516   if (!anObj->_is_nil())
517     objects.push_back(anObj._retn());
518
519   return res;
520 }