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