1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : GEOMBase_Helper.cxx
25 // Author : Sergey ANIKIN, Open CASCADE S.A.S. (sergey.anikin@opencascade.com)
27 #include "GEOMBase_Helper.h"
29 #include "GEOM_Operation.h"
31 #include <GeometryGUI.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ViewManager.h>
36 #include <SUIT_ViewWindow.h>
37 #include <SUIT_ViewModel.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_OverrideCursor.h>
41 #include <SalomeApp_Module.h>
42 #include <SalomeApp_Application.h>
43 #include <SalomeApp_Study.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <LightApp_DataOwner.h>
46 #include <SalomeApp_Tools.h>
47 #include <SALOME_ListIteratorOfListIO.hxx>
49 #include <SALOME_Prs.h>
51 #include <OCCViewer_ViewModel.h>
52 #include <SVTK_ViewModel.h>
54 #include <TColStd_MapOfInteger.hxx>
55 #include <TCollection_AsciiString.hxx>
56 #include <TColStd_IndexedMapOfInteger.hxx>
58 //To disable automatic genericobj management, the following line should be commented.
59 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
60 #define WITHGENERICOBJ
62 //================================================================
63 // Function : getActiveView
64 // Purpose : Get active view window, returns 0 if no open study frame
65 //================================================================
66 static SUIT_ViewWindow* getActiveView()
68 SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
70 return SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
76 //================================================================
77 // Function : getGeomEngine
78 // Purpose : Static method
79 //================================================================
80 GEOM::GEOM_Gen_ptr GEOMBase_Helper::getGeomEngine()
82 return GeometryGUI::GetGeomGen();
85 //================================================================
86 // Function : GEOMBase_Helper
88 //================================================================
89 GEOMBase_Helper::GEOMBase_Helper( SUIT_Desktop* desktop )
90 : myDesktop( desktop ), myViewWindow( 0 ), myDisplayer( 0 ), myCommand( 0 ), isPreview( false ),
91 myIsApplyAndClose( false ), myIsOptimizedBrowsing( false )
95 //================================================================
96 // Function : ~GEOMBase_Helper
98 //================================================================
99 GEOMBase_Helper::~GEOMBase_Helper()
101 //rnv: Fix for the "IPAL21922 : WinTC5.1.4: incorrect quit salome"
102 if ( !SUIT_Session::session()->activeApplication() || !SUIT_Session::session()->activeApplication()->desktop() )
105 if ( myPreview.size() )
109 SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
111 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
113 globalSelection(aGeomGUI->getLocalSelectionMode() , true );
118 if ( !CORBA::is_nil( myOperation ) )
119 myOperation->UnRegister();
122 //================================================================
123 // Function : display
125 //================================================================
126 void GEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
128 ObjectList::const_iterator it;
129 for ( it = objList.begin(); it != objList.end(); it++ ) {
130 display( *it, false );
132 if ( !objList.empty() && updateView )
133 getDisplayer()->UpdateViewer();
136 //================================================================
137 // Function : display
138 // Purpose : Display object.
139 // Important : Object must be already in study
140 //================================================================
141 void GEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
143 // Unset color of shape ( this color may be set during preview displaying )
144 // Default color will be used
145 getDisplayer()->UnsetColor();
146 getDisplayer()->UnsetWidth();
148 // Enable activisation of selection
149 getDisplayer()->SetToActivate( true );
152 getDisplayer()->Display( object, updateView );
155 //================================================================
158 //================================================================
159 void GEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
161 ObjectList::const_iterator it = objList.begin();
162 for ( ; it != objList.end(); it++ ) {
165 if ( !objList.empty() && updateView )
166 getDisplayer()->UpdateViewer();
169 //================================================================
172 //================================================================
173 void GEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
175 if ( !object->_is_nil() ) {
176 QString entry = getEntry( object );
177 getDisplayer()->Erase( new SALOME_InteractiveObject(
178 entry.toLatin1().constData(),
179 "GEOM", strdup( GEOMBase::GetName( object ).toLatin1().constData() ) ), true, updateView );
183 //================================================================
184 // Function : redisplay
186 //================================================================
187 void GEOMBase_Helper::redisplay( const ObjectList& objList,
188 const bool withChildren,
189 const bool updateView )
191 ObjectList::const_iterator it = objList.begin();
192 for ( ; it != objList.end(); it++ ) {
193 redisplay( *it, withChildren, false );
195 if ( !objList.empty() && updateView )
196 getDisplayer()->UpdateViewer();
199 //================================================================
200 // Function : redisplay
202 //================================================================
203 void GEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
204 const bool withChildren,
205 const bool updateView )
207 if ( !object->_is_nil() ) {
208 // Unset color of shape ( this color may be set during preview displaying )
209 // Default color will be used
210 getDisplayer()->UnsetColor();
211 getDisplayer()->UnsetWidth();
213 // Enable activisation of selection
214 getDisplayer()->SetToActivate( true );
216 QString entry = getEntry( object );
217 getDisplayer()->Redisplay(new SALOME_InteractiveObject
218 (entry.toLatin1().constData(), "GEOM", strdup(GEOMBase::GetName(object).toLatin1().constData())), false);
221 if ( withChildren ) {
222 SalomeApp_Study* aDoc = getStudy();
223 if ( aDoc && aDoc->studyDS() ) {
224 _PTR(Study) aStudy = aDoc->studyDS();
225 CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(object);
226 _PTR(SObject) aSObj (aStudy->FindObjectIOR(std::string(objStr.in())));
228 _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
229 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
230 GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow
231 (GeometryGUI::ClientSObjectToObject(anIt->Value()));
232 if ( !CORBA::is_nil( aChild ) ) {
233 if ( !aChild->_is_nil() ) {
234 QString entry = getEntry( aChild );
235 getDisplayer()->Redisplay( new SALOME_InteractiveObject(
236 entry.toLatin1().constData(), "GEOM", strdup( GEOMBase::GetName( aChild ).toLatin1().constData() ) ), false );
245 getDisplayer()->UpdateViewer();
248 //================================================================
249 // Function : displayPreview
250 // Purpose : Method for displaying preview based on execute() results
251 //================================================================
252 void GEOMBase_Helper::displayPreview( const bool display,
255 const bool toRemoveFromEngine,
256 const double lineWidth,
257 const int displayMode,
261 erasePreview( update );
267 if ( !isValid( msg ) )
269 erasePreview( update );
274 erasePreview( false );
277 SUIT_OverrideCursor wc;
279 if ( !execute( objects ) || !getOperation()->IsDone() ) {
283 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
285 GEOM::GEOM_Object_var obj=*it;
286 displayPreview( obj, true, activate, false, lineWidth, displayMode, color );
287 if ( toRemoveFromEngine )
292 catch( const SALOME::SALOME_Exception& e ) {
293 SalomeApp_Tools::QtCatchCorbaException( e );
302 //================================================================
303 // Function : displayPreview
304 // Purpose : Method for displaying preview of resulting shape
305 //================================================================
306 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
310 const double lineWidth,
311 const int displayMode,
314 // Set color for preview shape
315 getDisplayer()->SetColor( color == -1 ? Quantity_NOC_VIOLET : color );
317 // set width of displayed shape
318 getDisplayer()->SetWidth( lineWidth );
320 // set display mode of displayed shape
321 int aPrevDispMode = getDisplayer()->SetDisplayMode( displayMode );
323 // Disable activation of selection
324 getDisplayer()->SetToActivate( activate );
326 // Make a reference to GEOM_Object
327 CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string( object );
328 getDisplayer()->SetName( objStr.in() );
331 SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
332 if ( aPrs == 0 || aPrs->IsNull() )
336 displayPreview( aPrs, append, update );
338 getDisplayer()->UnsetName();
339 getDisplayer()->UnsetColor();
340 getDisplayer()->SetDisplayMode( aPrevDispMode );
342 // Enable activation of displayed objects
343 getDisplayer()->SetToActivate( true );
346 //================================================================
347 // Function : displayPreview
348 // Purpose : Method for displaying arbitrary preview objects (not limited to shapes)
349 //================================================================
350 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
355 erasePreview( false );
357 // remember current view frame to make correct erase preview later
358 myViewWindow = getActiveView();
360 if ( myViewWindow == 0 )
364 SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
365 if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
366 aViewManager->getType() == SVTK_Viewer::Type() )
368 SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
369 SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
371 aView->Display( prs );
374 // Add prs to the preview list
375 myPreview.push_back( (SALOME_Prs*)prs );
379 getDisplayer()->UpdateViewer();
382 //================================================================
383 // Function : erasePreview
385 //================================================================
386 void GEOMBase_Helper::erasePreview( const bool update )
388 // check view frame where the preview was displayed
389 bool vfOK = checkViewWindow() && myViewWindow;
390 // Iterate through presentations and delete them
391 for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
394 SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
395 if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
396 aViewManager->getType() == SVTK_Viewer::Type() )
398 SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
399 SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
401 aView->Erase( *anIter, true );
413 //================================================================
414 // Function : localSelection
415 // Purpose : Activate selection of objects of a given type
416 // IMPORTANT : Works after localSelection( ... ) method call only
417 //================================================================
418 void GEOMBase_Helper::activate( const int theType )
420 if (!getStudy()) return;
421 _PTR(Study) aStudy = getStudy()->studyDS();
422 _PTR(SComponent) aGeom ( aStudy->FindComponent( "GEOM" ) );
427 _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( aGeom ) );
428 for ( ; anIter->More(); anIter->Next() )
430 _PTR(SObject) aSO ( anIter->Value() );
433 _PTR(SObject) aRefSO;
434 if ( !aSO->ReferencedObject( aRefSO ) )
436 GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow
437 (GeometryGUI::ClientSObjectToObject(aSO));
438 if ( !anObj->_is_nil() && anObj->GetType() == theType )
439 aList.Append( new SALOME_InteractiveObject( aSO->GetID().c_str(), "GEOM", aSO->GetName().c_str()) );
444 getDisplayer()->LocalSelection( aList, 0 );
447 //================================================================
448 // Function : localSelection
449 // Purpose : Activate selection of subshapes in accordance with mode
450 // theMode is from TopAbs_ShapeEnum
451 //================================================================
452 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
454 SALOME_ListIO aListOfIO;
456 ObjectList::const_iterator anIter = theObjs.begin();
457 for ( ; anIter != theObjs.end(); ++anIter )
459 GEOM::GEOM_Object_ptr anObj = *anIter;
460 if ( anObj->_is_nil() )
462 QString anEntry = getEntry( anObj );
464 aListOfIO.Append( new SALOME_InteractiveObject(
465 anEntry.toLatin1().constData(), "GEOM", strdup( GEOMBase::GetName( anObj ).toLatin1().constData() ) ) );
468 getDisplayer()->LocalSelection( aListOfIO, theMode );
471 //================================================================
472 // Function : localSelection
473 // Purpose : Activate selection of subshapes in accordance with mode
474 // theMode is from TopAbs_ShapeEnum
475 //================================================================
476 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
478 // If object is null local selection for all objects is activated
479 if ( obj->_is_nil() ) {
480 getDisplayer()->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
485 objList.push_back( obj );
486 localSelection( objList, mode );
490 //================================================================
491 // Function : globalSelection
492 // Purpose : Activate selection of subshapes. Set selection filters
493 // in accordance with mode. theMode is from GEOMImpl_Types
494 //================================================================
495 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
497 getDisplayer()->GlobalSelection( theMode, update );
500 //================================================================
501 // Function : globalSelection
502 // Purpose : Activate selection of subshapes. Set selection filters
503 // in accordance with mode. theMode is from GEOMImpl_Types
504 //================================================================
505 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
508 getDisplayer()->GlobalSelection( theModes, update );
511 //================================================================
512 // Function : globalSelection
513 // Purpose : Activate selection of subshapes. Set selection filters
514 // in accordance with mode. theMode is from GEOMImpl_Types
515 //================================================================
516 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
517 const QList<int>& subShapes,
520 getDisplayer()->GlobalSelection( theModes, update, &subShapes );
523 //================================================================
524 // Function : addInStudy
525 // Purpose : Add object in study
526 //================================================================
527 QString GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
532 _PTR(Study) aStudy = getStudy()->studyDS();
533 if ( !aStudy || theObj->_is_nil() )
536 SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy(aStudy);
538 GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
540 SALOMEDS::SObject_var aSO =
541 getGeomEngine()->AddInStudy(aStudyDS, theObj, theName, aFatherObj);
544 if ( !aSO->_is_nil() )
545 anEntry = aSO->GetID();
547 // Each dialog is responsible for this method implementation,
548 // default implementation does nothing
549 restoreSubShapes(aStudyDS, aSO);
555 //================================================================
556 // Function : restoreSubShapes
557 // Purpose : restore tree of argument's sub-shapes under the resulting shape
558 //================================================================
559 void GEOMBase_Helper::restoreSubShapes (SALOMEDS::Study_ptr /*theStudy*/,
560 SALOMEDS::SObject_ptr /*theSObject*/)
562 // do nothing by default
564 // example of implementation in particular dialog:
565 // GEOM::ListOfGO anArgs;
566 // anArgs.length(0); // empty list means that all arguments should be restored
567 // getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, anArgs,
568 // /*theFindMethod=*/GEOM::FSM_GetInPlace,
569 // /*theInheritFirstArg=*/false);
572 //================================================================
573 // Function : updateObjBrowser
574 // Purpose : Update object browser
575 //================================================================
576 void GEOMBase_Helper::updateObjBrowser() const
578 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
580 CAM_Module* module = app->module( "Geometry" );
581 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
583 appMod->updateObjBrowser( true );
588 //================================================================
589 // Function : updateViewer
590 // Purpose : Update active 3D view
591 //================================================================
592 void GEOMBase_Helper::updateViewer()
594 getDisplayer()->UpdateViewer();
597 //================================================================
598 // Function : getStudyId
599 // Purpose : Get study Id
600 //================================================================
601 int GEOMBase_Helper::getStudyId() const
605 anId = getStudy()->id();
609 //================================================================
610 // Function : getStudy
611 // Purpose : Returns the active study. It is recommended to use
612 // this method instead of direct desktop->getActiveStudy() calls
613 //================================================================
614 SalomeApp_Study* GEOMBase_Helper::getStudy() const
616 SUIT_Desktop* aDesktop = getDesktop();
620 QList<SUIT_Application*> anAppList = SUIT_Session::session()->applications();
622 SUIT_Application* anApp = 0;
623 QListIterator<SUIT_Application*> it( anAppList );
624 while ( it.hasNext() )
627 if ( anApp && anApp->desktop() == aDesktop )
631 return dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
634 //================================================================
635 // Function : getEntry
637 //================================================================
638 QString GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
640 SalomeApp_Study* study = getStudy();
642 QString objIOR = GEOMBase::GetIORFromObject( object );
643 if ( objIOR != "" ) {
644 _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( objIOR.toLatin1().constData() ) );
646 return QString::fromStdString(SO->GetID());
652 //================================================================
653 // Function : getDisplayer
655 //================================================================
656 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
659 myDisplayer = new GEOM_Displayer( getStudy() );
663 //================================================================
664 // Function : clearShapeBuffer
666 //================================================================
667 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
669 if ( CORBA::is_nil( theObj ) )
672 CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( theObj );
673 TCollection_AsciiString asciiIOR( (char *)IOR.in() );
674 GEOM_Client::get_client().RemoveShapeFromBuffer( asciiIOR );
676 if ( !getStudy() || !getStudy()->studyDS() )
679 _PTR(Study) aStudy = getStudy()->studyDS();
680 _PTR(SObject) aSObj ( aStudy->FindObjectIOR( std::string( IOR ) ) );
684 _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
685 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
686 _PTR(GenericAttribute) anAttr;
687 if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
688 _PTR(AttributeIOR) anIOR ( anAttr );
689 TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
690 GEOM_Client::get_client().RemoveShapeFromBuffer( asciiIOR );
695 //================================================================
696 // Function : openCommand
698 //================================================================
699 bool GEOMBase_Helper::openCommand()
702 if ( !getStudy() || hasCommand() )
705 GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
706 if ( !anOp->_is_nil() ) {
707 myCommand = new GEOM_Operation( SUIT_Session::session()->activeApplication(), anOp.in() );
715 //================================================================
716 // Function : abortCommand
718 //================================================================
719 bool GEOMBase_Helper::abortCommand()
725 delete myCommand; // I don't know where to delete this object here ?
731 //================================================================
732 // Function : commitCommand
734 //================================================================
735 bool GEOMBase_Helper::commitCommand( const char* )
741 delete myCommand; // I don't know where to delete this object here ?
747 //================================================================
748 // Function : hasCommand
750 //================================================================
751 bool GEOMBase_Helper::hasCommand() const
753 return (bool)myCommand;
756 //================================================================
757 // Function : getOperation
759 //================================================================
760 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
762 if ( myOperation->_is_nil() )
763 myOperation = createOperation();
770 //================================================================
771 // Function : checkViewWindow
773 //================================================================
774 bool GEOMBase_Helper::checkViewWindow()
777 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
778 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
779 while ( it.hasNext() )
781 if ( myViewWindow == it.next() )
789 //================================================================
790 // Function : onAccept
791 // Purpose : This method should be called from dialog's slots onOk() and onApply()
792 // It perfroms user input validation, then it
793 // performs a proper operation and manages transactions, etc.
794 //================================================================
795 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
797 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
798 if ( !appStudy ) return false;
799 _PTR(Study) aStudy = appStudy->studyDS();
801 bool aLocked = (_PTR(AttributeStudyProperties) (aStudy->GetProperties()))->IsLocked();
803 MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
804 SUIT_MessageBox::warning ( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
805 QObject::tr("WRN_WARNING"),
806 QObject::tr("WRN_STUDY_LOCKED"),
807 QObject::tr("BUT_OK") );
812 if ( !isValid( msg ) ) {
817 erasePreview( false );
822 if ( ( !publish && !useTransaction ) || openCommand() ) {
823 SUIT_OverrideCursor wc;
824 SUIT_Session::session()->activeApplication()->putInfo( "" );
826 if ( !execute( objects ) || !getOperation()->IsDone() ) {
832 addSubshapesToStudy(); // add Subshapes if local selection
833 const int nbObjs = objects.size();
834 QStringList anEntryList;
836 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
837 GEOM::GEOM_Object_var obj=*it;
839 QString aName = getNewObjectName();
842 aName = getPrefix(obj);
844 // Try to find a unique name
845 aName = GEOMBase::GetDefaultName(aName, extractPrefix());
847 // Don't check name uniqueness in case of numerous objects
848 aName = aName + "_" + QString::number(aNumber++);
851 // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
852 if ( aName.isEmpty() )
853 aName = GEOMBase::GetDefaultName( getPrefix( obj ) );
855 anEntryList << addInStudy( obj, aName.toLatin1().constData() );
857 display( obj, false );
858 #ifdef WITHGENERICOBJ
859 // obj has been published in study. Its refcount has been incremented.
860 // It is safe to decrement its refcount
861 // so that it will be destroyed when the entry in study will be removed
866 // asv : fix of PAL6454. If publish==false, then the original shape
867 // was modified, and need to be re-cached in GEOM_Client before redisplay
868 clearShapeBuffer( obj );
869 // withChildren=true, updateView=false
870 redisplay( obj, true, false );
877 if( SUIT_Application* anApp = SUIT_Session::session()->activeApplication() ) {
878 if( LightApp_Application* aLightApp = dynamic_cast<LightApp_Application*>( anApp ) )
879 aLightApp->browseObjects( anEntryList, isApplyAndClose(), isOptimizedBrowsing() );
880 anApp->putInfo( QObject::tr("GEOM_PRP_DONE") );
889 catch( const SALOME::SALOME_Exception& e ) {
890 SalomeApp_Tools::QtCatchCorbaException( e );
900 //================================================================
901 // Function : showError
902 // Purpose : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
903 //================================================================
904 void GEOMBase_Helper::showError()
907 if ( !getOperation()->_is_nil() )
908 msg = QObject::tr( getOperation()->GetErrorCode() );
911 msg = QObject::tr( "GEOM_PRP_ABORT" );
913 SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
914 QObject::tr( "GEOM_ERROR_STATUS" ),
916 QObject::tr( "BUT_OK" ) );
919 //================================================================
920 // Function : showError
921 // Purpose : Shows a error message followed by <msg>
922 //================================================================
923 void GEOMBase_Helper::showError( const QString& msg )
925 QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
926 if ( !msg.isEmpty() )
928 SUIT_MessageBox::critical(SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
931 //////////////////////////////////////////////////////////////////
932 // Virtual methods to be redefined in dialogs
933 //////////////////////////////////////////////////////////////////
935 //================================================================
936 // Function : createOperation
937 // Purpose : Redefine this method to return proper IOperation reference
938 //================================================================
939 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
941 GEOM::GEOM_IOperations_var aNilOp;
942 return aNilOp._retn();
945 //================================================================
946 // Function : isValid
947 // Purpose : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
948 //================================================================
949 bool GEOMBase_Helper::isValid( QString& )
954 //================================================================
955 // Function : execute
956 // Purpose : This method is called by onAccept().
957 // It should perform the required operation and put all new or modified objects into
958 // <objects> argument.Should return <false> if some error occurs during its execution.
959 //================================================================
960 bool GEOMBase_Helper::execute( ObjectList& objects )
965 //================================================================
966 // Function : getFather
967 // Purpose : This method is called by addInStudy(). It should return a father object
968 // for <theObj> or a nil reference if <theObj> should be published
969 // as a top-level object.
970 //================================================================
971 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
973 return GEOM::GEOM_Object::_nil();
976 //================================================================
977 // Function : getNewObjectName
978 // Purpose : Redefine this method to return proper name for a new object
979 //================================================================
980 QString GEOMBase_Helper::getNewObjectName() const
982 return QString::null;
985 //================================================================
986 // Function : extractPrefix
987 // Purpose : Redefine this method to return \c true if necessary
988 // to extract prefix when generating new name for the
989 // object(s) being created
990 //================================================================
991 bool GEOMBase_Helper::extractPrefix() const
996 //================================================================
997 // Function : getPrefix
998 // Purpose : Get prefix for name of created object
999 //================================================================
1000 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
1002 if ( !myPrefix.isEmpty() || theObj->_is_nil() )
1005 GEOM::shape_type aType = theObj->GetShapeType();
1009 case GEOM::VERTEX : return QObject::tr( "GEOM_VERTEX" );
1010 case GEOM::EDGE : return QObject::tr( "GEOM_EDGE" );
1011 case GEOM::WIRE : return QObject::tr( "GEOM_WIRE" );
1012 case GEOM::FACE : return QObject::tr( "GEOM_FACE" );
1013 case GEOM::SHELL : return QObject::tr( "GEOM_SHELL" );
1014 case GEOM::SOLID : return QObject::tr( "GEOM_SOLID" );
1015 case GEOM::COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
1016 case GEOM::COMPOUND : return QObject::tr( "GEOM_COMPOUND" );
1017 default : return "";
1021 //================================================================
1022 // Function : getDesktop
1023 // Purpose : Returns myDesktop field. Initialized in constructor,
1024 // usually as dynamic_cast<SUIT_Desktop*>(parentWidget())
1025 //================================================================
1026 SUIT_Desktop* GEOMBase_Helper::getDesktop() const
1031 //================================================================
1032 // Function : selectObjects
1033 // Purpose : Selects list of objects
1034 //================================================================
1035 bool GEOMBase_Helper::selectObjects( ObjectList& objects )
1037 SUIT_DataOwnerPtrList aList;
1038 ObjectList::iterator anIter;
1039 for ( anIter = objects.begin(); anIter != objects.end(); ++anIter )
1041 QString anEntry = getEntry( *anIter );
1042 LightApp_DataOwner* anOwher = new LightApp_DataOwner( anEntry );
1043 aList.append( anOwher );
1046 SUIT_Session* session = SUIT_Session::session();
1047 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1051 LightApp_SelectionMgr* aMgr = app->selectionMgr();
1055 aMgr->setSelected( aList, false );
1060 //================================================================
1061 // Function : findObjectInFather
1062 // Purpose : It should return an object if its founded in study or
1063 // return Null object if the object is not founded
1064 //================================================================
1065 GEOM::GEOM_Object_ptr GEOMBase_Helper::findObjectInFather (GEOM::GEOM_Object_ptr theFather,
1066 const QString& theName)
1068 SalomeApp_Application* app =
1069 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
1070 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1071 _PTR(Study) aDStudy = appStudy->studyDS();
1072 QString IOR = GEOMBase::GetIORFromObject( theFather );
1073 _PTR(SObject) SObj ( aDStudy->FindObjectIOR( IOR.toLatin1().constData() ) );
1075 bool inStudy = false;
1076 GEOM::GEOM_Object_var aReturnObject;
1077 for (_PTR(ChildIterator) iit (aDStudy->NewChildIterator( SObj )); iit->More() && !inStudy; iit->Next()) {
1078 _PTR(SObject) child (iit->Value());
1079 QString aChildName = child->GetName().c_str();
1080 if (aChildName == theName) {
1082 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(iit->Value());
1083 aReturnObject = GEOM::GEOM_Object::_narrow( corbaObj );
1087 return aReturnObject._retn();
1089 return GEOM::GEOM_Object::_nil();
1092 //================================================================
1093 // Function : findObjectInFather
1094 // Purpose : It should return an object if its founded in study or
1095 // return Null object if the object is not founded
1096 //================================================================
1097 GEOM::GEOM_Object_ptr GEOMBase_Helper::findObjectInFather( GEOM::GEOM_Object_ptr theFather,
1100 GEOM::GEOM_Object_var object;
1103 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
1105 _PTR(Study) studyDS = study->studyDS();
1106 QString IOR = GEOMBase::GetIORFromObject( theFather );
1107 _PTR(SObject) sobject( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
1109 _PTR(ChildIterator) it( studyDS->NewChildIterator( sobject ) );
1110 for ( ; it->More() && !found; it->Next() ) {
1111 GEOM::GEOM_Object_var cobject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( it->Value() ) );
1112 if ( !CORBA::is_nil( cobject ) ) {
1113 GEOM::ListOfLong_var indices = cobject->GetSubShapeIndices();
1114 int length = indices->length();
1115 for ( int i = 0; i < length && !found; i++ ) {
1116 if ( indices[i] == theIndex ) {
1126 return object._retn();
1129 //================================================================
1130 // Function : addSubshapesToStudy
1131 // Purpose : Virtual method to add subshapes if needs
1132 //================================================================
1133 void GEOMBase_Helper::addSubshapesToStudy()
1135 //Impemented in Dialogs, called from Accept method
1138 //================================================================
1139 // Function : getSelected
1140 // Purpose : Get selected object by specified type
1142 // Returns valid object if only one object of the specified type is selected
1143 // (no matter global or local selection is activated). If \a type is TopAbs_SHAPE,
1144 // geometrical object of any valid type is expected.
1146 // \param type type of the object to be obtained from selection
1147 // \return selected geometrical object or nil object if selection is not satisfactory
1148 //================================================================
1149 GEOM::GeomObjPtr GEOMBase_Helper::getSelected( TopAbs_ShapeEnum type )
1151 QList<TopAbs_ShapeEnum> types;
1153 return getSelected( types );
1156 //================================================================
1157 // Function : getSelected
1158 // Purpose : Get selected object by specified types
1160 // Returns valid object if only one object of the specified type is selected
1161 // (no matter global or local selection is activated). The list of allowed
1162 // shape types is passed via \a types. If \a types includes TopAbs_SHAPE,
1163 // geometrical object of any valid type is expected.
1165 // \param types list of allowed shape types for the objects to be obtained from selection
1166 // \return selected geometrical object or nil object if selection is not satisfactory
1167 //================================================================
1168 GEOM::GeomObjPtr GEOMBase_Helper::getSelected( const QList<TopAbs_ShapeEnum>& types )
1170 QList<GEOM::GeomObjPtr> selected = getSelected( types, 1 );
1171 return selected.count() > 0 ? selected[0] : GEOM::GeomObjPtr();
1174 //================================================================
1175 // Function : getSelected
1176 // Purpose : Get selected object(s) by specified type
1178 // Returns list of selected objects if selection satisfies specifies selection options.
1179 // (no matter global or local selection is activated). If \a type is TopAbs_SHAPE,
1180 // geometrical objects of any valid type are expected.
1182 // The \a type parameter specifies allowed type of the object(s) being selected.
1183 // The \a count parameter specifies exact number of the objects to be retrieved from selection.
1184 // The \a strict parameter specifies policy being applied to the selection.
1185 // If \a count < 0, then any number of the selected objects is valid (including 0).
1186 // In this case, if \a strict is \c true (default), all selected objects should satisfy
1187 // the specified \a type.
1188 // If \a count > 0, only specified number of the objects is retrieved from the selection.
1189 // In this case, if \a strict is \c true (default), function returns empty list if total number of selected
1190 // objects does not correspond to the \a count parameter. Otherwise (if \a strict is \c false),
1191 // function returns valid list of objects if at least \a count objects satisfy specified \a type.
1193 // \param type type of the object(s) to be obtained from selection
1194 // \param count number of items to be retrieved from selection
1195 // \param strict selection policy
1196 // \return list of selected geometrical objects or empty list if selection is not satisfactory
1197 //================================================================
1198 QList<GEOM::GeomObjPtr> GEOMBase_Helper::getSelected( TopAbs_ShapeEnum type, int count, bool strict )
1200 QList<TopAbs_ShapeEnum> types;
1202 return getSelected( types, count, strict );
1205 static bool typeInList( TopAbs_ShapeEnum type, const QList<TopAbs_ShapeEnum>& types )
1208 for ( int i = 0; i < types.count() && !ok; i++ )
1209 ok = types[i] == TopAbs_SHAPE || types[i] == type;
1213 //================================================================
1214 // Function : getSelected
1215 // Purpose : Get selected objects by specified types
1217 // Returns list of selected objects if selection satisfies specifies selection options.
1218 // (no matter global or local selection is activated). If \a types includes TopAbs_SHAPE,
1219 // geometrical objects of any valid type are expected.
1221 // The \a types parameter specifies allowed types of the object(s) being selected.
1222 // The \a count parameter specifies exact number of the objects to be retrieved from selection.
1223 // The \a strict parameter specifies policy being applied to the selection.
1224 // If \a count < 0, then any number of the selected objects is valid (including 0).
1225 // In this case, if \a strict is \c true (default), all selected objects should satisfy
1226 // the specified \a type.
1227 // If \a count > 0, only specified number of the objects is retrieved from the selection.
1228 // In this case, if \a strict is \c true (default), function returns empty list if total number of selected
1229 // objects does not correspond to the \a count parameter. Otherwise (if \a strict is \c false),
1230 // function returns valid list of objects if at least \a count objects satisfy specified \a type.
1232 // \param types list of allowed shape types for the objects to be obtained from selection
1233 // \param count number of items to be retrieved from selection
1234 // \param strict selection policy
1235 // \return list of selected geometrical objects or empty list if selection is not satisfactory
1236 //================================================================
1237 QList<GEOM::GeomObjPtr> GEOMBase_Helper::getSelected( const QList<TopAbs_ShapeEnum>& types, int count, bool strict )
1239 SUIT_Session* session = SUIT_Session::session();
1240 QList<GEOM::GeomObjPtr> result;
1242 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1244 LightApp_SelectionMgr* selMgr = app->selectionMgr();
1246 SALOME_ListIO selected;
1247 selMgr->selectedObjects( selected );
1248 SALOME_ListIteratorOfListIO it( selected );
1249 bool stopped = false;
1250 for ( ; it.More() && !stopped; it.Next() ) {
1251 Handle(SALOME_InteractiveObject) IO = it.Value();
1252 GEOM::GeomObjPtr object = GEOMBase::ConvertIOinGEOMObject( IO );
1254 TColStd_IndexedMapOfInteger subShapes;
1255 selMgr->GetIndexes( IO, subShapes );
1256 int nbSubShapes = subShapes.Extent();
1257 if ( nbSubShapes == 0 ) {
1259 if ( typeInList( (TopAbs_ShapeEnum)(object->GetShapeType()), types ) ) {
1262 if ( strict && result.count() > count ) {
1266 else if ( !strict && result.count() == count )
1270 else if ( strict ) {
1277 for ( int i = 1; i <= nbSubShapes && !stopped; i++ ) {
1278 int idx = subShapes( i );
1279 GEOM::GeomObjPtr subShape = findObjectInFather( object.get(), idx );
1281 // sub-shape is not yet published in the study
1282 GEOM::ShapesOpPtr shapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
1283 subShape.take( shapesOp->GetSubShape( object.get(), idx ) ); // take ownership!
1285 if ( typeInList( (TopAbs_ShapeEnum)(subShape->GetShapeType()), types ) ) {
1288 if ( strict && result.count() > count ) {
1292 else if ( !strict && result.count() == count )
1296 else if ( strict ) {
1309 //================================================================
1310 // Function : setIsApplyAndClose
1311 // Purpose : Set value of the flag indicating that the dialog is
1312 // accepted by Apply & Close button
1313 //================================================================
1314 void GEOMBase_Helper::setIsApplyAndClose( const bool theFlag )
1316 myIsApplyAndClose = theFlag;
1319 //================================================================
1320 // Function : isApplyAndClose
1321 // Purpose : Get value of the flag indicating that the dialog is
1322 // accepted by Apply & Close button
1323 //================================================================
1324 bool GEOMBase_Helper::isApplyAndClose() const
1326 return myIsApplyAndClose;
1329 //================================================================
1330 // Function : setIsOptimizedBrowsing
1331 // Purpose : Set value of the flag switching to optimized
1332 // browsing mode (to select the first published
1334 //================================================================
1335 void GEOMBase_Helper::setIsOptimizedBrowsing( const bool theFlag )
1337 myIsOptimizedBrowsing = theFlag;
1340 //================================================================
1341 // Function : isOptimizedBrowsing
1342 // Purpose : Get value of the flag switching to optimized
1343 // browsing mode (to select the first published
1345 //================================================================
1346 bool GEOMBase_Helper::isOptimizedBrowsing() const
1348 return myIsOptimizedBrowsing;