Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / Plot2d / Plot2d_ViewFrame.cxx
index 2c9c8d7488d950e4e10ae735602376d28eeb2891..cde7a39613c5c7df7f04684a70a18f7b4cf821ad 100644 (file)
@@ -9,6 +9,8 @@
 
 #include "Plot2d_ViewFrame.h"
 #include "Plot2d_SetupViewDlg.h"
+#include "Plot2d_Prs.h"
+
 #include "QAD_Desktop.h"
 #include "QAD_ResourceMgr.h"
 #include "QAD_FileDlg.h"
@@ -24,6 +26,7 @@
 #include <qtoolbar.h>
 #include <qtoolbutton.h>
 #include <qcursor.h>
+#include <qcolordialog.h>
 #include <qwt_math.h>
 #include <qwt_plot_canvas.h>
 #include <stdlib.h>
@@ -33,6 +36,7 @@
 #include <SALOMEconfig.h>
 #include CORBA_SERVER_HEADER(SALOMEDS)
 #include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
+using namespace std;
 
 #define DEFAULT_LINE_WIDTH     0     // (default) line width
 #define DEFAULT_MARKER_SIZE    9     // default marker size
@@ -233,6 +237,13 @@ void Plot2d_ViewFrame::createActions()
   fitDataAction->setStatusTip ( tr( "PRP_PLOT2D_FITDATA" ) );
   myActions.insert( FitDataId, fitDataAction );
   connect( fitDataAction, SIGNAL( activated() ), this, SLOT( onFitData() ) );
