radixvtkchartwidget.cc 3.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * @file:   radixvtkchartwidget.cc
 * @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
 */

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingContextOpenGL2)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)

12
13
#include "vtkVersionMacros.h"
#if VTK_MAJOR_VERSION < 9
14
#include "QVTKOpenGLWidget.h"
15
16
17
18
19
typedef QVTKOpenGLWidget QVTKWidget_t;
#else
#include "QVTKOpenGLNativeWidget.h"
typedef QVTKOpenGLNativeWidget QVTKWidget_t;
#endif
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
48
49
50
51
52
53
#include "vtkChartXY.h"
#include "vtkContextScene.h"
#include "vtkContextView.h"
#include "vtkFloatArray.h"
#include "vtkGenericOpenGLRenderWindow.h"
#include "vtkMath.h"
#include "vtkNew.h"
#include "vtkPlot.h"
#include "vtkPlotHistogram2D.h"
#include "vtkQtTableView.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"
#include "vtkTimerLog.h"

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

#include <QApplication>
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
#include <limits>
#include "radixvtkchartwidget.hh"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
  setGeometry(400, 250, 542, 390);
  // needed to ensure appropriate OpenGL context is created for VTK rendering.
54
  QSurfaceFormat::setDefaultFormat(QVTKWidget_t::defaultFormat());
55

56
  QVTKWidget_t *qvtkWidget = new QVTKWidget_t(this);
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

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

  // Set up my 2D world...
  vtkNew<vtkContextView> view;  // This contains a chart object
  view->SetRenderWindow(renderWindow);
  view->SetInteractor(renderWindow->GetInteractor());

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

  // 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, std::cos(double(i * inc)));
    table->SetValue(i, 2, std::sin(double(i * inc)));
    table->SetValue(i, 3,
                    std::fabs(std::sin(double(i * inc))) +
                        std::fabs(std::cos(double(i * inc))));
  }

  //   table->Update();

  // Add multiple line plots, setting the colors etc
  vtkNew<vtkChartXY> chart;
  chart->SetShowLegend(true);
  view->GetScene()->AddItem(chart);
  vtkPlot *line = chart->AddPlot(vtkChart::LINE);
  line->SetInputData(table, 0, 1);  // x-axis and cos
  line->SetColor(255, 0, 0, 255);
  line->SetWidth(0.5);
  line = chart->AddPlot(vtkChart::LINE);
  line->SetInputData(table, 0, 2);  // x-axis and sin
  line->SetColor(0, 255, 0, 255);
  line->SetWidth(5.0);
  line = chart->AddPlot(vtkChart::LINE);
  line->SetInputData(table, 0, 3);  // x-axis and sum
  line->SetColor(255, 0, 255, 255);
  line->SetWidth(2.0);

  // Now lets try to add a table view
  QWidget *widget     = new QWidget(this);
  QGridLayout *layout = new QGridLayout(widget);
  layout->addWidget(qvtkWidget, 0, 0);
  setCentralWidget(widget);
}

MainWindow::~MainWindow() {}

/******************************************************************************/
/******************************** MAIN PROGRAM ********************************/
/******************************************************************************/

int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  MainWindow mainWindow;
  mainWindow.show();
  mainWindow.raise();

  return app.exec();
}