Salome HOME
0e3458f8fb7fc8093e85510c45739f97ca1a5ccd
[modules/gui.git] / src / LightApp / LightApp_Selection.cxx
1
2 #include "LightApp_Selection.h"
3 #include "LightApp_SelectionMgr.h"
4 #include "LightApp_DataOwner.h"
5 #include "LightApp_Study.h"
6 #include "LightApp_Application.h"
7 #include "LightApp_Displayer.h"
8
9 #include "SUIT_Session.h"
10 #include "SUIT_ViewWindow.h"
11
12 /*!
13   Constructor
14 */
15 LightApp_Selection::LightApp_Selection()
16 : myStudy( 0 )
17 {
18 }
19
20 /*!
21   Destructor.
22 */
23 LightApp_Selection::~LightApp_Selection()
24 {
25 }
26
27 /*!
28   Initialization.
29 */
30 void LightApp_Selection::init( const QString& client, LightApp_SelectionMgr* mgr)
31 {
32   myPopupClient = client;
33   myStudy = 0;
34   
35   if( mgr )
36   {
37     if( mgr->application() )
38       myStudy = dynamic_cast<LightApp_Study*>( mgr->application()->activeStudy() );
39     if( !myStudy )
40       return;
41
42     //1) to take owners from current popup client
43     SUIT_DataOwnerPtrList sel( true ), cur_sel( true );
44     mgr->selected( sel, client );
45
46     //2) to take such owners from other popup clients that it's entry is different with every entry from current list
47     QPtrList<SUIT_Selector> aSelectors;
48     mgr->selectors( aSelectors );
49     for( SUIT_Selector* selector = aSelectors.first(); selector; selector = aSelectors.next() )
50       if( selector->type()!=client )
51       {
52         mgr->selected( cur_sel, selector->type() );
53         SUIT_DataOwnerPtrList::const_iterator aLIt = cur_sel.begin(), aLLast = cur_sel.end();
54         for( ; aLIt!=aLLast; aLIt++ )
55           sel.append( *aLIt ); //check entry and don't append if such entry is in list already
56       }
57
58     //3) to analyse owner and fill internal data structures
59     SUIT_DataOwnerPtrList::const_iterator anIt = sel.begin(), aLast = sel.end();
60     QString entry;
61     for( ; anIt!=aLast; anIt++ )
62     {
63       LightApp_DataOwner* sowner = dynamic_cast<LightApp_DataOwner*>( (*anIt ).get() );
64       if( sowner )
65       {
66         entry = myStudy->referencedToEntry( sowner->entry() );
67         myEntries.append( entry );
68         myIsReferences.append( sowner->entry() == entry );
69         processOwner( sowner );
70       }
71     }
72   }
73 }
74
75 /*!
76   Gets count of entries.
77 */
78 int LightApp_Selection::count() const
79 {
80   return myEntries.count();
81 }
82
83 /*!
84   Gets QtxValue();
85 */
86 QtxValue LightApp_Selection::param( const int ind, const QString& p ) const
87 {
88   LightApp_Application* app = dynamic_cast<LightApp_Application*>( myStudy ? myStudy->application() : 0 );
89   if( !( ind>=0 && ind<count() ) || !app )
90     return QtxValue();
91
92   if( p=="isVisible" )
93   {
94     QString mod_name = app->moduleTitle( param( ind, "component" ).toString() );
95     LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
96     // false in last parameter means that now we doesn't load module, if it isn't loaded
97
98     bool vis = false;
99     if( d )
100       vis = d->IsDisplayed( myEntries[ ind ] );
101     else
102     {
103       LightApp_Displayer local_d;
104       vis = local_d.IsDisplayed( myEntries[ ind ] );
105     }
106     return QtxValue( vis, 0 );
107   }
108
109   else if( p=="component" )
110   {
111     return myStudy->componentDataType( myEntries[ ind ] );
112   }
113
114   else if( p=="isReference" )
115     return QtxValue( isReference( ind ), false );
116
117   else if( p=="canBeDisplayed" )
118   {
119     QString mod_name = app->moduleTitle( param( ind, "component" ).toString() );
120     LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
121     // false in last parameter means that now we doesn't load module, if it isn't loaded
122
123     return QtxValue( d ? d->canBeDisplayed( myEntries[ ind ] ) : true, 0 );
124     //now if displayer is null, it means, that according module isn't loaded, so that we allow to all display/erase
125     //operations under object
126   }
127
128   return QtxValue();
129 }
130
131 /*!
132   Gets global parameters. client, isActiveView, activeView etc.
133 */
134 QtxValue LightApp_Selection::globalParam( const QString& p ) const
135 {
136   if      ( p == "client" )        return QtxValue( myPopupClient );
137   else if ( p == "activeModule" )
138   {
139     LightApp_Application* app = dynamic_cast<LightApp_Application*>( myStudy->application() );
140     QString mod_name = app ? QString( app->activeModule()->name() ) : QString::null;
141     //cout << "activeModule : " << mod_name.latin1() << endl;
142     if( !mod_name.isEmpty() )
143       return mod_name;
144     else
145       return QtxValue();
146   }
147   else if ( p == "isActiveView" )  return QtxValue( (bool)activeVW() );
148   else if ( p == "activeView" )    return QtxValue( activeViewType() );
149 #ifndef WNT
150   else                             return QtxPopupMgr::Selection::globalParam( p );
151 #else
152   else                             return Selection::globalParam( p );
153 #endif
154 }
155
156 /*!
157   Do nothing. To be redefined by successors
158 */
159 void LightApp_Selection::processOwner( const LightApp_DataOwner* )
160 {
161 }
162
163 /*!
164   Gets entry with index \a index.
165 */
166 QString LightApp_Selection::entry( const int index ) const
167 {
168   if ( index >= 0 && index < count() )
169     return myEntries[ index ];
170   return QString();
171 }
172
173 /*!
174   Returns true if i-th selected object was reference to object with entry( i )
175 */
176 bool LightApp_Selection::isReference( const int index ) const
177 {
178   if( index >= 0 && index < count() )
179     return myIsReferences[ index ];
180   else
181     return false;
182 }
183
184 /*!
185   Gets type of active view manager.
186 */
187 QString LightApp_Selection::activeViewType() const
188 {
189   SUIT_ViewWindow* win = activeVW();
190   if ( win ) {
191     SUIT_ViewManager* vm = win->getViewManager();
192     if ( vm )
193       return vm->getType();
194   }
195   return QString::null;
196 }
197
198 /*!
199   Gets active view window.
200 */
201 SUIT_ViewWindow* LightApp_Selection::activeVW() const
202 {
203   SUIT_Session* session = SUIT_Session::session();
204   if ( session ) {
205     SUIT_Application* app = session->activeApplication();
206     if ( app ) {
207       SUIT_Desktop* desk = app->desktop();
208       if ( desk ) 
209         return desk->activeWindow();
210     }
211   }
212   return 0;
213 }