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