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