Salome HOME
Unicode support: correct handling of unicode on GUI level
[modules/gui.git] / src / SPlot2d / SPlot2d_ViewModel.cxx
index 554927ff59508645b8213cf08bc822516c7d45f4..0d8250f24822a3322f53d1bee0caac6b8a493205 100644 (file)
@@ -1,77 +1,58 @@
-// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : SPlot2d_Viewer.cxx
 //  Author : Sergey RUIN
 //  Module : SUIT
-
+//
 #include "SPlot2d_ViewModel.h"
 
+#include "SPlot2d_ViewWindow.h"
+
 #include "SPlot2d_Prs.h"
+#include "SPlot2d_Histogram.h"
 #include "SUIT_Session.h"
 #include "SUIT_Application.h"
+#include "SUIT_ViewManager.h"
 
-//#include "utilities.h"
-#include "qapplication.h"
-#include <qtoolbar.h>
-#include <qtoolbutton.h>
-#include <qcursor.h>
-#include <qcolordialog.h>
-#include <qwt_math.h>
-#include <qwt_plot_canvas.h>
-#include <stdlib.h>
-
-using namespace std;
+#include "SALOME_ListIO.hxx"
 
-//ASL: Temporary commented in order to avoir dependency on SALOMEDS
+#include <QApplication>
+#include <QToolBar>
+#include <QToolButton>
+#include <QCursor>
+#include <QColorDialog>
 
-//#include "SALOMEDSClient.hxx"
-//#include "SALOMEDS_StudyManager.hxx"
-
-// in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
-// SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from
-// SALOMEDS::StudyManager - no linkage with SalomeApp.  
-
-/*static _PTR(Study) getStudyDS()
-{
-  SALOMEDSClient_Study* aStudy = NULL;
-  _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
-
-  // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
-  //    id of its underlying SALOMEDS::Study
-  SUIT_Application* app = SUIT_Session::session()->activeApplication();
-  if ( !app )  return _PTR(Study)(aStudy);
-  SUIT_Study* stud = app->activeStudy();
-  if ( !stud ) return _PTR(Study)(aStudy);
-  const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
-  // get SALOMEDS_Study with this id from StudyMgr
-  return aMgr->GetStudyByID( id );
-} */               
-
-//=================================================================================
-// SPlot2d_Viewer implementation
-//=================================================================================
+//#include <qwt_math>
+#include <qwt_plot_canvas.h>
+#include <qwt_plot_curve.h>
+#include <stdlib.h>
 
 /*!
   Constructor
 */
 SPlot2d_Viewer::SPlot2d_Viewer(  bool theAutoDel )
-: Plot2d_Viewer( theAutoDel )    
+: Plot2d_Viewer( theAutoDel ),
+  myDeselectAnalytical(true)
 {
 }
 
@@ -86,37 +67,42 @@ SPlot2d_Viewer::~SPlot2d_Viewer()
   Renames curve if it is found
 */
 void SPlot2d_Viewer::rename( const Handle(SALOME_InteractiveObject)& IObject,
-                            const QString& newName, Plot2d_ViewFrame* fr ) 
+                             const QString& newName, Plot2d_ViewFrame* fr ) 
 {
   Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
   if( !aViewFrame )
     return;
 
-  QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
-  for( ; it.current(); ++it )
+  CurveDict aCurves = aViewFrame->getCurves();
+  CurveDict::Iterator it = aCurves.begin();
+  for( ; it != aCurves.end(); ++it )
   {
-    SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.current() );
+    SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
     if( aCurve && aCurve->hasIO() && aCurve->getIO()->isSame( IObject ) )
     {
       aCurve->setVerTitle( newName );
-      int key = aViewFrame->hasCurve( aCurve );
-      if( key )
-       aViewFrame->setCurveTitle( key, newName );
+      it.key()->setTitle( newName );
     }
 
     if( aCurve && aCurve->hasTableIO() && aCurve->getTableIO()->isSame( IObject ) )
-      aCurve->getTableIO()->setName( newName.latin1() );
+      aCurve->getTableIO()->setName( newName.toUtf8() );
   }
   aViewFrame->updateTitles();
 }
 
