1 // Copyright (C) 2009-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MyGEOMBase_Helper.hxx"
22 #include "GEOM_Operation.h"
23 #include "HEXABLOCKGUI.hxx"
25 #include <GeometryGUI.h>
27 #include <SUIT_Desktop.h>
28 #include <SUIT_Session.h>
29 #include <SUIT_ViewManager.h>
30 #include <SUIT_ViewModel.h>
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_OverrideCursor.h>
34 #include <SalomeApp_Module.h>
35 #include <SalomeApp_Application.h>
36 #include <SalomeApp_Study.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_DataOwner.h>
39 #include <SalomeApp_Tools.h>
40 #include <SALOME_ListIteratorOfListIO.hxx>
42 #include <SALOME_Prs.h>
44 #include <OCCViewer_ViewModel.h>
45 #include <SOCC_ViewModel.h>
46 #include <SVTK_ViewModel.h>
48 #include <TColStd_MapOfInteger.hxx>
49 #include <TCollection_AsciiString.hxx>
50 #include <TColStd_IndexedMapOfInteger.hxx>
51 #include <AIS_ListIteratorOfListOfInteractive.hxx>
53 //To disable automatic genericobj management, the following line should be commented.
54 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
55 #define WITHGENERICOBJ
57 //================================================================
58 // Function : getActiveView
59 // Purpose : Get active view window, returns 0 if no open study frame
60 //================================================================
61 static SUIT_ViewWindow* getActiveView()
63 SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
65 return SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
71 //================================================================
72 // Function : getGeomEngine
73 // Purpose : Static method
74 //================================================================
75 GEOM::GEOM_Gen_ptr MyGEOMBase_Helper::getGeomEngine()
77 return GeometryGUI::GetGeomGen();
80 //================================================================
81 // Function : MyGEOMBase_Helper
83 //================================================================
84 MyGEOMBase_Helper::MyGEOMBase_Helper( SUIT_Desktop* desktop )
85 : myDesktop( desktop ), myViewWindow( 0 ), myDisplayer( 0 ), myCommand( 0 ), isPreview( false ),
86 myIsApplyAndClose( false ), myIsOptimizedBrowsing( false )
90 //================================================================
91 // Function : ~MyGEOMBase_Helper
93 //================================================================
94 MyGEOMBase_Helper::~MyGEOMBase_Helper()
96 //rnv: Fix for the "IPAL21922 : WinTC5.1.4: incorrect quit salome"
97 if ( !SUIT_Session::session()->activeApplication() || !SUIT_Session::session()->activeApplication()->desktop() )
100 if ( myPreview.size() )
104 // SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
106 // GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
108 // globalSelection(aGeomGUI->getLocalSelectionMode() , true );
113 if ( !CORBA::is_nil( myOperation ) )
114 myOperation->UnRegister();
117 //================================================================
118 // Function : display
120 //================================================================
121 void MyGEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
123 ObjectList::const_iterator it;
124 for ( it = objList.begin(); it != objList.end(); it++ ) {
125 display( *it, false );
127 if ( !objList.empty() && updateView )
128 getDisplayer()->UpdateViewer();
131 //================================================================
132 // Function : display
133 // Purpose : Display object.
134 // Important : Object must be already in study
135 //================================================================
136 void MyGEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
138 // Unset color of shape ( this color may be set during preview displaying )
139 // Default color will be used
140 getDisplayer()->UnsetColor();
141 getDisplayer()->UnsetWidth();
143 // Enable activisation of selection
144 getDisplayer()->SetToActivate( true );
147 getDisplayer()->Display( object, updateView );
150 //================================================================
153 //================================================================
154 void MyGEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
156 ObjectList::const_iterator it = objList.begin();
157 for ( ; it != objList.end(); it++ ) {
160 if ( !objList.empty() && updateView )
161 getDisplayer()->UpdateViewer();
164 //================================================================
167 //================================================================
168 void MyGEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
170 if ( !object->_is_nil() ) {
171 QString entry = getEntry( object );
172 getDisplayer()->Erase( new SALOME_InteractiveObject(
173 entry.toLatin1().constData(),
174 "GEOM", strdup( GEOMBase::GetName( object ).toLatin1().constData() ) ), true, updateView );
178 //================================================================
179 // Function : redisplay
181 //================================================================
182 void MyGEOMBase_Helper::redisplay( const ObjectList& objList,
183 const bool withChildren,
184 const bool updateView )
186 ObjectList::const_iterator it = objList.begin();
187 for ( ; it != objList.end(); it++ ) {
188 redisplay( *it, withChildren, false );
190 if ( !objList.empty() && updateView )
191 getDisplayer()->UpdateViewer();
194 //================================================================
195 // Function : redisplay
197 //================================================================
198 void MyGEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
199 const bool withChildren,
200 const bool updateView )
202 if ( !object->_is_nil() ) {
203 // Unset color of shape ( this color may be set during preview displaying )
204 // Default color will be used
205 getDisplayer()->UnsetColor();
206 getDisplayer()->UnsetWidth();
208 // Enable activisation of selection
209 getDisplayer()->SetToActivate( true );
211 QString entry = getEntry( object );
212 getDisplayer()->Redisplay(new SALOME_InteractiveObject
213 (entry.toLatin1().constData(), "GEOM", strdup(GEOMBase::GetName(object).toLatin1().constData())), false);
216 if ( withChildren ) {
217 SalomeApp_Study* aDoc = getStudy();
218 if ( aDoc && aDoc->studyDS() ) {
219 _PTR(Study) aStudy = aDoc->studyDS();
220 CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(object);
221 _PTR(SObject) aSObj (aStudy->FindObjectIOR(std::string(objStr.in())));
223 _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
224 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
225 GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow
226 (GeometryGUI::ClientSObjectToObject(anIt->Value()));
227 if ( !CORBA::is_nil( aChild ) ) {
228 if ( !aChild->_is_nil() ) {
229 QString entry = getEntry( aChild );
230 getDisplayer()->Redisplay( new SALOME_InteractiveObject(
231 entry.toLatin1().constData(), "GEOM", strdup( GEOMBase::GetName( aChild ).toLatin1().constData() ) ), false );
240 getDisplayer()->UpdateViewer();
243 //================================================================
244 // Function : displayPreview
245 // Purpose : Method for displaying preview based on execute() results
246 //================================================================
247 void MyGEOMBase_Helper::displayPreview( const bool display,
250 const bool toRemoveFromEngine,
251 const double lineWidth,
252 const int displayMode,
256 erasePreview( update );
262 if ( !isValid( msg ) )
264 erasePreview( update );
269 erasePreview( false );
272 SUIT_OverrideCursor wc;
274 if ( !execute( objects ) || !getOperation()->IsDone() ) {
278 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
280 GEOM::GEOM_Object_var obj=*it;
281 displayPreview( obj, true, activate, false, lineWidth, displayMode, color );
282 if ( toRemoveFromEngine )
287 catch( const SALOME::SALOME_Exception& e ) {
288 SalomeApp_Tools::QtCatchCorbaException( e );
297 //================================================================
298 // Function : displayPreview
299 // Purpose : Method for displaying preview of resulting shape
300 //================================================================
301 void MyGEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
305 const double lineWidth,
306 const int displayMode,
309 // Set color for preview shape
310 getDisplayer()->SetColor( color == -1 ? Quantity_NOC_VIOLET : color );
312 // set width of displayed shape
313 getDisplayer()->SetWidth( lineWidth );
315 // set display mode of displayed shape
316 int aPrevDispMode = getDisplayer()->SetDisplayMode( displayMode );
318 // Disable activation of selection
319 getDisplayer()->SetToActivate( activate );
321 // Make a reference to GEOM_Object
322 CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string( object );
323 getDisplayer()->SetName( objStr.in() );
326 SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
327 if ( aPrs == 0 || aPrs->IsNull() )
331 displayPreview( aPrs, append, update );
333 getDisplayer()->UnsetName();
334 getDisplayer()->UnsetColor();
335 getDisplayer()->SetDisplayMode( aPrevDispMode );
337 // Enable activation of displayed objects
338 getDisplayer()->SetToActivate( true );
341 //================================================================
342 // Function : displayPreview
343 // Purpose : Method for displaying arbitrary preview objects (not limited to shapes)
344 //================================================================
345 void MyGEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
350 erasePreview( false );
352 // remember current view frame to make correct erase preview later
353 myViewWindow = getActiveView();
355 if ( myViewWindow == 0 )
359 SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
360 if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
361 aViewManager->getType() == SVTK_Viewer::Type() )
363 SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
364 SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
366 aView->Display( prs );
369 // Add prs to the preview list
370 myPreview.push_back( (SALOME_Prs*)prs );
374 getDisplayer()->UpdateViewer();
377 //================================================================
378 // Function : erasePreview
380 //================================================================
381 void MyGEOMBase_Helper::erasePreview( const bool update )
383 // check view frame where the preview was displayed
384 bool vfOK = checkViewWindow() && myViewWindow;
385 // Iterate through presentations and delete them
386 for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
389 SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
390 if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
391 aViewManager->getType() == SVTK_Viewer::Type() )
393 SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
394 SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
396 aView->Erase( *anIter, true );
408 //================================================================
409 // Function : localSelection
410 // Purpose : Activate selection of objects of a given type
411 // IMPORTANT : Works after localSelection( ... ) method call only
412 //================================================================
413 void MyGEOMBase_Helper::activate( const int theType )
415 if (!getStudy()) return;
416 _PTR(Study) aStudy = getStudy()->studyDS();
417 _PTR(SComponent) aGeom ( aStudy->FindComponent( "GEOM" ) );
422 _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( aGeom ) );
423 for ( ; anIter->More(); anIter->Next() )
425 _PTR(SObject) aSO ( anIter->Value() );
428 _PTR(SObject) aRefSO;
429 if ( !aSO->ReferencedObject( aRefSO ) )
431 GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow
432 (GeometryGUI::ClientSObjectToObject(aSO));
433 if ( !anObj->_is_nil() && anObj->GetType() == theType )
434 aList.Append( new SALOME_InteractiveObject( aSO->GetID().c_str(), "GEOM", aSO->GetName().c_str()) );
439 getDisplayer()->LocalSelection( aList, 0 );
442 //================================================================
443 // Function : localSelection
444 // Purpose : Activate selection of subshapes in accordance with mode
445 // theMode is from TopAbs_ShapeEnum
446 //================================================================
447 void MyGEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
449 SALOME_ListIO aListOfIO;
451 ObjectList::const_iterator anIter = theObjs.begin();
452 for ( ; anIter != theObjs.end(); ++anIter )
454 GEOM::GEOM_Object_ptr anObj = *anIter;
455 if ( anObj->_is_nil() )
457 QString anEntry = getEntry( anObj );
459 aListOfIO.Append( new SALOME_InteractiveObject(
460 anEntry.toLatin1().constData(), "GEOM", strdup( GEOMBase::GetName( anObj ).toLatin1().constData() ) ) );
463 getDisplayer()->LocalSelection( aListOfIO, theMode );
466 //================================================================
467 // Function : localSelection
468 // Purpose : Activate selection of subshapes in accordance with mode
469 // theMode is from TopAbs_ShapeEnum
470 //================================================================
471 void MyGEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
473 // If object is null local selection for all objects is activated
474 if ( obj->_is_nil() ) {
475 getDisplayer()->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
480 objList.push_back( obj );
481 localSelection( objList, mode );
484 //================================================================
486 // Function : localSelection
487 // Purpose : Activate selection of subshapes in accordance with mode
488 // theMode is from TopAbs_ShapeEnum
489 //================================================================
490 void MyGEOMBase_Helper::localSelection(SUIT_ViewWindow *view, const int theMode )
492 SUIT_Session* session = SUIT_Session::session();
493 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
494 if ( app == NULL || view == NULL ) return;
496 LightApp_SelectionMgr* sm = app->selectionMgr();
498 // remove all filters from selection
501 SALOME_View* vf = dynamic_cast<SALOME_View*>(view->getViewManager()->getViewModel());
502 if (vf == NULL) return;
504 SALOME_Prs* prs = vf->CreatePrs( NULL );
505 vf->LocalSelection( prs, theMode );
506 delete prs; // delete presentation because displayer is its owner
511 //================================================================
513 // Function : globalSelection2
514 // Purpose : Activate selection of subshapes. Set selection filters
515 // in accordance with mode. theMode is from GEOMImpl_Types
516 // mode = GEOM_ALLOBJECTS
517 //================================================================
518 void MyGEOMBase_Helper::globalSelection(SUIT_ViewWindow *view, const bool update )
520 SUIT_Session* session = SUIT_Session::session();
521 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
522 if ( app == NULL || view == NULL ) return;
524 SALOME_View* vf = dynamic_cast<SALOME_View*>(view->getViewManager()->getViewModel());
525 if ( vf == NULL ) return;
527 // Close local context
528 vf->GlobalSelection( update );
530 // Set selection filters in accordance with current mode
531 LightApp_SelectionMgr* sm = app->selectionMgr();
536 //@ aSel->ClearIndex();
540 // Remove filters from AIS_InteractiveContext
541 Handle(AIS_InteractiveContext) ic;
542 SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
545 ic = viewer->getAISContext();
552 //================================================================
553 // Function : globalSelection
554 // Purpose : Activate selection of subshapes. Set selection filters
555 // in accordance with mode. theMode is from GEOMImpl_Types
556 //================================================================
557 void MyGEOMBase_Helper::globalSelection( const int theMode, const bool update )
559 getDisplayer()->GlobalSelection( theMode, update );
562 //================================================================
563 // Function : globalSelection
564 // Purpose : Activate selection of subshapes. Set selection filters
565 // in accordance with mode. theMode is from GEOMImpl_Types
566 //================================================================
567 void MyGEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
570 getDisplayer()->GlobalSelection( theModes, update );
573 //================================================================
574 // Function : globalSelection
575 // Purpose : Activate selection of subshapes. Set selection filters
576 // in accordance with mode. theMode is from GEOMImpl_Types
577 //================================================================
578 void MyGEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
579 const QList<int>& subShapes,
582 getDisplayer()->GlobalSelection( theModes, update, &subShapes );
585 //================================================================
586 // Function : addInStudy
587 // Purpose : Add object in study
588 //================================================================
589 QString MyGEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
594 _PTR(Study) aStudy = getStudy()->studyDS();
595 if ( !aStudy || theObj->_is_nil() )
598 SALOMEDS::Study_var aStudyDS = GeometryGUI::ClientStudyToStudy(aStudy);
600 GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
602 SALOMEDS::SObject_var aSO =
603 getGeomEngine()->AddInStudy(aStudyDS, theObj, theName, aFatherObj);
606 if ( !aSO->_is_nil() )
607 anEntry = aSO->GetID();
609 // Each dialog is responsible for this method implementation,
610 // default implementation does nothing
611 restoreSubShapes(aStudyDS, aSO);
617 //================================================================
618 // Function : restoreSubShapes
619 // Purpose : restore tree of argument's sub-shapes under the resulting shape
620 //================================================================
621 void MyGEOMBase_Helper::restoreSubShapes (SALOMEDS::Study_ptr /*theStudy*/,
622 SALOMEDS::SObject_ptr /*theSObject*/)
624 // do nothing by default
626 // example of implementation in particular dialog:
627 // GEOM::ListOfGO anArgs;
628 // anArgs.length(0); // empty list means that all arguments should be restored
629 // getGeomEngine()->RestoreSubShapesSO(theStudy, theSObject, anArgs,
630 // /*theFindMethod=*/GEOM::FSM_GetInPlace,
631 // /*theInheritFirstArg=*/false);
634 //================================================================
635 // Function : updateObjBrowser
636 // Purpose : Update object browser
637 //================================================================
638 void MyGEOMBase_Helper::updateObjBrowser() const
640 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
642 CAM_Module* module = app->module( "Geometry" );
643 SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
645 appMod->updateObjBrowser( true );
650 //================================================================
651 // Function : updateViewer
652 // Purpose : Update active 3D view
653 //================================================================
654 void MyGEOMBase_Helper::updateViewer()
656 getDisplayer()->UpdateViewer();
659 //================================================================
660 // Function : getStudyId
661 // Purpose : Get study Id
662 //================================================================
663 int MyGEOMBase_Helper::getStudyId() const
667 anId = getStudy()->id();
671 //================================================================
672 // Function : getStudy
673 // Purpose : Returns the active study. It is recommended to use
674 // this method instead of direct desktop->getActiveStudy() calls
675 //================================================================
676 SalomeApp_Study* MyGEOMBase_Helper::getStudy() const
678 SUIT_Desktop* aDesktop = getDesktop();
682 QList<SUIT_Application*> anAppList = SUIT_Session::session()->applications();
684 SUIT_Application* anApp = 0;
685 QListIterator<SUIT_Application*> it( anAppList );
686 while ( it.hasNext() )
689 if ( anApp && anApp->desktop() == aDesktop )
693 return dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
696 //================================================================
697 // Function : getEntry
699 //================================================================
700 QString MyGEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
702 SalomeApp_Study* study = getStudy();
704 QString objIOR = GEOMBase::GetIORFromObject( object );
705 if ( objIOR != "" ) {
706 _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( objIOR.toLatin1().constData() ) );
708 return QString::fromStdString(SO->GetID());
714 //================================================================
715 // Function : getDisplayer
717 //================================================================
718 GEOM_Displayer* MyGEOMBase_Helper::getDisplayer()
721 myDisplayer = new GEOM_Displayer( getStudy() );
725 //================================================================
726 // Function : clearShapeBuffer
728 //================================================================
729 void MyGEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
731 if ( CORBA::is_nil( theObj ) )
734 CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( theObj );
735 TCollection_AsciiString asciiIOR( (char *)IOR.in() );
736 GEOM_Client::get_client().RemoveShapeFromBuffer( asciiIOR );
738 if ( !getStudy() || !getStudy()->studyDS() )
741 _PTR(Study) aStudy = getStudy()->studyDS();
742 _PTR(SObject) aSObj ( aStudy->FindObjectIOR( std::string( IOR ) ) );
746 _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
747 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
748 _PTR(GenericAttribute) anAttr;
749 if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
750 _PTR(AttributeIOR) anIOR ( anAttr );
751 TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
752 GEOM_Client::get_client().RemoveShapeFromBuffer( asciiIOR );
757 //================================================================
758 // Function : openCommand
760 //================================================================
761 bool MyGEOMBase_Helper::openCommand()
764 if ( !getStudy() || hasCommand() )
767 GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
768 if ( !anOp->_is_nil() ) {
769 myCommand = new GEOM_Operation( SUIT_Session::session()->activeApplication(), anOp.in() );
777 //================================================================
778 // Function : abortCommand
780 //================================================================
781 bool MyGEOMBase_Helper::abortCommand()
787 delete myCommand; // I don't know where to delete this object here ?
793 //================================================================
794 // Function : commitCommand
796 //================================================================
797 bool MyGEOMBase_Helper::commitCommand( const char* )
803 delete myCommand; // I don't know where to delete this object here ?
809 //================================================================
810 // Function : hasCommand
812 //================================================================
813 bool MyGEOMBase_Helper::hasCommand() const
815 return (bool)myCommand;
818 //================================================================
819 // Function : getOperation
821 //================================================================
822 GEOM::GEOM_IOperations_ptr MyGEOMBase_Helper::getOperation()
824 if ( myOperation->_is_nil() )
825 myOperation = createOperation();
832 //================================================================
833 // Function : checkViewWindow
835 //================================================================
836 bool MyGEOMBase_Helper::checkViewWindow()
839 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
840 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
841 while ( it.hasNext() )
843 if ( myViewWindow == it.next() )
851 //================================================================
852 // Function : onAccept
853 // Purpose : This method should be called from dialog's slots onOk() and onApply()
854 // It perfroms user input validation, then it
855 // performs a proper operation and manages transactions, etc.
856 //================================================================
857 bool MyGEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
859 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
860 if ( !appStudy ) return false;
861 _PTR(Study) aStudy = appStudy->studyDS();
863 bool aLocked = (_PTR(AttributeStudyProperties) (aStudy->GetProperties()))->IsLocked();
865 SUIT_MessageBox::warning ( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
866 QObject::tr("WRN_WARNING"),
867 QObject::tr("WRN_STUDY_LOCKED"),
868 QObject::tr("BUT_OK") );
873 if ( !isValid( msg ) ) {
878 erasePreview( false );
883 if ( ( !publish && !useTransaction ) || openCommand() ) {
884 SUIT_OverrideCursor wc;
885 SUIT_Session::session()->activeApplication()->putInfo( "" );
887 if ( !execute( objects ) || !getOperation()->IsDone() ) {
893 addSubshapesToStudy(); // add Subshapes if local selection
894 const int nbObjs = objects.size();
895 QStringList anEntryList;
897 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
898 GEOM::GEOM_Object_var obj=*it;
900 QString aName = getNewObjectName();
903 aName = getPrefix(obj);
905 // Try to find a unique name
906 aName = GEOMBase::GetDefaultName(aName, extractPrefix());
908 // Don't check name uniqueness in case of numerous objects
909 aName = aName + "_" + QString::number(aNumber++);
912 // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
913 if ( aName.isEmpty() )
914 aName = GEOMBase::GetDefaultName( getPrefix( obj ) );
916 anEntryList << addInStudy( obj, aName.toLatin1().constData() );
918 display( obj, false );
919 #ifdef WITHGENERICOBJ
920 // obj has been published in study. Its refcount has been incremented.
921 // It is safe to decrement its refcount
922 // so that it will be destroyed when the entry in study will be removed
927 // asv : fix of PAL6454. If publish==false, then the original shape
928 // was modified, and need to be re-cached in GEOM_Client before redisplay
929 clearShapeBuffer( obj );
930 // withChildren=true, updateView=false
931 redisplay( obj, true, false );
938 if( SUIT_Application* anApp = SUIT_Session::session()->activeApplication() ) {
939 if( LightApp_Application* aLightApp = dynamic_cast<LightApp_Application*>( anApp ) )
940 aLightApp->browseObjects( anEntryList, isApplyAndClose(), isOptimizedBrowsing() );
941 anApp->putInfo( QObject::tr("GEOM_PRP_DONE") );
950 catch( const SALOME::SALOME_Exception& e ) {
951 SalomeApp_Tools::QtCatchCorbaException( e );
961 //================================================================
962 // Function : showError
963 // Purpose : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
964 //================================================================
965 void MyGEOMBase_Helper::showError()
968 if ( !getOperation()->_is_nil() )
969 msg = QObject::tr( getOperation()->GetErrorCode() );
972 msg = QObject::tr( "GEOM_PRP_ABORT" );
974 SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
975 QObject::tr( "GEOM_ERROR_STATUS" ),
977 QObject::tr( "BUT_OK" ) );
980 //================================================================
981 // Function : showError
982 // Purpose : Shows a error message followed by <msg>
983 //================================================================
984 void MyGEOMBase_Helper::showError( const QString& msg )
986 QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
987 if ( !msg.isEmpty() )
989 SUIT_MessageBox::critical(SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
992 //////////////////////////////////////////////////////////////////
993 // Virtual methods to be redefined in dialogs
994 //////////////////////////////////////////////////////////////////
996 //================================================================
997 // Function : createOperation
998 // Purpose : Redefine this method to return proper IOperation reference
999 //================================================================
1000 GEOM::GEOM_IOperations_ptr MyGEOMBase_Helper::createOperation()
1002 GEOM::GEOM_IOperations_var aNilOp;
1003 return aNilOp._retn();
1006 //================================================================
1007 // Function : isValid
1008 // Purpose : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
1009 //================================================================
1010 bool MyGEOMBase_Helper::isValid( QString& )
1015 //================================================================
1016 // Function : execute
1017 // Purpose : This method is called by onAccept().
1018 // It should perform the required operation and put all new or modified objects into
1019 // <objects> argument.Should return <false> if some error occurs during its execution.
1020 //================================================================
1021 bool MyGEOMBase_Helper::execute( ObjectList& objects )
1026 //================================================================
1027 // Function : getFather
1028 // Purpose : This method is called by addInStudy(). It should return a father object
1029 // for <theObj> or a nil reference if <theObj> should be published
1030 // as a top-level object.
1031 //================================================================
1032 GEOM::GEOM_Object_ptr MyGEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
1034 return GEOM::GEOM_Object::_nil();
1037 //================================================================
1038 // Function : getNewObjectName
1039 // Purpose : Redefine this method to return proper name for a new object
1040 //================================================================
1041 QString MyGEOMBase_Helper::getNewObjectName() const
1043 return QString::null;
1046 //================================================================
1047 // Function : extractPrefix
1048 // Purpose : Redefine this method to return \c true if necessary
1049 // to extract prefix when generating new name for the
1050 // object(s) being created
1051 //================================================================
1052 bool MyGEOMBase_Helper::extractPrefix() const
1057 //================================================================
1058 // Function : getPrefix
1059 // Purpose : Get prefix for name of created object
1060 //================================================================
1061 QString MyGEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
1063 if ( !myPrefix.isEmpty() || theObj->_is_nil() )
1066 GEOM::shape_type aType = theObj->GetShapeType();
1070 case GEOM::VERTEX : return QObject::tr( "GEOM_VERTEX" );
1071 case GEOM::EDGE : return QObject::tr( "GEOM_EDGE" );
1072 case GEOM::WIRE : return QObject::tr( "GEOM_WIRE" );
1073 case GEOM::FACE : return QObject::tr( "GEOM_FACE" );
1074 case GEOM::SHELL : return QObject::tr( "GEOM_SHELL" );
1075 case GEOM::SOLID : return QObject::tr( "GEOM_SOLID" );
1076 case GEOM::COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
1077 case GEOM::COMPOUND : return QObject::tr( "GEOM_COMPOUND" );
1078 default : return "";
1082 //================================================================
1083 // Function : getDesktop
1084 // Purpose : Returns myDesktop field. Initialized in constructor,
1085 // usually as dynamic_cast<SUIT_Desktop*>(parentWidget())
1086 //================================================================
1087 SUIT_Desktop* MyGEOMBase_Helper::getDesktop() const
1092 //================================================================
1093 // Function : selectObjects
1094 // Purpose : Selects list of objects
1095 //================================================================
1096 bool MyGEOMBase_Helper::selectObjects( ObjectList& objects )
1098 SUIT_DataOwnerPtrList aList;
1099 ObjectList::iterator anIter;
1100 for ( anIter = objects.begin(); anIter != objects.end(); ++anIter )
1102 QString anEntry = getEntry( *anIter );
1103 LightApp_DataOwner* anOwher = new LightApp_DataOwner( anEntry );
1104 aList.append( anOwher );
1107 SUIT_Session* session = SUIT_Session::session();
1108 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1112 LightApp_SelectionMgr* aMgr = app->selectionMgr();
1116 aMgr->setSelected( aList, false );
1121 //================================================================
1122 // Function : findObjectInFather
1123 // Purpose : It should return an object if its founded in study or
1124 // return Null object if the object is not founded
1125 //================================================================
1126 GEOM::GEOM_Object_ptr MyGEOMBase_Helper::findObjectInFather (GEOM::GEOM_Object_ptr theFather,
1127 const QString& theName)
1129 SalomeApp_Application* app =
1130 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
1131 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1132 _PTR(Study) aDStudy = appStudy->studyDS();
1133 QString IOR = GEOMBase::GetIORFromObject( theFather );
1134 _PTR(SObject) SObj ( aDStudy->FindObjectIOR( IOR.toLatin1().constData() ) );
1136 bool inStudy = false;
1137 GEOM::GEOM_Object_var aReturnObject;
1138 for (_PTR(ChildIterator) iit (aDStudy->NewChildIterator( SObj )); iit->More() && !inStudy; iit->Next()) {
1139 _PTR(SObject) child (iit->Value());
1140 QString aChildName = child->GetName().c_str();
1141 if (aChildName == theName) {
1143 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(iit->Value());
1144 aReturnObject = GEOM::GEOM_Object::_narrow( corbaObj );
1148 return aReturnObject._retn();
1150 return GEOM::GEOM_Object::_nil();
1153 //================================================================
1154 // Function : findObjectInFather
1155 // Purpose : It should return an object if its founded in study or
1156 // return Null object if the object is not founded
1157 //================================================================
1158 GEOM::GEOM_Object_ptr MyGEOMBase_Helper::findObjectInFather( GEOM::GEOM_Object_ptr theFather,
1161 GEOM::GEOM_Object_var object;
1164 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
1166 _PTR(Study) studyDS = study->studyDS();
1167 QString IOR = GEOMBase::GetIORFromObject( theFather );
1168 _PTR(SObject) sobject( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
1170 _PTR(ChildIterator) it( studyDS->NewChildIterator( sobject ) );
1171 for ( ; it->More() && !found; it->Next() ) {
1172 GEOM::GEOM_Object_var cobject = GEOM::GEOM_Object::_narrow( GeometryGUI::ClientSObjectToObject( it->Value() ) );
1173 if ( !CORBA::is_nil( cobject ) ) {
1174 GEOM::ListOfLong_var indices = cobject->GetSubShapeIndices();
1175 int length = indices->length();
1176 for ( int i = 0; i < length && !found; i++ ) {
1177 if ( indices[i] == theIndex ) {
1187 return object._retn();
1190 //================================================================
1191 // Function : addSubshapesToStudy
1192 // Purpose : Virtual method to add subshapes if needs
1193 //================================================================
1194 void MyGEOMBase_Helper::addSubshapesToStudy()
1196 //Impemented in Dialogs, called from Accept method
1199 //================================================================
1200 // Function : getSelected
1201 // Purpose : Get selected object by specified type
1203 // Returns valid object if only one object of the specified type is selected
1204 // (no matter global or local selection is activated). If \a type is TopAbs_SHAPE,
1205 // geometrical object of any valid type is expected.
1207 // \param type type of the object to be obtained from selection
1208 // \return selected geometrical object or nil object if selection is not satisfactory
1209 //================================================================
1210 GEOM::GeomObjPtr MyGEOMBase_Helper::getSelected( TopAbs_ShapeEnum type )
1212 QList<TopAbs_ShapeEnum> types;
1214 return getSelected( types );
1217 //================================================================
1218 // Function : getSelected
1219 // Purpose : Get selected object by specified types
1221 // Returns valid object if only one object of the specified type is selected
1222 // (no matter global or local selection is activated). The list of allowed
1223 // shape types is passed via \a types. If \a types includes TopAbs_SHAPE,
1224 // geometrical object of any valid type is expected.
1226 // \param types list of allowed shape types for the objects to be obtained from selection
1227 // \return selected geometrical object or nil object if selection is not satisfactory
1228 //================================================================
1229 GEOM::GeomObjPtr MyGEOMBase_Helper::getSelected( const QList<TopAbs_ShapeEnum>& types )
1231 QList<GEOM::GeomObjPtr> selected = getSelected( types, 1 );
1232 return selected.count() > 0 ? selected[0] : GEOM::GeomObjPtr();
1235 //================================================================
1236 // Function : getSelected
1237 // Purpose : Get selected object(s) by specified type
1239 // Returns list of selected objects if selection satisfies specifies selection options.
1240 // (no matter global or local selection is activated). If \a type is TopAbs_SHAPE,
1241 // geometrical objects of any valid type are expected.
1243 // The \a type parameter specifies allowed type of the object(s) being selected.
1244 // The \a count parameter specifies exact number of the objects to be retrieved from selection.
1245 // The \a strict parameter specifies policy being applied to the selection.
1246 // If \a count < 0, then any number of the selected objects is valid (including 0).
1247 // In this case, if \a strict is \c true (default), all selected objects should satisfy
1248 // the specified \a type.
1249 // If \a count > 0, only specified number of the objects is retrieved from the selection.
1250 // In this case, if \a strict is \c true (default), function returns empty list if total number of selected
1251 // objects does not correspond to the \a count parameter. Otherwise (if \a strict is \c false),
1252 // function returns valid list of objects if at least \a count objects satisfy specified \a type.
1254 // \param type type of the object(s) to be obtained from selection
1255 // \param count number of items to be retrieved from selection
1256 // \param strict selection policy
1257 // \return list of selected geometrical objects or empty list if selection is not satisfactory
1258 //================================================================
1259 QList<GEOM::GeomObjPtr> MyGEOMBase_Helper::getSelected( TopAbs_ShapeEnum type, int count, bool strict )
1261 QList<TopAbs_ShapeEnum> types;
1263 return getSelected( types, count, strict );
1266 static bool typeInList( TopAbs_ShapeEnum type, const QList<TopAbs_ShapeEnum>& types )
1269 for ( int i = 0; i < types.count() && !ok; i++ )
1270 ok = types[i] == TopAbs_SHAPE || types[i] == type;
1274 //================================================================
1275 // Function : getSelected
1276 // Purpose : Get selected objects by specified types
1278 // Returns list of selected objects if selection satisfies specifies selection options.
1279 // (no matter global or local selection is activated). If \a types includes TopAbs_SHAPE,
1280 // geometrical objects of any valid type are expected.
1282 // The \a types parameter specifies allowed types of the object(s) being selected.
1283 // The \a count parameter specifies exact number of the objects to be retrieved from selection.
1284 // The \a strict parameter specifies policy being applied to the selection.
1285 // If \a count < 0, then any number of the selected objects is valid (including 0).
1286 // In this case, if \a strict is \c true (default), all selected objects should satisfy
1287 // the specified \a type.
1288 // If \a count > 0, only specified number of the objects is retrieved from the selection.
1289 // In this case, if \a strict is \c true (default), function returns empty list if total number of selected
1290 // objects does not correspond to the \a count parameter. Otherwise (if \a strict is \c false),
1291 // function returns valid list of objects if at least \a count objects satisfy specified \a type.
1293 // \param types list of allowed shape types for the objects to be obtained from selection
1294 // \param count number of items to be retrieved from selection
1295 // \param strict selection policy
1296 // \return list of selected geometrical objects or empty list if selection is not satisfactory
1297 //================================================================
1298 QList<GEOM::GeomObjPtr> MyGEOMBase_Helper::getSelected( const QList<TopAbs_ShapeEnum>& types, int count, bool strict )
1300 SUIT_Session* session = SUIT_Session::session();
1301 QList<GEOM::GeomObjPtr> result;
1303 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1305 // LightApp_SelectionMgr* selMgr = app->selectionMgr();
1307 if ( HEXABLOCKGUI::selectionMgr() != NULL) {
1308 SALOME_ListIO selected;
1310 //======================================
1311 // SUIT_DataOwnerPtrList selectedDataOwner;
1312 // AIS_ListOfInteractive listOfSelected;
1314 // if (HEXABLOCKGUI::currentOccSelector != NULL)
1315 // HEXABLOCKGUI::currentOccSelector->viewer()->getSelectedObjects(listOfSelected);
1317 // AIS_ListIteratorOfListOfInteractive it (listOfSelected);
1318 // while (it.More ())
1320 // //do something with the current item : it.Value ()
1321 // selected.Append(it.Value());
1324 //======================================
1326 // selMgr->selectedObjects( selected );
1327 HEXABLOCKGUI::selectionMgr()->selectedObjects( selected/*, "", false*/ );
1328 SALOME_ListIteratorOfListIO it( selected );
1329 bool stopped = false;
1330 for ( ; it.More() && !stopped; it.Next() ) {
1331 Handle(SALOME_InteractiveObject) IO = it.Value();
1332 GEOM::GeomObjPtr object = GEOMBase::ConvertIOinGEOMObject( IO );
1334 TColStd_IndexedMapOfInteger subShapes;
1335 HEXABLOCKGUI::selectionMgr()->GetIndexes( IO, subShapes );
1336 int nbSubShapes = subShapes.Extent();
1337 if ( nbSubShapes == 0 ) {
1339 if ( typeInList( (TopAbs_ShapeEnum)(object->GetShapeType()), types ) ) {
1342 if ( strict && result.count() > count ) {
1346 else if ( !strict && result.count() == count )
1350 else if ( strict ) {
1357 for ( int i = 1; i <= nbSubShapes && !stopped; i++ ) {
1358 int idx = subShapes( i );
1359 GEOM::GeomObjPtr subShape = findObjectInFather( object.get(), idx );
1361 // sub-shape is not yet published in the study
1362 GEOM::ShapesOpPtr shapesOp = getGeomEngine()->GetIShapesOperations( getStudyId() );
1363 subShape.take( shapesOp->GetSubShape( object.get(), idx ) ); // take ownership!
1365 if ( typeInList( (TopAbs_ShapeEnum)(subShape->GetShapeType()), types ) ) {
1368 if ( strict && result.count() > count ) {
1372 else if ( !strict && result.count() == count )
1376 else if ( strict ) {
1389 //================================================================
1390 // Function : setIsApplyAndClose
1391 // Purpose : Set value of the flag indicating that the dialog is
1392 // accepted by Apply & Close button
1393 //================================================================
1394 void MyGEOMBase_Helper::setIsApplyAndClose( const bool theFlag )
1396 myIsApplyAndClose = theFlag;
1399 //================================================================
1400 // Function : isApplyAndClose
1401 // Purpose : Get value of the flag indicating that the dialog is
1402 // accepted by Apply & Close button
1403 //================================================================
1404 bool MyGEOMBase_Helper::isApplyAndClose() const
1406 return myIsApplyAndClose;
1409 //================================================================
1410 // Function : setIsOptimizedBrowsing
1411 // Purpose : Set value of the flag switching to optimized
1412 // browsing mode (to select the first published
1414 //================================================================
1415 void MyGEOMBase_Helper::setIsOptimizedBrowsing( const bool theFlag )
1417 myIsOptimizedBrowsing = theFlag;
1420 //================================================================
1421 // Function : isOptimizedBrowsing
1422 // Purpose : Get value of the flag switching to optimized
1423 // browsing mode (to select the first published
1425 //================================================================
1426 bool MyGEOMBase_Helper::isOptimizedBrowsing() const
1428 return myIsOptimizedBrowsing;