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