QChartTable.cxx 3.79 KB
Newer Older
1 2 3
/*=========================================================================

  Program:   Visualization Toolkit
4
  Module:    QChartTable.cxx
5 6 7 8 9 10 11 12 13 14

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
15 16 17 18
#include "QVTKOpenGLWidget.h"
#include "vtkChartXY.h"
#include "vtkContextScene.h"
#include "vtkContextView.h"
19
#include "vtkFloatArray.h"
20
#include "vtkGenericOpenGLRenderWindow.h"
21
#include "vtkMath.h"
22 23 24
#include "vtkNew.h"
#include "vtkPlot.h"
#include "vtkQtTableView.h"
25 26 27 28 29 30 31 32 33 34
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"
#include "vtkTimerLog.h"

#include <QApplication>
#include <QWidget>
#include <QMainWindow>
#include <QHBoxLayout>
35
#include <QSurfaceFormat>
36 37 38 39

//----------------------------------------------------------------------------
int main( int argc, char * argv [] )
{
40 41 42
  // needed to ensure appropriate OpenGL context is created for VTK rendering.
  QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());

43 44
  // Qt initialization
  QApplication app(argc, argv);
45 46
  QMainWindow mainWindow;
  mainWindow.setGeometry(0, 0, 1150, 600);
47

48 49 50 51
  QVTKOpenGLWidget *qvtkWidget = new QVTKOpenGLWidget(&mainWindow);

  vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
  qvtkWidget->SetRenderWindow(renderWindow);
52 53

  // Set up my 2D world...
54 55
  vtkNew<vtkContextView> view; // This contains a chart object
  view->SetRenderWindow(renderWindow);
Dan Lipsa's avatar
Dan Lipsa committed
56
  view->SetInteractor(renderWindow->GetInteractor());
57 58

  // Create a table with some points in it...
59 60
  vtkNew<vtkTable> table;
  vtkNew<vtkFloatArray> arrX;
61 62
  arrX->SetName("X Axis");
  table->AddColumn(arrX);
63
  vtkNew<vtkFloatArray> arrC;
64 65
  arrC->SetName("Cosine");
  table->AddColumn(arrC);
66
  vtkNew<vtkFloatArray> arrS;
67 68 69 70
  arrS->SetName("Sine");
  table->AddColumn(arrS);

  // Make a timer object - need to get some frame rates/render times
71
  vtkNew<vtkTimerLog> timer;
72 73 74 75 76 77

  // Test charting with a few more points...
  int numPoints = 29;
  float inc = 7.0 / (numPoints-1);
  table->SetNumberOfRows(numPoints);
  for (int i = 0; i < numPoints; ++i)
78
  {
79 80 81
    table->SetValue(i, 0, i * inc);
    table->SetValue(i, 1, cos(i * inc) + 0.0);
    table->SetValue(i, 2, sin(i * inc) + 0.0);
82
  }
83

84
//   table->Update();
85 86

  // Add multiple line plots, setting the colors etc
87
  vtkNew<vtkChartXY> chart;
88 89
  view->GetScene()->AddItem(chart);
  vtkPlot *line = chart->AddPlot(vtkChart::LINE);
90
  line->SetInputData(table, 0, 1);
91 92
  line->SetColor(255, 0, 0, 255);
  line = chart->AddPlot(vtkChart::LINE);
93
  line->SetInputData(table, 0, 2);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  line->SetColor(0, 255, 0, 255);
  line->SetWidth(2.0);

  // Instantiate a vtkQtChart and use that too
/*  vtkQtChart *qtChart = new vtkQtChart;
  chart = qtChart->chart();
  line = chart->AddPlot(vtkChart::LINE);
  line->SetTable(table, 0, 1);
  line->SetColor(255, 0, 0, 255);
  line = chart->AddPlot(vtkChart::LINE);
  line->SetTable(table, 0, 2);
  line->SetColor(0, 255, 0, 255);
  line->SetWidth(2.0);
*/
  // Now lets try to add a table view
109
  QWidget *widget = new QWidget(&mainWindow);
110
  QHBoxLayout *layout = new QHBoxLayout(widget);
111
  vtkNew<vtkQtTableView> tableView;
112 113 114 115 116 117
  tableView->SetSplitMultiComponentColumns(true);
  tableView->AddRepresentationFromInput(table);
  tableView->Update();
  layout->addWidget(qvtkWidget, 2);
  //layout->addWidget(qtChart, 2);
  layout->addWidget(tableView->GetWidget());
118
  mainWindow.setCentralWidget(widget);
119 120

  // Now show the application and start the event loop
121
  mainWindow.show();
122 123 124

  return app.exec();
}