Back to index

salome-gui  6.5.0
LightApp_Selection.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "LightApp_Selection.h"
00024 #include "LightApp_SelectionMgr.h"
00025 #include "LightApp_DataOwner.h"
00026 #include "LightApp_Study.h"
00027 #include "LightApp_Application.h"
00028 #include "LightApp_Displayer.h"
00029 
00030 #include "CAM_Module.h"
00031 
00032 #include "SUIT_Session.h"
00033 #include "SUIT_ViewWindow.h"
00034 #include "SUIT_ViewManager.h"
00035 #include "SUIT_Desktop.h"
00036 #include "SUIT_Selector.h"
00037 
00038 #include <QtCore/QSet>
00039 
00043 LightApp_Selection::LightApp_Selection()
00044 : myStudy( 0 )
00045 {
00046 }
00047 
00051 LightApp_Selection::~LightApp_Selection()
00052 {
00053 }
00054 
00058 void LightApp_Selection::init( const QString& client, LightApp_SelectionMgr* mgr)
00059 {
00060   myContext = client;
00061 
00062   if ( mgr ) {
00063     if ( mgr->application() )
00064       myStudy = dynamic_cast<LightApp_Study*>( mgr->application()->activeStudy() );
00065 
00066     if( !myStudy )
00067       return;
00068 
00069     //1) to take owners from current popup client
00070     SUIT_DataOwnerPtrList sel( true ), cur_sel( true );
00071     mgr->selected( sel, client );
00072 
00073     //2) to take such owners from other popup clients that it's entry is different with every entry from current list
00074     QList<SUIT_Selector*> aSelectors;
00075     mgr->selectors( aSelectors );
00076     QListIterator<SUIT_Selector*> it( aSelectors );
00077     while ( it.hasNext() )
00078     {
00079       SUIT_Selector* selector = it.next();
00080       if ( selector->type() != client && selector->isEnabled() ) {
00081         selector->selected( cur_sel );
00082 
00083         for ( SUIT_DataOwnerPtrList::const_iterator aLIt = cur_sel.begin(); aLIt != cur_sel.end(); ++aLIt )
00084           sel.append( *aLIt ); //check entry and don't append if such entry is in list already
00085       }
00086     }
00087 
00088     //3) to analyse owner and fill internal data structures
00089 
00090     int num = 0;
00091     QSet<QString> entries;
00092     myObjects.resize( sel.size() );
00093     myObjects.fill( ObjectInfo() );
00094     for ( SUIT_DataOwnerPtrList::const_iterator anIt = sel.begin(); anIt != sel.end(); anIt++ )
00095     {
00096       LightApp_DataOwner* sowner = dynamic_cast<LightApp_DataOwner*>( (*anIt ).get() );
00097       if ( sowner )
00098       {
00099         QString entry = referencedToEntry( sowner->entry() );
00100         if ( entries.contains( entry ) )
00101           continue;
00102 
00103         entries.insert( entry );
00104 
00105        setObjectInfo( num, OI_Entry, entry );
00106        setObjectInfo( num, OI_Reference, sowner->entry() != entry );
00107 
00108         if ( processOwner( sowner ) )
00109           num++;
00110         else
00111           entries.remove( entry );
00112       }
00113     }
00114 
00115     myObjects.resize( num );
00116     /*
00117     myContextParams.clear();
00118     myObjectsParams.resize( num );
00119     myObjectsParams.fill( ParameterMap() );
00120     */
00121   }
00122 }
00123 
00124 QString LightApp_Selection::referencedToEntry( const QString& entry ) const
00125 {
00126   return myStudy->referencedToEntry( entry );
00127 }
00128 
00132 int LightApp_Selection::count() const
00133 {
00134   return myObjects.size();
00135 }
00136 
00140 /*
00141 QVariant LightApp_Selection::parameter( const QString& p ) const
00142 {
00143   QVariant v;
00144   if ( myContextParams.contains( p ) )
00145     v = myContextParams[p];
00146   else
00147     v = contextParameter( p );
00148     if ( !v.isValid() )
00149       v = QtxPopupSelection::parameter( p );
00150     LightApp_Selection* that = (LightApp_Selection*)this;
00151     that->myContextParams.insert( p, v );
00152   }
00153   return v;
00154 }
00155 */
00156 
00160  /*
00161 QVariant LightApp_Selection::parameter( const int idx, const QString& p ) const
00162 {
00163   QVariant v;
00164   if ( 0 <= idx && idx < myObjectsParams.size() ) {
00165     if ( myObjectsParams[idx].contains( p ) )
00166       v = myObjectsParams[idx][p];
00167     else {
00168       v = objectParameter( idx, p );
00169       LightApp_Selection* that = (LightApp_Selection*)this;
00170       that->myObjectsParams[idx].insert( p, v );
00171     }
00172   }
00173   return v;
00174 }
00175  */
00179   //QVariant LightApp_Selection::contextParameter( const QString& p ) const
00180 QVariant LightApp_Selection::parameter( const QString& p ) const
00181 {
00182   QVariant v;
00183 
00184   if ( p == "client" )
00185     v = myContext;
00186   else if ( p == "activeModule" ) {
00187     LightApp_Application* app = dynamic_cast<LightApp_Application*>( myStudy->application() );
00188     QString mod_name = app ? QString( app->activeModule()->name() ) : QString();
00189     if ( !mod_name.isEmpty() )
00190       v = mod_name;
00191   }
00192   else if ( p == "isActiveView" )
00193     v = activeVW() != 0;
00194   else if ( p == "activeView" )
00195     v = activeViewType();
00196   else
00197     v = QtxPopupSelection::parameter( p );
00198 
00199   return v;
00200 }
00201 
00205 //QVariant LightApp_Selection::objectParameter( const int idx, const QString& p ) const
00206 QVariant LightApp_Selection::parameter( const int idx, const QString& p ) const
00207 {
00208   LightApp_Application* app = 0;
00209   if ( myStudy )
00210     app = dynamic_cast<LightApp_Application*>( myStudy->application() );
00211 
00212   QVariant v;
00213   if ( app ) {
00214     QString e = entry( idx );
00215     if ( !e.isEmpty() ) {
00216       if ( p == "isVisible" ) {
00217        QString mod_name = app->moduleTitle( myStudy->componentDataType( e ) );
00218        LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
00219        // false in last parameter means that now we doesn't load module, if it isn't loaded
00220 
00221        bool vis = false;
00222        if ( d )
00223          vis = d->IsDisplayed( e );
00224        else
00225          vis = LightApp_Displayer().IsDisplayed( e );
00226        v = vis;
00227       }
00228       else if ( p == "component" || p == "displayer" )
00229        v = myStudy->componentDataType( e );
00230       else if ( p == "isComponent" )
00231        v = myStudy->isComponent( e );
00232       else if ( p == "isReference" )
00233        v = isReference( idx );
00234       else if ( p == "canBeDisplayed" ) {
00235        QString mod_name = app->moduleTitle( myStudy->componentDataType( e ) );
00236        LightApp_Displayer* d = LightApp_Displayer::FindDisplayer( mod_name, false );
00237        // false in last parameter means that now we doesn't load module, if it isn't loaded
00238 
00239        if ( d )
00240          v = d->canBeDisplayed( e );
00241        else if ( e.startsWith( QObject::tr( "SAVE_POINT_DEF_NAME" ) ) ) // object is a Save Point object
00242          v = false;
00243        else
00244          v = true;
00245        //now if displayer is null, it means, that according module isn't loaded, so that we allow to all display/erase
00246        //operations under object
00247       }
00248     }
00249   }
00250 
00251   return v;
00252 }
00253 
00263 bool LightApp_Selection::processOwner( const LightApp_DataOwner* /*owner*/ )
00264 {
00265   return true;
00266 }
00267 
00271 QString LightApp_Selection::entry( const int index ) const
00272 {
00273   QVariant v = objectInfo( index, OI_Entry );
00274   return v.canConvert( QVariant::String ) ? v.toString() : QString();
00275 }
00276 
00280 bool LightApp_Selection::isReference( const int index ) const
00281 {
00282   QVariant v = objectInfo( index, OI_Reference );
00283   return v.canConvert( QVariant::Bool ) ? v.toBool() : false;
00284 }
00285 
00289 QString LightApp_Selection::activeViewType() const
00290 {
00291   SUIT_ViewWindow* win = activeVW();
00292   if ( win ) {
00293     SUIT_ViewManager* vm = win->getViewManager();
00294     if ( vm )
00295       return vm->getType();
00296   }
00297   return QString();
00298 }
00299 
00303 SUIT_ViewWindow* LightApp_Selection::activeVW() const
00304 {
00305   SUIT_Session* session = SUIT_Session::session();
00306   if ( session ) {
00307     SUIT_Application* app = session->activeApplication();
00308     if ( app ) {
00309       SUIT_Desktop* desk = app->desktop();
00310       if ( desk )
00311         return desk->activeWindow();
00312     }
00313   }
00314   return 0;
00315 }
00316 
00320 QVariant LightApp_Selection::objectInfo( const int idx, const int inf ) const
00321 {
00322   QVariant res;
00323   if ( 0 <= idx && idx < myObjects.size() ) {
00324     if ( myObjects[idx].contains( inf ) )
00325       res = myObjects[idx][inf];
00326   }
00327   return res;
00328 }
00329 
00333 void LightApp_Selection::setObjectInfo( const int idx, const int inf, const QVariant& val )
00334 {
00335   if ( 0 <= idx && idx < myObjects.size() )
00336     myObjects[idx].insert( inf, val );
00337 }