+
+  // Change background
+  QActionP* changeBGAction = new QActionP ( tr( "TOT_PLOT2D_CHANGE_BACKGROUND"),
+                                           tr( "MEN_PLOT2D_CHANGE_BACKGROUND" ), 0, this );
+  fitDataAction->setStatusTip ( tr( "PRP_PLOT2D_CHANGE_BACKGROUND" ) );
+  myActions.insert( ChangeBackgroundId, changeBGAction );
+  connect( changeBGAction, SIGNAL( activated() ), this, SLOT( onChangeBackground() ) );
 }
 /*!
   Gets window's central widget
@@ -274,6 +285,9 @@ void Plot2d_ViewFrame::onCreatePopup()
     // settings
     myPopup->insertSeparator();
     myActions[ SettingsId ]->addTo( myPopup );
+    // Change background
+    myPopup->insertSeparator();
+    myActions[ ChangeBackgroundId ]->addTo( myPopup );
   }
 }
 /*!
@@ -299,7 +313,17 @@ void Plot2d_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject,
 */
 bool Plot2d_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
 {
-  return ( getCurveByIO( IObject ) != NULL );
+  if( getCurveByIO( IObject ) != NULL )
+    return 1;
+  else{
+    if(!IObject.IsNull()){
+      QIntDictIterator<Plot2d_Curve> it(myCurves);
+      for(; it.current();++it){
+       if(it.current()->hasIO() && it.current()->getTableIO()->isSame(IObject))
+         return 1;
+      }}
+  }
+  return 0;
 }
 /*!
   Returns true if interactive object is presented in the viewer and displayed
@@ -363,6 +387,21 @@ void Plot2d_ViewFrame::Erase( const Handle(SALOME_InteractiveObject)& IObject, b
   Plot2d_Curve* curve = getCurveByIO( IObject );
   if ( curve )
     eraseCurve( curve, update );
+  // it can be table or container object selected
+  QAD_Study* activeStudy = QAD_Application::getDesktop()->getActiveStudy();
+  SALOMEDS::SObject_var aSO = activeStudy->getStudyDocument()->FindObjectID(IObject->getEntry());
+  if ( !aSO->_is_nil() ) {
+    SALOMEDS::ChildIterator_var aIter = activeStudy->getStudyDocument()->NewChildIterator( aSO );
+    for ( ; aIter->More(); aIter->Next() ) {
+      SALOMEDS::SObject_var aChildSO = aIter->Value();
+      SALOMEDS::SObject_var refSO;
+      if ( aChildSO->ReferencedObject( refSO ) && !refSO->_is_nil() )
+       aChildSO = refSO;
+      curve = getCurveByIO( new SALOME_InteractiveObject( aChildSO->GetID(), "", "" ) );
+      if ( curve )
+       eraseCurve( curve, update );
+    }
+  }
 }
 /*!
   Actually this method just re-displays all curves which are presented in the viewer
@@ -373,7 +412,7 @@ void Plot2d_ViewFrame::DisplayAll()
   getCurves( clist );
   for ( int i = 0; i < clist.count(); i++ ) {
     updateCurve( clist.at( i ), false );
-  }
+   }
   myPlot->replot();
 }
 /*!
@@ -513,11 +552,59 @@ void Plot2d_ViewFrame::writePreferences()
 */
 QString Plot2d_ViewFrame::getInfo( const QPoint& pnt ) 
 {
-  QString info;
-  info.sprintf( "X : %g\tY : %g",
-               myPlot->invTransform( QwtPlot::xBottom, pnt.x() ),
-               myPlot->invTransform( QwtPlot::yLeft,   pnt.y() ) );
-  info = tr( "INF_COORDINATES" ) + " : " + info;
+  bool xFound = false, yFound = false;
+  double xCoord, yCoord;
+  const QwtScaleDiv* aXscale = myPlot->axisScale( QwtPlot::xBottom );
+  for ( int i = 0; i < aXscale->majCnt(); i++ ) {
+    double majXmark = aXscale->majMark( i );
+    int xmark = myPlot->transform( QwtPlot::xBottom, majXmark );
+    if ( xmark-2 == pnt.x() ) {
+      xCoord = majXmark; 
+      xFound = true;
+      MESSAGE("Plot2d_ViewFrame::getInfo : close maj X mark("<<i<<") = "<<majXmark<<" "<<xmark<<" "<<pnt.x());
+      break;
+    }
+  }
+  if ( !xFound ) {
+    for ( int i = 0; i < aXscale->minCnt(); i++ ) {
+      double minXmark = aXscale->minMark( i );
+      int xmark = myPlot->transform( QwtPlot::xBottom, minXmark );
+      if ( xmark-2 == pnt.x() ) {
+       xCoord = minXmark; 
+       xFound = true;
+       MESSAGE("Plot2d_ViewFrame::getInfo : close min X mark("<<i<<") = "<<minXmark<<" "<<xmark<<" "<<pnt.x());
+       break;
+      }
+    }
+  }  
+  const QwtScaleDiv* aYscale = myPlot->axisScale( QwtPlot::yLeft );
+  for ( int i = 0; i < aYscale->majCnt(); i++ ) {
+    double majYmark = aYscale->majMark( i );
+    int ymark = myPlot->transform( QwtPlot::yLeft, majYmark );
+    if ( ymark-2 == pnt.y() ) {
+      yCoord = majYmark; 
+      yFound = true;
+      break;
+    }
+  }
+  if ( !yFound ) {
+    for ( int i = 0; i < aYscale->minCnt(); i++ ) {
+      double minYmark = aYscale->minMark( i );
+      int ymark = myPlot->transform( QwtPlot::yLeft, minYmark );
+      if ( ymark-2 == pnt.y() ) {
+       yCoord = minYmark; 
+       yFound = true;
+       break;
+      }
+    }
+  }  
+  QString strX = QString::number( xFound ? xCoord : myPlot->invTransform( QwtPlot::xBottom, pnt.x() ) ).stripWhiteSpace();
+  if ( strX == "-0" )
+    strX = "0";
+  QString strY = QString::number( yFound ? yCoord : myPlot->invTransform( QwtPlot::yLeft, pnt.y() ) ).stripWhiteSpace();
+  if ( strY == "-0" )
+    strY = "0";
+  QString info = tr("INF_COORDINATES").arg( strX ).arg( strY );
   return info;
 }
 /*!
@@ -737,6 +824,7 @@ void Plot2d_ViewFrame::updateCurve( Plot2d_Curve* curve, bool update )
                                                   QSize( myMarkerSize, myMarkerSize ) ) );
     }
     myPlot->setCurveTitle( curveKey, curve->getVerTitle() );
+    myPlot->setCurveData( curveKey, curve->horData(), curve->verData(), curve->nbPoints() );
     myPlot->curve( curveKey )->setEnabled( true );
     updateTitles();
     if ( update )
@@ -1072,6 +1160,16 @@ void Plot2d_ViewFrame::onFitData()
   }
   delete dlg;
 }
+/*!
+  Change background color
+*/
+void Plot2d_ViewFrame::onChangeBackground()
+{
+  QColor selColor = QColorDialog::getColor ( backgroundColor(), this );        
+  if ( selColor.isValid() ) {
+    setBackgroundColor( selColor );
+  }
+}
 /*!
   Sets curve type
 */
