Browse Source

Support custom bounds in ctkVTKChartViews

Julien Finet 14 years ago
parent
commit
d40b3d6870

+ 100 - 62
Libs/Visualization/VTK/Widgets/ctkVTKChartView.cpp

@@ -49,9 +49,11 @@ protected:
 public:
   ctkVTKChartViewPrivate(ctkVTKChartView& object);
   void init();
+  void chartBounds(double* bounds)const;
 
   vtkSmartPointer<vtkContextView> ContextView;
   vtkSmartPointer<vtkChartXY> Chart;
+  double UserBounds[8];
 };
 
 // ----------------------------------------------------------------------------
@@ -64,6 +66,8 @@ ctkVTKChartViewPrivate::ctkVTKChartViewPrivate(ctkVTKChartView& object)
   this->ContextView = vtkSmartPointer<vtkContextView>::New();
   this->Chart = vtkSmartPointer<vtkChartXY>::New();
   this->ContextView->GetScene()->AddItem(this->Chart);
+  this->UserBounds[0] = this->UserBounds[2] = this->UserBounds[4] = this->UserBounds[6] = 0.;
+  this->UserBounds[1] = this->UserBounds[3] = this->UserBounds[5] = this->UserBounds[7] = -1.;
 }
 
 // ----------------------------------------------------------------------------
@@ -81,71 +85,12 @@ void ctkVTKChartViewPrivate::init()
 }
 
 // ----------------------------------------------------------------------------
