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