@@ -1349,29 +1447,11 @@ void Plot2d_ViewFrame::plotMouseMoved( const QMouseEvent& me )
 
   if ( myOperation != NoOpId) {
     if ( myOperation == ZoomId ) {
-      QwtDiMap xMap = myPlot->canvasMap( QwtPlot::xBottom );
-      QwtDiMap yMap = myPlot->canvasMap( QwtPlot::yLeft );
-
-      myPlot->setAxisScale( QwtPlot::yLeft, 
-                           myPlot->invTransform( QwtPlot::yLeft, yMap.i1() ), 
-                           myPlot->invTransform( QwtPlot::yLeft, yMap.i2() + dy ) );
-      myPlot->setAxisScale( QwtPlot::xBottom, 
-                           myPlot->invTransform( QwtPlot::xBottom, xMap.i1() ), 
-                           myPlot->invTransform( QwtPlot::xBottom, xMap.i2() - dx ) );
-      myPlot->replot();
+      this->incrementalZoom( dx, dy ); 
       myPnt = me.pos();
     }
     else if ( myOperation == PanId ) {
-      QwtDiMap xMap = myPlot->canvasMap( QwtPlot::xBottom );
-      QwtDiMap yMap = myPlot->canvasMap( QwtPlot::yLeft );
-      
-      myPlot->setAxisScale( QwtPlot::yLeft, 
-                           myPlot->invTransform( QwtPlot::yLeft, yMap.i1()-dy ), 
-                           myPlot->invTransform( QwtPlot::yLeft, yMap.i2()-dy ) );
-      myPlot->setAxisScale( QwtPlot::xBottom, 
-                           myPlot->invTransform( QwtPlot::xBottom, xMap.i1()-dx ),
-                           myPlot->invTransform( QwtPlot::xBottom, xMap.i2()-dx ) ); 
-      myPlot->replot();
+      this->incrementalPan( dx, dy );
       myPnt = me.pos();
     }
   }
@@ -1634,3 +1714,172 @@ bool Plot2d_Plot2d::existMarker( const QwtSymbol::Style typeMarker, const QColor
   }
   return false;
 }
