Salome HOME
Merge with OCC_development_01
[modules/geom.git] / src / RepairGUI / RepairGUI_GlueDlg.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   : RepairGUI_GlueDlg.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "RepairGUI_GlueDlg.h"
31
32 #include "DlgRef_1Sel_Ext.h"
33
34 #include "QAD_Desktop.h"
35 #include "QAD_SpinBoxDbl.h"
36 #include "QAD_MessageBox.h"
37 #include "QAD_WaitCursor.h"
38
39 #include "OCCViewer_Viewer3d.h"
40 #include "SALOME_ListIteratorOfListIO.hxx"
41 #include "SALOMEGUI_QtCatchCorbaException.hxx"
42
43 #include "GEOMImpl_Types.hxx"
44
45 #define DEFAULT_TOLERANCE_VALUE 1e-07
46
47
48 //=================================================================================
49 // class    : RepairGUI_GlueDlg()
50 // purpose  : Constructs a RepairGUI_GlueDlg  which is a child of 'parent', with the
51 //            name 'name' and widget flags set to 'f'.
52 //            The dialog will by default be modeless, unless you set 'modal' to
53 //            TRUE to construct a modal dialog.
54 //=================================================================================
55 RepairGUI_GlueDlg::RepairGUI_GlueDlg(QWidget* parent, const char* name, SALOME_Selection* Sel, bool modal, WFlags fl)
56   :GEOMBase_Skeleton(parent, name, Sel, modal, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu)
57 {
58   QPixmap image0(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_DLG_GLUE_FACES")));
59   QPixmap image1(QAD_Desktop::getResourceManager()->loadPixmap("GEOM",tr("ICON_SELECT")));
60
61   setCaption(tr("GEOM_GLUE_TITLE"));
62
63   /***************************************************************/
64   GroupConstructors->setTitle(tr("GEOM_GLUE_TITLE"));
65   RadioButton1->setPixmap(image0);
66   RadioButton2->close(TRUE);
67   RadioButton3->close(TRUE);
68
69   GroupPoints = new DlgRef_1Sel_Ext(this, "GroupPoints");
70   GroupPoints->GroupBox1->setTitle(tr("GEOM_GLUE"));
71   GroupPoints->TextLabel1->setText(tr("GEOM_SELECTED_SHAPE"));
72   GroupPoints->PushButton1->setPixmap(image1);
73   GroupPoints->LineEdit1->setReadOnly( true );
74
75   Layout1->addWidget(GroupPoints, 2, 0);
76
77   QGridLayout* aLay = new QGridLayout( 0, 2, 2, 0, 6, "aLay" );
78   QLabel* aLbl1 = new QLabel( tr( "GEOM_TOLERANCE" ), GroupPoints->GroupBox1 );
79   myTolEdt = new QAD_SpinBoxDbl( GroupPoints->GroupBox1, 0, 100, 1e-7, 10, 1e-10 );
80   myTolEdt->setValue( DEFAULT_TOLERANCE_VALUE );
81
82   aLay->addWidget( aLbl1, 0, 0 );
83   aLay->addWidget( myTolEdt, 0, 1 );
84
85   GroupPoints->getGroupBoxLayout()->addLayout( aLay, 3, 0 );
86   /***************************************************************/
87
88   Init();
89 }
90
91
92 //=================================================================================
93 // function : ~RepairGUI_GlueDlg()
94 // purpose  : Destroys the object and frees any allocated resources
95 //=================================================================================
96 RepairGUI_GlueDlg::~RepairGUI_GlueDlg()
97 {
98 }
99
100
101 //=================================================================================
102 // function : Init()
103 // purpose  :
104 //=================================================================================
105 void RepairGUI_GlueDlg::Init()
106 {
107   /* init variables */
108   myEditCurrentArgument = GroupPoints->LineEdit1;
109
110   myObject = GEOM::GEOM_Object::_nil();
111
112   myGeomGUI->SetState( 0 );
113   globalSelection( GEOM_COMPOUND );
114
115   /* signals and slots connections */
116   connect(buttonCancel, SIGNAL(clicked()), this, SLOT(ClickOnCancel()));
117   connect(myGeomGUI, SIGNAL(SignalDeactivateActiveDialog()), this, SLOT(DeactivateActiveDialog()));
118   connect(myGeomGUI, SIGNAL(SignalCloseAllDialogs()), this, SLOT(ClickOnCancel()));
119
120   connect(buttonOk, SIGNAL(clicked()), this, SLOT(ClickOnOk()));
121   connect(buttonApply, SIGNAL(clicked()), this, SLOT(ClickOnApply()));
122
123   connect(GroupPoints->PushButton1, SIGNAL(clicked()), this, SLOT(SetEditCurrentArgument()));
124   connect(GroupPoints->LineEdit1, SIGNAL(returnPressed()), this, SLOT(LineEditReturnPressed()));
125
126   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
127
128   initName( tr( "GLUE_NEW_OBJ_NAME" ) );
129 }
130
131
132 //=================================================================================
133 // function : ClickOnOk()
134 // purpose  : Same than click on apply but close this dialog.
135 //=================================================================================
136 void RepairGUI_GlueDlg::ClickOnOk()
137 {
138   if ( ClickOnApply() )
139     ClickOnCancel();
140 }
141
142
143
144 //=================================================================================
145 // function : ClickOnApply()
146 // purpose  :
147 //=================================================================================
148 bool RepairGUI_GlueDlg::ClickOnApply()
149 {
150   if ( !onAcceptLocal() )
151     return false;
152
153   initName();
154
155   GroupPoints->LineEdit1->setText("");
156   myObject = GEOM::GEOM_Object::_nil();
157
158   globalSelection( GEOM_COMPOUND );
159
160   return true;
161 }
162
163
164 //=================================================================================
165 // function : ClickOnCancel()
166 // purpose  :
167 //=================================================================================
168 void RepairGUI_GlueDlg::ClickOnCancel()
169 {
170   GEOMBase_Skeleton::ClickOnCancel();
171 }
172
173
174 //=================================================================================
175 // function : SelectionIntoArgument()
176 // purpose  : Called when selection
177 //=================================================================================
178 void RepairGUI_GlueDlg::SelectionIntoArgument()
179 {
180   erasePreview();
181   myEditCurrentArgument->setText("");
182   myObject = GEOM::GEOM_Object::_nil();
183
184   if ( mySelection->IObjectCount() == 1 ) {
185     Handle(SALOME_InteractiveObject) anIO = mySelection->firstIObject();
186     Standard_Boolean aRes;
187     myObject = GEOMBase::ConvertIOinGEOMObject( anIO, aRes );
188     if ( aRes )
189       myEditCurrentArgument->setText( GEOMBase::GetName( myObject ) );
190   }
191 }
192
193 //=================================================================================
194 // function : SetEditCurrentArgument()
195 // purpose  :
196 //=================================================================================
197 void RepairGUI_GlueDlg::SetEditCurrentArgument()
198 {
199   const QObject* send = sender();
200   if ( send == GroupPoints->PushButton1 )  {
201     myEditCurrentArgument->setFocus();
202     SelectionIntoArgument();
203   }
204 }
205
206
207 //=================================================================================
208 // function : LineEditReturnPressed()
209 // purpose  :
210 //=================================================================================
211 void RepairGUI_GlueDlg::LineEditReturnPressed()
212 {
213   const QObject* send = sender();
214   if( send == GroupPoints->LineEdit1 ) {
215     myEditCurrentArgument = GroupPoints->LineEdit1;
216     GEOMBase_Skeleton::LineEditReturnPressed();
217   }
218 }
219
220
221 //=================================================================================
222 // function : DeactivateActiveDialog()
223 // purpose  :
224 //=================================================================================
225 void RepairGUI_GlueDlg::DeactivateActiveDialog()
226 {
227   myGeomGUI->SetState( -1 );
228   GEOMBase_Skeleton::DeactivateActiveDialog();
229 }
230
231
232 //=================================================================================
233 // function : ActivateThisDialog()
234 // purpose  :
235 //=================================================================================
236 void RepairGUI_GlueDlg::ActivateThisDialog()
237 {
238   GEOMBase_Skeleton::ActivateThisDialog();
239   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(SelectionIntoArgument()));
240
241   GroupPoints->LineEdit1->setText("");
242   myObject = GEOM::GEOM_Object::_nil();
243
244   myGeomGUI->SetState( 0 );
245   globalSelection( GEOM_COMPOUND );
246 }
247
248
249 //=================================================================================
250 // function : enterEvent()
251 // purpose  : Mouse enter onto the dialog to activate it
252 //=================================================================================
253 void RepairGUI_GlueDlg::enterEvent(QEvent* e)
254 {
255   if ( !GroupConstructors->isEnabled() )
256     ActivateThisDialog();
257 }
258
259
260 //=================================================================================
261 // function : closeEvent()
262 // purpose  :
263 //=================================================================================
264 void RepairGUI_GlueDlg::closeEvent(QCloseEvent* e)
265 {
266   myGeomGUI->SetState( -1 );
267   GEOMBase_Skeleton::closeEvent( e );
268 }
269
270 //=================================================================================
271 // function : createOperation
272 // purpose  :
273 //=================================================================================
274 GEOM::GEOM_IOperations_ptr RepairGUI_GlueDlg::createOperation()
275 {
276   return getGeomEngine()->GetIShapesOperations( getStudyId() );
277 }
278
279 //=================================================================================
280 // function : isValid
281 // purpose  :
282 //=================================================================================
283 bool RepairGUI_GlueDlg::isValid( QString& msg )
284 {
285   return !myObject->_is_nil() && ( IsPreview() || myTolEdt->value() > 0. );
286 }
287
288 //=================================================================================
289 // function : execute
290 // purpose  :
291 //=================================================================================
292 bool RepairGUI_GlueDlg::execute( ObjectList& objects )
293 {
294   bool aResult = false;
295   GEOM::GEOM_Object_var anObj = GEOM::GEOM_IShapesOperations::_narrow
296     ( getOperation() )->MakeGlueFaces( myObject, myTolEdt->value() );
297   aResult = !anObj->_is_nil();
298   if ( aResult )
299     objects.push_back( anObj._retn() );
300
301   return aResult;
302 }
303
304 //================================================================
305 // Function : clearShapeBufferLocal
306 // Purpose  : 
307 //================================================================
308 void RepairGUI_GlueDlg::clearShapeBufferLocal( GEOM::GEOM_Object_ptr theObj )
309 {
310   if ( CORBA::is_nil( theObj ) )
311     return;
312
313   string IOR = GeometryGUI::GetORB()->object_to_string( theObj );
314   TCollection_AsciiString asciiIOR( strdup( IOR.c_str() ) );
315   GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );
316
317   if ( !getStudy() || CORBA::is_nil( getStudy()->getStudyDocument() ) )
318     return;
319
320   SALOMEDS::Study_var aStudy = getStudy()->getStudyDocument();
321   SALOMEDS::SObject_var aSObj = aStudy->FindObjectIOR( IOR.c_str() );
322   if ( CORBA::is_nil( aSObj ) )
323     return;
324
325   SALOMEDS::ChildIterator_var anIt = aStudy->NewChildIterator( aSObj );
326   for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
327     SALOMEDS::GenericAttribute_var anAttr;
328     if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
329       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
330       TCollection_AsciiString asciiIOR( anIOR->Value() );
331       GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );      
332     }
333   }
334 }
335
336 //================================================================
337 // Function : onAccept
338 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
339 //            It perfroms user input validation, then it 
340 //            performs a proper operation and manages transactions, etc.
341 //================================================================
342 bool RepairGUI_GlueDlg::onAcceptLocal( const bool publish, const bool useTransaction )
343 {
344   QAD_Study* aDoc = QAD_Application::getDesktop()->getActiveStudy();
345   SALOMEDS::Study_var aStudy = aDoc->getStudyDocument();
346
347   bool aLocked = aStudy->GetProperties()->IsLocked();
348   if ( aLocked ) {
349     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
350     QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
351                            QObject::tr("WRN_WARNING"), 
352                            QObject::tr("WRN_STUDY_LOCKED"),
353                            QObject::tr("BUT_OK") );
354     return false;
355   }
356
357   QString msg;
358   if ( !isValid( msg ) ) {
359     showError( msg );
360     return false;
361   }
362
363   erasePreview( false );
364
365   try {
366     if ( ( !publish && !useTransaction ) || openCommand() ) {
367       QAD_WaitCursor wc;
368       QAD_Application::getDesktop()->putInfo( "" );
369       ObjectList objects;
370       // JFA 28.12.2004 if ( !execute( objects ) || !getOperation()->IsDone() ) {
371       if ( !execute( objects ) ) { // JFA 28.12.2004 // To enable warnings
372         wc.stop();
373         abortCommand();
374         showError();
375       }
376       else {
377         const int nbObjs = objects.size();
378         bool withChildren = false;
379         for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
380           if ( publish ) {
381             QString aName("");
382             if ( nbObjs > 1 )
383               aName = strlen( getNewObjectName() ) ? GEOMBase::GetDefaultName( getNewObjectName() ) : GEOMBase::GetDefaultName( getPrefix( *it ) );
384             else {
385               aName = getNewObjectName();
386               // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
387               if ( aName.isEmpty() )
388                 aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
389             }
390             addInStudy( *it, aName.latin1() );
391             withChildren = false;
392             display( *it, false );
393           }
394           else { // asv : fix of PAL6454. If publish==false, then the original shape was modified, and need to be re-cached in GEOM_Client 
395                  // before redisplay
396             clearShapeBufferLocal( *it );
397             withChildren = true;
398             redisplay( *it, withChildren, false );
399           }
400         }
401
402         if ( nbObjs ) {
403           commitCommand();
404           updateObjBrowser();
405           QAD_Application::getDesktop()->putInfo( QObject::tr("GEOM_PRP_DONE") );
406         }
407         else {
408           abortCommand();
409         }
410
411         // JFA 28.12.2004 BEGIN // To enable warnings
412         if ( !getOperation()->_is_nil() ) {
413           if ( !getOperation()->IsDone() ) {
414             wc.stop();
415             QString msgw = QObject::tr( getOperation()->GetErrorCode() );
416             QAD_MessageBox::warn1(QAD_Application::getDesktop(), 
417                                   QObject::tr( "WRN_WARNING" ), 
418                                   msgw, 
419                                   QObject::tr( "BUT_OK" ));
420           }
421         }
422         // JFA 28.12.2004 END
423       }
424     }
425   }
426   catch( const SALOME::SALOME_Exception& e ) {
427     QtCatchCorbaException( e );
428     abortCommand();
429   }
430
431   updateViewer();
432
433   return true;
434 }