Salome HOME
Merge branch 'V7_dev'
[modules/gui.git] / src / LightApp / LightApp_Selection.cxx
1 // Copyright (C) 2007-2016  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
108         if ( processOwner( sowner ) )
109           num++;
110         else
111           entries.remove( entry );
112       }
113     }
114
115     myObjects.resize( num );
116     /*
117     myContextParams.clear();
118     myObjectsParams.resize( num );
119     myObjectsParams.fill( ParameterMap() );
120     */
121   }
122 }
123
124 QString LightApp_Selection::referencedToEntry( const QString& entry ) const
125 {
126   return myStudy->referencedToEntry( entry );
127 }
128
129 /*!
130   Gets count of entries.
131 */
132 int LightApp_Selection::count() const
133 {
134   return myObjects.size();
135 }
136
137 /*!
138   Gets global parameters.
139 */
140 /*
141 QVariant LightApp_Selection::parameter( const QString& p ) const
142 {
143   QVariant v;
144   if ( myContextParams.contains( p ) )
145     v = myContextParams[p];
146   else
147     v = contextParameter( p );
148     if ( !v.isValid() )
149       v = QtxPopupSelection::parameter( p );
150     LightApp_Selection* that = (LightApp_Selection*)this;
151     that->myContextParams.insert( p, v );
152   }
153   return v;
154 }
155 */
156
157 /*!
158   Gets the object parameter.
159 */
160  /*
161 QVariant LightApp_Selection::parameter( const int idx, const QString& p ) const
162 {
163   QVariant v;
164   if ( 0 <= idx && idx < myObjectsParams.size() ) {
165     if ( myObjectsParams[idx].contains( p ) )
166       v = myObjectsParams[idx][p];
167     else {
168       v = objectParameter( idx, p );
169       LightApp_Selection* that = (LightApp_Selection*)this;
170       that->myObjectsParams[idx].insert( p, v );
171     }
172   }
173   return v;
174 }
175  */
176 /*!
177   Gets global parameters. client, isActiveView, activeView etc.
178 */
179   //QVariant LightApp_Selection::contextParameter( const QString& p ) const
180 QVariant LightApp_Selection::parameter( const QString& p ) const
181 {
182   QVariant v;
183
184   if ( p == "client" )
185     v = myContext;
186   else if ( p == "activeModule" ) {
187     LightApp_Application* app = dynamic_cast<LightApp_Application*>( myStudy->application() );
188     QString mod_name = app ? QString( app->activeModule()->name() ) : QString();
189     if ( !mod_name.isEmpty() )
190       v = mod_name;
191   }
192   else if ( p == "isActiveView" )
193     v = activeVW() != 0;
194   else if ( p == "activeView" )
195     v = activeViewType();
196   else
197     v = QtxPopupSelection::parameter( p );
198
199   return v;
200 }
201
202 /*!
203   Gets the object parameter.
204 */
205 //QVariant LightApp_Selection::objectParameter( const int idx, const QString& p ) const
206 QVariant LightApp_Selection::parameter( const int idx, const QString& p ) const
207 {
208   LightApp_Application* app = 0;
209   if ( myStudy )
210     app = dynamic_cast<LightApp_Application*>( myStudy->application() );
211
212   QVariant v;
213   if ( app ) {
214     QString e = entry( idx );
215     if ( !e.isEmpty() ) {
216       if ( p == "isVisible" ) {
217         QString mod_name = app->moduleTitle( myStudy->componentDataType( e ) );
218         LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
219         // false in last parameter means that now we doesn't load module, if it isn't loaded
220
221         bool vis = false;
222         if ( d )
223           vis = d->IsDisplayed( e );
224         else
225           vis = LightApp_Displayer().IsDisplayed( e );
226         v = vis;
227       }
228       else if ( p == "component" || p == "displayer" )
229         v = myStudy->componentDataType( e );
230       else if ( p == "isComponent" )
231         v = myStudy->isComponent( e );
232       else if ( p == "isReference" )
233         v = isReference( idx );
234       else if ( p == "canBeDisplayed" ) {
235         QString mod_name = app->moduleTitle( myStudy->componentDataType( e ) );
236         LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
237         // false in last parameter means that now we doesn't load module, if it isn't loaded
238
239         if ( d )
240           v = d->canBeDisplayed( e );
241         else if ( e.startsWith( QObject::tr( "SAVE_POINT_DEF_NAME" ) ) ) // object is a Save Point object
242           v = false;
243         else
244           v = true;
245         //now if displayer is null, it means, that according module isn't loaded, so that we allow to all display/erase
246         //operations under object
247       }
248     }
249   }
250
251   return v;
252 }
253
254 /*!
255   Perform additional processing of the selected item (to be redefined by successors if necessary).
256   Returns \c true by default.
257   Note: if this method returns \c false, the item will be removed from the items list and
258   not taken into account when showing popup menu.
259
260   \param owner a data owner being processed
261   \return \c true if the owner should be collected and \c false otherwise
262 */
263 bool LightApp_Selection::processOwner( const LightApp_DataOwner* /*owner*/ )
264 {
265   return true;
266 }
267
268 /*!
269   Gets entry with index \a index.
270 */
271 QString LightApp_Selection::entry( const int index ) const
272 {
273   QVariant v = objectInfo( index, OI_Entry );
274   return v.canConvert( QVariant::String ) ? v.toString() : QString();
275 }
276
277 /*!
278   Returns true if i-th selected object was reference to object with entry( i )
279 */
280 bool LightApp_Selection::isReference( const int index ) const
281 {
282   QVariant v = objectInfo( index, OI_Reference );
283   return v.canConvert( QVariant::Bool ) ? v.toBool() : false;
284 }
285
286 /*!
287   Gets type of active view manager.
288 */
289 QString LightApp_Selection::activeViewType() const
290 {
291   SUIT_ViewWindow* win = activeVW();
292   if ( win ) {
293     SUIT_ViewManager* vm = win->getViewManager();
294     if ( vm )
295       return vm->getType();
296   }
297   return QString();
298 }
299
300 /*!
301   Gets active view window.
302 */
303 SUIT_ViewWindow* LightApp_Selection::activeVW() const
304 {
305   SUIT_Session* session = SUIT_Session::session();
306   if ( session ) {
307     SUIT_Application* app = session->activeApplication();
308     if ( app ) {
309       SUIT_Desktop* desk = app->desktop();
310       if ( desk )
311         return desk->activeWindow();
312     }
313   }
314   return 0;
315 }
316
317 /*!
318   Gets specified information about object with index idx.
319 */
320 QVariant LightApp_Selection::objectInfo( const int idx, const int inf ) const
321 {
322   QVariant res;
323   if ( 0 <= idx && idx < myObjects.size() ) {
324     if ( myObjects[idx].contains( inf ) )
325       res = myObjects[idx][inf];
326   }
327   return res;
328 }
329
330 /*!
331   Sets specified information about object with index idx.
332 */
333 void LightApp_Selection::setObjectInfo( const int idx, const int inf, const QVariant& val )
334 {
335   if ( 0 <= idx && idx < myObjects.size() )
336     myObjects[idx].insert( inf, val );
337 }