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