Salome HOME
2c1dbc537a311fcaf3391d831c323aaa60c6f8f4
[modules/geom.git] / src / BasicGUI / BasicGUI_ArcDlg.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : BasicGUI_ArcDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "BasicGUI_ArcDlg.h"
30
31 #include "QAD_Desktop.h"
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <GC_MakeArcOfCircle.hxx>
34 #include <Geom_TrimmedCurve.hxx>
35 #include <Precision.hxx>
36
37 #include "GEOMImpl_Types.hxx"
38
39 #include "utilities.h"
40
41 using namespace std;
42
43 //=================================================================================
44 // class    : BasicGUI_ArcDlg()
45 // purpose  : Constructs a BasicGUI_ArcDlg which is a child of 'parent', with the 
46 //            name 'name' and widget flags set to 'f'.
47 //            The dialog will by default be modeless, unless you set 'modal' to
48 //            TRUE to construct a modal dialog.
49 //=================================================================================
50 BasicGUI_ArcDlg::BasicGUI_ArcDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
51   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
52 {
53   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_ARC")));
54   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
55
56   setCaption(tr("GEOM_ARC_TITLE"));
57
58   /***************************************************************/
59   GroupConstructors->setTitle(tr("GEOM_ARC"));
60   RadioButton1->setPixmap(image0);
61   RadioButton2->close(TRUE);
62   RadioButton3->close(TRUE);
63
64   Group3Pnts = new DlgRef_3Sel_QTD(this, "Group3Pnts");
65   Group3Pnts->GroupBox1->setTitle(tr("GEOM_POINTS"));
66   Group3Pnts->TextLabel1->setText(tr("GEOM_POINT_I").arg("1"));
67   Group3Pnts->TextLabel2->setText(tr("GEOM_POINT_I").arg("2"));
68   Group3Pnts->TextLabel3->setText(tr("GEOM_POINT_I").arg("3"));
69
70   Group3Pnts->LineEdit1->setReadOnly( true );
71   Group3Pnts->LineEdit2->setReadOnly( true );
72   Group3Pnts->LineEdit3->setReadOnly( true );
73   
74   Group3Pnts->PushButton1->setPixmap(image1);
75   Group3Pnts->PushButton2->setPixmap(image1);
76   Group3Pnts->PushButton3->setPixmap(image1);
77
78   Layout1->addWidget( Group3Pnts, 2, 0 );
79
80   Init();
81 }
82
83
84 //=================================================================================
85 // function : ~BasicGUI_ArcDlg()
86 // purpose  : Destroys the object and frees any allocated resources
87 //=================================================================================
88 BasicGUI_ArcDlg::~BasicGUI_ArcDlg()
89 {
90 }
91
92
93 //=================================================================================
94 // function : Init()
95 // purpose  :
96 //=================================================================================
97 void BasicGUI_ArcDlg::Init()
98 {
99   /* init variables */
100   myGeomGUI->SetState( 0 );
101   globalSelection( GEOM_POINT );
102
103   myEditCurrentArgument = Group3Pnts->LineEdit1;
104   myEditCurrentArgument->setFocus();
105
106   myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
107
108   /* signals and slots connections */
109   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
110   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
111   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
112
113   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
114   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
115
116   connect(Group3Pnts->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
117   connect(Group3Pnts->PushButton2, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
118   connect(Group3Pnts->PushButton3, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
119
120   connect(Group3Pnts->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
121   connect(Group3Pnts->LineEdit2, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
122   connect(Group3Pnts->LineEdit3, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
123
124   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
125
126   initName( tr( "GEOM_ARC" ) );
127 }
128
129
130 //=================================================================================
131 // function : ClickOnOk()
132 // purpose  :
133 //=================================================================================
134 void BasicGUI_ArcDlg::ClickOnOk()
135 {
136   if ( ClickOnApply() )
137     ClickOnCancel();
138 }
139
140 //=======================================================================
141 // function : ClickOnCancel()
142 // purpose  :
143 //=======================================================================
144 void BasicGUI_ArcDlg::ClickOnCancel()
145 {
146   GEOMBase_Skeleton::ClickOnCancel();
147 }
148
149 //=================================================================================
150 // function : ClickOnApply()
151 // purpose  :
152 //=================================================================================
153 bool BasicGUI_ArcDlg::ClickOnApply()
154 {
155   if ( !onAccept() )
156     return false;
157
158   initName();
159
160   // reset
161   myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
162   Group3Pnts->LineEdit1->setText( "" );
163   Group3Pnts->LineEdit2->setText( "" );
164   Group3Pnts->LineEdit3->setText( "" );
165   myEditCurrentArgument = Group3Pnts->LineEdit1;
166   
167   return true;
168 }
169
170
171 //=================================================================================
172 // function : SelectionIntoArgument()
173 // purpose  : Called when selection as changed or other case
174 //=================================================================================
175 void BasicGUI_ArcDlg::SelectionIntoArgument()
176 {
177   myEditCurrentArgument->setText("");
178   
179   if ( mySelection->IObjectCount() != 1 )  
180   {
181     if      ( myEditCurrentArgument == Group3Pnts->LineEdit1 )   myPoint1 = GEOM::GEOM_Object::_nil();
182     else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )   myPoint2 = GEOM::GEOM_Object::_nil();
183     else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )   myPoint3 = GEOM::GEOM_Object::_nil();
184     return;
185   }
186
187   // nbSel == 1
188   Standard_Boolean aRes = Standard_False;
189   GEOM::GEOM_Object_var aSelectedObject = GEOMBase::ConvertIOinGEOMObject( mySelection->firstIObject(), aRes );
190   if ( !CORBA::is_nil( aSelectedObject ) && aRes )
191   {  
192     myEditCurrentArgument->setText( GEOMBase::GetName( aSelectedObject ) ); 
193     if      ( myEditCurrentArgument == Group3Pnts->LineEdit1 )   myPoint1 = aSelectedObject;
194     else if ( myEditCurrentArgument == Group3Pnts->LineEdit2 )   myPoint2 = aSelectedObject;
195     else if ( myEditCurrentArgument == Group3Pnts->LineEdit3 )   myPoint3 = aSelectedObject;
196   }
197
198   displayPreview();
199 }
200
201
202 //=================================================================================
203 // function : SetEditCurrentArgument()
204 // purpose  :
205 //=================================================================================
206 void BasicGUI_ArcDlg::SetEditCurrentArgument()
207 {
208   QPushButton* send = (QPushButton*)sender();
209
210   if      ( send == Group3Pnts->PushButton1 )   myEditCurrentArgument = Group3Pnts->LineEdit1;
211   else if ( send == Group3Pnts->PushButton2 )   myEditCurrentArgument = Group3Pnts->LineEdit2;
212   else if ( send == Group3Pnts->PushButton3 )   myEditCurrentArgument = Group3Pnts->LineEdit3;
213   
214   myEditCurrentArgument->setFocus();
215   SelectionIntoArgument();
216 }
217
218
219 //=================================================================================
220 // function : LineEditReturnPressed()
221 // purpose  :
222 //=================================================================================
223 void BasicGUI_ArcDlg::LineEditReturnPressed()
224
225   QLineEdit* send = (QLineEdit*)sender();
226   if ( send == Group3Pnts->LineEdit1 ||
227        send == Group3Pnts->LineEdit2 ||
228        send == Group3Pnts->LineEdit3 )
229   {
230     myEditCurrentArgument = send;
231     GEOMBase_Skeleton::LineEditReturnPressed();
232   }
233 }
234
235
236 //=================================================================================
237 // function : ActivateThisDialog()
238 // purpose  :
239 //=================================================================================
240 void BasicGUI_ArcDlg::ActivateThisDialog()
241 {
242   GEOMBase_Skeleton::ActivateThisDialog();
243   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
244
245   myGeomGUI->SetState( 0 );
246   globalSelection( GEOM_POINT );
247
248   myEditCurrentArgument = Group3Pnts->LineEdit1;
249   myEditCurrentArgument->setFocus();
250
251   Group3Pnts->LineEdit1->setText( "" );
252   Group3Pnts->LineEdit2->setText( "" );
253   Group3Pnts->LineEdit3->setText( "" );
254   myPoint1 = myPoint2 = myPoint3 = GEOM::GEOM_Object::_nil();
255 }
256
257 //=================================================================================
258 // function : DeactivateActiveDialog()
259 // purpose  : public slot to deactivate if active
260 //=================================================================================
261 void BasicGUI_ArcDlg::DeactivateActiveDialog()
262 {
263   myGeomGUI->SetState( -1 );
264   GEOMBase_Skeleton::DeactivateActiveDialog();
265 }
266
267 //=================================================================================
268 // function : enterEvent()
269 // purpose  :
270 //=================================================================================
271 void BasicGUI_ArcDlg::enterEvent(QEvent* e)
272 {
273   if ( !GroupConstructors->isEnabled() )
274     ActivateThisDialog();
275 }
276
277 //=================================================================================
278 // function : createOperation
279 // purpose  :
280 //=================================================================================
281 GEOM::GEOM_IOperations_ptr BasicGUI_ArcDlg::createOperation()
282 {
283   return getGeomEngine()->GetICurvesOperations( getStudyId() );
284 }
285
286 //=================================================================================
287 // function : isEqual
288 // purpose  : it may also be needed to check for min distance between gp_Pnt-s...
289 //=================================================================================
290 static bool isEqual( const GEOM::GEOM_Object_var& thePnt1, const GEOM::GEOM_Object_var& thePnt2 )
291 {
292         return thePnt1->_is_equivalent( thePnt2 );
293 }
294
295 //=================================================================================
296 // function : isValid
297 // purpose  :
298 //=================================================================================
299 bool BasicGUI_ArcDlg::isValid( QString& msg )
300 {
301   return !myPoint1->_is_nil() && !myPoint2->_is_nil() && !myPoint3->_is_nil() &&
302                      !isEqual( myPoint1, myPoint2 ) && !isEqual( myPoint1, myPoint3 ) && !isEqual( myPoint2, myPoint3 );
303 }
304
305 //=================================================================================
306 // function : execute
307 // purpose  :
308 //=================================================================================
309 bool BasicGUI_ArcDlg::execute( ObjectList& objects )
310 {
311   GEOM::GEOM_Object_var anObj = GEOM::GEOM_ICurvesOperations::_narrow( getOperation() )->MakeArc( myPoint1, myPoint2, myPoint3 );
312         if ( !anObj->_is_nil() )
313         objects.push_back( anObj._retn() );
314   return true;
315 }
316
317 //=================================================================================
318 // function : closeEvent
319 // purpose  :
320 //=================================================================================
321 void BasicGUI_ArcDlg::closeEvent( QCloseEvent* e )
322 {
323   myGeomGUI->SetState( -1 );
324   GEOMBase_Skeleton::closeEvent( e );
325 }