]> SALOME platform Git repositories - modules/geom.git/blob - src/BlocksGUI/BlocksGUI_TrsfDlg.cxx
Salome HOME
Remove unnecessary function setCustomData() / getCustomData()
[modules/geom.git] / src / BlocksGUI / BlocksGUI_TrsfDlg.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : BlocksGUI_TrsfDlg.cxx
25 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S. (julia.dorovskikh@opencascade.com)
26 //
27 #include "BlocksGUI_TrsfDlg.h"
28
29 #include <DlgRef.h>
30 #include <GeometryGUI.h>
31 #include <GEOMBase.h>
32
33 #include <SUIT_Session.h>
34 #include <SUIT_Desktop.h>
35 #include <SUIT_ResourceMgr.h>
36 #include <SUIT_ViewWindow.h>
37 #include <SUIT_ViewManager.h>
38 #include <SalomeApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40 #include <OCCViewer_ViewModel.h>
41
42 // OCCT Includes
43 #include <TColStd_IndexedMapOfInteger.hxx>
44
45 //=================================================================================
46 // class    : BlocksGUI_TrsfDlg()
47 // purpose  : Constructs a BlocksGUI_TrsfDlg which is a child of 'parent'.
48 //=================================================================================
49 BlocksGUI_TrsfDlg::BlocksGUI_TrsfDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
50   : GEOMBase_Skeleton(theGeometryGUI, parent),
51     myInitial(true)
52 {
53   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
54   QPixmap image1 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_BLOCK_MULTITRSF_SIMPLE")));
55   QPixmap image2 (aResMgr->loadPixmap("GEOM", tr("ICON_DLG_BLOCK_MULTITRSF_DOUBLE")));
56   QPixmap imageS (aResMgr->loadPixmap("GEOM", tr("ICON_SELECT")));
57
58   setWindowTitle(tr("GEOM_BLOCK_MULTITRSF_TITLE"));
59
60   /***************************************************************/
61   mainFrame()->GroupConstructors->setTitle(tr("GEOM_BLOCK_MULTITRSF"));
62
63   mainFrame()->RadioButton1->setIcon(image1);
64   mainFrame()->RadioButton2->setIcon(image2);
65   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
66   mainFrame()->RadioButton3->close();
67
68   // Create first group
69   myGrp1 = new QGroupBox(tr("GEOM_BLOCK_MULTITRSF_SIMPLE"), centralWidget());
70
71   createSelWg(tr("GEOM_MAIN_OBJECT"), imageS, myGrp1, MainObj1);
72   createSelWg(tr("FACE_1"),           imageS, myGrp1, Face1);
73   createSelWg(tr("FACE_2"),           imageS, myGrp1, Face2);
74   createSpinWg(tr("GEOM_NB_TIMES"),           myGrp1, SpinBox1);
75
76   // Create second group
77   myGrp2 = new QGroupBox(tr("GEOM_BLOCK_MULTITRSF_DOUBLE"), centralWidget());
78
79   createSelWg(tr("GEOM_MAIN_OBJECT"), imageS, myGrp2, MainObj2);
80   createSelWg(tr("FACE_1U"),          imageS, myGrp2, Face1U);
81   createSelWg(tr("FACE_2U"),          imageS, myGrp2, Face2U);
82   createSpinWg(tr("GEOM_NB_TIMES_U"),         myGrp2, SpinBox2U);
83   createSelWg(tr("FACE_1V"),          imageS, myGrp2, Face1V);
84   createSelWg(tr("FACE_2V"),          imageS, myGrp2, Face2V);
85   createSpinWg(tr("GEOM_NB_TIMES_V"),         myGrp2, SpinBox2V);
86
87   // Add groups to layout
88   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
89   layout->setMargin(0); layout->setSpacing(6);
90   layout->addWidget(myGrp1);
91   layout->addWidget(myGrp2);
92   /***************************************************************/
93
94   setHelpFileName("multi_transformation_operation_page.html");
95
96   Init();
97 }
98
99 //=================================================================================
100 // function : ~BlocksGUI_TrsfDlg()
101 // purpose  : Destroys the object and frees any allocated resources
102 //=================================================================================
103 BlocksGUI_TrsfDlg::~BlocksGUI_TrsfDlg()
104 {
105   // no need to delete child widgets, Qt does it all for us
106 }
107
108 //=================================================================================
109 // function : Init()
110 // purpose  :
111 //=================================================================================
112 void BlocksGUI_TrsfDlg::Init()
113 {
114   // Set range of spinboxes
115   int SpecificStep = 1;
116   QMap<int, SalomeApp_IntSpinBox*>::iterator anIter;
117   for (anIter = mySpinBox.begin(); anIter != mySpinBox.end(); ++anIter) {
118     initSpinBox(anIter.value(), 1, 999, SpecificStep);
119   }
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)), this, SLOT(ConstructorsClicked(int)));
126
127   QMap<int, QPushButton*>::iterator anIterBtn;
128   for (anIterBtn = mySelBtn.begin(); anIterBtn != mySelBtn.end(); ++anIterBtn)
129     connect(anIterBtn.value(), SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
130
131   QMap<int, SalomeApp_IntSpinBox*>::iterator anIterSpin;
132   for (anIterSpin = mySpinBox.begin(); anIterSpin != mySpinBox.end(); ++anIterSpin)
133     connect(anIterSpin.value(), SIGNAL(valueChanged(int)), this, SLOT(ValueChangedInSpinBox(int)));
134
135   // init controls and fields
136   initName(tr("GEOM_BLOCK_MULTITRSF"));
137
138   myConstructorId = -1;
139   ConstructorsClicked(0);
140 }
141
142 //=================================================================================
143 // function : ConstructorsClicked()
144 // purpose  : Radio button management
145 //=================================================================================
146 void BlocksGUI_TrsfDlg::ConstructorsClicked (int constructorId)
147 {
148   if (myConstructorId == constructorId)
149     return;
150
151   myConstructorId = constructorId;
152
153   // init fields
154   myShape = GEOM::GEOM_Object::_nil();
155   myFaces[Face1] = myFaces[Face2] = -1;
156   myFaces[Face1U] = myFaces[Face2U] = -1;
157   myFaces[Face1V] = myFaces[Face2V] = -1;
158
159   // clear line edits
160   QMap<int, QLineEdit*>::iterator anIterLE;
161   for (anIterLE = mySelName.begin(); anIterLE != mySelName.end(); ++anIterLE)
162     anIterLE.value()->setText("");
163
164   switch (constructorId) {
165   case 0:
166     myGrp2->hide();
167     myGrp1->show();
168     mySpinBox[SpinBox1]->setValue(2);
169     mySelBtn[MainObj1]->click();
170     break;
171   case 1:
172     myGrp1->hide();
173     myGrp2->show();
174     mySpinBox[SpinBox2U]->setValue(2);
175     mySpinBox[SpinBox2V]->setValue(2);
176     mySelBtn[MainObj2]->click();
177     break;
178   default:
179     break;
180   }
181
182   qApp->processEvents();
183   updateGeometry();
184   resize(minimumSizeHint());
185
186   // on dialog initialization we init the first field with a selected object (if any)
187   SelectionIntoArgument();
188 }
189
190 //=================================================================================
191 // function : ClickOnOk()
192 // purpose  :
193 //=================================================================================
194 void BlocksGUI_TrsfDlg::ClickOnOk()
195 {
196   if (ClickOnApply())
197     ClickOnCancel();
198 }
199
200 //=================================================================================
201 // function : ClickOnApply()
202 // purpose  :
203 //=================================================================================
204 bool BlocksGUI_TrsfDlg::ClickOnApply()
205 {
206   if (!onAccept())
207     return false;
208
209   initName();
210   return true;
211 }
212
213 //=================================================================================
214 // function : SelectionIntoArgument()
215 // purpose  : Called when selection is changed or on dialog initialization or activation
216 //=================================================================================
217 void BlocksGUI_TrsfDlg::SelectionIntoArgument()
218 {
219   erasePreview();
220   myEditCurrentArgument->setText("");
221
222   // Get index of current selection focus
223   int aCurrFocus = -1;
224   QMap<int, QLineEdit*>::iterator anIter;
225   for (anIter = mySelName.begin(); anIter != mySelName.end(); ++anIter) {
226     if (myEditCurrentArgument == anIter.value()) {
227       aCurrFocus = anIter.key();
228       break;
229     }
230   }
231
232   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
233   SALOME_ListIO aSelList;
234   aSelMgr->selectedObjects(aSelList);
235
236   QString aName;
237   GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_nil();
238
239   if (aCurrFocus == MainObj1 || aCurrFocus == MainObj2)
240   {
241     // If selection of main object is activated
242     if (aSelList.Extent() == 1) {
243       anObj = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
244
245       if ( GEOMBase::IsShape(anObj) ) {
246         aName = GEOMBase::GetName(anObj);
247       }
248     }
249     myEditCurrentArgument->setText(aName);
250     myShape = anObj;
251     enableWidgets();
252   }
253   else if (aCurrFocus == Face1  || aCurrFocus == Face2  ||
254            aCurrFocus == Face1U || aCurrFocus == Face2U ||
255            aCurrFocus == Face1V || aCurrFocus == Face2V) {
256     // If face selection is activated
257     int anIndex = -1;
258     if (aSelList.Extent() == 1) {
259       anObj = GEOMBase::ConvertIOinGEOMObject( aSelList.First() );
260       if ( GEOMBase::IsShape(anObj) ) {
261         aName = GEOMBase::GetName(anObj);
262         TColStd_IndexedMapOfInteger anIndexes;
263         aSelMgr->GetIndexes(aSelList.First(), anIndexes);
264
265         if (anIndexes.Extent() == 1) {
266           anIndex = anIndexes(1);
267           aName += QString(":face_%1").arg(anIndex);
268         }
269       }
270     }
271     myEditCurrentArgument->setText(aName);
272     myFaces[aCurrFocus] = anIndex;
273     displayPreview();
274   }
275
276   switch (aCurrFocus) {
277     // 1D
278   case MainObj1:
279     if (!myShape->_is_nil() && myFaces[Face1] == -1)
280       mySelBtn[Face1]->click();
281     break;
282   case Face1:
283     if (myFaces[Face1] != -1 && myFaces[Face2] == -1)
284       mySelBtn[Face2]->click();
285     break;
286   case Face2:
287     if (myFaces[Face2] != -1 && myShape->_is_nil())
288       mySelBtn[MainObj1]->click();
289     break;
290
291     // 2D
292   case MainObj2:
293     if (!myShape->_is_nil() && myFaces[Face1U] == -1)
294       mySelBtn[Face1U]->click();
295     break;
296   case Face1U:
297     if (myFaces[Face1U] != -1 && myFaces[Face2U] == -1)
298       mySelBtn[Face2U]->click();
299     break;
300   case Face2U:
301     if (myFaces[Face2U] != -1 && myFaces[Face1V] == -1)
302       mySelBtn[Face1V]->click();
303     break;
304   case Face1V:
305     if (myFaces[Face1V] != -1 && myFaces[Face2V] == -1)
306       mySelBtn[Face2V]->click();
307     break;
308   case Face2V:
309     if (myFaces[Face2V] != -1 && myShape->_is_nil())
310       mySelBtn[MainObj1]->click();
311     break;
312
313   default:
314     break;
315   }
316 }
317
318 //=================================================================================
319 // function : SetEditCurrentArgument()
320 // purpose  :
321 //=================================================================================
322 void BlocksGUI_TrsfDlg::SetEditCurrentArgument()
323 {
324   QPushButton* aSender = (QPushButton*)sender();
325
326   // clear selection
327   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
328   if (myInitial)
329     myInitial = false;
330   else
331     myGeomGUI->getApp()->selectionMgr()->clearSelected();
332
333   // disable all
334   switch (myConstructorId) {
335   case 0:
336     mySelBtn[MainObj1]->setDown(false);
337     mySelBtn[Face1]->setDown(false);
338     mySelBtn[Face2]->setDown(false);
339
340     mySelName[MainObj1]->setEnabled(false);
341     mySelName[Face1]->setEnabled(false);
342     mySelName[Face2]->setEnabled(false);
343     break;
344   case 1:
345     mySelBtn[MainObj2]->setDown(false);
346     mySelBtn[Face1U]->setDown(false);
347     mySelBtn[Face2U]->setDown(false);
348     mySelBtn[Face1V]->setDown(false);
349     mySelBtn[Face2V]->setDown(false);
350
351     mySelName[MainObj2]->setEnabled(false);
352     mySelName[Face1U]->setEnabled(false);
353     mySelName[Face2U]->setEnabled(false);
354     mySelName[Face1V]->setEnabled(false);
355     mySelName[Face2V]->setEnabled(false);
356     break;
357   default:
358     break;
359   }
360
361   // set line edit as current argument
362   QMap<int, QPushButton*>::iterator anIter;
363   for (anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter) {
364     if (anIter.value() == aSender) {
365       myEditCurrentArgument = mySelName[anIter.key()];
366       break;
367     }
368   }
369
370   // enable line edit
371   myEditCurrentArgument->setEnabled(true);
372   myEditCurrentArgument->setFocus();
373
374   // enable push button
375   // after setFocus(), because it will be setDown(false) when loses focus
376   aSender->setDown(true);
377
378   activateSelection();
379 }
380
381 //=================================================================================
382 // function : ActivateThisDialog()
383 // purpose  :
384 //=================================================================================
385 void BlocksGUI_TrsfDlg::ActivateThisDialog()
386 {
387   GEOMBase_Skeleton::ActivateThisDialog();
388   activateSelection();
389
390   // ??
391   displayPreview();
392 }
393
394 //=================================================================================
395 // function : enterEvent()
396 // purpose  :
397 //=================================================================================
398 void BlocksGUI_TrsfDlg::enterEvent (QEvent*)
399 {
400   if (!mainFrame()->GroupConstructors->isEnabled())
401     ActivateThisDialog();
402 }
403
404 //=================================================================================
405 // function : ValueChangedInSpinBox()
406 // purpose  :
407 //=================================================================================
408 void BlocksGUI_TrsfDlg::ValueChangedInSpinBox(int)
409 {
410   displayPreview();
411 }
412
413 //=================================================================================
414 // function : createSelWg()
415 // purpose  :
416 //=================================================================================
417 void BlocksGUI_TrsfDlg::createSelWg (const QString& theLbl,
418                                      QPixmap&       thePix,
419                                      QWidget*       theParent,
420                                      const int      theId)
421 {
422   QLabel* lab = new QLabel(theLbl, theParent);
423   mySelBtn[theId] = new QPushButton(theParent);
424   mySelBtn[theId]->setIcon(thePix);
425   mySelBtn[theId]->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
426   mySelName[theId] = new QLineEdit(theParent);
427   mySelName[theId]->setReadOnly(true);
428   QGridLayout* l = 0;
429   if (!theParent->layout()) {
430     l = new QGridLayout(theParent);
431     l->setMargin(9); l->setSpacing(6);
432   }
433   else {
434     l = qobject_cast<QGridLayout*>(theParent->layout());
435   }
436   int row = l->rowCount();
437   l->addWidget(lab,              row, 0);
438   l->addWidget(mySelBtn[theId],  row, 1);
439   l->addWidget(mySelName[theId], row, 2);
440 }
441
442 //=================================================================================
443 // function : createSpinWg()
444 // purpose  :
445 //=================================================================================
446 void BlocksGUI_TrsfDlg::createSpinWg (const QString& theLbl,
447                                       QWidget*       theParent,
448                                       const int      theId)
449 {
450   QLabel* lab = new QLabel(theLbl, theParent);
451   mySpinBox[theId] = new SalomeApp_IntSpinBox(theParent);
452   QGridLayout* l = 0;
453   if (!theParent->layout()) {
454     l = new QGridLayout(theParent);
455     l->setMargin(9); l->setSpacing(6);
456   }
457   else {
458     l = qobject_cast<QGridLayout*>(theParent->layout());
459   }
460   int row = l->rowCount();
461   l->addWidget(lab,              row, 0);
462   l->addWidget(mySpinBox[theId], row, 2);
463 }
464
465 //=================================================================================
466 // function : activateSelection
467 // purpose  : Activate selection in accordance with myEditCurrentArgument
468 //=================================================================================
469 void BlocksGUI_TrsfDlg::activateSelection()
470 {
471   globalSelection(GEOM_ALLSHAPES);
472   if (!myShape->_is_nil() &&
473       (myEditCurrentArgument == mySelName[Face1 ] ||
474        myEditCurrentArgument == mySelName[Face2 ] ||
475        myEditCurrentArgument == mySelName[Face1U] ||
476        myEditCurrentArgument == mySelName[Face2U] ||
477        myEditCurrentArgument == mySelName[Face1V] ||
478        myEditCurrentArgument == mySelName[Face2V]))
479   {
480     // Local selection is available only in the OCC Viewer
481     if (getDesktop()->activeWindow()->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
482       localSelection(myShape, TopAbs_FACE);
483     }
484   }
485   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
486           this, SLOT(SelectionIntoArgument()));
487 }
488
489 //=================================================================================
490 // function : enableWidgets
491 // purpose  : Enable widgets of faces in accordance with value of main object
492 //=================================================================================
493 void BlocksGUI_TrsfDlg::enableWidgets()
494 {
495   int anId = getConstructorId();
496
497   bool toEnable = !myShape->_is_nil();
498
499   if (anId == 0) {
500     //mySelName[Face1]->setEnabled(toEnable);
501     //mySelName[Face2]->setEnabled(toEnable);
502     //mySelBtn[Face1]->setEnabled(toEnable);
503     //mySelBtn[Face2]->setEnabled(toEnable);
504
505     if (!toEnable)  {
506       mySelName[Face1]->setText("");
507       mySelName[Face2]->setText("");
508       myFaces[Face1] = -1;
509       myFaces[Face2] = -1;
510     }
511   }
512   else if (anId == 1) {
513     //mySelName[Face1U]->setEnabled(toEnable);
514     //mySelName[Face2U]->setEnabled(toEnable);
515     //mySelName[Face1V]->setEnabled(toEnable);
516     //mySelName[Face2V]->setEnabled(toEnable);
517     //mySelBtn[Face1U]->setEnabled(toEnable);
518     //mySelBtn[Face2U]->setEnabled(toEnable);
519     //mySelBtn[Face1V]->setEnabled(toEnable);
520     //mySelBtn[Face2V]->setEnabled(toEnable);
521
522     if (!toEnable) {
523       mySelName[Face1U]->setText("");
524       mySelName[Face2U]->setText("");
525       mySelName[Face1V]->setText("");
526       mySelName[Face2V]->setText("");
527       myFaces[Face1U] = -1;
528       myFaces[Face2U] = -1;
529       myFaces[Face1V] = -1;
530       myFaces[Face2V] = -1;
531     }
532   }
533 }
534
535 //=================================================================================
536 // function : createOperation
537 // purpose  :
538 //=================================================================================
539 GEOM::GEOM_IOperations_ptr BlocksGUI_TrsfDlg::createOperation()
540 {
541   return getGeomEngine()->GetIBlocksOperations(getStudyId());
542 }
543
544 //=================================================================================
545 // function : isValid
546 // purpose  : Verify validity of input data
547 //=================================================================================
548 bool BlocksGUI_TrsfDlg::isValid (QString& msg)
549 {
550   bool ok = false, okSP = true;
551   switch (getConstructorId()) {
552   case 0:
553     ok = !myShape->_is_nil() && myFaces[Face1] > 0;
554     okSP = mySpinBox[SpinBox1]->isValid( msg, !IsPreview() );
555     break;
556   case 1:
557     ok = !myShape->_is_nil() && myFaces[Face1U] > 0 && myFaces[Face1V] > 0;
558     okSP = mySpinBox[SpinBox2U]->isValid( msg, !IsPreview() ) && okSP;
559     okSP = mySpinBox[SpinBox2V]->isValid( msg, !IsPreview() ) && okSP;
560     break;
561   default:
562     break;
563   }
564   return ok && okSP;
565 }
566
567 //=================================================================================
568 // function : execute
569 // purpose  :
570 //=================================================================================
571 bool BlocksGUI_TrsfDlg::execute (ObjectList& objects)
572 {
573   bool res = false;
574
575   GEOM::GEOM_Object_var anObj;
576
577   GEOM::GEOM_IBlocksOperations_var anOper = GEOM::GEOM_IBlocksOperations::_narrow(getOperation());
578
579   switch (getConstructorId()) {
580   case 0:
581     anObj = anOper->MakeMultiTransformation1D(myShape,
582                                               myFaces[Face1], myFaces[Face2],
583                                               mySpinBox[SpinBox1]->value());
584     if (!anObj->_is_nil() && !IsPreview())
585     {
586       QStringList aParameters;
587       aParameters << "" << "";
588       aParameters << mySpinBox[SpinBox1]->text();
589       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
590     }
591     res = true;
592     break;
593   case 1:
594     anObj = anOper->MakeMultiTransformation2D (myShape,
595                                                myFaces[Face1U], myFaces[Face2U],
596                                                mySpinBox[SpinBox2U]->value(),
597                                                myFaces[Face1V], myFaces[Face2V],
598                                                mySpinBox[SpinBox2V]->value());
599     if (!anObj->_is_nil() && !IsPreview())
600     {
601       QStringList aParameters;
602       aParameters << "" << "";
603       aParameters << mySpinBox[SpinBox2U]->text();
604       aParameters << "" << "";
605       aParameters << mySpinBox[SpinBox2V]->text();
606       anObj->SetParameters(aParameters.join(":").toLatin1().constData());
607     }
608     res = true;
609     break;
610   default:
611     break;
612   }
613
614   if (!anObj->_is_nil())
615     objects.push_back(anObj._retn());
616
617   return res;
618 }