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