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