Salome HOME
Merge with PAL/SALOME 2.1.0d
[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.org
20 //
21 //
22 //
23 //  File   : BlocksGUI_QuadFaceDlg.cxx
24 //  Author : Julia DOROVSKIKH
25 //  Module : GEOM
26 //  $Header$
27
28 #include "BlocksGUI_QuadFaceDlg.h"
29
30 #include "QAD_Desktop.h"
31
32 #include "GEOMImpl_Types.hxx"
33
34 //using namespace std;
35
36 //=================================================================================
37 // class    : BlocksGUI_QuadFaceDlg()
38 // purpose  : Constructs a BlocksGUI_QuadFaceDlg which is a child of 'parent'.
39 //=================================================================================
40 BlocksGUI_QuadFaceDlg::BlocksGUI_QuadFaceDlg (QWidget* parent,
41                                         SALOME_Selection* Sel,
42                                         bool modal)
43      : GEOMBase_Skeleton(parent, "QuadFaceDlg", Sel, modal,
44                          WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
45 {
46   QPixmap image1 (QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_QUAD_FACE_4_VERT")));
47   QPixmap image2 (QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_QUAD_FACE_2_EDGE")));
48   QPixmap image3 (QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_QUAD_FACE_4_EDGE")));
49   QPixmap imageS (QAD_Desktop::getResourceManager()->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   Init();
104 }
105
106 //=================================================================================
107 // function : ~BlocksGUI_QuadFaceDlg()
108 // purpose  : Destroys the object and frees any allocated resources
109 //=================================================================================
110 BlocksGUI_QuadFaceDlg::~BlocksGUI_QuadFaceDlg()
111 {
112   // no need to delete child widgets, Qt does it all for us
113 }
114
115 //=================================================================================
116 // function : Init()
117 // purpose  :
118 //=================================================================================
119 void BlocksGUI_QuadFaceDlg::Init()
120 {
121   // signals and slots connections
122   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
123   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
124   connect(GroupConstructors, SIGNAL(clicked(int)), this, SLOT(ConstructorsClicked(int)));
125
126   QMap<int, QPushButton*>::iterator anIterBtn;
127   for (anIterBtn = mySelBtn.begin(); anIterBtn != mySelBtn.end(); ++anIterBtn)
128     connect(anIterBtn.data(), SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
129
130   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
131
132   // init controls and fields
133   initName(tr("GEOM_QUAD_FACE"));
134
135   myConstructorId = -1;
136   ConstructorsClicked(0);
137 }
138
139 //=================================================================================
140 // function : ConstructorsClicked()
141 // purpose  : Radio button management
142 //=================================================================================
143 void BlocksGUI_QuadFaceDlg::ConstructorsClicked (int constructorId)
144 {
145   if (myConstructorId == constructorId)
146     return;
147
148   myConstructorId = constructorId;
149
150   switch (constructorId) {
151   case 0:
152     myGrp2->hide();
153     myGrp3->hide();
154     myGrp1->show();
155     myEditCurrentArgument = mySelName[Vertex1];
156     break;
157   case 1:
158     myGrp1->hide();
159     myGrp3->hide();
160     myGrp2->show();
161     myEditCurrentArgument = mySelName[Edge12];
162     break;
163   case 2:
164     myGrp1->hide();
165     myGrp2->hide();
166     myGrp3->show();
167     myEditCurrentArgument = mySelName[Edge14];
168     break;
169   default:
170     break;
171   }
172
173   // clear line edits
174   QMap<int, QLineEdit*>::iterator anIterLE;
175   for (anIterLE = mySelName.begin(); anIterLE != mySelName.end(); ++anIterLE)
176     anIterLE.data()->setText("");
177
178   // init fields
179   myShape1 = myShape2 = GEOM::GEOM_Object::_nil();
180   myShape3 = myShape4 = myShape1;
181
182   activateSelection();
183 }
184
185 //=================================================================================
186 // function : ClickOnOk()
187 // purpose  :
188 //=================================================================================
189 void BlocksGUI_QuadFaceDlg::ClickOnOk()
190 {
191   if (ClickOnApply())
192     ClickOnCancel();
193 }
194
195 //=================================================================================
196 // function : ClickOnApply()
197 // purpose  :
198 //=================================================================================
199 bool BlocksGUI_QuadFaceDlg::ClickOnApply()
200 {
201   if (!onAccept())
202     return false;
203
204   initName();
205   return true;
206 }
207
208 //=================================================================================
209 // function : SelectionIntoArgument()
210 // purpose  : Called when selection has changed
211 //=================================================================================
212 void BlocksGUI_QuadFaceDlg::SelectionIntoArgument()
213 {
214   erasePreview();
215   myEditCurrentArgument->setText("");
216
217   // Get index of current selection focus
218   int aCurrFocus = -1;
219   QMap<int, QLineEdit*>::iterator anIter;
220   for (anIter = mySelName.begin(); anIter != mySelName.end(); ++anIter) {
221     if (myEditCurrentArgument == anIter.data()) {
222       aCurrFocus = anIter.key();
223       break;
224     }
225   }
226
227   GEOM::GEOM_Object_var anObj;
228   Standard_Boolean aResult = Standard_False;
229   if (mySelection->IObjectCount() == 1) {
230     anObj = GEOMBase::ConvertIOinGEOMObject(mySelection->firstIObject(), aResult);
231     if (aResult) {
232       if (anObj->_is_nil()) {
233         aResult = Standard_False;
234       } else {
235         mySelName[aCurrFocus]->setText(GEOMBase::GetName(anObj));
236       }
237     } else {
238       anObj = GEOM::GEOM_Object::_nil();
239     }
240   }
241
242   if (aCurrFocus == Vertex1 || aCurrFocus == Edge12 || aCurrFocus == Edge14) {
243     myShape1 = anObj;
244   } else if (aCurrFocus == Vertex2 || aCurrFocus == Edge22 || aCurrFocus == Edge24) {
245     myShape2 = anObj;
246   } else if (aCurrFocus == Vertex3 || aCurrFocus == Edge34) {
247     myShape3 = anObj;
248   } else if (aCurrFocus == Vertex4 || aCurrFocus == Edge44) {
249     myShape4 = anObj;
250   } else {
251     return;
252   }
253
254   displayPreview();
255 }
256
257 //=================================================================================
258 // function : SetEditCurrentArgument()
259 // purpose  :
260 //=================================================================================
261 void BlocksGUI_QuadFaceDlg::SetEditCurrentArgument()
262 {
263   QPushButton* aSender = (QPushButton*)sender();
264
265   QMap<int, QPushButton*>::iterator anIter;
266   for (anIter = mySelBtn.begin(); anIter != mySelBtn.end(); ++anIter) {
267     if (anIter.data() == aSender) {
268       mySelName[anIter.key()]->setFocus();
269       myEditCurrentArgument = mySelName[anIter.key()];
270     }
271   }
272
273   activateSelection();
274 }
275
276 //=================================================================================
277 // function : ActivateThisDialog()
278 // purpose  :
279 //=================================================================================
280 void BlocksGUI_QuadFaceDlg::ActivateThisDialog()
281 {
282   GEOMBase_Skeleton::ActivateThisDialog();
283   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
284
285   activateSelection();
286   displayPreview();
287 }
288
289 //=================================================================================
290 // function : enterEvent()
291 // purpose  :
292 //=================================================================================
293 void BlocksGUI_QuadFaceDlg::enterEvent (QEvent* e)
294 {
295   if (!GroupConstructors->isEnabled())
296     this->ActivateThisDialog();
297 }
298
299 //=================================================================================
300 // function : DeactivateActiveDialog()
301 // purpose  :
302 //=================================================================================
303 //void BlocksGUI_QuadFaceDlg::DeactivateActiveDialog()
304 //{
305 //  // disconnect selection
306 //  GEOMBase_Skeleton::DeactivateActiveDialog();
307 //}
308
309 //=================================================================================
310 // function : createSelWg()
311 // purpose  :
312 //=================================================================================
313 void BlocksGUI_QuadFaceDlg::createSelWg (const QString& theLbl,
314                                          QPixmap&       thePix,
315                                          QWidget*       theParent,
316                                          const int      theId)
317 {
318   new QLabel(theLbl, theParent);
319   mySelBtn[theId] = new QPushButton(theParent);
320   mySelBtn[theId]->setPixmap(thePix);
321   mySelName[theId] = new QLineEdit(theParent);
322   mySelName[theId]->setReadOnly(true);
323 }
324
325 //=================================================================================
326 // function : activateSelection
327 // purpose  : Activate selection in accordance with myEditCurrentArgument
328 //=================================================================================
329 void BlocksGUI_QuadFaceDlg::activateSelection()
330 {
331   if (myEditCurrentArgument == mySelName[Vertex1] ||
332       myEditCurrentArgument == mySelName[Vertex2] ||
333       myEditCurrentArgument == mySelName[Vertex3] ||
334       myEditCurrentArgument == mySelName[Vertex4]) {
335
336     globalSelection(GEOM_POINT);
337
338   } else {
339     globalSelection(GEOM_EDGE);
340   }
341
342   SelectionIntoArgument();
343 }
344
345 //=================================================================================
346 // function : createOperation
347 // purpose  :
348 //=================================================================================
349 GEOM::GEOM_IOperations_ptr BlocksGUI_QuadFaceDlg::createOperation()
350 {
351   return getGeomEngine()->GetIBlocksOperations(getStudyId());
352 }
353
354 //=================================================================================
355 // function : isValid
356 // purpose  : Verify validity of input data
357 //=================================================================================
358 bool BlocksGUI_QuadFaceDlg::isValid (QString&)
359 {
360   switch (getConstructorId()) {
361     case 0:
362       return (!myShape1->_is_nil() && !myShape2->_is_nil() &&
363               !myShape3->_is_nil() && !myShape4->_is_nil());
364     case 1:
365       return (!myShape1->_is_nil() && !myShape2->_is_nil());
366     case 2:
367       return (!myShape1->_is_nil() && !myShape2->_is_nil() &&
368               !myShape3->_is_nil() && !myShape4->_is_nil());
369     default:
370       return false;
371   }
372   return false;
373 }
374
375 //=================================================================================
376 // function : execute
377 // purpose  :
378 //=================================================================================
379 bool BlocksGUI_QuadFaceDlg::execute (ObjectList& objects)
380 {
381   bool res = false;
382
383   GEOM::GEOM_Object_var anObj;
384
385   switch (getConstructorId()) {
386     case 0:
387       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeQuad4Vertices
388         (myShape1, myShape2, myShape3, myShape4);
389       res = true;
390       break;
391     case 1:
392       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeQuad2Edges
393         (myShape1, myShape2);
394       res = true;
395       break;
396     case 2:
397       anObj = GEOM::GEOM_IBlocksOperations::_narrow(getOperation())->MakeQuad
398         (myShape1, myShape2, myShape3, myShape4);
399       res = true;
400       break;
401     default:
402       break;
403   }
404
405   if (!anObj->_is_nil())
406     objects.push_back(anObj._retn());
407
408   return res;
409 }