1 // Copyright (C) 2007-2011 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 == "hasHiddenChildren" )
153 v = hasHiddenChildren( idx );
154 else if ( p == "hasShownChildren" )
155 v = hasShownChildren( idx );
156 else if ( p == "compoundOfVertices" )
157 v = compoundOfVertices( idx );
158 else if ( p == "imported" )
159 v = isImported( idx );
161 v = LightApp_Selection::parameter( idx, p );
166 // the method to skip temporary objects from selection (called from LightApp)
167 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
169 return !theOwner->entry().contains("_");
172 QString GEOMGUI_Selection::typeName( const int index ) const
174 if ( isComponent( index ) )
177 static QString aGroup( "Group" );
178 static QString aShape( "Shape" );
179 static QString anUnknown( "Unknown" );
181 GEOM::GEOM_Object_var anObj = getObject( index );
182 if ( !CORBA::is_nil( anObj ) ) {
183 const int aGeomType = anObj->GetType();
184 if ( aGeomType == GEOM_GROUP )
192 int GEOMGUI_Selection::typeId( const int index ) const
195 GEOM::GEOM_Object_var anObj = getObject( index );
196 if ( !CORBA::is_nil( anObj ) )
197 aType = (int)anObj->GetShapeType();
201 bool GEOMGUI_Selection::isVisible( const int index ) const
205 #ifdef USE_VISUAL_PROP_MAP
207 QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
208 if ( v.canConvert( QVariant::Bool ) ) {
215 GEOM::GEOM_Object_var obj = getObject( index );
216 SALOME_View* view = GEOM_Displayer::GetActiveView();
217 if ( !CORBA::is_nil( obj ) && view ) {
218 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
219 res = view->isVisible( io );
221 #ifdef USE_VISUAL_PROP_MAP
228 bool GEOMGUI_Selection::isAutoColor( const int index ) const
230 GEOM::GEOM_Object_var obj = getObject( index );
231 if ( !CORBA::is_nil( obj ) )
232 return obj->GetAutoColor();
236 bool GEOMGUI_Selection::isImported( const int index ) const
238 GEOM::GEOM_Object_var obj = getObject( index );
239 if ( !CORBA::is_nil( obj ) )
240 return obj->GetType() == GEOM_IMPORT;
244 bool GEOMGUI_Selection::hasImported() const
247 for ( int i = 0; i < count() && !res; i++ )
248 res = isImported( i );
252 bool GEOMGUI_Selection::allImported() const
255 for ( int i = 0; i < count() && res; i++ )
256 res = isImported( i );
260 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
263 LightApp_Study* aStudy = study();
265 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
266 if ( anApp && anApp->activeViewManager() ) {
267 int id = anApp->activeViewManager()->getGlobalId();
268 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
274 QString GEOMGUI_Selection::displayMode( const int index ) const
277 QString viewType = activeViewType();
278 #ifdef USE_VISUAL_PROP_MAP
279 QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
280 if ( v.canConvert( QVariant::Int ) ) {
282 if ( viewType == OCCViewer_Viewer::Type() ) {
283 OCC_DISPLAY_MODE_TO_STRING( res, dm );
284 } else if ( viewType == SVTK_Viewer::Type() ) {
285 VTK_DISPLAY_MODE_TO_STRING( res, dm );
289 if ( res.isEmpty() ) {
291 SALOME_View* view = GEOM_Displayer::GetActiveView();
292 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
293 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
295 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
296 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
297 AIS_ListOfInteractive lst;
298 occPrs->GetObjects( lst );
299 if ( lst.Extent() ) {
300 Handle(AIS_InteractiveObject) io = lst.First();
301 if ( !io.IsNull() ) {
302 int dm = io->DisplayMode();
303 OCC_DISPLAY_MODE_TO_STRING( res, dm );
304 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
305 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
306 desktop()->activeWindow()->getViewManager()->getViewModel();
307 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
308 dm = ic->DisplayMode();
309 OCC_DISPLAY_MODE_TO_STRING( res, dm );
314 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
315 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
316 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
318 lst->InitTraversal();
319 vtkActor* actor = lst->GetNextActor();
321 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
323 int dm = salActor->getDisplayMode();
324 VTK_DISPLAY_MODE_TO_STRING( res, dm );
325 } // if ( salome actor )
327 } // if ( lst == vtkPrs->GetObjects() )
332 #ifdef USE_VISUAL_PROP_MAP
339 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
343 #ifdef USE_VISUAL_PROP_MAP
345 QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
346 if ( v.canConvert( QVariant::Bool ) ) {
353 SALOME_View* view = GEOM_Displayer::GetActiveView();
354 QString viewType = activeViewType();
355 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
356 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
358 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
359 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
360 AIS_ListOfInteractive lst;
361 occPrs->GetObjects( lst );
362 if ( lst.Extent() ) {
363 Handle(AIS_InteractiveObject) io = lst.First();
364 if ( !io.IsNull() ) {
365 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
367 res = aSh->isShowVectors();
370 } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
371 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
372 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
374 lst->InitTraversal();
375 vtkActor* actor = lst->GetNextActor();
377 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
379 res = aGeomActor->GetVectorMode();
385 #ifdef USE_VISUAL_PROP_MAP
392 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
396 _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
397 for ( ; it->More() && !ok; it->Next() ) {
398 _PTR(SObject) child = it->Value();
400 _PTR(SObject) refObj;
401 if ( child->ReferencedObject( refObj ) ) continue; // omit references
402 if ( child->GetName() != "" ) ok = true;
409 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
412 _PTR(GenericAttribute) anAttr;
413 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
414 _PTR(AttributeExpandable) aAttrExp = anAttr;
415 exp = aAttrExp->IsExpandable();
420 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
424 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
425 (SUIT_Session::session()->activeApplication()->activeStudy());*/
426 if ( /*appStudy && */!CORBA::is_nil( obj ) )
427 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
431 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
434 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
437 QString anEntry = entry( index );
438 _PTR(Study) study = appStudy->studyDS();
439 if ( study && !anEntry.isEmpty() ) {
440 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
441 OK = !expandable( aSO ) && hasChildren( aSO );
447 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
450 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
453 QString anEntry = entry( index );
454 _PTR(Study) study = appStudy->studyDS();
455 if ( study && !anEntry.isEmpty() ) {
456 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
457 OK = expandable( aSO ) && hasChildren( aSO );
463 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
465 GEOM::GEOM_Object_var obj = getObject( index );
466 return isCompoundOfVertices( obj );
469 bool GEOMGUI_Selection::isComponent( const int index ) const
471 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
474 QString anEntry = entry( index );
475 _PTR(Study) study = appStudy->studyDS();
476 if ( study && !anEntry.isNull() ) {
477 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
478 if ( aSO && aSO->GetFatherComponent() )
479 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
485 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
487 GEOM::GEOM_Object_var o;
488 if ( 0 <= index && index < myObjects.size() )
489 o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
493 QString GEOMGUI_Selection::selectionMode() const
495 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
497 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
499 switch ( aGeomGUI->getLocalSelectionMode() )
501 case GEOM_POINT : return "VERTEX";
502 case GEOM_EDGE : return "EDGE";
503 case GEOM_WIRE : return "WIRE";
504 case GEOM_FACE : return "FACE";
505 case GEOM_SHELL : return "SHELL";
506 case GEOM_SOLID : return "SOLID";
507 case GEOM_COMPOUND : return "COMPOUND";
508 case GEOM_ALLOBJECTS : return "ALL";