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