+/*!
+  Renames all copies of object in all view windows
+  \param IObj - object to be renamed
+  \param name - new name
+*/
 void SPlot2d_Viewer::renameAll( const Handle(SALOME_InteractiveObject)& IObj, const QString& name )
 {
   SUIT_ViewManager* vm = getViewManager();
-  if( vm )
+  if ( vm )
   {
-    const QPtrVector<SUIT_ViewWindow>& wnds = vm->getViews();
-    for( int i=0; i<wnds.size(); i++ )
+    const QVector<SUIT_ViewWindow*>& wnds = vm->getViews();
+
+    for ( uint i = 0; i < wnds.size(); i++ )
     {
       Plot2d_ViewWindow* pwnd = dynamic_cast<Plot2d_ViewWindow*>( wnds.at( i ) );
       rename( IObj, name, pwnd->getViewFrame() );
@@ -136,11 +122,12 @@ bool SPlot2d_Viewer::isInViewer( const Handle(SALOME_InteractiveObject)& IObject
     return 1;
   else{
     if(!IObject.IsNull()){
-      QIntDictIterator<Plot2d_Curve> it(aViewFrame->getCurves());
-      for(; it.current();++it) {
-       SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
-       if(aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
-         return 1;
+      CurveDict aCurves = aViewFrame->getCurves();
+      CurveDict::Iterator it = aCurves.begin();
+      for( ; it != aCurves.end(); ++it ) {
+        SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
+        if(aCurve && aCurve->hasIO() && aCurve->getTableIO()->isSame(IObject))
+          return 1;
       }
     }
   }
@@ -171,9 +158,10 @@ void SPlot2d_Viewer::DisplayOnly( const Handle(SALOME_InteractiveObject)& IObjec
   if(aViewFrame == NULL) return;
 
   Plot2d_Curve* curve = getCurveByIO( IObject );
-  QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
-  for ( ; it.current(); ++it ) {
-    if(it.current() != curve)
+  CurveDict aCurves = aViewFrame->getCurves();
+  CurveDict::Iterator it = aCurves.begin();
+  for( ; it != aCurves.end(); ++it ) {
+    if(it.value() != curve)
       aViewFrame->eraseCurve( curve );
     else
       aViewFrame->updateCurve( curve, false );
@@ -204,10 +192,10 @@ void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, boo
       _PTR(SObject) aChildSO = aIter->Value();
       _PTR(SObject) refSO;
       if ( aChildSO->ReferencedObject( refSO ) && refSO )
-       aChildSO = refSO;
+        aChildSO = refSO;
       curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID().c_str(), "") );
       if ( curve )
-       aViewFrame->eraseCurve( curve, update );
+        aViewFrame->eraseCurve( curve, update );
     }
   }
 */
@@ -217,10 +205,11 @@ void SPlot2d_Viewer::Erase( const Handle(SALOME_InteractiveObject)& IObject, boo
 /*!
    Removes all curves from the view
 */
-void SPlot2d_Viewer::EraseAll(const bool /*forced*/
+void SPlot2d_Viewer::EraseAll(SALOME_Displayer* d, const bool forced
 {
   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
   if(aViewFrame) aViewFrame->EraseAll();
+  SALOME_View::EraseAll(d, forced);
 }
 
 /*!
@@ -232,12 +221,9 @@ void SPlot2d_Viewer::Repaint()
   if(aViewFrame) aViewFrame->Repaint();
 }
 
-//==========================================================
 /*!
- *  SPlot2d_Viewer::Display
- *  Display presentation
- */
-//==========================================================
+  Display presentation
+*/
 void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
 {
   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
@@ -245,12 +231,9 @@ void SPlot2d_Viewer::Display( const SALOME_Prs2d* prs )
   if(aViewFrame && aPrs) aViewFrame->Display(aPrs);
 }
 
-//==========================================================
 /*!
- *  SPlot2d_Viewer::Erase
- *  Erase presentation
- */
-//==========================================================
+  Erase presentation
+*/
 void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
 {
   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
@@ -258,45 +241,32 @@ void SPlot2d_Viewer::Erase( const SALOME_Prs2d* prs, const bool )
   if(aViewFrame && aPrs) aViewFrame->Erase(aPrs);
 }
   
-//==========================================================
 /*!
- *  SPlot2d_Viewer::CreatePrs
- *  Create presentation by entry
- */
-//==========================================================
+  Create presentation by entry
+*/
 SALOME_Prs* SPlot2d_Viewer::CreatePrs( const char* entry )
 {
   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
+  SPlot2d_Prs *prs = new SPlot2d_Prs( entry );
   if(aViewFrame)
   {
-    Plot2d_Prs* prs = aViewFrame->CreatePrs(entry);
-    if( prs )
-      return new SPlot2d_Prs( prs );
+    CurveDict aCurves = aViewFrame->getCurves();
+    CurveDict::Iterator it = aCurves.begin();
+    for( ; it != aCurves.end(); ++it ) {
+      SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value());
+      OwnerSet owners = aCurve->getOwners();
+      if(aCurve) {
+       if ( 
+           (aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), entry )) ||
+           (aCurve->hasTableIO() && !strcmp( aCurve->getTableIO()->getEntry(), entry )) ||
+           owners.contains(entry)
+           ) {
+         prs->AddObject(aCurve);
+       }
+      }      
+    }
   }
-
-  return NULL;
-}
-
-//==========================================================
-/*!
- *  SPlot2d_Viewer::BeforeDisplay
- *  Axiluary method called before displaying of objects
- */
-//==========================================================
-void  SPlot2d_Viewer::BeforeDisplay( SALOME_Displayer* d )
-{
-  d->BeforeDisplay( this, SALOME_Plot2dViewType() );
-}
-
-//==========================================================
-/*!
- *  SPlot2d_Viewer::AfterDisplay
- *  Axiluary method called after displaying of objects
- */
-//==========================================================
-void  SPlot2d_Viewer::AfterDisplay( SALOME_Displayer* d )
-{
-  d->AfterDisplay( this, SALOME_Plot2dViewType() );
+  return prs;
 }
 
 /*!
@@ -311,6 +281,23 @@ bool SPlot2d_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& IObject
   return aViewFrame->isVisible( curve );
 }
 
+/*!
+  \Collect objects visible in viewer
+  \param theList - visible objects collection
+*/
+void SPlot2d_Viewer::GetVisible( SALOME_ListIO& theList )
+{
+  Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
+  if(aViewFrame == NULL) return;
+  CurveDict aCurves = aViewFrame->getCurves();
+  CurveDict::Iterator it = aCurves.begin();
+  for( ; it != aCurves.end(); ++it ) {
+    SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value()); 
+    if ( aCurve && aCurve->hasIO() && aViewFrame->isVisible( aCurve ) )
+      theList.Append( aCurve->getIO() );
+  }
+}
+
 /*!
   Return interactive obeject if is presented in the viewer
 */
@@ -320,10 +307,11 @@ Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry
   Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
   if(aViewFrame == NULL) return anIO;
 
-  QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
-  for ( ; it.current(); ++it ) {
-    SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
-    if ( aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
+  CurveDict aCurves = aViewFrame->getCurves();
+  CurveDict::Iterator it = aCurves.begin();
+  for( ; it != aCurves.end(); ++it ) {
+    SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.value()); 
+    if ( aCurve && aCurve->hasIO() && !strcmp( aCurve->getIO()->getEntry(), Entry ) ) {
       anIO = aCurve->getIO();
       break;
     }
@@ -331,12 +319,9 @@ Handle(SALOME_InteractiveObject) SPlot2d_Viewer::FindIObject( const char* Entry
   return anIO;
 }
 
-//==========================================================
 /*!
- *  SPlot2d_Viewer::getActiveViewFrame
- *  Returns an active Plot2d ViewFrame or NULL
- */
-//==========================================================
+  Returns an active Plot2d ViewFrame or NULL
+*/
 Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
 {
   SUIT_ViewManager* aViewMgr = getViewManager();
@@ -349,42 +334,156 @@ Plot2d_ViewFrame* SPlot2d_Viewer::getActiveViewFrame()
   return NULL;
 }
 
+/*!
+  \return curve by object and viewframe
+  \param theIObject - object
+  \param fr - viewframe
+*/
 SPlot2d_Curve* SPlot2d_Viewer::getCurveByIO( const Handle(SALOME_InteractiveObject)& theIObject,
-                                            Plot2d_ViewFrame* fr )
+                                             Plot2d_ViewFrame* fr )
 {
   if ( !theIObject.IsNull() ) {
     Plot2d_ViewFrame* aViewFrame = fr ? fr : getActiveViewFrame();
     if(aViewFrame) {
-      QIntDictIterator<Plot2d_Curve> it( aViewFrame->getCurves() );
-      for ( ; it.current(); ++it ) {
-       SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>(it.current()); 
-       if(aCurve) {
-         if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
-           return aCurve;
-       }
+      CurveDict aCurves = aViewFrame->getCurves();
+      CurveDict::Iterator it = aCurves.begin();
+      for( ; it != aCurves.end(); ++it ) {
+        SPlot2d_Curve* aCurve = dynamic_cast<SPlot2d_Curve*>( it.value() );
+        if(aCurve) {
+          if ( aCurve->hasIO() && aCurve->getIO()->isSame( theIObject ) )
+            return aCurve;
+        }
       }
     }
   }
   return NULL;
 }
 
-void SPlot2d_Viewer::onCloneView( Plot2d_ViewFrame* clonedVF, Plot2d_ViewFrame* newVF )
+/*!
+  create SPlot2d_ViewWindow
+*/
+SUIT_ViewWindow* SPlot2d_Viewer::createView( SUIT_Desktop* theDesktop )
+{
+  SPlot2d_ViewWindow* aPlot2dView = new SPlot2d_ViewWindow(theDesktop, this);
+  aPlot2dView->initLayout();
+  if (getPrs())
+    aPlot2dView->getViewFrame()->Display(getPrs());
+  return aPlot2dView;
+}
+
+/*!
+  SLOT: called when action "Legend Clicked" is activated.
+  override "onLegendClicked" method from Plot2d_ViewModel.
+*/
+void SPlot2d_Viewer::onClicked( const QVariant& itemInfo, int index )
 {
-  if( !clonedVF || !newVF )
-    return;
+  Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
+  if(aViewFrame == NULL) return;
 
-  // 1) Copy all properties of view
+  QwtPlotItem* plotItem = aViewFrame->getPlot()->infoToItem( itemInfo );
 
-  newVF->copyPreferences( clonedVF );
+  bool isCurveSelected = false;
+  CurveDict aCurves = aViewFrame->getCurves();
+  for( CurveDict::Iterator it = aCurves.begin(); it != aCurves.end(); ++it )
+  {
+    if(plotItem == it.key()) {
+      isCurveSelected = true;
+      it.value()->setSelected(true);
+    } else {
+      it.value()->setSelected(false);
+    }
+  }
 
-  // 2) Display all curves displayed in cloned view
+  AnalyticalCurveList curves = aViewFrame->getAnalyticalCurves();
+   foreach ( Plot2d_AnalyticalCurve* curve, curves ) {
+          if(plotItem == curve->plotItem()) {
+          isCurveSelected = true;
+                 curve->setSelected(true);
+          } else {
+                 curve->setSelected(false);
+          }
+   }
+  if(isCurveSelected) {
+    for( CurveDict::Iterator it = aCurves.begin(); it != aCurves.end(); ++it )
+      aViewFrame->updateCurve( it.value() );
+
+       myDeselectAnalytical = false;
+       emit clearSelected();
+       aViewFrame->updateAnalyticalCurves();
+       myDeselectAnalytical = true;
+       return;
+  }
 
-  QList<Plot2d_Curve> aCurves;
-  clonedVF->getCurves( aCurves );
-  QList<Plot2d_Curve>::const_iterator anIt = aCurves.begin(), aLast = aCurves.end();
+  Plot2d_Object* anObject = aViewFrame->getPlotObject(plotItem);
+  if(anObject) {
+    
+    // Highlight object in Object Browser
+    QString anEntry;
+    if(SPlot2d_Curve* aSCurve = dynamic_cast<SPlot2d_Curve*>(anObject)) {
+      if(aSCurve->hasIO())
+       anEntry = aSCurve->getIO()->getEntry();
+    } else if( SPlot2d_Histogram* aSHisto = dynamic_cast<SPlot2d_Histogram*>(anObject)) {
+      if(aSHisto->hasIO())
+       anEntry = aSHisto->getIO()->getEntry();
+    }
+    
+    if(!anEntry.isEmpty())
+      emit legendSelected( anEntry );
+  }    
+}
+
+/*!
+  
+*/
+void SPlot2d_Viewer::setObjectsSelected( SALOME_ListIO& theList ) {
+  Plot2d_ViewFrame* aViewFrame = getActiveViewFrame();
+  if(aViewFrame) {
+
+    objectList allObjects;
+    aViewFrame->getObjects( allObjects );
+    
+    bool isSelected = false;
+    SPlot2d_Histogram* h = 0;
+    SPlot2d_Curve* c =0;
+    
+    foreach ( Plot2d_Object* o, allObjects ) {
+      isSelected = false;
+      
+      Handle(SALOME_InteractiveObject) io;
+      if( (h = dynamic_cast<SPlot2d_Histogram*>(o)) && h->hasIO() ) {
+       io = h->getIO();
+      } else if((c = dynamic_cast<SPlot2d_Curve*>(o)) && c->hasIO()) {
+       io = c->getIO();
+      } else {
+       continue;
+      }
 
-  for( ; anIt!=aLast; anIt++ )
-    if( clonedVF->isVisible( *anIt ) )
-      newVF->displayCurve( *anIt, false );
-  newVF->Repaint();
+      SALOME_ListIteratorOfListIO anIter( theList ); 
+      
+      for( ; anIter.More(); anIter.Next() ) {
+       if ( anIter.Value()->hasEntry() ) {
+         if( io->isSame(anIter.Value()) ) {
+           isSelected = o->isSelected();
+           if( !isSelected ) {
+             o->setSelected(true);
+             aViewFrame->updateObject(o);
+             theList.Remove(anIter);
+             isSelected = true;
+             break;
+           } else 
+             break;
+         }
+       }
+      }
+      if( !isSelected && o->isSelected() != false ) {  
+       o->setSelected(false);
+       aViewFrame->updateObject(o);
+      }
+    }
+       if( myDeselectAnalytical ) {
+               aViewFrame->deselectAnalyticalCurves();
+               aViewFrame->updateAnalyticalCurves(); 
+       }
+    aViewFrame->Repaint();
+  }
 }