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