Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / BlocksGUI / BlocksGUI_QuadFaceDlg.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_QuadFaceDlg.cxx
24 //  Author : Julia DOROVSKIKH
25 //  Module : GEOM
26 //  $Header$
27
28 #include "BlocksGUI_QuadFaceDlg.h"
29 #include "GEOMImpl_Types.hxx"
30
31 #include "SUIT_Session.h"
32 #include "SalomeApp_Application.h"
33 #include "LightApp_SelectionMgr.h"
34
35 #include <qlabel.h>
36
37 //=================================================================================
38 // class    : BlocksGUI_QuadFaceDlg()
39 // purpose  : Constructs a BlocksGUI_QuadFaceDlg which is a child of 'parent'.
40 //=================================================================================
41 BlocksGUI_QuadFaceDlg::BlocksGUI_QuadFaceDlg (GeometryGUI* theGeometryGUI, QWidget* parent, bool modal)
42      : GEOMBase_Skeleton(theGeometryGUI, parent, "QuadFaceDlg", modal,
43                          WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
44 {
45   SUIT_ResourceMgr* aResMgr = myGeomGUI->getApp()->resourceMgr();
46   QPixmap image1 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_QUAD_FACE_4_VERT")));
47   QPixmap image2 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_QUAD_FACE_2_EDGE")));
48   QPixmap image3 (aResMgr->loadPixmap("GEOM",tr("ICON_DLG_QUAD_FACE_4_EDGE")));
49   QPixmap imageS (aResMgr->loadPixmap("GEOM",tr("ICON_SELECT")));
50
51   setCaption(tr("GEOM_QUAD_FACE_TITLE"));
52
53   /***************************************************************/
54   GroupConstructors->setTitle(tr("GEOM_QUAD_FACE"));
55
56   RadioButton1->setPixmap(image1);
57   RadioButton2->setPixmap(image2);
58   RadioButton3->setPixmap(image3);
59
60   // Create first group
61   myGrp1 = new QGroupBox(1, Qt::Horizontal, tr("GEOM_ARGUMENTS"), this);
62
63   QGroupBox* aSelGrp1 = new QGroupBox(3, Qt::Horizontal, myGrp1);
64   aSelGrp1->setFrameStyle(QFrame::NoFrame);
65   aSelGrp1->setInsideMargin(0);
66
67   createSelWg(tr("VERTEX_1"), imageS, aSelGrp1, Vertex1);
68   createSelWg(tr("VERTEX_2"), imageS, aSelGrp1, Vertex2);
69   createSelWg(tr("VERTEX_3"), imageS, aSelGrp1, Vertex3);
70   createSelWg(tr("VERTEX_4"), imageS, aSelGrp1, Vertex4);
71
72   // Create second group
73   myGrp2 = new QGroupBox(1, Qt::Horizontal, tr("GEOM_ARGUMENTS"), this);
74
75   QGroupBox* aSelGrp2 = new QGroupBox(3, Qt::Horizontal, myGrp2);
76   aSelGrp2->setFrameStyle(QFrame::NoFrame);
77   aSelGrp2->setInsideMargin(0);
78
79   createSelWg(tr("EDGE_1"), imageS, aSelGrp2, Edge12);
80   createSelWg(tr("EDGE_2"), imageS, aSelGrp2, Edge22);
81
82   // Create fird group
83   myGrp3 = new QGroupBox(1, Qt::Horizontal, tr("GEOM_ARGUMENTS"), this);
84
85   QGroupBox* aSelGrp3 = new QGroupBox(3, Qt::Horizontal, myGrp3);
86   aSelGrp3->setFrameStyle(QFrame::NoFrame);
87   aSelGrp3->setInsideMargin(0);
88
89   createSelWg(tr("EDGE_1"), imageS, aSelGrp3, Edge14);
90   createSelWg(tr("EDGE_2"), imageS, aSelGrp3, Edge24);
91   createSelWg(tr("EDGE_3"), imageS, aSelGrp3, Edge34);
92   createSelWg(tr("EDGE_4"), imageS, aSelGrp3, Edge44);
93
94   (new QLabel(myGrp3))->setSizePolicy(
95     QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
96
97   // Add groups to layout
98   Layout1->addWidget(myGrp1, 2, 0);
99   Layout1->addWidget(myGrp2, 2, 0);
100   Layout1->addWidget(myGrp3, 2, 0);
101   /***************************************************************/
102
103   setHelpFileName("build_by_blocks_page.html#quad_face_anchor");
104
105   Init();
106 }
107
108 //=================================================================================
109 // function : ~BlocksGUI_QuadFaceDlg()
110 // purpose  : Destroys the object and frees any allocated resources
111 //=================================================================================
112 BlocksGUI_QuadFaceDlg::~BlocksGUI_QuadFaceDlg()
113 {
114   // no need to delete child widgets, Qt does it all for us
115 }
116
117 //=================================================================================
118 // function : Init()
119 // purpose  :
120 //=================================================================================
121 void BlocksGUI_QuadFaceDlg::Init()
122 {
123   // signals and slots connections
124   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
125   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
126   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
127
128   QMap<int, QPushButton*>::iterator anIterBtn;
129   for (anIterBtn = mySelBtn.begin(); anIterBtn != mySelBtn.end(); ++anIterBtn)
130     connect(anIterBtn.data(), SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
131
132   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
133           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
134
135   // init controls and fields
136   initName(tr("GEOM_QUAD_FACE"));
137
138   myConstructorId = -1;
139   ConstructorsClicked(0);
140 }
141
142 //=================================================================================
143 // function : ConstructorsClicked()
144 // purpose  : Radio button management
145 //=================================================================================
146 void BlocksGUI_QuadFaceDlg::ConstructorsClicked (int constructorId)
147 {
148   if (myConstructorId == constructorId)
149     return;
150
151   myConstructorId = constructorId;
152
153   switch (constructorId) {
154   case 0:
155     myGrp2->hide();
156     myGrp3->hide();
157     myGrp1->show();
158     myEditCurrentArgument = mySelName[Vertex1];
159     break;
160   case 1:
161     myGrp1->hide();
162     myGrp3->hide();
163     myGrp2->show();
164     myEditCurrentArgument = mySelName[Edge12];
165     break;
166   case 2:
167     myGrp1->hide();
168     myGrp2->hide();
169     myGrp3->show();
170     myEditCurrentArgument = mySelName[Edge14];
171     break;
172   default:
173     break;
174   }
175
176   // clear line edits
177   QMap<int, QLineEdit*>::iterator anIterLE;
178   for (anIterLE = mySelName.begin(); anIterLE != mySelName.end(); ++anIterLE)
179     anIterLE.data()->setText("");
180
181   // init fields
182   myShape1 = myShape2 = GEOM::GEOM_Object::_nil();
183   myShape3 = myShape4 = myShape1;
184
185   activateSelection();
186 }
187
188 //=================================================================================
189 // function : ClickOnOk()
190 // purpose  :
191 //=================================================================================
192 void BlocksGUI_QuadFaceDlg::ClickOnOk()
193 {
194   if (ClickOnApply())
195     ClickOnCancel();
196 }
197
198 //=================================================================================
199 // function : ClickOnApply()
200 // purpose  :
201 //=================================================================================
202 bool BlocksGUI_QuadFaceDlg::ClickOnApply()
203 {
204   if (!onAccept())
205     return false;
206
207   initName();
208   return true;
209 }
210
211 //=================================================================================
212 // function : SelectionIntoArgument()
213 // purpose  : Called when selection has changed
214 //=================================================================================
215 void BlocksGUI_QuadFaceDlg::SelectionIntoArgument()
216 {
217   erasePreview();
218   myEditCurrentArgument->setText("");
219
220   // Get index of current selection focus
221   int aCurrFocus = -1;
222   QMap<int, QLineEdit*>::iterator anIter;
223   for (anIter = mySelName.begin(); anIter != mySelName.end(); ++anIter) {
224     if (myEditCurrentArgument == anIter.data()) {
225       aCurrFocus = anIter.key();
226       break;
227     }
228   }
229
230   GEOM::GEOM_Object_var anObj;
231   Standard_Boolean aResult = Standard_False;
232   if (IObjectCount() == 1) {
233     anObj = GEOMBase::ConvertIOinGEOMObject(firstIObject(), aResult);
234     if (aResult) {
235       if (anObj->_is_nil()) {
236         aResult = Standard_False;
237       } else {
238         mySelName[aCurrFocus]->setText(GEOMBase::GetName(anObj));
239       }
240     } else {
241       anObj = GEOM::GEOM_Object::_nil();
242     }
243   }
244
245   if (aCurrFocus == Vertex1 || aCurrFocus == Edge12 || aCurrFocus == Edge14) {
246     myShape1 = anObj;
247   } else if (aCurrFocus == Vertex2 || aCurrFocus == Edge22 || aCurrFocus == Edge24) {
248     myShape2 = anObj;
249   } else if (aCurrFocus == Vertex3 || aCurrFocus == Edge34) {
250     myShape3 = anObj;
251   } else if (aCurrFocus == Vertex4 || aCurrFocus == Edge44) {
252     myShape4 = anObj;
253   } else {
254     return;
255   }
256
257   displayPreview();
258 }
259
260 //=================================================================================
261 // function : SetEditCurrentArgument()
262 // purpose  :
263 //=================================================================================
264 void BlocksGUI_QuadFaceDlg::SetEditCurrentArgument()
265 {
266   QPushButton* aSender = (QPushButton*)sender();
267
268   QMap<int, QPushButton*>::iterator anIter;
269   for (anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter) {
270     if (anIter.data() == aSender) {
271       mySelName[anIter.key()]->setFocus();
272       myEditCurrentArgument = mySelName[anIter.key()];
273     }
274   }
275
276   activateSelection();
277 }
278
279 //=================================================================================
280 // function : ActivateThisDialog()
281 // purpose  :
282 //=================================================================================
283 void BlocksGUI_QuadFaceDlg::ActivateThisDialog()
284 {
285   GEOMBase_Skeleton::ActivateThisDialog();
286   connect(((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr(), 
287           SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument())) ;
288
289   activateSelection();
290   displayPreview();
291 }
292
293 //=================================================================================
294 // function : enterEvent()
295 // purpose  :
296 //=================================================================================
297 void BlocksGUI_QuadFaceDlg::enterEvent (QEvent* e)
298 {
299   if (!GroupConstructors->isEnabled())
300     this->ActivateThisDialog();
301 }
302
303 //=================================================================================
304 // function : DeactivateActiveDialog()
305 // purpose  :
306 //=================================================================================
307 //void BlocksGUI_QuadFaceDlg::DeactivateActiveDialog()
308 //{
309 //  // disconnect selection
310 //  GEOMBase_Skeleton::DeactivateActiveDialog();
311 //}
312
313 //=================================================================================
314 // function : createSelWg()
315 // purpose  :
316 //=================================================================================
317 void BlocksGUI_QuadFaceDlg::createSelWg (const QString& theLbl,
318                                          QPixmap&       thePix,
319                                          QWidget*       theParent,
320                                          const int      theId)
321 {
322   new QLabel(theLbl, theParent);
323   mySelBtn[theId] = new QPushButton(theParent);
324   mySelBtn[theId]->setPixmap(thePix);
325   mySelName[theId] = new QLineEdit(theParent);
326   mySelName[theId]->setReadOnly(true);
327 }
328
329 //=================================================================================
330 // function : activateSelection
331 // purpose  : Activate selection in accordance with myEditCurrentArgument
332 //=================================================================================
333 void BlocksGUI_QuadFaceDlg::activateSelection()
334 {
335   if (myEditCurrentArgument == mySelName[Vertex1] ||
336       myEditCurrentArgument == mySelName[Vertex2] ||
337       myEditCurrentArgument == mySelName[Vertex3] ||
338       myEditCurrentArgument == mySelName[Vertex4]) {
339
340     globalSelection(GEOM_POINT);
341
342   } else {
343     globalSelection(GEOM_EDGE);
344   }
345
346   SelectionIntoArgument();
347 }
348
349 //=================================================================================
350 // function : createOperation
351 // purpose  :
352 //=================================================================================
353 GEOM::GEOM_IOperations_ptr BlocksGUI_QuadFaceDlg::createOperation()
354 {
355   return getGeomEngine()->GetIBlocksOperations(getStudyId());
356 }
357
358 //=================================================================================
359 // function : isValid
360 // purpose  : Verify validity of input data
361 //=================================================================================
362 bool BlocksGUI_QuadFaceDlg::isValid (QString&)
363 {
364   switch (getConstructorId()) {
365     case 0:
366       return (!myShape1->_is_nil() && !myShape2->_is_nil() &&
367               !myShape3->_is_nil() && !myShape4->_is_nil());
368     case 1:
369       return (!myShape1->_is_nil() && !myShape2->_is_nil());
370     case 2:
371       return (!myShape1->_is_nil() && !myShape2->_is_nil() &&
372               !myShape3->_is_nil() && !myShape4->_is_nil());
373     default:
374       return false;
375   }
376   return false;
377 }
378
379 //=================================================================================
380 // function : execute
381 // purpose  :
382 //=================================================================================
383 bool BlocksGUI_QuadFaceDlg::execute (ObjectList& objects)
384 {
385   bool res = false;
386
387   GEOM::GEOM_Object_var anObj;
388
389   switch (getConstructorId()) {
390     case 0:
391       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeQuad4Vertices
392         (myShape1, myShape2, myShape3, myShape4);
393       res = true;
394       break;
395     case 1:
396       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeQuad2Edges
397         (myShape1, myShape2);
398       res = true;
399       break;
400     case 2:
401       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeQuad
402         (myShape1, myShape2, myShape3, myShape4);
403       res = true;
404       break;
405     default:
406       break;
407   }
408
409   if (!anObj->_is_nil())
410     objects.push_back(anObj._retn());
411
412   return res;
413 }