Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / LightApp / LightApp_Selection.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "LightApp_Selection.h"
21 #include "LightApp_SelectionMgr.h"
22 #include "LightApp_DataOwner.h"
23 #include "LightApp_Study.h"
24 #include "LightApp_Application.h"
25 #include "LightApp_Displayer.h"
26
27 #include "SUIT_Session.h"
28 #include "SUIT_ViewWindow.h"
29
30 /*!
31   Constructor
32 */
33 LightApp_Selection::LightApp_Selection()
34 : myStudy( 0 )
35 {
36 }
37
38 /*!
39   Destructor.
40 */
41 LightApp_Selection::~LightApp_Selection()
42 {
43 }
44
45 /*!
46   Initialization.
47 */
48 void LightApp_Selection::init( const QString& client, LightApp_SelectionMgr* mgr)
49 {
50   myPopupClient = client;
51   myStudy = 0;
52   
53   if( mgr )
54   {
55     if( mgr->application() )
56       myStudy = dynamic_cast<LightApp_Study*>( mgr->application()->activeStudy() );
57     if( !myStudy )
58       return;
59
60     //1) to take owners from current popup client
61     SUIT_DataOwnerPtrList sel( true ), cur_sel( true );
62     mgr->selected( sel, client );
63
64     //2) to take such owners from other popup clients that it's entry is different with every entry from current list
65     QPtrList<SUIT_Selector> aSelectors;
66     mgr->selectors( aSelectors );
67     for( SUIT_Selector* selector = aSelectors.first(); selector; selector = aSelectors.next() )
68     {
69       qDebug( selector->type() );
70       if( selector->type() != client && selector->isEnabled() )
71       {
72         //mgr->selected( cur_sel, selector->type() );
73         selector->selected( cur_sel );
74         SUIT_DataOwnerPtrList::const_iterator aLIt = cur_sel.begin(), aLLast = cur_sel.end();
75         for( ; aLIt!=aLLast; aLIt++ )
76           sel.append( *aLIt ); //check entry and don't append if such entry is in list already
77       }
78     }
79
80     //3) to analyse owner and fill internal data structures
81     SUIT_DataOwnerPtrList::const_iterator anIt = sel.begin(), aLast = sel.end();
82     QMap<QString,int> entries;
83     QString entry;
84     int num=0;
85     for( ; anIt!=aLast; anIt++ )
86     {
87       LightApp_DataOwner* sowner = dynamic_cast<LightApp_DataOwner*>( (*anIt ).get() );
88       if( sowner )
89       {
90         entry = referencedToEntry( sowner->entry() );
91         if( entries.contains( entry ) )
92           continue;
93
94         entries.insert( entry, 0 );
95         myEntries.insert( num, entry );
96         myIsReferences.insert( num, sowner->entry() != entry );
97         processOwner( sowner );
98         num++;
99       }
100     }
101   }
102 }
103
104 QString LightApp_Selection::referencedToEntry( const QString& entry ) const
105 {
106   return myStudy->referencedToEntry( entry );
107 }
108
109 /*!
110   Gets count of entries.
111 */
112 int LightApp_Selection::count() const
113 {
114   return myEntries.count();
115 }
116
117 /*!
118   Gets QtxValue();
119 */
120 QtxValue LightApp_Selection::param( const int ind, const QString& p ) const
121 {
122   LightApp_Application* app = dynamic_cast<LightApp_Application*>( myStudy ? myStudy->application() : 0 );
123   if( !( ind>=0 && ind<count() ) || !app )
124     return QtxValue();
125
126   if( p=="isVisible" )
127   {
128     QString mod_name = app->moduleTitle( param( ind, "component" ).toString() );
129     LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
130     // false in last parameter means that now we doesn't load module, if it isn't loaded
131
132     bool vis = false;
133     if( d )
134       vis = d->IsDisplayed( myEntries[ ind ] );
135     else
136     {
137       LightApp_Displayer local_d;
138       vis = local_d.IsDisplayed( myEntries[ ind ] );
139     }
140     return QtxValue( vis, 0 );
141   }
142
143   else if( p=="component" )
144   {
145     return myStudy->componentDataType( myEntries[ ind ] );
146   }
147   
148   else if( p=="isComponent" )
149   {
150     return QtxValue( myStudy->isComponent( myEntries[ ind ] ), 0 );
151   }
152
153   else if( p=="isReference" )
154     return QtxValue( isReference( ind ), false );
155
156   else if( p=="displayer" )
157     return param( ind, "component" );
158
159   else if( p=="canBeDisplayed" )
160   {
161     QString mod_name = app->moduleTitle( param( ind, "component" ).toString() );
162     LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
163     // false in last parameter means that now we doesn't load module, if it isn't loaded
164
165     if ( d )
166       return d->canBeDisplayed( myEntries[ ind ] );
167     else if ( myEntries[ ind ].startsWith( QObject::tr( "SAVE_POINT_DEF_NAME" ) ) ) // object is a Save Point object
168       return false;
169
170     return true;
171     //now if displayer is null, it means, that according module isn't loaded, so that we allow to all display/erase
172     //operations under object
173   }
174
175   return QtxValue();
176 }
177
178 /*!
179   Gets global parameters. client, isActiveView, activeView etc.
180 */
181 QtxValue LightApp_Selection::globalParam( const QString& p ) const
182 {
183   if      ( p == "client" )        return QtxValue( myPopupClient );
184   else if ( p == "activeModule" )
185   {
186     LightApp_Application* app = dynamic_cast<LightApp_Application*>( myStudy->application() );
187     QString mod_name = app ? QString( app->activeModule()->name() ) : QString::null;
188     //cout << "activeModule : " << mod_name.latin1() << endl;
189     if( !mod_name.isEmpty() )
190       return mod_name;
191     else
192       return QtxValue();
193   }
194   else if ( p == "isActiveView" )  return QtxValue( (bool)activeVW() );
195   else if ( p == "activeView" )    return QtxValue( activeViewType() );
196 #ifndef WNT
197   else                             return QtxPopupMgr::Selection::globalParam( p );
198 #else
199   else                             return Selection::globalParam( p );
200 #endif
201 }
202
203 /*!
204   Do nothing. To be redefined by successors
205 */
206 void LightApp_Selection::processOwner( const LightApp_DataOwner* )
207 {
208 }
209
210 /*!
211   Gets entry with index \a index.
212 */
213 QString LightApp_Selection::entry( const int index ) const
214 {
215   if ( index >= 0 && index < count() )
216     return myEntries[ index ];
217   return QString();
218 }
219
220 /*!
221   Returns true if i-th selected object was reference to object with entry( i )
222 */
223 bool LightApp_Selection::isReference( const int index ) const
224 {
225   if( index >= 0 && index < count() )
226     return myIsReferences[ index ];
227   else
228     return false;
229 }
230
231 /*!
232   Gets type of active view manager.
233 */
234 QString LightApp_Selection::activeViewType() const
235 {
236   SUIT_ViewWindow* win = activeVW();
237   if ( win ) {
238     SUIT_ViewManager* vm = win->getViewManager();
239     if ( vm )
240       return vm->getType();
241   }
242   return QString::null;
243 }
244
245 /*!
246   Gets active view window.
247 */
248 SUIT_ViewWindow* LightApp_Selection::activeVW() const
249 {
250   SUIT_Session* session = SUIT_Session::session();
251   if ( session ) {
252     SUIT_Application* app = session->activeApplication();
253     if ( app ) {
254       SUIT_Desktop* desk = app->desktop();
255       if ( desk ) 
256         return desk->activeWindow();
257     }
258   }
259   return 0;
260 }