-// ctkVTKChartView methods
-
-// ----------------------------------------------------------------------------
-ctkVTKChartView::ctkVTKChartView(QWidget* parentWidget)
-  :QVTKWidget(parentWidget)
-  , d_ptr(new ctkVTKChartViewPrivate(*this))
-{
-  Q_D(ctkVTKChartView);
-  d->init();
-  this->setAutomaticImageCacheEnabled(true);
-}
-
-// ----------------------------------------------------------------------------
-ctkVTKChartView::~ctkVTKChartView()
-{
-}
-
-// ----------------------------------------------------------------------------
-void ctkVTKChartView::setTitle(const QString& newTitle)
-{
-  Q_D(ctkVTKChartView);
-  d->Chart->SetTitle(newTitle.toLatin1().data());
-}
-
-// ----------------------------------------------------------------------------
-QString ctkVTKChartView::title()const
-{
-  Q_D(const ctkVTKChartView);
-  return QString(d->Chart->GetTitle());
-}
-
-// ----------------------------------------------------------------------------
-vtkChartXY* ctkVTKChartView::chart()const
-{
-  Q_D(const ctkVTKChartView);
-  return d->Chart;
-}
-
-// ----------------------------------------------------------------------------
-vtkContextScene* ctkVTKChartView::scene()const
-{
-  Q_D(const ctkVTKChartView);
-  return d->ContextView->GetScene();
-}
-
-// ----------------------------------------------------------------------------
-void ctkVTKChartView::addPlot(vtkPlot* plot)
-{
-  Q_D(ctkVTKChartView);
-  d->Chart->AddPlot(plot);
-  emit this->plotAdded(plot);
-  this->onChartUpdated();
-}
-
-// ----------------------------------------------------------------------------
-void ctkVTKChartView::onChartUpdated()
-{
-}
-
-// ----------------------------------------------------------------------------
-void ctkVTKChartView::chartBounds(double* bounds)
+void ctkVTKChartViewPrivate::chartBounds(double* bounds)const
 {
+  Q_Q(const ctkVTKChartView);
   bounds[0] = bounds[2] = bounds[4] = bounds[6] = VTK_DOUBLE_MAX;
   bounds[1] = bounds[3] = bounds[5] = bounds[7] = VTK_DOUBLE_MIN;
-  vtkChartXY* chart = this->chart();
+  vtkChartXY* chart = q->chart();
   const vtkIdType plotCount = chart->GetNumberOfPlots();
   for (vtkIdType i = 0; i < plotCount; ++i)
     {
@@ -213,6 +158,99 @@ void ctkVTKChartView::chartBounds(double* bounds)
 }
 
 // ----------------------------------------------------------------------------
+// ctkVTKChartView methods
+
+// ----------------------------------------------------------------------------
+ctkVTKChartView::ctkVTKChartView(QWidget* parentWidget)
+  :QVTKWidget(parentWidget)
+  , d_ptr(new ctkVTKChartViewPrivate(*this))
+{
+  Q_D(ctkVTKChartView);
+  d->init();
+  this->setAutomaticImageCacheEnabled(true);
+}
+
+// ----------------------------------------------------------------------------
+ctkVTKChartView::~ctkVTKChartView()
+{
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::setTitle(const QString& newTitle)
+{
+  Q_D(ctkVTKChartView);
+  d->Chart->SetTitle(newTitle.toLatin1().data());
+}
+
+// ----------------------------------------------------------------------------
+QString ctkVTKChartView::title()const
+{
+  Q_D(const ctkVTKChartView);
+  return QString(d->Chart->GetTitle());
+}
+
+// ----------------------------------------------------------------------------
+vtkChartXY* ctkVTKChartView::chart()const
+{
+  Q_D(const ctkVTKChartView);
+  return d->Chart;
+}
+
+// ----------------------------------------------------------------------------
+vtkContextScene* ctkVTKChartView::scene()const
+{
+  Q_D(const ctkVTKChartView);
+  return d->ContextView->GetScene();
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::addPlot(vtkPlot* plot)
+{
+  Q_D(ctkVTKChartView);
+  d->Chart->AddPlot(plot);
+  emit this->plotAdded(plot);
+  this->onChartUpdated();
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::onChartUpdated()
+{
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::chartBounds(double* bounds)const
+{
+  Q_D(const ctkVTKChartView);
+  if (d->UserBounds[1] < d->UserBounds[0])
+    {
+    d->chartBounds(bounds);
+    return;
+    }
+  this->chartUserBounds(bounds);
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::setChartUserBounds(double* userBounds)
+{
+  Q_D(ctkVTKChartView);
+  for (int i= 0; i < 8; ++i)
+    {
+    d->UserBounds[i] = userBounds[i];
+    }
+  emit boundsChanged();
+}
+
+// ----------------------------------------------------------------------------
+void ctkVTKChartView::chartUserBounds(double* bounds)const
+{
+  Q_D(const ctkVTKChartView);
+  for (int i= 0; i < 8; ++i)
+    {
+    bounds[i] = d->UserBounds[i];
+    }
+}
+
+// ----------------------------------------------------------------------------
 void ctkVTKChartView::setAxesToChartBounds()
 {
   vtkChartXY* chart = this->chart();

+ 10 - 3
Libs/Visualization/VTK/Widgets/ctkVTKChartView.h

@@ -54,9 +54,16 @@ public:
   QString title()const;
   void setTitle(const QString& title);
 
-  /// Compute the bounds for the 4 chart axes
-  void chartBounds(double* bounds);
-  void setAxesToChartBounds();
+  /// Return the chart bounds for the 4 chart axes.
+  /// bounds must be an array of 8 doubles.
+  /// If no bounds is provided by the user, compute the bounds for the 4 chart
+  /// axes from the vtkPlots bounds.
+  void chartBounds(double* bounds)const;
+  void setChartUserBounds(double* bounds);
+  void chartUserBounds(double* bounds)const;
+
+  /// 
+  virtual void setAxesToChartBounds();
   void boundAxesToChartBounds();
 
 signals:

+ 28 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsView.cpp

@@ -433,6 +433,22 @@ void ctkVTKScalarsToColorsView
 }
 
 // ----------------------------------------------------------------------------
+void ctkVTKScalarsToColorsView
+::setUserBoundsToPlots(double* bounds)
+{
+  foreach(vtkScalarsToColorsItem* plot,
+          this->plots<vtkScalarsToColorsItem>())
+    {
+    plot->SetUserBounds(bounds);
+    }
+  foreach(vtkControlPointsItem* plot,
+          this->plots<vtkControlPointsItem>())
+    {
+    plot->SetUserBounds(bounds);
+    }
+}
+
+// ----------------------------------------------------------------------------
 void ctkVTKScalarsToColorsView::editPoint(vtkObject* caller, void* callData)
 {
   vtkControlPointsItem* controlPoints = reinterpret_cast<vtkControlPointsItem*>(caller);
@@ -459,3 +475,15 @@ void ctkVTKScalarsToColorsView::editPoint(vtkObject* caller, void* callData)
       }
     }
 }
+
+// ----------------------------------------------------------------------------
+void ctkVTKScalarsToColorsView::setAxesToChartBounds()
+{
+  vtkChartXY* chart = this->chart();
+  double userBounds[8];
+  this->chartUserBounds(userBounds);
+  if (userBounds[0] < userBounds[1])
+    {
+    this->setUserBoundsToPlots(userBounds);
+    }
+}

+ 4 - 0
Libs/Visualization/VTK/Widgets/ctkVTKScalarsToColorsView.h

@@ -75,7 +75,11 @@ public:
   void setColorTransferFunctionToPlots(vtkColorTransferFunction* colorTF);
   void setOpacityFunctionToPlots(vtkPiecewiseFunction* opacityTF);
   void setPiecewiseFunctionToPlots(vtkPiecewiseFunction* piecewiseTF);
+  
+  void setUserBoundsToPlots(double* bounds);
 
+  /// Reimplemented to set the bounds to the plots as well
+  virtual void setAxesToChartBounds();
 public slots:
   void editPoint(vtkObject* plot, void * pointId);