Salome HOME
IMPs 19766 (Wire from unconnected edges) and 20004 (NumberOfSolids)
[modules/geom.git] / src / BuildGUI / BuildGUI_EdgeDlg.cxx
1 //  Copyright (C) 2007-2008  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 // GEOM GEOMGUI : GUI for Geometry component
23 // File   : BuildGUI_EdgeDlg.cxx
24 // Author : Lucien PIGNOLONI, Open CASCADE S.A.S.
25 //
26 #include "BuildGUI_EdgeDlg.h"
27
28 #include <DlgRef.h>
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31
32 #include <SUIT_Session.h>
33 #include <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <GEOMImpl_Types.hxx>
38
39 //=================================================================================
40 // class    : BuildGUI_EdgeDlg()
41 // purpose  : Constructs a BuildGUI_EdgeDlg which is a child of 'parent', with the
42 //            name 'name' and widget flags set to 'f'.
43 //            The dialog will by default be modeless, unless you set 'modal' to
44 //            TRUE to construct a modal dialog.
45 //=================================================================================
46 BuildGUI_EdgeDlg::BuildGUI_EdgeDlg (GeometryGUI* theGeometryGUI, QWidget* parent)
47   : GEOMBase_Skeleton(theGeometryGUI, parent)
48 {
49   QPixmap image0 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_DLG_BUILD_EDGE")));
50   QPixmap image1 (SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
51
52   setWindowTitle(tr("GEOM_EDGE_TITLE"));
53
54   /***************************************************************/
55   mainFrame()->GroupConstructors->setTitle(tr("GEOM_EDGE"));
56   mainFrame()->RadioButton1->setIcon(image0);
57   mainFrame()->RadioButton2->setAttribute(Qt::WA_DeleteOnClose);
58   mainFrame()->RadioButton2->close();
59   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
60   mainFrame()->RadioButton3->close();
61
62   GroupPoints = new DlgRef_2Sel(centralWidget());
63
64   GroupPoints->GroupBox1->setTitle(tr("GEOM_POINTS"));
65   GroupPoints->TextLabel1->setText(tr("GEOM_POINT_I").arg(1));
66   GroupPoints->TextLabel2->setText(tr("GEOM_POINT_I").arg(2));
67   GroupPoints->PushButton1->setIcon(image1);
68   GroupPoints->PushButton2->setIcon(image1);
69
70   GroupPoints->LineEdit1->setReadOnly(true);
71   GroupPoints->LineEdit2->setReadOnly(true);
72
73   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
74   layout->setMargin(0); layout->setSpacing(6);
75   layout->addWidget(GroupPoints);
76   /***************************************************************/
77
78   setHelpFileName("create_edge_page.html");
79
80   // Initialisation
81   Init();
82 }
83
84 //=================================================================================
85 // function : ~BuildGUI_EdgeDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 BuildGUI_EdgeDlg::~BuildGUI_EdgeDlg()
89 {
90   // no need to delete child widgets, Qt does it all for us
91 }
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void BuildGUI_EdgeDlg::Init()
98 {
99   // init variables
100   GroupPoints->LineEdit1->setReadOnly(true);
101   GroupPoints->LineEdit2->setReadOnly(true);
102
103   GroupPoints->LineEdit1->setText("");
104   GroupPoints->LineEdit2->setText("");
105   myPoint1 = myPoint2 = GEOM::GEOM_Object::_nil();
106   myOkPoint1 = myOkPoint2 = false;
107
108   // signals and slots connections
109   connect(buttonOk(),    SIGNAL(clicked()), this, SLOT(ClickOnOk()));
110   connect(buttonApply(), SIGNAL(clicked()), this, SLOT(ClickOnApply()));
111
112   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
113   connect(GroupPoints->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
114
115   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
116   connect(GroupPoints->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
117
118   initName(tr("GEOM_EDGE"));
119
120   GroupPoints->PushButton1->click();
121   SelectionIntoArgument();
122 }
123
124 //=================================================================================
125 // function : ClickOnOk()
126 // purpose  :
127 //=================================================================================
128 void BuildGUI_EdgeDlg::ClickOnOk()
129 {
130   if (ClickOnApply())
131     ClickOnCancel();
132 }
133
134 //=================================================================================
135 // function : ClickOnApply()
136 // purpose  :
137 //=================================================================================
138 bool BuildGUI_EdgeDlg::ClickOnApply()
139 {
140   if (!onAccept())
141     return false;
142
143   initName();
144   // activate selection and connect selection manager
145   GroupPoints->PushButton1->click();
146   return true;
147 }
148
149 //=================================================================================
150 // function : SelectionIntoArgument()
151 // purpose  : Called when selection is changed or on dialog initialization or activation
152 //=================================================================================
153 void BuildGUI_EdgeDlg::SelectionIntoArgument()
154 {
155   erasePreview();
156   myEditCurrentArgument->setText("");
157
158   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
159   SALOME_ListIO aSelList;
160   aSelMgr->selectedObjects(aSelList);
161
162   if (aSelList.Extent() != 1) {
163     if (myEditCurrentArgument == GroupPoints->LineEdit1)
164       myOkPoint1 = false;
165     else if (myEditCurrentArgument == GroupPoints->LineEdit2)
166       myOkPoint2 = false;
167     return;
168   }
169
170   // nbSel == 1
171   Standard_Boolean testResult = Standard_False;
172   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject(aSelList.First(), testResult);
173   if (!testResult || aSelectedObject->_is_nil())
174     return;
175
176   myEditCurrentArgument->setText(GEOMBase::GetName(aSelectedObject));
177
178   // clear selection
179   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
180   myGeomGUI->getApp()->selectionMgr()->clearSelected();
181   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
182           this, SLOT(SelectionIntoArgument()));
183
184   if (myEditCurrentArgument == GroupPoints->LineEdit1) {
185     myPoint1 = aSelectedObject;
186     myOkPoint1 = true;
187     if (!myOkPoint2)
188       GroupPoints->PushButton2->click();
189   }
190   else if (myEditCurrentArgument == GroupPoints->LineEdit2) {
191     myPoint2 = aSelectedObject;
192     myOkPoint2 = true;
193     if (!myOkPoint1)
194       GroupPoints->PushButton1->click();
195   }
196
197   displayPreview();
198 }
199
200 //=================================================================================
201 // function : SetEditCurrentArgument()
202 // purpose  :
203 //=================================================================================
204 void BuildGUI_EdgeDlg::SetEditCurrentArgument()
205 {
206   QPushButton* send = (QPushButton*)sender();
207   //globalSelection();//??
208
209   if (send == GroupPoints->PushButton1) {
210     myEditCurrentArgument = GroupPoints->LineEdit1;
211     GroupPoints->PushButton2->setDown(false);
212     GroupPoints->LineEdit2->setEnabled(false);
213   }
214   else if (send == GroupPoints->PushButton2) {
215     myEditCurrentArgument = GroupPoints->LineEdit2;
216     GroupPoints->PushButton1->setDown(false);
217     GroupPoints->LineEdit1->setEnabled(false);
218   }
219
220   // enable line edit
221   myEditCurrentArgument->setEnabled(true);
222   myEditCurrentArgument->setFocus();
223   // after setFocus(), because it will be setDown(false) when loses focus
224   send->setDown(true);
225
226   disconnect(myGeomGUI->getApp()->selectionMgr(), 0, this, 0);
227   globalSelection(GEOM_POINT);
228   connect(myGeomGUI->getApp()->selectionMgr(), SIGNAL(currentSelectionChanged()),
229           this, SLOT(SelectionIntoArgument()));
230
231   // seems we need it only to avoid preview disappearing, caused by selection mode change
232   displayPreview();
233 }
234
235 //=================================================================================
236 // function : LineEditReturnPressed()
237 // purpose  :
238 //=================================================================================
239 void BuildGUI_EdgeDlg::LineEditReturnPressed()
240 {
241   QLineEdit* send = (QLineEdit*)sender();
242   if (send == GroupPoints->LineEdit1 || send == GroupPoints->LineEdit2) {
243     myEditCurrentArgument = send;
244     GEOMBase_Skeleton::LineEditReturnPressed();
245   }
246 }
247
248 //=================================================================================
249 // function : ActivateThisDialog()
250 // purpose  :
251 //=================================================================================
252 void BuildGUI_EdgeDlg::ActivateThisDialog()
253 {
254   GEOMBase_Skeleton::ActivateThisDialog();
255
256   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
257            this, SLOT( SelectionIntoArgument() ) );
258
259   displayPreview();
260 }
261
262 //=================================================================================
263 // function : enterEvent()
264 // purpose  :
265 //=================================================================================
266 void BuildGUI_EdgeDlg::enterEvent (QEvent*)
267 {
268   if (!mainFrame()->GroupConstructors->isEnabled())
269     ActivateThisDialog();
270 }
271
272 //=================================================================================
273 // function : createOperation
274 // purpose  :
275 //=================================================================================
276 GEOM::GEOM_IOperations_ptr BuildGUI_EdgeDlg::createOperation()
277 {
278   return getGeomEngine()->GetIShapesOperations(getStudyId());
279 }
280
281 //=================================================================================
282 // function : isValid
283 // purpose  :
284 //=================================================================================
285 bool BuildGUI_EdgeDlg::isValid (QString&)
286 {
287   return myOkPoint1 && myOkPoint2;
288 }
289
290 //=================================================================================
291 // function : execute
292 // purpose  :
293 //=================================================================================
294 bool BuildGUI_EdgeDlg::execute (ObjectList& objects)
295 {
296   GEOM::GEOM_Object_var anObj;
297
298   anObj = GEOM::GEOM_IShapesOperations::_narrow(getOperation())->MakeEdge(myPoint1, myPoint2);
299
300   if (!anObj->_is_nil())
301     objects.push_back(anObj._retn());
302
303   return true;
304 }