1 2 3 4 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#ifndef GraphicsView_hpp
#define GraphicsView_hpp
#include <QGraphicsView>
#include <QResizeEvent>
#include "QVTKWidget2.h"
#include "OpenGLScene.hpp"
#include "vtkGenericOpenGLRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkTextActor3D.h"
class GraphicsView : public QGraphicsView
{
public:
GraphicsView()
{
mCtx = new QGLContext(QGLFormat());
mWidget = new QVTKWidget2(mCtx);
this->setViewport(mWidget);
this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
this->setScene(new OpenGLScene(mCtx, this));
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
ren->SetBackground(0,0,0);
ren->SetBackground2(1,1,1);
ren->SetGradientBackground(1);
vtkSmartPointer<vtkTextActor3D> textActor = vtkSmartPointer<vtkTextActor3D>::New();
textActor->SetInput("Qt & VTK!!");
ren->AddViewProp(textActor);
ren->ResetCamera();
mWidget->GetRenderWindow()->AddRenderer(ren);
mWidget->GetRenderWindow()->SetSwapBuffers(0); // don't let VTK swap buffers on us
mWidget->setAutoBufferSwap(true);
}
~GraphicsView()
{
}
protected:
void drawBackground(QPainter* p, const QRectF& vtkNotUsed(r))
{
#if QT_VERSION >= 0x040600
p->beginNativePainting();
#endif
mWidget->GetRenderWindow()->PushState();
mWidget->GetRenderWindow()->OpenGLInitState();
mWidget->GetRenderWindow()->Render();
mWidget->GetRenderWindow()->PopState();
#if QT_VERSION >= 0x040600
p->endNativePainting();
#endif
}
void resizeEvent(QResizeEvent *event)
{
// give the same size to the scene that his widget has
if (scene())
scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
QGraphicsView::resizeEvent(event);
mWidget->GetRenderWindow()->SetSize(event->size().width(), event->size().height());
}
QGLContext* mCtx;
QVTKWidget2* mWidget;
};
#endif
|