Salome HOME
813b5e3a98fcba25fe50907b68d97c540765bbe3
[modules/gui.git] / src / LightApp / LightApp_Selection.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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
23 #include "LightApp_Selection.h"
24 #include "LightApp_SelectionMgr.h"
25 #include "LightApp_DataOwner.h"
26 #include "LightApp_Study.h"
27 #include "LightApp_Application.h"
28 #include "LightApp_Displayer.h"
29
30 #include "CAM_Module.h"
31
32 #include "SUIT_Session.h"
33 #include "SUIT_ViewWindow.h"
34 #include "SUIT_ViewManager.h"
35 #include "SUIT_Desktop.h"
36 #include "SUIT_Selector.h"
37
38 #include <QtCore/QSet>
39
40 /*!
41   Constructor
42 */
43 LightApp_Selection::LightApp_Selection()
44 : myStudy( 0 )
45 {
46 }
47
48 /*!
49   Destructor.
50 */
51 LightApp_Selection::~LightApp_Selection()
52 {
53 }
54
55 /*!
56   Initialization.
57 */
58 void LightApp_Selection::init( const QString& client, LightApp_SelectionMgr* mgr)
59 {
60   myContext = client;
61
62   if ( mgr ) {
63     if ( mgr->application() )
64       myStudy = dynamic_cast<LightApp_Study*>( mgr->application()->activeStudy() );
65
66     if( !myStudy )
67       return;
68
69     //1) to take owners from current popup client
70     SUIT_DataOwnerPtrList sel( true ), cur_sel( true );
71     mgr->selected( sel, client );
72
73     //2) to take such owners from other popup clients that it's entry is different with every entry from current list
74     QList<SUIT_Selector*> aSelectors;
75     mgr->selectors( aSelectors );
76     QListIterator<SUIT_Selector*> it( aSelectors );
77     while ( it.hasNext() )
78     {
79       SUIT_Selector* selector = it.next();
80       if ( selector->type() != client && selector->isEnabled() ) {
81         selector->selected( cur_sel );
82
83         for ( SUIT_DataOwnerPtrList::const_iterator aLIt = cur_sel.begin(); aLIt != cur_sel.end(); ++aLIt )
84           sel.append( *aLIt ); //check entry and don't append if such entry is in list already
85       }
86     }
87
88     //3) to analyse owner and fill internal data structures
89
90     int num = 0;
91     QSet<QString> entries;
92     myObjects.resize( sel.size() );
93     myObjects.fill( ObjectInfo() );
94     for ( SUIT_DataOwnerPtrList::const_iterator anIt = sel.begin(); anIt != sel.end(); anIt++ )
95     {
96       LightApp_DataOwner* sowner = dynamic_cast<LightApp_DataOwner*>( (*anIt ).get() );
97       if ( sowner )
98       {
99         QString entry = referencedToEntry( sowner->entry() );
100         if ( entries.contains( entry ) )
101           continue;
102
103         entries.insert( entry );
104
105         setObjectInfo( num, OI_Entry, entry );
106         setObjectInfo( num, OI_Reference, sowner->entry() != entry );
107         setObjectInfo( num, OI_RefEntry, sowner->entry() );
108
109         if ( processOwner( sowner ) )
110           num++;
111         else
112           entries.remove( entry );
113       }
114     }
115
116     myObjects.resize( num );
117     /*
118     myContextParams.clear();
119     myObjectsParams.resize( num );
120     myObjectsParams.fill( ParameterMap() );
121     */
122   }
123 }
124
125 QString LightApp_Selection::referencedToEntry( const QString& entry ) const
126 {
127   return myStudy->referencedToEntry( entry );
128 }
129
130 /*!
131   Gets count of entries.
132 */
133 int LightApp_Selection::count() const
134 {
135   return myObjects.size();
136 }
137
138 /*!
139   Gets global parameters.
140 */
141 /*
142 QVariant LightApp_Selection::parameter( const QString& p ) const
143 {
144   QVariant v;
145   if ( myContextParams.contains( p ) )
146     v = myContextParams[p];
147   else
148     v = contextParameter( p );
149     if ( !v.isValid() )
150       v = QtxPopupSelection::parameter( p );
151     LightApp_Selection* that = (LightApp_Selection*)this;
152     that->myContextParams.insert( p, v );
153   }
154   return v;
155 }
156 */
157
158 /*!
159   Gets the object parameter.
160 */
161  /*
162 QVariant LightApp_Selection::parameter( const int idx, const QString& p ) const
163 {
164   QVariant v;
165   if ( 0 <= idx && idx < myObjectsParams.size() ) {
166     if ( myObjectsParams[idx].contains( p ) )
167       v = myObjectsParams[idx][p];
168     else {
169       v = objectParameter( idx, p );
170       LightApp_Selection* that = (LightApp_Selection*)this;
171       that->myObjectsParams[idx].insert( p, v );
172     }
173   }
174   return v;
175 }
176  */
177 /*!
178   Gets global parameters. client, isActiveView, activeView etc.
179 */
180   //QVariant LightApp_Selection::contextParameter( const QString& p ) const
181 QVariant LightApp_Selection::parameter( const QString& p ) const
182 {
183   QVariant v;
184
185   if ( p == "client" )
186     v = myContext;
187   else if ( p == "activeModule" ) {
188     LightApp_Application* app = dynamic_cast<LightApp_Application*>( myStudy->application() );
189     QString mod_name = app ? QString( app->activeModule()->name() ) : QString();
190     if ( !mod_name.isEmpty() )
191       v = mod_name;
192   }
193   else if ( p == "isActiveView" )
194     v = activeVW() != 0;
195   else if ( p == "activeView" )
196     v = activeViewType();
197   else
198     v = QtxPopupSelection::parameter( p );
199
200   return v;
201 }
202
203 /*!
204   Gets the object parameter.
205 */
206 //QVariant LightApp_Selection::objectParameter( const int idx, const QString& p ) const
207 QVariant LightApp_Selection::parameter( const int idx, const QString& p ) const
208 {
209   LightApp_Application* app = 0;
210   if ( myStudy )
211     app = dynamic_cast<LightApp_Application*>( myStudy->application() );
212
213   QVariant v;
214   if ( app ) {
215     QString e = entry( idx );
216     if ( !e.isEmpty() ) {
217       if ( p == "isVisible" ) {
218         QString mname = myStudy->componentDataType( e );
219         LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mname, false );
220         // false in last parameter means that now we doesn't load module, if it isn't loaded
221
222         bool vis = false;
223         if ( d )
224           vis = d->IsDisplayed( e );
225         else
226           vis = LightApp_Displayer().IsDisplayed( e );
227         v = vis;
228       }
229       else if ( p == "component" )
230         v = myStudy->componentDataType( e );
231       else if ( p == "displayer" )
232         v = LightApp_Application::moduleDisplayer( myStudy->componentDataType( e ) );
233       else if ( p == "isComponent" )
234         v = myStudy->isComponent( e );
235       else if ( p == "isReference" )
236         v = isReference( idx );
237       else if ( p == "canBeDisplayed" ) {
238         QString mname = myStudy->componentDataType( e ) ;
239         LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mname, false );
240         // false in last parameter means that now we doesn't load module, if it isn't loaded
241
242         if ( d )
243           v = d->canBeDisplayed( e );
244         else if ( e.startsWith( QObject::tr( "SAVE_POINT_DEF_NAME" ) ) ) // object is a Save Point object
245           v = false;
246         else
247           v = true;
248         //now if displayer is null, it means, that according module isn't loaded, so that we allow to all display/erase
249         //operations under object
250       }
251     }
252   }
253
254   return v;
255 }
256
257 /*!
258   Perform additional processing of the selected item (to be redefined by successors if necessary).
259   Returns \c true by default.
260   Note: if this method returns \c false, the item will be removed from the items list and
261   not taken into account when showing popup menu.
262
263   \param owner a data owner being processed
264   \return \c true if the owner should be collected and \c false otherwise
265 */
266 bool LightApp_Selection::processOwner( const LightApp_DataOwner* /*owner*/ )
267 {
268   return true;
269 }
270
271 /*!
272   Gets entry with index \a index.
273 */
274 QString LightApp_Selection::entry( const int index ) const
275 {
276   QVariant v = objectInfo( index, OI_Entry );
277   return v.canConvert( QVariant::String ) ? v.toString() : QString();
278 }
279
280 /*!
281   Returns true if i-th selected object was reference to object with entry( i )
282 */
283 bool LightApp_Selection::isReference( const int index ) const
284 {
285   QVariant v = objectInfo( index, OI_Reference );
286   return v.canConvert( QVariant::Bool ) ? v.toBool() : false;
287 }
288
289 /*!
290   Gets type of active view manager.
291 */
292 QString LightApp_Selection::activeViewType() const
293 {
294   SUIT_ViewWindow* win = activeVW();
295   if ( win ) {
296     SUIT_ViewManager* vm = win->getViewManager();
297     if ( vm )
298       return vm->getType();
299   }
300   return QString();
301 }
302
303 /*!
304   Gets active view window.
305 */
306 SUIT_ViewWindow* LightApp_Selection::activeVW() const
307 {
308   SUIT_Session* session = SUIT_Session::session();
309   if ( session ) {
310     SUIT_Application* app = session->activeApplication();
311     if ( app ) {
312       SUIT_Desktop* desk = app->desktop();
313       if ( desk )
314         return desk->activeWindow();
315     }
316   }
317   return 0;
318 }
319
320 /*!
321   Gets specified information about object with index idx.
322 */
323 QVariant LightApp_Selection::objectInfo( const int idx, const int inf ) const
324 {
325   QVariant res;
326   if ( 0 <= idx && idx < myObjects.size() ) {
327     if ( myObjects[idx].contains( inf ) )
328       res = myObjects[idx][inf];
329   }
330   return res;
331 }
332
333 /*!
334   Sets specified information about object with index idx.
335 */
336 void LightApp_Selection::setObjectInfo( const int idx, const int inf, const QVariant& val )
337 {
338   if ( 0 <= idx && idx < myObjects.size() )
339     myObjects[idx].insert( inf, val );
340 }