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 <SalomeApp_Application.h>
32 #include <SalomeApp_Study.h>
34 #include "LightApp_DataOwner.h"
36 #include <SUIT_Session.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_ViewWindow.h>
39 #include <SUIT_ViewManager.h>
41 #include <SALOME_Prs.h>
42 #include <SALOME_InteractiveObject.hxx>
46 #include <SALOME_Actor.h>
47 #include <GEOM_Actor.h>
49 #include <OCCViewer_ViewModel.h>
50 #include <SVTK_ViewModel.h>
52 #include <GEOMImpl_Types.hxx>
54 #include <GEOM_AISShape.hxx>
58 #include <AIS_InteractiveObject.hxx>
59 #include <AIS_ListOfInteractive.hxx>
62 #include <vtkActorCollection.h>
64 #define OCC_DISPLAY_MODE_TO_STRING( str, dm ) { \
65 if ( dm == AIS_WireFrame ) \
66 str = QString( "Wireframe" ); \
67 else if ( dm == AIS_Shaded ) \
68 str = QString( "Shading" ); \
72 #define VTK_DISPLAY_MODE_TO_STRING( str, dm ) { \
74 str = QString( "Wireframe" ); \
76 str = QString( "Shading" ); \
80 #define USE_VISUAL_PROP_MAP
82 GEOMGUI_Selection::GEOMGUI_Selection()
83 : LightApp_Selection()
87 GEOMGUI_Selection::~GEOMGUI_Selection()
91 void GEOMGUI_Selection::init( const QString& context, LightApp_SelectionMgr* selMgr )
93 LightApp_Selection::init( context, selMgr );
95 myObjects.resize( count() );
97 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
99 _PTR(Study) study = appStudy->studyDS();
100 for ( int idx = 0; idx < count(); idx++ ) {
101 QString anEntry = entry( idx );
102 if ( study && !anEntry.isEmpty() ) {
103 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
105 CORBA::Object_var varObj = GeometryGUI::ClientSObjectToObject( aSO );
106 myObjects[idx] = GEOM::GEOM_Object::_narrow( varObj );
113 //QVariant GEOMGUI_Selection::contextParameter( const QString& p ) const
114 QVariant GEOMGUI_Selection::parameter( const QString& p ) const
118 v = activeViewType() == OCCViewer_Viewer::Type();
119 else if ( p == "selectionmode" )
121 else if ( p == "hasImported" )
123 else if ( p == "allImported" )
126 v = LightApp_Selection::parameter( p );
130 //QVariant GEOMGUI_Selection::objectParameter( const int idx, const QString& p ) const
131 QVariant GEOMGUI_Selection::parameter( const int idx, const QString& p ) const
136 else if ( p == "typeid" )
138 else if ( p == "displaymode" )
139 v = displayMode( idx );
140 else if ( p == "isAutoColor" )
141 v = isAutoColor( idx );
142 else if ( p == "isVectorsMode" )
143 v = isVectorsMode( idx );
144 else if ( p == "hasHiddenChildren" )
145 v = hasHiddenChildren( idx );
146 else if ( p == "hasShownChildren" )
147 v = hasShownChildren( idx );
148 else if ( p == "compoundOfVertices" )
149 v = compoundOfVertices( idx );
150 else if ( p == "imported" )
151 v = isImported( idx );
153 v = LightApp_Selection::parameter( idx, p );
158 // the method to skip temporary objects from selection (called from LightApp)
159 bool GEOMGUI_Selection::processOwner( const LightApp_DataOwner* theOwner )
161 return !theOwner->entry().contains("_");
164 QString GEOMGUI_Selection::typeName( const int index ) const
166 if ( isComponent( index ) )
169 static QString aGroup( "Group" );
170 static QString aShape( "Shape" );
171 static QString anUnknown( "Unknown" );
173 GEOM::GEOM_Object_var anObj = getObject( index );
174 if ( !CORBA::is_nil( anObj ) ) {
175 const int aGeomType = anObj->GetType();
176 if ( aGeomType == GEOM_GROUP )
184 int GEOMGUI_Selection::typeId( const int index ) const
187 GEOM::GEOM_Object_var anObj = getObject( index );
188 if ( !CORBA::is_nil( anObj ) )
189 aType = (int)anObj->GetShapeType();
193 bool GEOMGUI_Selection::isVisible( const int index ) const
197 #ifdef USE_VISUAL_PROP_MAP
199 QVariant v = visibleProperty( entry( index ), VISIBILITY_PROP );
200 if ( v.canConvert( QVariant::Bool ) ) {
207 GEOM::GEOM_Object_var obj = getObject( index );
208 SALOME_View* view = GEOM_Displayer::GetActiveView();
209 if ( !CORBA::is_nil( obj ) && view ) {
210 Handle(SALOME_InteractiveObject) io = new SALOME_InteractiveObject( entry( index ).toLatin1().constData(), "GEOM", "TEMP_IO" );
211 res = view->isVisible( io );
213 #ifdef USE_VISUAL_PROP_MAP
220 bool GEOMGUI_Selection::isAutoColor( const int index ) const
222 GEOM::GEOM_Object_var obj = getObject( index );
223 if ( !CORBA::is_nil( obj ) )
224 return obj->GetAutoColor();
228 bool GEOMGUI_Selection::isImported( const int index ) const
230 GEOM::GEOM_Object_var obj = getObject( index );
231 if ( !CORBA::is_nil( obj ) )
232 return obj->GetType() == GEOM_IMPORT;
236 bool GEOMGUI_Selection::hasImported() const
239 for ( int i = 0; i < count() && !res; i++ )
240 res = isImported( i );
244 bool GEOMGUI_Selection::allImported() const
247 for ( int i = 0; i < count() && res; i++ )
248 res = isImported( i );
252 QVariant GEOMGUI_Selection::visibleProperty( const QString& entry, const QString& propName ) const
255 LightApp_Study* aStudy = study();
257 LightApp_Application* anApp = ::qobject_cast<LightApp_Application*>( aStudy->application() );
258 if ( anApp && anApp->activeViewManager() ) {
259 int id = anApp->activeViewManager()->getGlobalId();
260 v = aStudy->getObjectProperty( id, entry, propName, QVariant() );
266 QString GEOMGUI_Selection::displayMode( const int index ) const
269 QString viewType = activeViewType();
270 #ifdef USE_VISUAL_PROP_MAP
271 QVariant v = visibleProperty( entry( index ), DISPLAY_MODE_PROP );
272 if ( v.canConvert( QVariant::Int ) ) {
274 if ( viewType == OCCViewer_Viewer::Type() ) {
275 OCC_DISPLAY_MODE_TO_STRING( res, dm );
276 } else if ( viewType == SVTK_Viewer::Type() ) {
277 VTK_DISPLAY_MODE_TO_STRING( res, dm );
281 if ( res.isEmpty() ) {
283 SALOME_View* view = GEOM_Displayer::GetActiveView();
284 if ( view /*fix for 9320==>*/&& ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
285 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
287 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
288 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
289 AIS_ListOfInteractive lst;
290 occPrs->GetObjects( lst );
291 if ( lst.Extent() ) {
292 Handle(AIS_InteractiveObject) io = lst.First();
293 if ( !io.IsNull() ) {
294 int dm = io->DisplayMode();
295 OCC_DISPLAY_MODE_TO_STRING( res, dm );
296 if ( res.isEmpty() ) { // return default display mode of AIS_InteractiveContext
297 OCCViewer_Viewer* occViewer = (OCCViewer_Viewer*)SUIT_Session::session()->activeApplication()->
298 desktop()->activeWindow()->getViewManager()->getViewModel();
299 Handle(AIS_InteractiveContext) ic = occViewer->getAISContext();
300 dm = ic->DisplayMode();
301 OCC_DISPLAY_MODE_TO_STRING( res, dm );
306 else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
307 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
308 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
310 lst->InitTraversal();
311 vtkActor* actor = lst->GetNextActor();
313 SALOME_Actor* salActor = dynamic_cast<SALOME_Actor*>( actor );
315 int dm = salActor->getDisplayMode();
316 VTK_DISPLAY_MODE_TO_STRING( res, dm );
317 } // if ( salome actor )
319 } // if ( lst == vtkPrs->GetObjects() )
324 #ifdef USE_VISUAL_PROP_MAP
331 bool GEOMGUI_Selection::isVectorsMode( const int index ) const
335 #ifdef USE_VISUAL_PROP_MAP
337 QVariant v = visibleProperty( entry( index ), VECTOR_MODE_PROP );
338 if ( v.canConvert( QVariant::Bool ) ) {
345 SALOME_View* view = GEOM_Displayer::GetActiveView();
346 QString viewType = activeViewType();
347 if ( view && ( viewType == OCCViewer_Viewer::Type() || viewType == SVTK_Viewer::Type() ) ) {
348 SALOME_Prs* prs = view->CreatePrs( entry( index ).toLatin1().constData() );
350 if ( viewType == OCCViewer_Viewer::Type() ) { // assuming OCC
351 SOCC_Prs* occPrs = (SOCC_Prs*) prs;
352 AIS_ListOfInteractive lst;
353 occPrs->GetObjects( lst );
354 if ( lst.Extent() ) {
355 Handle(AIS_InteractiveObject) io = lst.First();
356 if ( !io.IsNull() ) {
357 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(io);
359 res = aSh->isShowVectors();
362 } else if ( viewType == SVTK_Viewer::Type() ) { // assuming VTK
363 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
364 vtkActorCollection* lst = vtkPrs ? vtkPrs->GetObjects() : 0;
366 lst->InitTraversal();
367 vtkActor* actor = lst->GetNextActor();
369 GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(actor);
371 res = aGeomActor->GetVectorMode();
377 #ifdef USE_VISUAL_PROP_MAP
384 bool GEOMGUI_Selection::hasChildren( const _PTR(SObject)& obj )
388 _PTR(ChildIterator) it ( obj->GetStudy()->NewChildIterator( obj ) );
389 for ( ; it->More() && !ok; it->Next() ) {
390 _PTR(SObject) child = it->Value();
392 _PTR(SObject) refObj;
393 if ( child->ReferencedObject( refObj ) ) continue; // omit references
394 if ( child->GetName() != "" ) ok = true;
401 bool GEOMGUI_Selection::expandable( const _PTR(SObject)& obj )
404 _PTR(GenericAttribute) anAttr;
405 if ( obj && obj->FindAttribute( anAttr, "AttributeExpandable" ) ) {
406 _PTR(AttributeExpandable) aAttrExp = anAttr;
407 exp = aAttrExp->IsExpandable();
412 bool GEOMGUI_Selection::isCompoundOfVertices( GEOM::GEOM_Object_ptr obj )
416 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
417 (SUIT_Session::session()->activeApplication()->activeStudy());*/
418 if ( /*appStudy && */!CORBA::is_nil( obj ) )
419 ret = obj->GetShapeType() == GEOM::COMPOUND && obj->GetMaxShapeType() == GEOM::VERTEX;
423 bool GEOMGUI_Selection::hasHiddenChildren( const int index ) const
426 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
429 QString anEntry = entry( index );
430 _PTR(Study) study = appStudy->studyDS();
431 if ( study && !anEntry.isEmpty() ) {
432 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
433 OK = !expandable( aSO ) && hasChildren( aSO );
439 bool GEOMGUI_Selection::hasShownChildren( const int index ) const
442 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
445 QString anEntry = entry( index );
446 _PTR(Study) study = appStudy->studyDS();
447 if ( study && !anEntry.isEmpty() ) {
448 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
449 OK = expandable( aSO ) && hasChildren( aSO );
455 bool GEOMGUI_Selection::compoundOfVertices( const int index ) const
457 GEOM::GEOM_Object_var obj = getObject( index );
458 return isCompoundOfVertices( obj );
461 bool GEOMGUI_Selection::isComponent( const int index ) const
463 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( study() );
466 QString anEntry = entry( index );
467 _PTR(Study) study = appStudy->studyDS();
468 if ( study && !anEntry.isNull() ) {
469 _PTR(SObject) aSO( study->FindObjectID( anEntry.toStdString() ) );
470 if ( aSO && aSO->GetFatherComponent() )
471 return aSO->GetFatherComponent()->GetIOR() == aSO->GetIOR();
477 GEOM::GEOM_Object_ptr GEOMGUI_Selection::getObject( const int index ) const
479 GEOM::GEOM_Object_var o;
480 if ( 0 <= index && index < myObjects.size() )
481 o = GEOM::GEOM_Object::_duplicate( myObjects[index] );
485 QString GEOMGUI_Selection::selectionMode() const
487 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( study()->application() );
489 GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
491 switch ( aGeomGUI->getLocalSelectionMode() )
493 case GEOM_POINT : return "VERTEX";
494 case GEOM_EDGE : return "EDGE";
495 case GEOM_WIRE : return "WIRE";
496 case GEOM_FACE : return "FACE";
497 case GEOM_SHELL : return "SHELL";
498 case GEOM_SOLID : return "SOLID";
499 case GEOM_COMPOUND : return "COMPOUND";
500 case GEOM_ALLOBJECTS : return "ALL";