1 // Copyright (C) 2007-2012 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 // File : GEOMGUI_Selection.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
26 #include "GEOMGUI_Selection.h"
28 #include "GeometryGUI.h"
29 #include "GEOM_Displayer.h"
31 #include <GEOM_Constants.h>
33 #include <SalomeApp_Application.h>
34 #include <SalomeApp_Study.h>
36 #include "LightApp_DataOwner.h"
38 #include <SUIT_Session.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_ViewWindow.h>
41 #include <SUIT_ViewManager.h>
43 #include <SALOME_Prs.h>
44 #include <SALOME_InteractiveObject.hxx>
48 #include <SALOME_Actor.h>
49 #include <GEOM_Actor.h>
51 #include <OCCViewer_ViewModel.h>
52 #include <SVTK_ViewModel.h>
54 #include <GEOMImpl_Types.hxx>
56 #include <GEOM_AISShape.hxx>
60 #include <AIS_InteractiveObject.hxx>
61 #include <AIS_ListOfInteractive.hxx>
64 #include <vtkActorCollection.h>
66 #define OCC_DISPLAY_MODE_TO_STRING( str, dm ) { \
67 if ( dm == AIS_WireFrame ) \
68 str = QString( "Wireframe" ); \
69 else if ( dm == AIS_Shaded ) \
70 str = QString( "Shading" ); \
71 else if ( dm == GEOM_AISShape::ShadingWithEdges ) \
72 str = QString( "ShadingWithEdges" ); \
73 else if ( dm == GEOM_AISShape::TexturedShape ) \
74 str = QString( "Texture" ); \
78 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
80 str = QString( "Wireframe" ); \
82 str = QString( "Shading" ); \
84 str = QString( "ShadingWithEdges" ); \
88 #define USE_VISUAL_PROP_MAP
90 GEOMGUI_Selection::GEOMGUI_Selection()
91 : LightApp_Selection()
95 GEOMGUI_Selection::~GEOMGUI_Selection()
99 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
101 LightApp_Selection::init( context, selMgr );
103 myObjects.resize( count() );
105 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
107 _PTR(Study) study = appStudy->studyDS();
108 for ( int idx = 0; idx < count(); idx++ ) {
109 QString anEntry = entry( idx );
110 if ( study && !anEntry.isEmpty() ) {
111 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
113 CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
114 myObjects[idx] = GEOM::GEOM_Object::_narrow( varObj );
121 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
122 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
126 v = activeViewType() == OCCViewer_Viewer::Type();
127 else if ( p == "selectionmode" )
129 else if ( p == "hasImported" )
131 else if ( p == "allImported" )
134 v = LightApp_Selection::parameter( p );
138 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
139 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
144 else if ( p == "typeid" )
146 else if ( p == "displaymode" )
147 v = displayMode( idx );
148 else if ( p == "isAutoColor" )
149 v = isAutoColor( idx );
150 else if ( p == "isVectorsMode" )
151 v = isVectorsMode( idx );
152 else if ( p == "topLevel" )
154 else if ( p == "hasHiddenChildren" )
155 v = hasHiddenChildren( idx );
156 else if ( p == "hasShownChildren" )
157 v = hasShownChildren( idx );
158 else if ( p == "compoundOfVertices" )
159 v = compoundOfVertices( idx );
160 else if ( p == "imported" )
161 v = isImported( idx );
163 v = LightApp_Selection::parameter( idx, p );
168 // the method to skip temporary objects from selection (called from LightApp)
169 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
171 return !theOwner->entry().contains("_");
174 QString GEOMGUI_Selection::typeName( const int index ) const
176 if ( isComponent( index ) )
179 static QString aGroup( "Group" );
180 static QString aShape( "Shape" );
181 static QString anUnknown( "Unknown" );
183 GEOM::GEOM_Object_var anObj = getObject( index );
184 if ( !CORBA::is_nil( anObj ) ) {
185 const int aGeomType = anObj->GetType();
186 if ( aGeomType == GEOM_GROUP )
194 int GEOMGUI_Selection::typeId( const int index ) const
197 GEOM::GEOM_Object_var anObj = getObject( index );
198 if ( !CORBA::is_nil( anObj ) )
199 aType = (int)anObj->GetShapeType();
203 bool GEOMGUI_Selection::isVisible( const int index ) const
207 #ifdef USE_VISUAL_PROP_MAP
209 QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
210 if ( v.canConvert( QVariant::Bool ) ) {
217 GEOM::GEOM_Object_var obj = getObject( index );
218 SALOME_View* view = GEOM_Displayer::GetActiveView();
219 if ( !CORBA::is_nil( obj ) && view ) {
220 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
221 res = view->isVisible( io );
223 #ifdef USE_VISUAL_PROP_MAP
230 bool GEOMGUI_Selection::isAutoColor( const int index ) const
232 GEOM::GEOM_Object_var obj = getObject( index );
233 if ( !CORBA::is_nil( obj ) )
234 return obj->GetAutoColor();
238 bool GEOMGUI_Selection::isImported( const int index ) const
240 GEOM::GEOM_Object_var obj = getObject( index );
241 if ( !CORBA::is_nil( obj ) )
242 return obj->GetType() == GEOM_IMPORT;
246 bool GEOMGUI_Selection::hasImported() const
249 for ( int i = 0; i < count() && !res; i++ )
250 res = isImported( i );
254 bool GEOMGUI_Selection::allImported() const
257 for ( int i = 0; i < count() && res; i++ )
258 res = isImported( i );
262 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
265 LightApp_Study* aStudy = study();
267 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
268 if ( anApp && anApp->activeViewManager() ) {
269 int id = anApp->activeViewManager()->getGlobalId();
270 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
276 QString GEOMGUI_Selection::displayMode( const int index ) const
279 QString viewType = activeViewType();
280 #ifdef USE_VISUAL_PROP_MAP
281 QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
282 if ( v.canConvert( QVariant::Int ) ) {
284 if ( viewType == OCCViewer_Viewer::Type() ) {
285 OCC_DISPLAY_MODE_TO_STRING( res, dm );
286 } else if ( viewType == SVTK_Viewer::Type() ) {
287 VTK_DISPLAY_MODE_TO_STRING( res, dm );
291 if ( res.isEmpty() ) {
293 SALOME_View* view = GEOM_Displayer::GetActiveView();
294 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
295 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
297 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
298 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
299 AIS_ListOfInteractive lst;
300 occPrs->GetObjects( lst );
301 if ( lst.Extent() ) {
302 Handle(AIS_InteractiveObject) io = lst.First();
303 if ( !io.IsNull() ) {
304 int dm = io->DisplayMode();
305 OCC_DISPLAY_MODE_TO_STRING( res, dm );
306 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
307 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
308 desktop()->activeWindow()->getViewManager()->getViewModel();
309 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
310 dm = ic->DisplayMode();
311 OCC_DISPLAY_MODE_TO_STRING( res, dm );
316 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
317 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
318 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
320 lst->InitTraversal();
321 vtkActor* actor = lst->GetNextActor();
323 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
325 int dm = salActor->getDisplayMode();
326 VTK_DISPLAY_MODE_TO_STRING( res, dm );
327 } // if ( salome actor )
329 } // if ( lst == vtkPrs->GetObjects() )
334 #ifdef USE_VISUAL_PROP_MAP
341 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
345 #ifdef USE_VISUAL_PROP_MAP
347 QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
348 if ( v.canConvert( QVariant::Bool ) ) {
355 SALOME_View* view = GEOM_Displayer::GetActiveView();
356 QString viewType = activeViewType();
357 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
358 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
360 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
361 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
362 AIS_ListOfInteractive lst;
363 occPrs->GetObjects( lst );
364 if ( lst.Extent() ) {
365 Handle(AIS_InteractiveObject) io = lst.First();
366 if ( !io.IsNull() ) {
367 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
369 res = aSh->isShowVectors();
372 } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
373 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
374 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
376 lst->InitTraversal();
377 vtkActor* actor = lst->GetNextActor();
379 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
381 res = aGeomActor->GetVectorMode();
387 #ifdef USE_VISUAL_PROP_MAP
394 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
398 _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
399 for ( ; it->More() && !ok; it->Next() ) {
400 _PTR(SObject) child = it->Value();
402 _PTR(SObject) refObj;
403 if ( child->ReferencedObject( refObj ) ) continue; // omit references
404 if ( child->GetName() != "" ) ok = true;
411 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
414 _PTR(GenericAttribute) anAttr;
415 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
416 _PTR(AttributeExpandable) aAttrExp = anAttr;
417 exp = aAttrExp->IsExpandable();
422 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
426 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
427 (SUIT_Session::session()->activeApplication()->activeStudy());*/
428 if ( /*appStudy && */!CORBA::is_nil( obj ) )
429 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
433 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
436 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
439 QString anEntry = entry( index );
440 _PTR(Study) study = appStudy->studyDS();
441 if ( study && !anEntry.isEmpty() ) {
442 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
443 OK = !expandable( aSO ) && hasChildren( aSO );
449 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
452 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
455 QString anEntry = entry( index );
456 _PTR(Study) study = appStudy->studyDS();
457 if ( study && !anEntry.isEmpty() ) {
458 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
459 OK = expandable( aSO ) && hasChildren( aSO );
465 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
467 GEOM::GEOM_Object_var obj = getObject( index );
468 return isCompoundOfVertices( obj );
471 bool GEOMGUI_Selection::isComponent( const int index ) const
473 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
476 QString anEntry = entry( index );
477 _PTR(Study) study = appStudy->studyDS();
478 if ( study && !anEntry.isNull() ) {
479 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
480 if ( aSO && aSO->GetFatherComponent() )
481 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
487 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
489 GEOM::GEOM_Object_var o;
490 if ( 0 <= index && index < myObjects.size() )
491 o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
495 QString GEOMGUI_Selection::selectionMode() const
497 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
499 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
501 switch ( aGeomGUI->getLocalSelectionMode() )
503 case GEOM_POINT : return "VERTEX";
504 case GEOM_EDGE : return "EDGE";
505 case GEOM_WIRE : return "WIRE";
506 case GEOM_FACE : return "FACE";
507 case GEOM_SHELL : return "SHELL";
508 case GEOM_SOLID : return "SOLID";
509 case GEOM_COMPOUND : return "COMPOUND";
510 case GEOM_ALLOBJECTS : return "ALL";
518 bool GEOMGUI_Selection::topLevel( const int index ) const {
521 #ifdef USE_VISUAL_PROP_MAP
523 QVariant v = visibleProperty( entry( index ), TOP_LEVEL_PROP );
524 if ( v.canConvert<bool>() ) {
531 SALOME_View* view = GEOM_Displayer::GetActiveView();
532 QString viewType = activeViewType();
533 if ( view && viewType == OCCViewer_Viewer::Type() ) {
534 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
536 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
537 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
538 AIS_ListOfInteractive lst;
539 occPrs->GetObjects( lst );
540 if ( lst.Extent() ) {
541 Handle(AIS_InteractiveObject) io = lst.First();
542 if ( !io.IsNull() ) {
543 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
545 res = (bool)aSh->isTopLevel();