Back to index

salome-geom  6.5.0
GEOMBase_Helper.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  GEOM GEOMGUI : GUI for Geometry component
00024 //  File   : GEOMBase_Helper.cxx
00025 //  Author : Sergey ANIKIN, Open CASCADE S.A.S. (sergey.anikin@opencascade.com)
00026 //
00027 #include "GEOMBase_Helper.h"
00028 #include "GEOMBase.h"
00029 #include "GEOM_Operation.h"
00030 
00031 #include <GeometryGUI.h>
00032 
00033 #include <SUIT_Desktop.h>
00034 #include <SUIT_Session.h>
00035 #include <SUIT_ViewManager.h>
00036 #include <SUIT_ViewWindow.h>
00037 #include <SUIT_ViewModel.h>
00038 #include <SUIT_MessageBox.h>
00039 #include <SUIT_OverrideCursor.h>
00040 #include <SUIT_ResourceMgr.h>
00041 
00042 #include <SalomeApp_Module.h>
00043 #include <SalomeApp_Application.h>
00044 #include <SalomeApp_Study.h>
00045 #include <LightApp_SelectionMgr.h>
00046 #include <LightApp_DataOwner.h>
00047 #include <SalomeApp_Tools.h>
00048 #include <SALOME_ListIteratorOfListIO.hxx>
00049 
00050 #include <SALOME_Prs.h>
00051 
00052 #include <OCCViewer_ViewModel.h>
00053 #include <SVTK_ViewModel.h>
00054 
00055 #include <TColStd_MapOfInteger.hxx>
00056 #include <TCollection_AsciiString.hxx>
00057 #include <TColStd_IndexedMapOfInteger.hxx>
00058 
00059 //To disable automatic genericobj management, the following line should be commented.
00060 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
00061 #define WITHGENERICOBJ
00062 
00063 //================================================================
00064 // Function : getActiveView
00065 // Purpose  : Get active view window, returns 0 if no open study frame
00066 //================================================================
00067 static SUIT_ViewWindow* getActiveView()
00068 {
00069   SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
00070   if ( activeStudy )
00071     return SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
00072 
00073   return 0;
00074 }
00075 
00076 
00077 //================================================================
00078 // Function : getGeomEngine
00079 // Purpose  : Static method
00080 //================================================================
00081 GEOM::GEOM_Gen_ptr GEOMBase_Helper::getGeomEngine()
00082 {
00083   return GeometryGUI::GetGeomGen();
00084 }
00085 
00086 //================================================================
00087 // Function : GEOMBase_Helper
00088 // Purpose  :
00089 //================================================================
00090 GEOMBase_Helper::GEOMBase_Helper( SUIT_Desktop* desktop )
00091   : myDesktop( desktop ), myViewWindow( 0 ), myDisplayer( 0 ), myCommand( 0 ), isPreview( false ),
00092     myIsApplyAndClose( false ), myIsOptimizedBrowsing( false ), myIsWaitCursorEnabled( true ),
00093     myIsDisableBrowsing(false)
00094 {
00095 }
00096 
00097 //================================================================
00098 // Function : ~GEOMBase_Helper
00099 // Purpose  :
00100 //================================================================
00101 GEOMBase_Helper::~GEOMBase_Helper()
00102 {
00103   //rnv: Fix for the "IPAL21922 : WinTC5.1.4: incorrect quit salome"
00104   if ( !SUIT_Session::session()->activeApplication() || !SUIT_Session::session()->activeApplication()->desktop() )
00105     return;
00106 
00107   if ( myPreview.size() )
00108     erasePreview();
00109   if ( hasCommand() )
00110     abortCommand();
00111   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
00112   if (app) {
00113     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
00114     if(aGeomGUI)
00115       globalSelection(aGeomGUI->getLocalSelectionMode() , true );
00116   }
00117 
00118   if (myDisplayer)
00119     delete myDisplayer;
00120   if ( !CORBA::is_nil( myOperation ) )
00121     myOperation->UnRegister();
00122 }
00123 
00124 //================================================================
00125 // Function : display
00126 // Purpose  :
00127 //================================================================
00128 void GEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
00129 {
00130   ObjectList::const_iterator it;
00131   for ( it = objList.begin(); it != objList.end(); it++ ) {
00132     display( *it, false );
00133   }
00134   if ( !objList.empty() && updateView )
00135     getDisplayer()->UpdateViewer();
00136 }
00137 
00138 //================================================================
00139 // Function  : display
00140 // Purpose   : Display object.
00141 // Important : Object must be already in study
00142 //================================================================
00143 void GEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
00144 {
00145   // Unset color of shape ( this color may be set during preview displaying )
00146   // Default color will be used
00147 //   getDisplayer()->UnsetColor();
00148   getDisplayer()->UnsetWidth();
00149   
00150   MESSAGE("GEOMBase_Helper::display myTexture = "<<getDisplayer()->GetTexture())
00151 
00152   // Enable activisation of selection
00153   getDisplayer()->SetToActivate( true );
00154 
00155   // Display object
00156   getDisplayer()->Display( object, updateView );
00157 }
00158 
00159 //================================================================
00160 // Function : erase
00161 // Purpose  :
00162 //================================================================
00163 void GEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
00164 {
00165   ObjectList::const_iterator it = objList.begin();
00166   for ( ; it != objList.end(); it++ ) {
00167     erase( *it, false );
00168   }
00169   if ( !objList.empty() && updateView )
00170     getDisplayer()->UpdateViewer();
00171 }
00172 
00173 //================================================================
00174 // Function : erase
00175 // Purpose  :
00176 //================================================================
00177 void GEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
00178 {
00179   if ( !object->_is_nil() ) {
00180     QString entry = getEntry( object );
00181     getDisplayer()->Erase( new SALOME_InteractiveObject(
00182       entry.toLatin1().constData(), 
00183       "GEOM", strdup( GEOMBase::GetName( object ).toLatin1().constData() ) ), true, updateView );
00184   }
00185 }
00186 
00187 //================================================================
00188 // Function : redisplay
00189 // Purpose  :
00190 //================================================================
00191 void GEOMBase_Helper::redisplay( const ObjectList& objList,
00192                                  const bool withChildren,
00193                                  const bool updateView )
00194 {
00195   ObjectList::const_iterator it = objList.begin();
00196   for ( ; it != objList.end(); it++ ) {
00197     redisplay( *it, withChildren, false );
00198   }
00199   if ( !objList.empty() && updateView )
00200     getDisplayer()->UpdateViewer();
00201 }
00202 
00203 //================================================================
00204 // Function : redisplay
00205 // Purpose  :
00206 //================================================================
00207 void GEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
00208                                  const bool withChildren,
00209                                  const bool updateView )
00210 {
00211   if ( !object->_is_nil() ) {
00212     // Unset color of shape ( this color may be set during preview displaying )
00213     // Default color will be used
00214     getDisplayer()->UnsetColor();
00215     getDisplayer()->UnsetWidth();
00216 
00217     // Enable activisation of selection
00218     getDisplayer()->SetToActivate( true );
00219 
00220     QString entry = getEntry( object );
00221     getDisplayer()->Redisplay(new SALOME_InteractiveObject
00222                               (entry.toLatin1().constData(), "GEOM", strdup(GEOMBase::GetName(object).toLatin1().constData())), false);
00223   }
00224 
00225   if ( withChildren ) {
00226     SalomeApp_Study* aDoc = getStudy();
00227     if ( aDoc && aDoc->studyDS() ) {
00228       _PTR(Study) aStudy = aDoc->studyDS();
00229       CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(object);
00230       _PTR(SObject) aSObj (aStudy->FindObjectIOR(std::string(objStr.in())));
00231       if ( aSObj  ) {
00232         _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
00233         for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
00234           GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow
00235             (GeometryGUI::ClientSObjectToObject(anIt->Value()));
00236           if ( !CORBA::is_nil( aChild ) ) {
00237             if ( !aChild->_is_nil() ) {
00238               QString entry = getEntry( aChild );
00239               getDisplayer()->Redisplay( new SALOME_InteractiveObject(
00240                 entry.toLatin1().constData(), "GEOM", strdup( GEOMBase::GetName( aChild ).toLatin1().constData() ) ), false );
00241             }
00242           }
00243         }
00244       }
00245     }
00246   }
00247 
00248   if ( updateView )
00249     getDisplayer()->UpdateViewer();
00250 }
00251 
00252 //================================================================
00253 // Function : displayPreview
00254 // Purpose  : Method for displaying preview based on execute() results
00255 //================================================================
00256 void GEOMBase_Helper::displayPreview( const bool   display,
00257                                       const bool   activate,
00258                                       const bool   update,
00259                                       const bool   toRemoveFromEngine,
00260                                       const double lineWidth,
00261                                       const int    displayMode,
00262                                       const int    color )
00263 {
00264   if(!display) {
00265     erasePreview( update );
00266     return;
00267   }
00268   
00269   isPreview = true;
00270   QString msg;
00271   if ( !isValid( msg ) )
00272   {
00273     erasePreview( update );
00274     isPreview = false;
00275     return;
00276   }
00277 
00278   erasePreview( false );
00279 
00280   try {
00281     SUIT_OverrideCursor wc;
00282     ObjectList objects;
00283  
00284     if ( !isWaitCursorEnabled() )
00285       wc.suspend();
00286     
00287     if ( !execute( objects ) || !getOperation()->IsDone() ) {
00288       wc.suspend();
00289     }
00290     else {
00291       for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
00292       {
00293             GEOM::GEOM_Object_var obj=*it;
00294         displayPreview( obj, true, activate, false, lineWidth, displayMode, color );
00295         if ( toRemoveFromEngine )
00296               obj->UnRegister();
00297       }
00298     }
00299   }
00300   catch( const SALOME::SALOME_Exception& e ) {
00301     SalomeApp_Tools::QtCatchCorbaException( e );
00302   }
00303 
00304   isPreview = false;
00305 
00306   if ( update )
00307     updateViewer();
00308 }
00309 
00310 //================================================================
00311 // Function : displayPreview
00312 // Purpose  : Method for displaying preview of resulting shape
00313 //================================================================
00314 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
00315                                       const bool            append,
00316                                       const bool            activate,
00317                                       const bool            update,
00318                                       const double          lineWidth,
00319                                       const int             displayMode,
00320                                       const int             color )
00321 {
00322   // Set color for preview shape
00323   getDisplayer()->SetColor( color == -1 ? Quantity_NOC_VIOLET : color );
00324 
00325   // set width of displayed shape
00326   int lw = lineWidth;
00327   if(lw == -1) {
00328     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();    
00329     lw = resMgr->integerValue("Geometry", "preview_edge_width", -1);
00330   }
00331   getDisplayer()->SetWidth( lw );
00332 
00333   // set display mode of displayed shape
00334   int aPrevDispMode = getDisplayer()->SetDisplayMode( displayMode );
00335 
00336   // Disable activation of selection
00337   getDisplayer()->SetToActivate( activate );
00338 
00339   // Make a reference to GEOM_Object
00340   CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string( object );
00341   getDisplayer()->SetName( objStr.in() );
00342 
00343   // Build prs
00344   SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
00345   if ( aPrs == 0 || aPrs->IsNull() )
00346     return;
00347 
00348   // Display prs
00349   displayPreview( aPrs, append, update );
00350 
00351   getDisplayer()->UnsetName();
00352   getDisplayer()->UnsetColor();
00353   getDisplayer()->SetDisplayMode( aPrevDispMode );
00354 
00355   // Enable activation of displayed objects
00356   getDisplayer()->SetToActivate( true );
00357 }
00358 
00359 //================================================================
00360 // Function : displayPreview
00361 // Purpose  : Method for displaying arbitrary preview objects (not limited to shapes)
00362 //================================================================
00363 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
00364                                       const bool        append,
00365                                       const bool        update )
00366 {
00367   if ( !append )
00368     erasePreview( false );
00369 
00370   // remember current view frame to make correct erase preview later
00371   myViewWindow = getActiveView();
00372 
00373   if ( myViewWindow == 0 )
00374     return;
00375   
00376   // Display prs
00377   SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
00378   if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
00379        aViewManager->getType() == SVTK_Viewer::Type() )
00380     {
00381       SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
00382       SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
00383       if (aView)
00384         aView->Display( prs );
00385     }
00386 
00387   // Add prs to the preview list
00388   myPreview.push_back( (SALOME_Prs*)prs );
00389 
00390   // Update viewer
00391   if ( update )
00392     getDisplayer()->UpdateViewer();
00393 }
00394 
00395 //================================================================
00396 // Function : erasePreview
00397 // Purpose  :
00398 //================================================================
00399 void GEOMBase_Helper::erasePreview( const bool update )
00400 {
00401   // check view frame where the preview was displayed
00402   bool vfOK = checkViewWindow() && myViewWindow;
00403   // Iterate through presentations and delete them
00404   for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
00405     if ( vfOK )
00406       {
00407          SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
00408          if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
00409               aViewManager->getType() == SVTK_Viewer::Type() )
00410            {
00411              SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
00412              SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
00413              if (aView)
00414                aView->Erase( *anIter, true );
00415            }
00416       }
00417     delete *anIter;
00418   }
00419   myPreview.clear();
00420 
00421   // Update viewer
00422   if ( update )
00423     updateViewer();
00424 }
00425 
00426 //================================================================
00427 // Function  : localSelection
00428 // Purpose   : Activate selection of objects of a given type
00429 // IMPORTANT : Works after localSelection( ... ) method call only
00430 //================================================================
00431 void GEOMBase_Helper::activate( const int theType )
00432 {
00433   if (!getStudy()) return;
00434   _PTR(Study) aStudy = getStudy()->studyDS();
00435   _PTR(SComponent) aGeom ( aStudy->FindComponent( "GEOM" ) );
00436   if ( !aGeom )
00437     return;
00438 
00439   SALOME_ListIO aList;
00440   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( aGeom ) );
00441   for ( ; anIter->More(); anIter->Next() )
00442   {
00443     _PTR(SObject) aSO ( anIter->Value() );
00444     if ( aSO )
00445     {
00446       _PTR(SObject) aRefSO;
00447       if ( !aSO->ReferencedObject( aRefSO ) )
00448       {
00449         GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow
00450           (GeometryGUI::ClientSObjectToObject(aSO));
00451         if ( !anObj->_is_nil() && anObj->GetType() == theType )
00452           aList.Append( new SALOME_InteractiveObject( aSO->GetID().c_str(), "GEOM", aSO->GetName().c_str()) );
00453       }
00454     }
00455   }
00456 
00457   getDisplayer()->LocalSelection( aList, 0 );
00458 }
00459 
00460 //================================================================
00461 // Function : localSelection
00462 // Purpose  : Activate selection of sub-shapes in accordance with mode
00463 //            theMode is from TopAbs_ShapeEnum
00464 //================================================================
00465 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
00466 {
00467   SALOME_ListIO aListOfIO;
00468 
00469   ObjectList::const_iterator anIter = theObjs.begin();
00470   for ( ; anIter != theObjs.end(); ++anIter )
00471   {
00472     GEOM::GEOM_Object_ptr anObj = *anIter;
00473     if ( anObj->_is_nil() )
00474       continue;
00475     QString anEntry = getEntry( anObj );
00476     if ( anEntry != "" )
00477       aListOfIO.Append( new SALOME_InteractiveObject(
00478         anEntry.toLatin1().constData(), "GEOM", strdup( GEOMBase::GetName( anObj ).toLatin1().constData() ) ) );
00479   }
00480 
00481   getDisplayer()->LocalSelection( aListOfIO, theMode );
00482 }
00483 
00484 //================================================================
00485 // Function : localSelection
00486 // Purpose  : Activate selection of sub-shapes in accordance with mode
00487 //            theMode is from TopAbs_ShapeEnum
00488 //================================================================
00489 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
00490 {
00491   // If object is null local selection for all objects is activated
00492   if ( obj->_is_nil() ) {
00493     getDisplayer()->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
00494     return;
00495   }
00496 
00497   ObjectList objList;
00498   objList.push_back( obj );
00499   localSelection( objList, mode );
00500 }
00501 
00502 
00503 //================================================================
00504 // Function : globalSelection
00505 // Purpose  : Activate selection of sub-shapes. Set selection filters
00506 //            in accordance with mode. theMode is from GEOMImpl_Types
00507 //================================================================
00508 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
00509 {
00510   getDisplayer()->GlobalSelection( theMode, update );
00511 }
00512 
00513 //================================================================
00514 // Function : globalSelection
00515 // Purpose  : Activate selection of sub-shapes. Set selection filters
00516 //            in accordance with mode. theMode is from GEOMImpl_Types
00517 //================================================================
00518 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
00519                                        const bool update )
00520 {
00521   getDisplayer()->GlobalSelection( theModes, update );
00522 }
00523 
00524 //================================================================
00525 // Function : globalSelection
00526 // Purpose  : Activate selection of sub-shapes. Set selection filters
00527 //            in accordance with mode. theMode is from GEOMImpl_Types
00528 //================================================================
00529 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
00530                                        const QList<int>& subShapes,
00531                                        const bool update )
00532 {
00533   getDisplayer()->GlobalSelection( theModes, update, &subShapes );
00534 }
00535 
00536 //================================================================
00537 // Function : addInStudy
00538 // Purpose  : Add object in study
00539 //================================================================
00540 QString GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
00541 {
00542   if ( !hasCommand() )
00543     return QString();
00544 
00545   _PTR(Study) aStudy = getStudy()->studyDS();
00546   if ( !aStudy || theObj->_is_nil() )
00547     return QString();
00548 
00549   SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy(aStudy);
00550 
00551   GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
00552 
00553   SALOMEDS::SObject_var aSO =
00554     getGeomEngine()->AddInStudy(aStudyDS, theObj, theName, aFatherObj);
00555 
00556   QString anEntry;
00557   if ( !aSO->_is_nil() )
00558     anEntry = aSO->GetID();
00559 
00560   // Each dialog is responsible for this method implementation,
00561   // default implementation does nothing
00562   restoreSubShapes(aStudyDS, aSO);
00563   aSO->UnRegister();
00564 
00565   return anEntry;
00566 }
00567 
00568 //================================================================
00569 // Function : restoreSubShapes
00570 // Purpose  : restore tree of argument's sub-shapes under the resulting shape
00571 //================================================================
00572 void GEOMBase_Helper::restoreSubShapes (SALOMEDS::Study_ptr   /*theStudy*/,
00573                                         SALOMEDS::SObject_ptr /*theSObject*/)
00574 {
00575   // do nothing by default
00576 
00577   // example of implementation in particular dialog:
00578   // GEOM::ListOfGO anArgs;
00579   // anArgs.length(0); // empty list means that all arguments should be restored
00580   // getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, anArgs,
00581   //                                     /*theFindMethod=*/GEOM::FSM_GetInPlace,
00582   //                                     /*theInheritFirstArg=*/false);
00583 }
00584 
00585 //================================================================
00586 // Function : updateObjBrowser
00587 // Purpose  : Update object browser
00588 //================================================================
00589 void GEOMBase_Helper::updateObjBrowser() const
00590 {
00591   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
00592   if (app) {
00593     CAM_Module* module = app->module( "Geometry" );
00594     SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
00595     if ( appMod ) {
00596       appMod->updateObjBrowser( true );
00597     }
00598   }
00599 }
00600 
00601 //================================================================
00602 // Function : updateViewer
00603 // Purpose  : Update active 3D view
00604 //================================================================
00605 void GEOMBase_Helper::updateViewer()
00606 {
00607   getDisplayer()->UpdateViewer();
00608 }
00609 
00610 //================================================================
00611 // Function : getStudyId
00612 // Purpose  : Get study Id
00613 //================================================================
00614 int GEOMBase_Helper::getStudyId() const
00615 {
00616   int anId = -1;
00617   if ( getStudy() )
00618     anId = getStudy()->id();
00619   return anId;
00620 }
00621 
00622 //================================================================
00623 // Function : getStudy
00624 // Purpose  : Returns the active study. It is recommended to use
00625 //            this method instead of direct desktop->getActiveStudy() calls
00626 //================================================================
00627 SalomeApp_Study* GEOMBase_Helper::getStudy() const
00628 {
00629   SUIT_Desktop* aDesktop = getDesktop();
00630   if (!aDesktop)
00631     return 0;
00632 
00633   QList<SUIT_Application*> anAppList = SUIT_Session::session()->applications();
00634 
00635   SUIT_Application* anApp = 0;
00636   QListIterator<SUIT_Application*> it( anAppList );
00637   while ( it.hasNext() )
00638     {
00639       anApp = it.next();
00640       if ( anApp && anApp->desktop() == aDesktop )
00641         break;
00642     }
00643 
00644   return dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
00645 }
00646 
00647 //================================================================
00648 // Function : getEntry
00649 // Purpose  :
00650 //================================================================
00651 QString GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
00652 {
00653   SalomeApp_Study* study = getStudy();
00654   if ( study )  {
00655     QString objIOR = GEOMBase::GetIORFromObject( object );
00656     if ( objIOR != "" ) {
00657       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( objIOR.toLatin1().constData() ) );
00658       if ( SO )
00659         return QString::fromStdString(SO->GetID());
00660     }
00661   }
00662   return "";
00663 }
00664 
00665 //================================================================
00666 // Function : getDisplayer
00667 // Purpose  :
00668 //================================================================
00669 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
00670 {
00671   if ( !myDisplayer )
00672     myDisplayer = new GEOM_Displayer( getStudy() );
00673   return myDisplayer;
00674 }
00675 
00676 //================================================================
00677 // Function : clearShapeBuffer
00678 // Purpose  :
00679 //================================================================
00680 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
00681 {
00682   if ( CORBA::is_nil( theObj ) )
00683     return;
00684 
00685   CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( theObj );
00686   TCollection_AsciiString asciiIOR( (char *)IOR.in() );
00687   GEOM_Client::get_client().RemoveShapeFromBuffer( asciiIOR );
00688 
00689   if ( !getStudy() || !getStudy()->studyDS() )
00690     return;
00691 
00692   _PTR(Study) aStudy = getStudy()->studyDS();
00693   _PTR(SObject) aSObj ( aStudy->FindObjectIOR( std::string( IOR ) ) );
00694   if ( !aSObj )
00695     return;
00696 
00697   _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
00698   for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
00699     _PTR(GenericAttribute) anAttr;
00700     if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
00701       _PTR(AttributeIOR) anIOR ( anAttr );
00702       TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
00703       GEOM_Client::get_client().RemoveShapeFromBuffer( asciiIOR );
00704     }
00705   }
00706 }
00707 
00708 //================================================================
00709 // Function : openCommand
00710 // Purpose  :
00711 //================================================================
00712 bool GEOMBase_Helper::openCommand()
00713 {
00714   bool res = false;
00715   if ( !getStudy() || hasCommand() )
00716   {
00717     MESSAGE("Getting out from openCommand()")
00718     return res;
00719   }
00720 
00721   GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
00722   if ( !anOp->_is_nil() ) {
00723     myCommand = new GEOM_Operation( SUIT_Session::session()->activeApplication(), anOp.in() );
00724     myCommand->start();
00725     res = true;
00726   }
00727   else
00728   {
00729     MESSAGE("anOp->_is_nil() = true")
00730   }
00731 
00732   return res;
00733 }
00734 
00735 //================================================================
00736 // Function : abortCommand
00737 // Purpose  :
00738 //================================================================
00739 bool GEOMBase_Helper::abortCommand()
00740 {
00741   if ( !hasCommand() )
00742     return false;
00743 
00744   myCommand->abort();
00745   delete myCommand; // I don't know where to delete this object here ?
00746   myCommand = 0;
00747 
00748   return true;
00749 }
00750 
00751 //================================================================
00752 // Function : commitCommand
00753 // Purpose  :
00754 //================================================================
00755 bool GEOMBase_Helper::commitCommand( const char* )
00756 {
00757   if ( !hasCommand() )
00758     return false;
00759 
00760   myCommand->commit();
00761   delete myCommand; // I don't know where to delete this object here ?
00762   myCommand = 0;
00763 
00764   return true;
00765 }
00766 
00767 //================================================================
00768 // Function : hasCommand
00769 // Purpose  :
00770 //================================================================
00771 bool GEOMBase_Helper::hasCommand() const
00772 {
00773   bool res = (bool) myCommand;
00774   return (bool)myCommand;
00775 }
00776 
00777 //================================================================
00778 // Function : getOperation
00779 // Purpose  :
00780 //================================================================
00781 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
00782 {
00783   if ( myOperation->_is_nil() )
00784     myOperation = createOperation();
00785 
00786   return myOperation;
00787 }
00788 
00789 
00790 
00791 //================================================================
00792 // Function : checkViewWindow
00793 // Purpose  :
00794 //================================================================
00795 bool GEOMBase_Helper::checkViewWindow()
00796 {
00797   if ( myViewWindow ){
00798     QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
00799     QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
00800     while ( it.hasNext() )
00801       {
00802         if ( myViewWindow == it.next() )
00803           return true;
00804       }
00805   }
00806   myViewWindow = 0;
00807   return false;
00808 }
00809 
00810 //================================================================
00811 // Function : onAccept
00812 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
00813 //            It perfroms user input validation, then it
00814 //            performs a proper operation and manages transactions, etc.
00815 //================================================================
00816 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction, bool erasePreviewFlag )
00817 {
00818   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00819   if ( !appStudy ) 
00820   {
00821     MESSAGE("appStudy is empty")
00822     return false;
00823   }
00824   _PTR(Study) aStudy = appStudy->studyDS();
00825 
00826   bool aLocked = (_PTR(AttributeStudyProperties) (aStudy->GetProperties()))->IsLocked();
00827   if ( aLocked ) {
00828     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
00829     SUIT_MessageBox::warning ( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
00830                                QObject::tr("WRN_WARNING"),
00831                                QObject::tr("WRN_STUDY_LOCKED"),
00832                                QObject::tr("BUT_OK") );
00833     return false;
00834   }
00835 
00836   QString msg;
00837   if ( !isValid( msg ) ) {
00838     showError( msg );
00839     return false;
00840   }
00841   
00842   if(erasePreviewFlag)
00843     erasePreview( false );
00844 
00845   bool result = false;
00846 
00847   try {
00848     if ( ( !publish && !useTransaction ) || openCommand() ) {
00849       SUIT_OverrideCursor wc;
00850       SUIT_Session::session()->activeApplication()->putInfo( "" );
00851       ObjectList objects;
00852       if ( !execute( objects ) || !getOperation()->IsDone() ) {
00853         wc.suspend();
00854         abortCommand();
00855         showError();
00856       }
00857       else {
00858         addSubshapesToStudy(); // add Sub-shapes if local selection
00859         const int nbObjs = objects.size();
00860         QStringList anEntryList;
00861         int aNumber = 1;
00862         for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
00863           GEOM::GEOM_Object_var obj=*it;
00864           if ( publish ) {
00865             QString aName = getNewObjectName();
00866             if ( nbObjs > 1 ) {
00867               if (aName.isEmpty())
00868                 aName = getPrefix(obj);
00869               if (nbObjs <= 30) {
00870                 // Try to find a unique name
00871                 aName = GEOMBase::GetDefaultName(aName, extractPrefix());
00872               } else {
00873                 // Don't check name uniqueness in case of numerous objects
00874                 aName = aName + "_" + QString::number(aNumber++);
00875               }
00876             } else {
00877               // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
00878               if ( aName.isEmpty() )
00879                 aName = GEOMBase::GetDefaultName( getPrefix( obj ) );
00880             }
00881             anEntryList << addInStudy( obj, aName.toLatin1().constData() );
00882             // updateView=false
00883             display( obj, false );
00884 #ifdef WITHGENERICOBJ
00885             // obj has been published in study. Its refcount has been incremented.
00886             // It is safe to decrement its refcount
00887             // so that it will be destroyed when the entry in study will be removed
00888             obj->UnRegister();
00889 #endif
00890           }
00891           else {
00892             // asv : fix of PAL6454. If publish==false, then the original shape
00893             // was modified, and need to be re-cached in GEOM_Client before redisplay
00894             clearShapeBuffer( obj );
00895             // withChildren=true, updateView=false
00896             redisplay( obj, true, false );
00897           }
00898         }
00899 
00900         if ( nbObjs ) {
00901           commitCommand();
00902           updateObjBrowser();
00903           if( SUIT_Application* anApp = SUIT_Session::session()->activeApplication() ) {
00904             LightApp_Application* aLightApp = dynamic_cast<LightApp_Application*>( anApp );
00905             if(aLightApp && !isDisableBrowsing() )
00906               aLightApp->browseObjects( anEntryList, isApplyAndClose(), isOptimizedBrowsing() );
00907             anApp->putInfo( QObject::tr("GEOM_PRP_DONE") );
00908           }
00909           result = true;
00910         }
00911         else
00912           abortCommand();
00913       }
00914     }
00915   }
00916   catch( const SALOME::SALOME_Exception& e ) {
00917     SalomeApp_Tools::QtCatchCorbaException( e );
00918     abortCommand();
00919     MESSAGE("Exception catched")
00920   }
00921 
00922   updateViewer();
00923 
00924   MESSAGE("result ="<<result)
00925   return result;
00926 }
00927 
00928 
00929 //================================================================
00930 // Function : showError
00931 // Purpose  : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
00932 //================================================================
00933 void GEOMBase_Helper::showError()
00934 {
00935   QString msg;
00936   if ( !getOperation()->_is_nil() )
00937     msg = QObject::tr( getOperation()->GetErrorCode() );
00938 
00939   if ( msg.isEmpty() )
00940     msg = QObject::tr( "GEOM_PRP_ABORT" );
00941 
00942   SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
00943                              QObject::tr( "GEOM_ERROR_STATUS" ),
00944                              msg,
00945                              QObject::tr( "BUT_OK" ) );
00946 }
00947 
00948 //================================================================
00949 // Function : showError
00950 // Purpose  : Shows a error message followed by <msg>
00951 //================================================================
00952 void GEOMBase_Helper::showError( const QString& msg )
00953 {
00954   QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
00955   if ( !msg.isEmpty() )
00956     str += "\n" + msg;
00957   SUIT_MessageBox::critical(SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
00958 }
00959 
00961 // Virtual methods to be redefined in dialogs
00963 
00964 //================================================================
00965 // Function : createOperation
00966 // Purpose  : Redefine this method to return proper IOperation reference
00967 //================================================================
00968 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
00969 {
00970   GEOM::GEOM_IOperations_var aNilOp;
00971   return aNilOp._retn();
00972 }
00973 
00974 //================================================================
00975 // Function : isValid
00976 // Purpose  : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
00977 //================================================================
00978 bool GEOMBase_Helper::isValid( QString& )
00979 {
00980   return true;
00981 }
00982 
00983 //================================================================
00984 // Function : execute
00985 // Purpose  : This method is called by onAccept().
00986 //            It should perform the required operation and put all new or modified objects into
00987 //            <objects> argument.Should return <false> if some error occurs during its execution.
00988 //================================================================
00989 bool GEOMBase_Helper::execute( ObjectList& objects )
00990 {
00991   return false;
00992 }
00993 
00994 //================================================================
00995 // Function : getFather
00996 // Purpose  : This method is called by addInStudy(). It should return a father object
00997 //            for <theObj> or a nil reference if <theObj> should be published
00998 //            as a top-level object.
00999 //================================================================
01000 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
01001 {
01002   return GEOM::GEOM_Object::_nil();
01003 }
01004 
01005 //================================================================
01006 // Function : getNewObjectName
01007 // Purpose  : Redefine this method to return proper name for a new object
01008 //================================================================
01009 QString GEOMBase_Helper::getNewObjectName() const
01010 {
01011   return QString::null;
01012 }
01013 
01014 //================================================================
01015 // Function : extractPrefix
01016 // Purpose  : Redefine this method to return \c true if necessary
01017 //            to extract prefix when generating new name for the
01018 //            object(s) being created
01019 //================================================================
01020 bool GEOMBase_Helper::extractPrefix() const
01021 {
01022   return false;
01023 }
01024 
01025 //================================================================
01026 // Function : getPrefix
01027 // Purpose  : Get prefix for name of created object
01028 //================================================================
01029 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
01030 {
01031   if ( !myPrefix.isEmpty() || theObj->_is_nil() )
01032     return myPrefix;
01033 
01034   GEOM::shape_type aType = theObj->GetShapeType();
01035 
01036   switch ( aType )
01037   {
01038     case GEOM::VERTEX   : return QObject::tr( "GEOM_VERTEX" );
01039     case GEOM::EDGE     : return QObject::tr( "GEOM_EDGE" );
01040     case GEOM::WIRE     : return QObject::tr( "GEOM_WIRE" );
01041     case GEOM::FACE     : return QObject::tr( "GEOM_FACE" );
01042     case GEOM::SHELL    : return QObject::tr( "GEOM_SHELL" );
01043     case GEOM::SOLID    : return QObject::tr( "GEOM_SOLID" );
01044     case GEOM::COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
01045     case GEOM::COMPOUND : return QObject::tr( "GEOM_COMPOUND" );
01046     default : return "";
01047   }
01048 }
01049 
01050 //================================================================
01051 // Function : getDesktop
01052 // Purpose  : Returns myDesktop field. Initialized in constructor,
01053 //            usually as dynamic_cast<SUIT_Desktop*>(parentWidget())
01054 //================================================================
01055 SUIT_Desktop* GEOMBase_Helper::getDesktop() const
01056 {
01057   return myDesktop;
01058 }
01059 
01060 //================================================================
01061 // Function : selectObjects
01062 // Purpose  : Selects list of objects
01063 //================================================================
01064 bool GEOMBase_Helper::selectObjects( ObjectList& objects )
01065 {
01066   SUIT_DataOwnerPtrList aList;
01067   ObjectList::iterator anIter;
01068   for ( anIter = objects.begin(); anIter != objects.end(); ++anIter )
01069   {
01070     QString anEntry = getEntry( *anIter );
01071     LightApp_DataOwner* anOwher = new LightApp_DataOwner( anEntry );
01072     aList.append( anOwher );
01073   }
01074 
01075   SUIT_Session* session = SUIT_Session::session();
01076   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
01077   if ( !app )
01078     return false;
01079 
01080   LightApp_SelectionMgr* aMgr = app->selectionMgr();
01081   if ( !aMgr )
01082     return false;
01083 
01084   aMgr->setSelected( aList, false );
01085 
01086   return true;
01087 }
01088 
01089 //================================================================
01090 // Function : findObjectInFather
01091 // Purpose  : It should return an object if its founded in study or
01092 //            return Null object if the object is not founded
01093 //================================================================
01094 GEOM::GEOM_Object_ptr GEOMBase_Helper::findObjectInFather (GEOM::GEOM_Object_ptr theFather,
01095                                                            const QString& theName)
01096 {
01097   SalomeApp_Application* app =
01098     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
01099   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
01100   _PTR(Study) aDStudy = appStudy->studyDS();
01101   QString IOR = GEOMBase::GetIORFromObject( theFather );
01102   _PTR(SObject) SObj ( aDStudy->FindObjectIOR( IOR.toLatin1().constData() ) );
01103 
01104   bool inStudy = false;
01105   GEOM::GEOM_Object_var aReturnObject;
01106   for (_PTR(ChildIterator) iit (aDStudy->NewChildIterator( SObj )); iit->More() && !inStudy; iit->Next()) {
01107     _PTR(SObject) child (iit->Value());
01108     QString aChildName = child->GetName().c_str();
01109     if (aChildName == theName) {
01110       inStudy = true;
01111       CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(iit->Value());
01112       aReturnObject = GEOM::GEOM_Object::_narrow( corbaObj );
01113     }
01114   }
01115   if (inStudy)
01116     return aReturnObject._retn();
01117   
01118   return GEOM::GEOM_Object::_nil();
01119 }
01120 
01121 //================================================================
01122 // Function : findObjectInFather
01123 // Purpose  : It should return an object if its founded in study or
01124 //            return Null object if the object is not founded
01125 //================================================================
01126 GEOM::GEOM_Object_ptr GEOMBase_Helper::findObjectInFather( GEOM::GEOM_Object_ptr theFather,
01127                                                            int theIndex )
01128 {
01129   GEOM::GEOM_Object_var object;
01130   bool found = false;
01131   
01132   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
01133   if ( study ) {
01134     _PTR(Study) studyDS = study->studyDS();
01135     QString IOR = GEOMBase::GetIORFromObject( theFather );
01136     _PTR(SObject) sobject( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
01137     if ( sobject ) {
01138       _PTR(ChildIterator) it( studyDS->NewChildIterator( sobject ) );
01139       for ( ; it->More() && !found; it->Next() ) {
01140        GEOM::GEOM_Object_var cobject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( it->Value() ) );
01141        if ( !CORBA::is_nil( cobject ) ) {
01142          GEOM::ListOfLong_var indices = cobject->GetSubShapeIndices();
01143          int length = indices->length();
01144          for ( int i = 0; i < length && !found; i++ ) {
01145            if ( indices[i] == theIndex ) {
01146              object = cobject;
01147              found = true;
01148            }
01149          }
01150        }
01151       }
01152     }
01153   }
01154   
01155   return object._retn();
01156 }
01157 
01158 //================================================================
01159 // Function : addSubshapesToStudy
01160 // Purpose  : Virtual method to add sub-shapes if needs
01161 //================================================================
01162 void GEOMBase_Helper::addSubshapesToStudy()
01163 {
01164   //Impemented in Dialogs, called from Accept method
01165 }
01166 
01167 //================================================================
01168 // Function : getSelected
01169 // Purpose  : Get selected object by specified type
01170 //
01171 // Returns valid object if only one object of the specified type is selected
01172 // (no matter global or local selection is activated). If \a type is TopAbs_SHAPE,
01173 // geometrical object of any valid type is expected.
01174 // 
01175 // \param type type of the object to be obtained from selection
01176 // \return selected geometrical object or nil object if selection is not satisfactory
01177 //================================================================
01178 GEOM::GeomObjPtr GEOMBase_Helper::getSelected( TopAbs_ShapeEnum type )
01179 {
01180   QList<TopAbs_ShapeEnum> types;
01181   types << type;
01182   return getSelected( types );
01183 }
01184 
01185 //================================================================
01186 // Function : getSelected
01187 // Purpose  : Get selected object by specified types
01188 //
01189 // Returns valid object if only one object of the specified type is selected
01190 // (no matter global or local selection is activated). The list of allowed
01191 // shape types is passed via \a types. If \a types includes TopAbs_SHAPE,
01192 // geometrical object of any valid type is expected.
01193 // 
01194 // \param types list of allowed shape types for the objects to be obtained from selection
01195 // \return selected geometrical object or nil object if selection is not satisfactory
01196 //================================================================
01197 GEOM::GeomObjPtr GEOMBase_Helper::getSelected( const QList<TopAbs_ShapeEnum>& types )
01198 {
01199   QList<GEOM::GeomObjPtr> selected = getSelected( types, 1 );
01200   return selected.count() > 0 ? selected[0] : GEOM::GeomObjPtr();
01201 }
01202 
01203 //================================================================
01204 // Function : getSelected
01205 // Purpose  : Get selected object(s) by specified type
01206 //
01207 // Returns list of selected objects if selection satisfies specifies selection options.
01208 // (no matter global or local selection is activated). If \a type is TopAbs_SHAPE,
01209 // geometrical objects of any valid type are expected.
01210 //
01211 // The \a type parameter specifies allowed type of the object(s) being selected.
01212 // The \a count parameter specifies exact number of the objects to be retrieved from selection.
01213 // The \a strict parameter specifies policy being applied to the selection. 
01214 // If \a count < 0, then any number of the selected objects is valid (including 0).
01215 // In this case, if \a strict is \c true (default), all selected objects should satisfy
01216 // the specified \a type.
01217 // If \a count > 0, only specified number of the objects is retrieved from the selection.
01218 // In this case, if \a strict is \c true (default), function returns empty list if total number of selected
01219 // objects does not correspond to the \a count parameter. Otherwise (if \a strict is \c false),
01220 // function returns valid list of objects if at least \a count objects satisfy specified \a type.
01221 // 
01222 // \param type type of the object(s) to be obtained from selection
01223 // \param count number of items to be retrieved from selection
01224 // \param strict selection policy
01225 // \return list of selected geometrical objects or empty list if selection is not satisfactory
01226 //================================================================
01227 QList<GEOM::GeomObjPtr> GEOMBase_Helper::getSelected( TopAbs_ShapeEnum type, int count, bool strict )
01228 {
01229   QList<TopAbs_ShapeEnum> types;
01230   types << type;
01231   return getSelected( types, count, strict );
01232 }
01233 
01234 static bool typeInList( TopAbs_ShapeEnum type, const QList<TopAbs_ShapeEnum>& types )
01235 {
01236   bool ok = false;
01237   for ( int i = 0; i < types.count() && !ok; i++ )
01238     ok = types[i] == TopAbs_SHAPE || types[i] == type;
01239   return ok;
01240 }
01241 
01242 //================================================================
01243 // Function : getSelected
01244 // Purpose  : Get selected objects by specified types
01245 //
01246 // Returns list of selected objects if selection satisfies specifies selection options.
01247 // (no matter global or local selection is activated). If \a types includes TopAbs_SHAPE,
01248 // geometrical objects of any valid type are expected.
01249 //
01250 // The \a types parameter specifies allowed types of the object(s) being selected.
01251 // The \a count parameter specifies exact number of the objects to be retrieved from selection.
01252 // The \a strict parameter specifies policy being applied to the selection. 
01253 // If \a count < 0, then any number of the selected objects is valid (including 0).
01254 // In this case, if \a strict is \c true (default), all selected objects should satisfy
01255 // the specified \a type.
01256 // If \a count > 0, only specified number of the objects is retrieved from the selection.
01257 // In this case, if \a strict is \c true (default), function returns empty list if total number of selected
01258 // objects does not correspond to the \a count parameter. Otherwise (if \a strict is \c false),
01259 // function returns valid list of objects if at least \a count objects satisfy specified \a type.
01260 // 
01261 // \param types list of allowed shape types for the objects to be obtained from selection
01262 // \param count number of items to be retrieved from selection
01263 // \param strict selection policy
01264 // \return list of selected geometrical objects or empty list if selection is not satisfactory
01265 //================================================================
01266 QList<GEOM::GeomObjPtr> GEOMBase_Helper::getSelected( const QList<TopAbs_ShapeEnum>& types, int count, bool strict )
01267 {
01268   SUIT_Session* session = SUIT_Session::session();
01269   QList<GEOM::GeomObjPtr> result;
01270 
01271   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
01272   if ( app ) {
01273     LightApp_SelectionMgr* selMgr = app->selectionMgr();
01274     if ( selMgr ) {
01275       SALOME_ListIO selected;
01276       selMgr->selectedObjects( selected );
01277       SALOME_ListIteratorOfListIO it( selected );
01278       bool stopped = false;
01279       for ( ; it.More() && !stopped; it.Next() ) {
01280        Handle(SALOME_InteractiveObject) IO = it.Value();
01281        GEOM::GeomObjPtr object = GEOMBase::ConvertIOinGEOMObject( IO );
01282        if ( object ) {
01283          TColStd_IndexedMapOfInteger subShapes;
01284          selMgr->GetIndexes( IO, subShapes );
01285          int nbSubShapes = subShapes.Extent();
01286          if ( nbSubShapes == 0 ) {
01287            // global selection
01288            if ( typeInList( (TopAbs_ShapeEnum)(object->GetShapeType()), types ) ) {
01289              result << object;
01290              if ( count > 0 ) {
01291               if ( strict && result.count() > count ) {
01292                 result.clear();
01293                 stopped = true;
01294               }
01295               else if ( !strict && result.count() == count )
01296                 stopped = true;
01297              }
01298            }
01299            else if ( strict ) {
01300              result.clear();
01301              stopped = true;
01302            }
01303          }
01304          else {
01305            // local selection
01306            for ( int i = 1; i <= nbSubShapes && !stopped; i++ ) {
01307              int idx = subShapes( i );
01308              GEOM::GeomObjPtr subShape = findObjectInFather( object.get(), idx );
01309              if ( !subShape ) {
01310               // sub-shape is not yet published in the study
01311               GEOM::ShapesOpPtr shapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
01312               subShape.take( shapesOp->GetSubShape( object.get(), idx ) ); // take ownership!
01313              }
01314              if ( typeInList( (TopAbs_ShapeEnum)(subShape->GetShapeType()), types ) ) {
01315               result << subShape;
01316               if ( count > 0 ) {
01317                 if ( strict && result.count() > count ) {
01318                   result.clear();
01319                   stopped = true;
01320                 }
01321                 else if ( !strict && result.count() == count )
01322                   stopped = true;
01323               }
01324              }
01325              else if ( strict ) {
01326               result.clear();
01327               stopped = true;
01328              }
01329            }
01330          }
01331        }
01332       }
01333     }
01334   }
01335   return result;
01336 }
01337 
01338 //================================================================
01339 // Function : setIsApplyAndClose
01340 // Purpose  : Set value of the flag indicating that the dialog is
01341 //            accepted by Apply & Close button
01342 //================================================================
01343 void GEOMBase_Helper::setIsApplyAndClose( const bool theFlag )
01344 {
01345   myIsApplyAndClose = theFlag;
01346 }
01347 
01348 //================================================================
01349 // Function : isApplyAndClose
01350 // Purpose  : Get value of the flag indicating that the dialog is
01351 //            accepted by Apply & Close button
01352 //================================================================
01353 bool GEOMBase_Helper::isApplyAndClose() const
01354 {
01355   return myIsApplyAndClose;
01356 }
01357 
01358 //================================================================
01359 // Function : setIsOptimizedBrowsing
01360 // Purpose  : Set value of the flag switching to optimized
01361 //            browsing mode (to select the first published
01362 //            object only)
01363 //================================================================
01364 void GEOMBase_Helper::setIsOptimizedBrowsing( const bool theFlag )
01365 {
01366   myIsOptimizedBrowsing = theFlag;
01367 }
01368 
01369 //================================================================
01370 // Function : isOptimizedBrowsing
01371 // Purpose  : Get value of the flag switching to optimized
01372 //            browsing mode (to select the first published
01373 //            object only)
01374 //================================================================
01375 bool GEOMBase_Helper::isOptimizedBrowsing() const
01376 {
01377   return myIsOptimizedBrowsing;
01378 }