Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/geom.git] / src / GEOMBase / GEOMBase_Helper.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2004  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.org
21 //
22 //
23 //
24 //  File   : GEOMBase_Helper.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : GEOM
27 //  $Header$
28
29 #include <SUIT_ViewModel.h>
30
31 #include "GEOMBase_Helper.h"
32 #include "GEOMBase.h"
33 #include "GEOM_Operation.h"
34 #include "GeometryGUI.h"
35 #include "GEOM_Displayer.h"
36 #include "GEOMImpl_Types.hxx"
37
38 #include <SUIT_Session.h>
39 #include <SUIT_ViewWindow.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_OverrideCursor.h>
42
43 #include <SalomeApp_Module.h>
44 #include <SalomeApp_Application.h>
45 #include <SalomeApp_Study.h>
46 #include <LightApp_SelectionMgr.h>
47 #include <SalomeApp_Tools.h>
48 #include <SalomeApp_DataModel.h>
49 #include <SalomeApp_Module.h>
50
51 #include <OCCViewer_ViewModel.h>
52 #include <SVTK_ViewModel.h>
53
54 #include <OB_Browser.h>
55
56 #include <TColStd_MapOfInteger.hxx>
57 #include <TCollection_AsciiString.hxx>
58
59 using namespace std;
60
61 #include <SALOMEDSClient.hxx>
62
63
64 //================================================================
65 // Function : getActiveView
66 // Purpose  : Get active view window, returns 0 if no open study frame
67 //================================================================
68 static SUIT_ViewWindow* getActiveView()
69 {
70   SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
71   if ( activeStudy )
72     return SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
73
74   return 0;
75 }
76
77
78 //================================================================
79 // Function : getGeomEngine
80 // Purpose  : Static method
81 //================================================================
82 GEOM::GEOM_Gen_ptr GEOMBase_Helper::getGeomEngine()
83 {
84   return GeometryGUI::GetGeomGen();
85 }
86
87 //================================================================
88 // Function : GEOMBase_Helper
89 // Purpose  :
90 //================================================================
91 GEOMBase_Helper::GEOMBase_Helper( SUIT_Desktop* desktop )
92   : myDesktop( desktop ), myViewWindow( 0 ), myDisplayer( 0 ), myCommand( 0 ), isPreview( false )
93 {
94 }
95
96 //================================================================
97 // Function : ~GEOMBase_Helper
98 // Purpose  :
99 //================================================================
100 GEOMBase_Helper::~GEOMBase_Helper()
101 {
102   if ( !SUIT_Session::session()->activeApplication()->desktop() )
103     return;
104
105   if ( myPreview.size() )
106     erasePreview();
107   if ( hasCommand() )
108     abortCommand();
109
110   globalSelection( GEOM_ALLOBJECTS, true );
111
112   if (myDisplayer)
113     delete myDisplayer;
114 }
115
116 //================================================================
117 // Function : display
118 // Purpose  :
119 //================================================================
120 void GEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
121 {
122   ObjectList::const_iterator it;
123   for ( it = objList.begin(); it != objList.end(); it++ ) {
124     display( *it, false );
125   }
126   if ( !objList.empty() && updateView )
127     getDisplayer()->UpdateViewer();
128 }
129
130 //================================================================
131 // Function  : display
132 // Purpose   : Display object.
133 // Important : Object must be already in study
134 //================================================================
135 void GEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
136 {
137   // Unset color of shape ( this color may be set during preview displaying )
138   // Default color will be used
139   getDisplayer()->UnsetColor();
140   getDisplayer()->UnsetWidth();
141
142   // Enable activisation of selection
143   getDisplayer()->SetToActivate( true );
144
145   // Display object
146   getDisplayer()->Display( object, updateView );
147 }
148
149 //================================================================
150 // Function : erase
151 // Purpose  :
152 //================================================================
153 void GEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
154 {
155   ObjectList::const_iterator it = objList.begin();
156   for ( ; it != objList.end(); it++ ) {
157     erase( *it, false );
158   }
159   if ( !objList.empty() && updateView )
160     getDisplayer()->UpdateViewer();
161 }
162
163 //================================================================
164 // Function : erase
165 // Purpose  :
166 //================================================================
167 void GEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
168 {
169   if ( !object->_is_nil() ) {
170     string entry = getEntry( object );
171     getDisplayer()->Erase( new SALOME_InteractiveObject(
172       entry.c_str(), "GEOM", strdup( GEOMBase::GetName( object ) ) ), true, updateView );
173   }
174 }
175
176 //================================================================
177 // Function : redisplay
178 // Purpose  :
179 //================================================================
180 void GEOMBase_Helper::redisplay( const ObjectList& objList,
181                                  const bool withChildren,
182                                  const bool updateView )
183 {
184   ObjectList::const_iterator it = objList.begin();
185   for ( ; it != objList.end(); it++ ) {
186     redisplay( *it, withChildren, false );
187   }
188   if ( !objList.empty() && updateView )
189     getDisplayer()->UpdateViewer();
190 }
191
192 //================================================================
193 // Function : redisplay
194 // Purpose  :
195 //================================================================
196 void GEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
197                                  const bool withChildren,
198                                  const bool updateView )
199 {
200   if ( !object->_is_nil() ) {
201     // Unset color of shape ( this color may be set during preview displaying )
202     // Default color will be used
203     getDisplayer()->UnsetColor();
204     getDisplayer()->UnsetWidth();
205
206     // Enable activisation of selection
207     getDisplayer()->SetToActivate( true );
208
209     string entry = getEntry( object );
210     getDisplayer()->Redisplay(new SALOME_InteractiveObject
211                               (entry.c_str(), "GEOM", strdup(GEOMBase::GetName(object))), false);
212   }
213
214   if ( withChildren ) {
215     SalomeApp_Study* aDoc = getStudy();
216     if ( aDoc && aDoc->studyDS() ) {
217       _PTR(Study) aStudy = aDoc->studyDS();
218       _PTR(SObject) aSObj (aStudy->FindObjectIOR(SalomeApp_Application::orb()->object_to_string(object)));
219       if ( aSObj  ) {
220         _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
221         for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
222           GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow
223             (GeometryGUI::ClientSObjectToObject(anIt->Value()));
224           if ( !CORBA::is_nil( aChild ) ) {
225             if ( !aChild->_is_nil() ) {
226               string entry = getEntry( aChild );
227               getDisplayer()->Redisplay( new SALOME_InteractiveObject(
228                 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( aChild ) ) ), false );
229             }
230           }
231         }
232       }
233     }
234   }
235
236   if ( updateView )
237     getDisplayer()->UpdateViewer();
238 }
239
240 //================================================================
241 // Function : displayPreview
242 // Purpose  : Method for displaying preview based on execute() results
243 //================================================================
244 void GEOMBase_Helper::displayPreview( const bool   activate,
245                                       const bool   update,
246                                       const bool   toRemoveFromEngine,
247                                       const double lineWidth )
248 {
249   isPreview = true;
250   QString msg;
251   if ( !isValid( msg ) )
252   {
253     erasePreview( update );
254     isPreview = false;
255     return;
256   }
257
258   erasePreview( false );
259
260   try {
261     SUIT_OverrideCursor wc;
262     ObjectList objects;
263     if ( !execute( objects ) || !getOperation()->IsDone() ) {
264       wc.suspend();
265     }
266     else {
267       for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
268       {
269         displayPreview( *it, true, activate, false, lineWidth );
270         if ( toRemoveFromEngine )
271           getGeomEngine()->RemoveObject( *it );
272       }
273     }
274   }
275   catch( const SALOME::SALOME_Exception& e ) {
276     SalomeApp_Tools::QtCatchCorbaException( e );
277   }
278
279   isPreview = false;
280
281   if ( update )
282     updateViewer();
283 }
284
285 //================================================================
286 // Function : displayPreview
287 // Purpose  : Method for displaying preview of resulting shape
288 //================================================================
289 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
290                                       const bool            append,
291                                       const bool            activate,
292                                       const bool            update,
293                                       const double          lineWidth )
294 {
295   // Set color for preview shape
296   getDisplayer()->SetColor( Quantity_NOC_VIOLET );
297
298   // set width of displayed shape
299   getDisplayer()->SetWidth( lineWidth );
300
301   // Disable activation of selection
302   getDisplayer()->SetToActivate( activate );
303
304   // Make a reference to GEOM_Object
305   getDisplayer()->SetName( SalomeApp_Application::orb()->object_to_string( object ) );
306
307   // Build prs
308   SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
309   if ( aPrs == 0 || aPrs->IsNull() )
310     return;
311
312   // Display prs
313   displayPreview( aPrs, append, update );
314
315   getDisplayer()->UnsetName();
316
317   // Enable activation of displayed objects
318   getDisplayer()->SetToActivate( true );
319 }
320
321 //================================================================
322 // Function : displayPreview
323 // Purpose  : Method for displaying arbitrary preview objects (not limited to shapes)
324 //================================================================
325 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
326                                       const bool        append,
327                                       const bool        update )
328 {
329   if ( !append )
330     erasePreview( false );
331
332   // remember current view frame to make correct erase preview later
333   myViewWindow = getActiveView();
334
335   if ( myViewWindow == 0 )
336     return;
337
338   // Display prs
339   SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
340   if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
341        aViewManager->getType() == SVTK_Viewer::Type() )
342     {
343       SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
344       SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
345       if (aView)
346         aView->Display( prs );
347     }
348
349   // Add prs to the preview list
350   myPreview.push_back( (SALOME_Prs*)prs );
351
352   // Update viewer
353   if ( update )
354     getDisplayer()->UpdateViewer();
355 }
356
357 //================================================================
358 // Function : erasePreview
359 // Purpose  :
360 //================================================================
361 void GEOMBase_Helper::erasePreview( const bool update )
362 {
363   // check view frame where the preview was displayed
364   bool vfOK = checkViewWindow() && myViewWindow;
365   // Iterate through presentations and delete them
366   for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
367     if ( vfOK )
368       {
369          SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
370          if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
371               aViewManager->getType() == SVTK_Viewer::Type() )
372            {
373              SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
374              SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
375              if (aView)
376                aView->Erase( *anIter, true );
377            }
378       }
379     delete *anIter;
380   }
381   myPreview.clear();
382
383   // Update viewer
384   if ( update )
385     updateViewer();
386 }
387
388 //================================================================
389 // Function  : localSelection
390 // Purpose   : Activate selection of objects of a given type
391 // IMPORTANT : Works after localSelection( ... ) method call only
392 //================================================================
393 void GEOMBase_Helper::activate( const int theType )
394 {
395   if (!getStudy()) return;
396   _PTR(Study) aStudy = getStudy()->studyDS();
397   _PTR(SComponent) aGeom ( aStudy->FindComponent( "GEOM" ) );
398   if ( !aGeom )
399     return;
400
401   SALOME_ListIO aList;
402   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( aGeom ) );
403   for ( ; anIter->More(); anIter->Next() )
404   {
405     _PTR(SObject) aSO ( anIter->Value() );
406     if ( aSO )
407     {
408       _PTR(SObject) aRefSO;
409       if ( !aSO->ReferencedObject( aRefSO ) )
410       {
411         GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow
412           (GeometryGUI::ClientSObjectToObject(aSO));
413         if ( !anObj->_is_nil() && anObj->GetType() == theType )
414           aList.Append( new SALOME_InteractiveObject( aSO->GetID().c_str(), "GEOM", aSO->GetName().c_str()) );
415       }
416     }
417   }
418
419   getDisplayer()->LocalSelection( aList, 0 );
420 }
421
422 //================================================================
423 // Function : localSelection
424 // Purpose  : Activate selection of subshapes in accordance with mode
425 //            theMode is from TopAbs_ShapeEnum
426 //================================================================
427 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
428 {
429   SALOME_ListIO aListOfIO;
430
431   ObjectList::const_iterator anIter = theObjs.begin();
432   for ( ; anIter != theObjs.end(); ++anIter )
433   {
434     GEOM::GEOM_Object_ptr anObj = *anIter;
435     if ( anObj->_is_nil() )
436       continue;
437     string aEntry = getEntry( anObj );
438     if ( aEntry != "" )
439       aListOfIO.Append( new SALOME_InteractiveObject(
440         aEntry.c_str(), "GEOM", strdup( GEOMBase::GetName( anObj ) ) ) );
441   }
442
443   getDisplayer()->LocalSelection( aListOfIO, theMode );
444 }
445
446 //================================================================
447 // Function : localSelection
448 // Purpose  : Activate selection of subshapes in accordance with mode
449 //            theMode is from TopAbs_ShapeEnum
450 //================================================================
451 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
452 {
453   // If object is null local selection for all objects is activated
454   if ( obj->_is_nil() ) {
455     getDisplayer()->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
456     return;
457   }
458
459   ObjectList objList;
460   objList.push_back( obj );
461   localSelection( objList, mode );
462 }
463
464
465 //================================================================
466 // Function : globalSelection
467 // Purpose  : Activate selection of subshapes. Set selection filters
468 //            in accordance with mode. theMode is from GEOMImpl_Types
469 //================================================================
470 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
471 {
472   getDisplayer()->GlobalSelection( theMode, update );
473 }
474
475 //================================================================
476 // Function : globalSelection
477 // Purpose  : Activate selection of subshapes. Set selection filters
478 //            in accordance with mode. theMode is from GEOMImpl_Types
479 //================================================================
480 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
481                                        const bool update )
482 {
483   getDisplayer()->GlobalSelection( theModes, update );
484 }
485
486 //================================================================
487 // Function : addInStudy
488 // Purpose  : Add object in study
489 //================================================================
490 void GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
491 {
492   if ( !hasCommand() )
493     return;
494
495   _PTR(Study) aStudy = getStudy()->studyDS();
496   if ( !aStudy || theObj->_is_nil() )
497     return;
498
499   GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
500
501   getGeomEngine()->AddInStudy(GeometryGUI::ClientStudyToStudy(aStudy),
502                               theObj, theName, aFatherObj);
503 }
504
505 //================================================================
506 // Function : updateObjBrowser
507 // Purpose  : Update object browser
508 //================================================================
509 void GEOMBase_Helper::updateObjBrowser() const
510 {
511   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
512   if (app) {
513     CAM_Module* module = app->module( "Geometry" );
514     SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
515     if ( appMod ) {
516       appMod->updateObjBrowser( true );
517     }
518   }
519 }
520
521 //================================================================
522 // Function : updateViewer
523 // Purpose  : Update active 3D view
524 //================================================================
525 void GEOMBase_Helper::updateViewer()
526 {
527   getDisplayer()->UpdateViewer();
528 }
529
530 //================================================================
531 // Function : getStudyId
532 // Purpose  : Get study Id
533 //================================================================
534 int GEOMBase_Helper::getStudyId() const
535 {
536   int anId = -1;
537   if ( getStudy() )
538     anId = getStudy()->id();
539   return anId;
540 }
541
542 //================================================================
543 // Function : getStudy
544 // Purpose  : Returns the active study. It is recommended to use
545 //            this method instead of direct desktop->getActiveStudy() calls
546 //================================================================
547 SalomeApp_Study* GEOMBase_Helper::getStudy() const
548 {
549   SUIT_Desktop* aDesktop = getDesktop();
550   if (!aDesktop)
551     return 0;
552
553   QPtrList<SUIT_Application> anAppList = SUIT_Session::session()->applications();
554
555   SUIT_Application* anApp = 0;
556   for ( QPtrListIterator<SUIT_Application> it( anAppList ); it.current() ; ++it )
557     {
558       anApp = it.current();
559       if ( anApp->desktop() == aDesktop )
560         break;
561     }
562
563   return dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
564 }
565
566 //================================================================
567 // Function : getEntry
568 // Purpose  :
569 //================================================================
570 char* GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
571 {
572   SalomeApp_Study* study = getStudy();
573   if ( study )  {
574     string IOR = GEOMBase::GetIORFromObject( object);
575     if ( IOR != "" ) {
576       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
577       if ( SO ) {
578         return TCollection_AsciiString((char*)SO->GetID().c_str()).ToCString();
579       }
580     }
581   }
582   return "";
583 }
584
585 //================================================================
586 // Function : getDisplayer
587 // Purpose  :
588 //================================================================
589 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
590 {
591   if ( !myDisplayer )
592     myDisplayer = new GEOM_Displayer( getStudy() );
593   return myDisplayer;
594 }
595
596 //================================================================
597 // Function : clearShapeBuffer
598 // Purpose  :
599 //================================================================
600 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
601 {
602   if ( CORBA::is_nil( theObj ) )
603     return;
604
605   string IOR = SalomeApp_Application::orb()->object_to_string( theObj );
606   TCollection_AsciiString asciiIOR( strdup( IOR.c_str() ) );
607   GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
608
609   if ( !getStudy() || !getStudy()->studyDS() )
610     return;
611
612   _PTR(Study) aStudy = getStudy()->studyDS();
613   _PTR(SObject) aSObj ( aStudy->FindObjectIOR( IOR ) );
614   if ( !aSObj )
615     return;
616
617   _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
618   for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
619     _PTR(GenericAttribute) anAttr;
620     if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
621       _PTR(AttributeIOR) anIOR ( anAttr );
622       TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
623       GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
624     }
625   }
626 }
627
628 //================================================================
629 // Function : openCommand
630 // Purpose  :
631 //================================================================
632 bool GEOMBase_Helper::openCommand()
633 {
634   bool res = false;
635   if ( !getStudy() || hasCommand() )
636     return res;
637
638   GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
639   if ( !anOp->_is_nil() ) {
640     myCommand = new GEOM_Operation( SUIT_Session::session()->activeApplication(), anOp.in() );
641     myCommand->start();
642     res = true;
643   }
644
645   return res;
646 }
647
648 //================================================================
649 // Function : abortCommand
650 // Purpose  :
651 //================================================================
652 bool GEOMBase_Helper::abortCommand()
653 {
654   if ( !hasCommand() )
655     return false;
656
657   myCommand->abort();
658   myCommand = 0;
659
660   return true;
661 }
662
663 //================================================================
664 // Function : commitCommand
665 // Purpose  :
666 //================================================================
667 bool GEOMBase_Helper::commitCommand( const char* )
668 {
669   if ( !hasCommand() )
670     return false;
671
672   myCommand->commit();
673   myCommand = 0;
674
675   return true;
676 }
677
678 //================================================================
679 // Function : hasCommand
680 // Purpose  :
681 //================================================================
682 bool GEOMBase_Helper::hasCommand() const
683 {
684   return (bool)myCommand;
685 }
686
687 //================================================================
688 // Function : getOperation
689 // Purpose  :
690 //================================================================
691 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
692 {
693   if ( myOperation->_is_nil() )
694     myOperation = createOperation();
695
696   return myOperation;
697 }
698
699
700
701 //================================================================
702 // Function : checkViewWindow
703 // Purpose  :
704 //================================================================
705 bool GEOMBase_Helper::checkViewWindow()
706 {
707   if ( myViewWindow ){
708     QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
709     for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it )
710       {
711         if ( myViewWindow == it.current() )
712           return true;
713       }
714   }
715   myViewWindow = 0;
716   return false;
717 }
718
719 //================================================================
720 // Function : onAccept
721 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
722 //            It perfroms user input validation, then it
723 //            performs a proper operation and manages transactions, etc.
724 //================================================================
725 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
726 {
727   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
728   if ( !appStudy ) return false;
729   _PTR(Study) aStudy = appStudy->studyDS();
730
731   bool aLocked = (_PTR(AttributeStudyProperties) (aStudy->GetProperties()))->IsLocked();
732   if ( aLocked ) {
733     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
734     SUIT_MessageBox::warn1 ( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
735                            QObject::tr("WRN_WARNING"),
736                            QObject::tr("WRN_STUDY_LOCKED"),
737                            QObject::tr("BUT_OK") );
738     return false;
739   }
740
741   QString msg;
742   if ( !isValid( msg ) ) {
743     showError( msg );
744     return false;
745   }
746
747   erasePreview( false );
748
749   try {
750     if ( ( !publish && !useTransaction ) || openCommand() ) {
751       SUIT_OverrideCursor wc;
752       SUIT_Session::session()->activeApplication()->putInfo( "" );
753       ObjectList objects;
754       if ( !execute( objects ) || !getOperation()->IsDone() ) {
755         wc.suspend();
756         abortCommand();
757         showError();
758       }
759       else {
760         const int nbObjs = objects.size();
761         bool withChildren = false;
762         for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
763           if ( publish ) {
764             QString aName("");
765             if ( nbObjs > 1 )
766               aName = strlen( getNewObjectName() ) ? GEOMBase::GetDefaultName( getNewObjectName() ) : GEOMBase::GetDefaultName( getPrefix( *it ) );
767             else {
768               aName = getNewObjectName();
769               // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
770               if ( aName.isEmpty() )
771                 aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
772             }
773             addInStudy( *it, aName.latin1() );
774             withChildren = false;
775             display( *it, false );
776           }
777           else { // asv : fix of PAL6454. If publish==false, then the original shape was modified, and need to be re-cached in GEOM_Client
778                  // before redisplay
779             clearShapeBuffer( *it );
780             withChildren = true;
781             redisplay( *it, withChildren, false );
782           }
783         }
784
785         if ( nbObjs ) {
786           commitCommand();
787           updateObjBrowser();
788           SUIT_Session::session()->activeApplication()->putInfo( QObject::tr("GEOM_PRP_DONE") );
789         }
790         else
791           abortCommand();
792       }
793     }
794   }
795   catch( const SALOME::SALOME_Exception& e ) {
796     SalomeApp_Tools::QtCatchCorbaException( e );
797     abortCommand();
798   }
799
800   updateViewer();
801
802   return true;
803 }
804
805
806 //================================================================
807 // Function : showError
808 // Purpose  : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
809 //================================================================
810 void GEOMBase_Helper::showError()
811 {
812   QString msg;
813   if ( !getOperation()->_is_nil() )
814     msg = QObject::tr( getOperation()->GetErrorCode() );
815
816   if ( msg.isEmpty() )
817     msg = QObject::tr( "GEOM_PRP_ABORT" );
818
819   SUIT_MessageBox::error1( SUIT_Session::session()->activeApplication()->desktop(),
820                            QObject::tr( "GEOM_ERROR_STATUS" ),
821                            msg,
822                            QObject::tr( "BUT_OK" ) );
823 }
824
825 //================================================================
826 // Function : showError
827 // Purpose  : Shows a error message followed by <msg>
828 //================================================================
829 void GEOMBase_Helper::showError( const QString& msg )
830 {
831   QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
832   if ( !msg.isEmpty() )
833     str += "\n" + msg;
834   SUIT_MessageBox::error1(SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
835 }
836
837 //////////////////////////////////////////////////////////////////
838 // Virtual methods to be redefined in dialogs
839 //////////////////////////////////////////////////////////////////
840
841 //================================================================
842 // Function : createOperation
843 // Purpose  : Redefine this method to return proper IOperation reference
844 //================================================================
845 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
846 {
847   GEOM::GEOM_IOperations_var aNilOp;
848   return aNilOp._retn();
849 }
850
851 //================================================================
852 // Function : isValid
853 // Purpose  : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
854 //================================================================
855 bool GEOMBase_Helper::isValid( QString& )
856 {
857   return true;
858 }
859
860 //================================================================
861 // Function : execute
862 // Purpose  : This method is called by onAccept().
863 //            It should perform the required operation and put all new or modified objects into
864 //            <objects> argument.Should return <false> if some error occurs during its execution.
865 //================================================================
866 bool GEOMBase_Helper::execute( ObjectList& objects )
867 {
868   return false;
869 }
870
871 //================================================================
872 // Function : getFather
873 // Purpose  : This method is called by addInStudy(). It should return a father object
874 //            for <theObj> or a nil reference if <theObj> should be published
875 //            as a top-level object.
876 //================================================================
877 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
878 {
879   return GEOM::GEOM_Object::_nil();
880 }
881
882 //================================================================
883 // Function : getNewObjectName
884 // Purpose  : Redefine this method to return proper name for a new object
885 //================================================================
886 const char* GEOMBase_Helper::getNewObjectName() const
887 {
888   return "";
889 }
890
891 //================================================================
892 // Function : getPrefix
893 // Purpose  : Get prefix for name of created object
894 //================================================================
895 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
896 {
897   if ( !myPrefix.isEmpty() || theObj->_is_nil() )
898     return myPrefix;
899
900   TopoDS_Shape aShape;
901   if ( !GEOMBase::GetShape( theObj, aShape ) )
902     return "";
903
904   long aType = aShape.ShapeType();
905
906   switch ( aType )
907   {
908     case TopAbs_VERTEX   : return QObject::tr( "GEOM_VERTEX" );
909     case TopAbs_EDGE     : return QObject::tr( "GEOM_EDGE" );
910     case TopAbs_WIRE     : return QObject::tr( "GEOM_WIRE" );
911     case TopAbs_FACE     : return QObject::tr( "GEOM_FACE" );
912     case TopAbs_SHELL    : return QObject::tr( "GEOM_SHELL" );
913     case TopAbs_SOLID    : return QObject::tr( "GEOM_SOLID" );
914     case TopAbs_COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
915     case TopAbs_COMPOUND : return QObject::tr( "GEOM_COMPOUND" );
916     default : return "";
917   }
918 }
919
920 //================================================================
921 // Function : selectedIO
922 // Purpose  : Return the list of selected SALOME_InteractiveObject's
923 //================================================================
924 const SALOME_ListIO& GEOMBase_Helper::selectedIO()
925 {
926   mySelected.Clear();
927
928   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
929   if ( app ) {
930     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
931     if ( aSelMgr )
932       aSelMgr->selectedObjects( mySelected );
933   }
934
935   return mySelected;
936 }
937
938 //================================================================
939 // Function : IObjectCount
940 // Purpose  : Return the number of selected objects
941 //================================================================
942 int GEOMBase_Helper::IObjectCount()
943 {
944   return selectedIO().Extent();
945 }
946
947 //================================================================
948 // Function : firstIObject
949 // Purpose  :  Return the first selected object in the selected object list
950 //================================================================
951 Handle(SALOME_InteractiveObject) GEOMBase_Helper::firstIObject()
952 {
953   const SALOME_ListIO& aList = selectedIO();
954   return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
955 }
956
957 //================================================================
958 // Function : lastIObject
959 // Purpose  : Return the last selected object in the selected object list
960 //================================================================
961 Handle(SALOME_InteractiveObject) GEOMBase_Helper::lastIObject()
962 {
963   const SALOME_ListIO& aList = selectedIO();
964   return aList.Extent() > 0 ? aList.Last() : Handle(SALOME_InteractiveObject)();
965 }
966
967 //================================================================
968 // Function : getDesktop
969 // Purpose  : Returns myDesktop field.  Initialized in constructor, usually as dynamic_cast<SUIT_Desktop*>(parentWidget())
970 //================================================================
971 SUIT_Desktop* GEOMBase_Helper::getDesktop() const
972 {
973   return myDesktop;
974 }
975