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

  Program:   Visualization Toolkit
4
  Module:    QChartTable.cxx
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

  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.

=========================================================================*/

#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"

#include "vtkSmartPointer.h"

#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkChartXY.h"
#include "vtkPlot.h"
#include "vtkTable.h"

#include "vtkTimerLog.h"

#include <QApplication>
#include <QWidget>
#include <QMainWindow>
#include <QHBoxLayout>

#include "QVTKWidget.h"
#include "vtkQtTableView.h"

#define VTK_CREATE(type, name) \
  vtkSmartPointer<type> name = vtkSmartPointer<type>::New()

//----------------------------------------------------------------------------
int main( int argc, char * argv [] )
{
  // Qt initialization
  QApplication app(argc, argv);
48 49
  QMainWindow mainWindow;
  mainWindow.setGeometry(0, 0, 1150, 600);
50 51

  // QVTK set up and initialization
52
  QVTKWidget *qvtkWidget = new QVTKWidget(&mainWindow);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

  // Set up my 2D world...
  VTK_CREATE(vtkContextView, view); // This contains a chart object
  view->SetInteractor(qvtkWidget->GetInteractor());
  qvtkWidget->SetRenderWindow(view->GetRenderWindow());

  // Create a table with some points in it...
  VTK_CREATE(vtkTable, table);
  VTK_CREATE(vtkFloatArray, arrX);
  arrX->SetName("X Axis");
  table->AddColumn(arrX);
  VTK_CREATE(vtkFloatArray, arrC);
  arrC->SetName("Cosine");
  table->AddColumn(arrC);
  VTK_CREATE(vtkFloatArray, arrS);
  arrS->SetName("Sine");
  table->AddColumn(arrS);

  // Make a timer object - need to get some frame rates/render times
  VTK_CREATE(vtkTimerLog, timer);

  // 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)
    {
    table->SetValue(i, 0, i * inc);
    table->SetValue(i, 1, cos(i * inc) + 0.0);
    table->SetValue(i, 2, sin(i * inc) + 0.0);
    }

85
//   table->Update();
86 87 88 89 90

  // Add multiple line plots, setting the colors etc
  vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
  view->GetScene()->AddItem(chart);
  vtkPlot *line = chart->AddPlot(vtkChart::LINE);
91
  line->SetInputData(table, 0, 1);
92 93
  line->SetColor(255, 0, 0, 255);
  line = chart->AddPlot(vtkChart::LINE);
94
  line->SetInputData(table, 0, 2);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  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
110
  QWidget *widget = new QWidget(&mainWindow);
111 112 113 114 115 116 117 118
  QHBoxLayout *layout = new QHBoxLayout(widget);
  VTK_CREATE(vtkQtTableView, tableView);
  tableView->SetSplitMultiComponentColumns(true);
  tableView->AddRepresentationFromInput(table);
  tableView->Update();
  layout->addWidget(qvtkWidget, 2);
  //layout->addWidget(qtChart, 2);
  layout->addWidget(tableView->GetWidget());
119
  mainWindow.setCentralWidget(widget);
120 121

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

  return app.exec();
}