Back to index

salome-gui  6.5.0
SPlot2d_ViewModel.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 //  File   : SPlot2d_Viewer.cxx
00024 //  Author : Sergey RUIN
00025 //  Module : SUIT
00026 //
00027 #include "SPlot2d_ViewModel.h"
00028 
00029 #include "SPlot2d_ViewWindow.h"
00030 
00031 #include "SPlot2d_Prs.h"
00032 #include "SPlot2d_Histogram.h"
00033 #include "SUIT_Session.h"
00034 #include "SUIT_Application.h"
00035 #include "SUIT_ViewManager.h"
00036 
00037 #include "SALOME_ListIO.hxx"
00038 #include "SALOME_ListIteratorOfListIO.hxx"
00039 
00040 #include <QApplication>
00041 #include <QToolBar>
00042 #include <QToolButton>
00043 #include <QCursor>
00044 #include <QColorDialog>
00045 
00046 //#include <qwt_math>
00047 #include <qwt_plot_canvas.h>
00048 #include <qwt_plot_curve.h>
00049 #include <stdlib.h>
00050 
00051 //ASL: Temporary commented in order to avoir dependency on SALOMEDS
00052 
00053 //#include "SALOMEDSClient.hxx"
00054 //#include "SALOMEDS_StudyManager.hxx"
00055 
00056 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
00057 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
00058 // SALOMEDS::StudyManager - no linkage with SalomeApp.  
00059 
00060 /*static _PTR(Study) getStudyDS()
00061 {
00062   SALOMEDSClient_Study* aStudy = NULL;
00063   _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
00064 
00065   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
00066   //    id of its underlying SALOMEDS::Study
00067   SUIT_Application* app = SUIT_Session::session()->activeApplication();
00068   if ( !app )  return _PTR(Study)(aStudy);
00069   SUIT_Study* stud = app->activeStudy();
00070   if ( !stud ) return _PTR(Study)(aStudy);
00071   const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
00072   // get SALOMEDS_Study with this id from StudyMgr
00073   return aMgr->GetStudyByID( id );
00074 } */               
00075 
00079 SPlot2d_Viewer::SPlot2d_Viewer(  bool theAutoDel )
00080 : Plot2d_Viewer( theAutoDel ),
00081   myDeselectAnalytical(true)
00082 {
00083 }
00084 
00088 SPlot2d_Viewer::~SPlot2d_Viewer()
00089 {
00090 }
00091 
00095 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject,
00096                              const QString& newName, Plot2d_ViewFrame* fr ) 
00097 {
00098   Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
00099   if( !aViewFrame )
00100     return;
00101 
00102   CurveDict aCurves = aViewFrame->getCurves();
00103   CurveDict::Iterator it = aCurves.begin();
00104   for( ; it != aCurves.end(); ++it )
00105   {
00106     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
00107     if( aCurve && aCurve->hasIO() && aCurve->getIO()->isSame( IObject ) )
00108     {
00109       aCurve->setVerTitle( newName );
00110       it.key()->setTitle( newName );
00111     }
00112 
00113     if( aCurve && aCurve->hasTableIO() && aCurve->getTableIO()->isSame( IObject ) )
00114       aCurve->getTableIO()->setName( newName.toLatin1() );
00115   }
00116   aViewFrame->updateTitles();
00117 }
00118 
00124 void SPlot2d_Viewer::renameAll( const Handle(SALOME_InteractiveObject)& IObj, const QString& name )
00125 {
00126   SUIT_ViewManager* vm = getViewManager();
00127   if ( vm )
00128   {
00129     const QVector<SUIT_ViewWindow*>& wnds = vm->getViews();
00130 
00131     for ( uint i = 0; i < wnds.size(); i++ )
00132     {
00133       Plot2d_ViewWindow* pwnd = dynamic_cast<Plot2d_ViewWindow*>( wnds.at( i ) );
00134       rename( IObj, name, pwnd->getViewFrame() );
00135     }
00136   }
00137 }
00138 
00142 bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
00143 {
00144   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00145   if(aViewFrame == NULL) return 0;
00146 
00147   if( getCurveByIO( IObject ) != NULL )
00148     return 1;
00149   else{
00150     if(!IObject.IsNull()){
00151       CurveDict aCurves = aViewFrame->getCurves();
00152       CurveDict::Iterator it = aCurves.begin();
00153       for( ; it != aCurves.end(); ++it ) {
00154         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
00155         if(aCurve && aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
00156           return 1;
00157       }
00158     }
00159   }
00160   return 0;
00161 }
00162 
00163 
00167 void SPlot2d_Viewer::Display( const Handle(SALOME_InteractiveObject)& IObject, bool update )
00168 {
00169   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00170   if(aViewFrame == NULL) return;
00171 
00172   SPlot2d_Curve* curve = getCurveByIO( IObject );
00173   if ( curve )
00174     aViewFrame->updateCurve( curve, update );
00175 }
00176 
00181 void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObject )
00182 {
00183   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00184   if(aViewFrame == NULL) return;
00185 
00186   Plot2d_Curve* curve = getCurveByIO( IObject );
00187   CurveDict aCurves = aViewFrame->getCurves();
00188   CurveDict::Iterator it = aCurves.begin();
00189   for( ; it != aCurves.end(); ++it ) {
00190     if(it.value() != curve)
00191       aViewFrame->eraseCurve( curve );
00192     else
00193       aViewFrame->updateCurve( curve, false );
00194   }
00195 
00196   aViewFrame->Repaint();
00197 }
00198 
00202 void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, bool update )
00203 {
00204   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00205   if(aViewFrame == NULL) return;
00206 
00207   SPlot2d_Curve* curve = getCurveByIO( IObject );
00208   if ( curve )
00209     aViewFrame->eraseCurve( curve, update );
00210 
00211   // it can be table or container object selected
00212   //ASL: Temporary commented in order to avoid dependency on SALOMEDS
00213 /*  _PTR(Study) aStudy = getStudyDS();
00214   _PTR(SObject) aSO = aStudy->FindObjectID(IObject->getEntry());
00215   if ( aSO ) {
00216     _PTR(ChildIterator) aIter = aStudy->NewChildIterator( aSO );
00217     for ( ; aIter->More(); aIter->Next() ) {
00218       _PTR(SObject) aChildSO = aIter->Value();
00219       _PTR(SObject) refSO;
00220       if ( aChildSO->ReferencedObject( refSO ) && refSO )
00221         aChildSO = refSO;
00222       curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
00223       if ( curve )
00224         aViewFrame->eraseCurve( curve, update );
00225     }
00226   }
00227 */
00228 
00229 }
00230 
00234 void SPlot2d_Viewer::EraseAll(const bool /*forced*/) 
00235 {
00236   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00237   if(aViewFrame) aViewFrame->EraseAll();
00238 }
00239 
00243 void SPlot2d_Viewer::Repaint()
00244 {
00245   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00246   if(aViewFrame) aViewFrame->Repaint();
00247 }
00248 
00252 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
00253 {
00254   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00255   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
00256   if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
00257 }
00258 
00262 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
00263 {
00264   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00265   Plot2d_Prs* aPrs = dynamic_cast<Plot2d_Prs*>(const_cast<SALOME_Prs2d*>(prs));
00266   if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
00267 }
00268   
00272 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
00273 {
00274   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00275   SPlot2d_Prs *prs = new SPlot2d_Prs();
00276   if(aViewFrame)
00277   {
00278     CurveDict aCurves = aViewFrame->getCurves();
00279     CurveDict::Iterator it = aCurves.begin();
00280     for( ; it != aCurves.end(); ++it ) {
00281       SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value());
00282       OwnerSet owners = aCurve->getOwners();
00283       if(aCurve) {
00284        if ( 
00285            (aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), entry )) ||
00286            (aCurve->hasTableIO() && !strcmp( aCurve->getTableIO()->getEntry(), entry )) ||
00287            owners.contains(entry)
00288            ) {
00289          prs->AddObject(aCurve);
00290        }
00291       }      
00292     }
00293   }
00294   return prs;
00295 }
00296 
00300 bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
00301 {
00302   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00303   if(aViewFrame == NULL) return false;
00304 
00305   SPlot2d_Curve* curve = getCurveByIO( IObject );
00306   return aViewFrame->isVisible( curve );
00307 }
00308 
00313 void SPlot2d_Viewer::GetVisible( SALOME_ListIO& theList )
00314 {
00315   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00316   if(aViewFrame == NULL) return;
00317   CurveDict aCurves = aViewFrame->getCurves();
00318   CurveDict::Iterator it = aCurves.begin();
00319   for( ; it != aCurves.end(); ++it ) {
00320     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value()); 
00321     if ( aCurve && aCurve->hasIO() && aViewFrame->isVisible( aCurve ) )
00322       theList.Append( aCurve->getIO() );
00323   }
00324 }
00325 
00329 Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry )
00330 {
00331   Handle(SALOME_InteractiveObject) anIO;
00332   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00333   if(aViewFrame == NULL) return anIO;
00334 
00335   CurveDict aCurves = aViewFrame->getCurves();
00336   CurveDict::Iterator it = aCurves.begin();
00337   for( ; it != aCurves.end(); ++it ) {
00338     SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value()); 
00339     if ( aCurve && aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
00340       anIO = aCurve->getIO();
00341       break;
00342     }
00343   }
00344   return anIO;
00345 }
00346 
00350 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
00351 {
00352   SUIT_ViewManager* aViewMgr = getViewManager();
00353   if(aViewMgr) {
00354     Plot2d_ViewWindow* aViewWnd = dynamic_cast<Plot2d_ViewWindow*>(aViewMgr->getActiveView());
00355     if(aViewWnd)
00356       return aViewWnd->getViewFrame();
00357   }
00358 
00359   return NULL;
00360 }
00361 
00367 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
00368                                              Plot2d_ViewFrame* fr )
00369 {
00370   if ( !theIObject.IsNull() ) {
00371     Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
00372     if(aViewFrame) {
00373       CurveDict aCurves = aViewFrame->getCurves();
00374       CurveDict::Iterator it = aCurves.begin();
00375       for( ; it != aCurves.end(); ++it ) {
00376         SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
00377         if(aCurve) {
00378           if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
00379             return aCurve;
00380         }
00381       }
00382     }
00383   }
00384   return NULL;
00385 }
00386 
00390 SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
00391 {
00392   SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
00393   aPlot2dView->initLayout();
00394   if (getPrs())
00395     aPlot2dView->getViewFrame()->Display(getPrs());
00396   return aPlot2dView;
00397 }
00398 
00403 void SPlot2d_Viewer::onLegendClicked( QwtPlotItem* plotItem )
00404 {
00405   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00406   if(aViewFrame == NULL) return;
00407 
00408   bool isAnalytical = false;
00409   AnalyticalCurveList curves = aViewFrame->getAnalyticalCurves();
00410    foreach ( Plot2d_AnalyticalCurve* curve, curves ) {
00411           if(plotItem == curve->plotItem()) {
00412                 isAnalytical = true;
00413                 curve->setSelected(true);
00414           } else {
00415                 curve->setSelected(false);
00416           }
00417    }
00418   if(isAnalytical) {
00419        myDeselectAnalytical = false;
00420        emit clearSelected();
00421        aViewFrame->updateAnalyticalCurves();
00422        myDeselectAnalytical = true;
00423        return;
00424   }
00425 
00426   Plot2d_Object* anObject = aViewFrame->getPlotObject(plotItem);
00427   
00428   if(anObject) {
00429     
00430     // Highlight object in Object Browser
00431     QString anEntry;
00432     if(SPlot2d_Curve* aSCurve = dynamic_cast<SPlot2d_Curve*>(anObject)) {
00433       if(aSCurve->hasIO())
00434        anEntry = aSCurve->getIO()->getEntry();
00435     } else if( SPlot2d_Histogram* aSHisto = dynamic_cast<SPlot2d_Histogram*>(anObject)) {
00436       if(aSHisto->hasIO())
00437        anEntry = aSHisto->getIO()->getEntry();
00438     }
00439     
00440     if(!anEntry.isEmpty())
00441       emit legendSelected( anEntry );
00442   }    
00443 }
00444 
00448 void SPlot2d_Viewer::setObjectsSelected( SALOME_ListIO& theList ) {
00449   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
00450   if(aViewFrame) {
00451 
00452     objectList allObjects;
00453     aViewFrame->getObjects( allObjects );
00454     
00455     bool isSelected = false;
00456     SPlot2d_Histogram* h = 0;
00457     SPlot2d_Curve* c =0;
00458     
00459     foreach ( Plot2d_Object* o, allObjects ) {
00460       isSelected = false;
00461       
00462       Handle(SALOME_InteractiveObject) io;
00463       if( (h = dynamic_cast<SPlot2d_Histogram*>(o)) && h->hasIO() ) {
00464        io = h->getIO();
00465       } else if((c = dynamic_cast<SPlot2d_Curve*>(o)) && c->hasIO()) {
00466        io = c->getIO();
00467       } else {
00468        continue;
00469       }
00470 
00471       SALOME_ListIteratorOfListIO anIter( theList ); 
00472       
00473       for( ; anIter.More(); anIter.Next() ) {
00474        if ( anIter.Value()->hasEntry() ) {
00475          if( io->isSame(anIter.Value()) ) {
00476            isSelected = o->isSelected();
00477            if( !isSelected ) {
00478              o->setSelected(true);
00479              aViewFrame->updateObject(o);
00480              theList.Remove(anIter);
00481              isSelected = true;
00482              break;
00483            } else 
00484              break;
00485          }
00486        }
00487       }
00488       if( !isSelected && o->isSelected() != false ) {   
00489        o->setSelected(false);
00490        aViewFrame->updateObject(o);
00491       }
00492     }
00493        if( myDeselectAnalytical ) {
00494               aViewFrame->deselectAnalyticalCurves();
00495               aViewFrame->updateAnalyticalCurves(); 
00496        }
00497     aViewFrame->Repaint();
00498   }
00499 }