Salome HOME
This file in LightApp package
[modules/gui.git] / src / SalomeApp / SalomeApp_Selection.cxx
1
2 #include "SalomeApp_Selection.h"
3 #include "SalomeApp_SelectionMgr.h"
4 #include "SalomeApp_DataOwner.h"
5 #include "SalomeApp_Study.h"
6 #include "SalomeApp_Application.h"
7 #include "SalomeApp_Displayer.h"
8
9 #include "SUIT_Session.h"
10 #include "SUIT_ViewWindow.h"
11
12 /*!
13   Constructor
14 */
15 SalomeApp_Selection::SalomeApp_Selection()
16 : myStudy( 0 )
17 {
18 }
19
20 /*!
21   Destructor.
22 */
23 SalomeApp_Selection::~SalomeApp_Selection()
24 {
25 }
26
27 /*!
28   Initializetion.
29 */
30 void SalomeApp_Selection::init( const QString& client, SalomeApp_SelectionMgr* mgr)
31 {
32   myPopupClient = client;
33   
34   if( mgr )
35   {
36     if ( mgr->application() )
37       myStudy = dynamic_cast<SalomeApp_Study*>( mgr->application()->activeStudy() );
38
39     if( !myStudy )
40       return;
41
42     _PTR(Study) studyds = myStudy->studyDS();
43     _PTR(SObject) refobj;
44
45     SUIT_DataOwnerPtrList sel;
46     mgr->selected( sel, client );
47     SUIT_DataOwnerPtrList::const_iterator anIt = sel.begin(), aLast = sel.end();
48     for( ; anIt!=aLast; anIt++ )
49     {
50       SUIT_DataOwner* owner = ( SUIT_DataOwner* )( (*anIt ).get() );
51       SalomeApp_DataOwner* sowner = dynamic_cast<SalomeApp_DataOwner*>( owner );
52       if( sowner )
53       {
54         _PTR(SObject) obj = studyds->FindObjectID( sowner->entry() );
55         if( obj->ReferencedObject( refobj ) )
56         {
57           myEntries.append( refobj->GetID() );
58           myIsReferences.append( true );
59         }
60         else
61         {
62           myEntries.append( sowner->entry() );
63           myIsReferences.append( false );
64         }
65         processOwner( sowner );
66       }
67     }
68   }
69 }
70
71 /*!
72   Gets count of entries.
73 */
74 int SalomeApp_Selection::count() const
75 {
76   return myEntries.count();
77 }
78
79 /*!
80   Gets QtxValue();
81 */
82 QtxValue SalomeApp_Selection::param( const int ind, const QString& p ) const
83 {
84   if( !( ind>=0 && ind<count() ) )
85     return QtxValue();
86
87   if( p=="isVisible" )
88   {
89     SalomeApp_Displayer d;
90     bool vis = d.IsDisplayed( myEntries[ ind ] );
91     return QtxValue( vis, 0 );
92   }
93   else if( p=="component" )
94   {
95     _PTR(SObject) obj( study()->studyDS()->FindObjectID( myEntries[ ind ].latin1() ) );
96     _PTR(SComponent) comp = obj->GetFatherComponent();
97     QString mod_name = comp->ComponentDataType().c_str();
98     //cout << "component : " << ind << " >> " << mod_name.latin1() << endl;
99     if( !mod_name.isEmpty() )
100       return mod_name;
101   }
102   else if( p=="isReference" )
103     return QtxValue( isReference( ind ), false );
104
105   return QtxValue();
106 }
107
108 /*!
109   Gets global parameters. client, isActiveView, activeView etc.
110 */
111 QtxValue SalomeApp_Selection::globalParam( const QString& p ) const
112 {
113   if      ( p == "client" )        return QtxValue( myPopupClient );
114   else if ( p == "activeModule" )
115   {
116     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( myStudy->application() );
117     QString mod_name = app ? QString( app->activeModule()->name() ) : QString::null;
118     //cout << "activeModule : " << mod_name.latin1() << endl;
119     if( !mod_name.isEmpty() )
120       return mod_name;
121     else
122       return QtxValue();
123   }
124   else if ( p == "isActiveView" )  return QtxValue( (bool)activeVW() );
125   else if ( p == "activeView" )    return QtxValue( activeViewType() );
126 #ifndef WNT
127   else                             return QtxPopupMgr::Selection::globalParam( p );
128 #else
129   else                             return Selection::globalParam( p );
130 #endif
131 }
132
133 /*!
134   Do nothing.
135 */
136 void SalomeApp_Selection::processOwner( const SalomeApp_DataOwner* )
137 {
138 }
139
140 /*!
141   Gets entry with index \a index.
142 */
143 QString SalomeApp_Selection::entry( const int index ) const
144 {
145   if ( index >= 0 && index < count() )
146     return myEntries[ index ];
147   return QString();
148 }
149
150 /*!
151   Returns true if i-th selected object was reference to object with entry( i )
152 */
153 bool SalomeApp_Selection::isReference( const int index ) const
154 {
155   if( index >= 0 && index < count() )
156     return myIsReferences[ index ];
157   else
158     return false;
159 }
160
161 /*!
162   Gets type of active view manager.
163 */
164 QString SalomeApp_Selection::activeViewType() const
165 {
166   SUIT_ViewWindow* win = activeVW();
167   if ( win ) {
168     SUIT_ViewManager* vm = win->getViewManager();
169     if ( vm )
170       return vm->getType();
171   }
172   return QString::null;
173 }
174
175 /*!
176   Gets active view window.
177 */
178 SUIT_ViewWindow* SalomeApp_Selection::activeVW() const
179 {
180   SUIT_Session* session = SUIT_Session::session();
181   if ( session ) {
182     SUIT_Application* app = session->activeApplication();
183     if ( app ) {
184       SUIT_Desktop* desk = app->desktop();
185       if ( desk ) 
186         return desk->activeWindow();
187     }
188   }
189   return 0;
190 }