| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 | /*=========================================================================  Library:   CTK  Copyright (c) Kitware Inc.  Licensed under the Apache License, Version 2.0 (the "License");  you may not use this file except in compliance with the License.  You may obtain a copy of the License at      http://www.commontk.org/LICENSE  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and  limitations under the License.=========================================================================*/// Qt includes#include <QDebug>#include <QMouseEvent>// CTK includes#include "ctkLogger.h"#include "ctkVTKChartView.h"// VTK includes#include <vtkAxis.h>#include <vtkChartXY.h>#include <vtkContext2D.h>#include <vtkContextMouseEvent.h>#include <vtkContextScene.h>#include <vtkContextView.h>#include <vtkOpenGLContextDevice2D.h>#include <vtkPlot.h>#include <vtkRenderWindow.h>//----------------------------------------------------------------------------static ctkLogger logger("org.commontk.visualization.vtk.widgets.ctkVTKChartView");//----------------------------------------------------------------------------class ctkVTKChartViewPrivate{  Q_DECLARE_PUBLIC(ctkVTKChartView);protected:  ctkVTKChartView* const q_ptr;public:  ctkVTKChartViewPrivate(ctkVTKChartView& object);  void init();  vtkSmartPointer<vtkContextView> ContextView;  vtkSmartPointer<vtkChartXY> Chart;};// ----------------------------------------------------------------------------// ctkVTKChartViewPrivate methods// ----------------------------------------------------------------------------ctkVTKChartViewPrivate::ctkVTKChartViewPrivate(ctkVTKChartView& object)  :q_ptr(&object){  this->ContextView = vtkSmartPointer<vtkContextView>::New();  this->Chart = vtkSmartPointer<vtkChartXY>::New();  this->ContextView->GetScene()->AddItem(this->Chart);}// ----------------------------------------------------------------------------void ctkVTKChartViewPrivate::init(){  Q_Q(ctkVTKChartView);  this->ContextView->SetInteractor(q->GetInteractor());  q->SetRenderWindow(this->ContextView->GetRenderWindow());  // low def for now (faster)  //q->GetRenderWindow()->SetMultiSamples(0);  //vtkOpenGLContextDevice2D::SafeDownCast(this->ContextView->GetContext()->GetDevice())  //                                       ->SetStringRendererToQt();  this->Chart->SetActionToButton(vtkChart::PAN, vtkContextMouseEvent::MIDDLE_BUTTON);  this->Chart->SetActionToButton(vtkChart::SELECT, vtkContextMouseEvent::RIGHT_BUTTON);}// ----------------------------------------------------------------------------// 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){  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();  const vtkIdType plotCount = chart->GetNumberOfPlots();  for (vtkIdType i = 0; i < plotCount; ++i)    {    vtkPlot* plot = chart->GetPlot(i);    int corner = chart->GetPlotCorner(plot);    double plotBounds[4];    plot->GetBounds(plotBounds);    switch (corner)      {      // bottom left      case 0:        // x        bounds[2] = bounds[2] > plotBounds[0] ?          plotBounds[0] : bounds[2];        bounds[3] = bounds[3] < plotBounds[1] ?          plotBounds[1] : bounds[3];        // y        bounds[0] = bounds[0] > plotBounds[2] ?          plotBounds[2] : bounds[0];        bounds[1] = bounds[1] < plotBounds[3] ?          plotBounds[3] : bounds[1];        break;      // bottom right      case 1:        // x        bounds[2] = bounds[2] > plotBounds[0] ?          plotBounds[0] : bounds[2];        bounds[3] = bounds[3] < plotBounds[1] ?          plotBounds[1] : bounds[3];        // y        bounds[4] = bounds[4] > plotBounds[2] ?          plotBounds[2] : bounds[4];        bounds[5] = bounds[5] < plotBounds[3] ?          plotBounds[3] : bounds[5];        break;      // top right      case 2:        // x        bounds[6] = bounds[6] > plotBounds[0] ?          plotBounds[0] : bounds[6];        bounds[7] = bounds[7] < plotBounds[1] ?          plotBounds[1] : bounds[7];        // y        bounds[4] = bounds[4] > plotBounds[2] ?          plotBounds[2] : bounds[4];        bounds[5] = bounds[5] < plotBounds[3] ?          plotBounds[3] : bounds[5];        break;      // top left      case 3:        // x        bounds[6] = bounds[6] > plotBounds[0] ?          plotBounds[0] : bounds[6];        bounds[7] = bounds[7] < plotBounds[1] ?          plotBounds[1] : bounds[7];        // y        bounds[0] = bounds[0] > plotBounds[2] ?          plotBounds[2] : bounds[1];        bounds[1] = bounds[0] < plotBounds[3] ?          plotBounds[3] : bounds[1];        break;      }    }}// ----------------------------------------------------------------------------void ctkVTKChartView::setAxesToChartBounds(){  vtkChartXY* chart = this->chart();  double bounds[8];  this->chartBounds(bounds);  for (int i = 0; i < chart->GetNumberOfAxes(); ++i)    {    if (bounds[2*i] != VTK_DOUBLE_MAX)      {      chart->GetAxis(i)->SetRange(bounds[2*i], bounds[2*i+1]);      chart->GetAxis(i)->SetBehavior(1);      }    }}// ----------------------------------------------------------------------------void ctkVTKChartView::boundAxesToChartBounds(){  vtkChartXY* chart = this->chart();  double bounds[8];  this->chartBounds(bounds);  for (int i = 0; i < chart->GetNumberOfAxes(); ++i)    {    if (bounds[2*i] != VTK_DOUBLE_MAX)      {      chart->GetAxis(i)->SetMinimumLimit(bounds[2*i]);      chart->GetAxis(i)->SetMaximumLimit(bounds[2*i + 1]);      }    }  emit boundsChanged();}// ----------------------------------------------------------------------------void ctkVTKChartView::mouseDoubleClickEvent(QMouseEvent* event){  if (event->button() == Qt::MidButton)    {    this->setAxesToChartBounds();    }  this->QVTKWidget::mouseDoubleClickEvent(event);}
 |