+
+//==========================================================
+/*!
+ *  Plot2d_ViewFrame::Display
+ *  Display presentation
+ */
+//==========================================================
+void Plot2d_ViewFrame::Display( const SALOME_Prs2d* prs )
+{
+  // try do downcast object
+  const Plot2d_Prs* aPlot2dPrs = dynamic_cast<const Plot2d_Prs*>( prs );
+  if ( !aPlot2dPrs || aPlot2dPrs->IsNull() )
+    return;
+
+  // display all curves from presentation
+  Plot2d_CurveContainer aCurves = aPlot2dPrs->GetObjects();
+  displayCurves( aCurves );
+}
+
+//==========================================================
+/*!
+ *  Plot2d_ViewFrame::Erase
+ *  Erase presentation
+ */
+//==========================================================
+void Plot2d_ViewFrame::Erase( const SALOME_Prs2d* prs, const bool )
+{
+  // try do downcast object
+  const Plot2d_Prs* aPlot2dPrs = dynamic_cast<const Plot2d_Prs*>( prs );
+  if ( !aPlot2dPrs || aPlot2dPrs->IsNull() )
+    return;
+
+  // erase all curves from presentation
+  Plot2d_CurveContainer aCurves = aPlot2dPrs->GetObjects();
+  eraseCurves( aCurves );
+}
+  
+//==========================================================
+/*!
+ *  Plot2d_ViewFrame::CreatePrs
+ *  Create presentation by entry
+ */
+//==========================================================
+SALOME_Prs* Plot2d_ViewFrame::CreatePrs( const char* entry )
+{
+  Plot2d_Prs* prs = new Plot2d_Prs();
+  if ( entry ) {
+    QIntDictIterator<Plot2d_Curve> it( myCurves );
+    for ( ; it.current(); ++it ) {
+      if ( it.current()->hasIO() && !strcmp( it.current()->getIO()->getEntry(), entry ) ) {
+       prs->AddObject( it.current() );
+      }
+    }
+  }
+  return prs;
+}
+
+//==========================================================
+/*!
+ *  Plot2d_ViewFrame::BeforeDisplay
+ *  Axiluary method called before displaying of objects
+ */
+//==========================================================
+void  Plot2d_ViewFrame::BeforeDisplay( SALOME_Displayer* d )
+{
+  d->BeforeDisplay( this, SALOME_Plot2dViewType() );
+}
+
+//==========================================================
+/*!
+ *  Plot2d_ViewFrame::AfterDisplay
+ *  Axiluary method called after displaying of objects
+ */
+//==========================================================
+void  Plot2d_ViewFrame::AfterDisplay( SALOME_Displayer* d )
+{
+  d->AfterDisplay( this, SALOME_Plot2dViewType() );
+}
+
+#define INCREMENT_FOR_OP 10
+
+//=======================================================================
+// Plot2d_ViewFrame::onPanLeft
+// Performs incremental panning to the left
+//=======================================================================
+void Plot2d_ViewFrame::onPanLeft()
+{
+  this->incrementalPan( -INCREMENT_FOR_OP, 0 );
+}
+
+//=======================================================================
+// Plot2d_ViewFrame::onPanRight
+// Performs incremental panning to the right
+//=======================================================================
+void Plot2d_ViewFrame::onPanRight()
+{
+  this->incrementalPan( INCREMENT_FOR_OP, 0 );
+}
+
+//=======================================================================
+// Plot2d_ViewFrame::onPanUp
+// Performs incremental panning to the top
+//=======================================================================
+void Plot2d_ViewFrame::onPanUp()
+{
+  this->incrementalPan( 0, -INCREMENT_FOR_OP );
+}
+
+//=======================================================================
+// Plot2d_ViewFrame::onPanDown
+// Performs incremental panning to the bottom
+//=======================================================================
+void Plot2d_ViewFrame::onPanDown()
+{
+  this->incrementalPan( 0, INCREMENT_FOR_OP );
+}
+
+//=======================================================================
+// Plot2d_ViewFrame::onZoomIn
+// Performs incremental zooming in
+//=======================================================================
+void Plot2d_ViewFrame::onZoomIn()
+{
+  this->incrementalZoom( INCREMENT_FOR_OP, INCREMENT_FOR_OP );
+}
+
+//=======================================================================
+// Plot2d_ViewFrame::onZoomOut
+// Performs incremental zooming out
+//=======================================================================
+void Plot2d_ViewFrame::onZoomOut()
+{
+  this->incrementalZoom( -INCREMENT_FOR_OP, -INCREMENT_FOR_OP );
+}
+
+//=======================================================================
+// Plot2d_ViewFrame::incrementalPan
+// Incremental zooming operation
+//=======================================================================
+void Plot2d_ViewFrame::incrementalPan( const int incrX, const int incrY ) {
+  QwtDiMap xMap = myPlot->canvasMap( QwtPlot::xBottom );
+  QwtDiMap yMap = myPlot->canvasMap( QwtPlot::yLeft );
+  
+  myPlot->setAxisScale( QwtPlot::yLeft,
+                       myPlot->invTransform( QwtPlot::yLeft, yMap.i1()-incrY ),
+                       myPlot->invTransform( QwtPlot::yLeft, yMap.i2()-incrY ) );
+  myPlot->setAxisScale( QwtPlot::xBottom,
+                       myPlot->invTransform( QwtPlot::xBottom, xMap.i1()-incrX ),
+                       myPlot->invTransform( QwtPlot::xBottom, xMap.i2()-incrX ) );
+  myPlot->replot();
+}
+
+//=======================================================================
+// Plot2d_ViewFrame::incrementalZoom
+// Incremental panning operation
+//=======================================================================
+void Plot2d_ViewFrame::incrementalZoom( const int incrX, const int incrY ) {
+  QwtDiMap xMap = myPlot->canvasMap( QwtPlot::xBottom );
+  QwtDiMap yMap = myPlot->canvasMap( QwtPlot::yLeft );
+  
+  myPlot->setAxisScale( QwtPlot::yLeft,
+                      myPlot->invTransform( QwtPlot::yLeft, yMap.i1() ),
+                      myPlot->invTransform( QwtPlot::yLeft, yMap.i2() + incrY ) );
+  myPlot->setAxisScale( QwtPlot::xBottom,
+                      myPlot->invTransform( QwtPlot::xBottom, xMap.i1() ),
+                      myPlot->invTransform( QwtPlot::xBottom, xMap.i2() - incrX ) );
+  myPlot->replot();
+}
+