Skip to content

Commit

Permalink
Overhaul the GraphicsView example.
Browse files Browse the repository at this point in the history
GraphicsView example now shows several new features and better performance
than the last one.
  • Loading branch information
Clinton Stimpson committed Jun 24, 2010
1 parent 7d994f4 commit 07a2db7
Show file tree
Hide file tree
Showing 14 changed files with 606 additions and 162 deletions.
6 changes: 5 additions & 1 deletion Examples/GUI/Qt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@


SUBDIRS(ImageViewer SimpleView Events GraphicsView)
SUBDIRS(ImageViewer SimpleView Events)

IF(VTK_USE_QVTK_QTOPENGL)
SUBDIRS(GraphicsView)
ENDIF(VTK_USE_QVTK_QTOPENGL)

CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.ctest.in"
"${CMAKE_CURRENT_BINARY_DIR}/CTestCustom.ctest" @ONLY IMMEDIATE)
72 changes: 36 additions & 36 deletions Examples/GUI/Qt/GraphicsView/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
PROJECT(QtImageViewer)

IF(NOT VTK_BINARY_DIR)
FIND_PACKAGE(VTK)
IF(NOT VTK_DIR)
MESSAGE(FATAL_ERROR "Please set VTK_DIR.")
ENDIF(NOT VTK_DIR)
INCLUDE(${VTK_USE_FILE})
ENDIF(NOT VTK_BINARY_DIR)

FIND_PACKAGE(Qt4)

IF(QT_USE_FILE)
INCLUDE(${QT_USE_FILE})
ELSE(QT_USE_FILE)
SET(QT_LIBRARIES ${QT_QT_LIBRARY})
ENDIF(QT_USE_FILE)

SET (SRCS
main.cxx
)

# Use the include path and library for Qt that is used by VTK.
INCLUDE_DIRECTORIES( ${QT_INCLUDE_DIR} ${QT_QTGUI_INCLUDE_DIR}
${QT_QTCORE_INCLUDE_DIR})

ADD_EXECUTABLE( qtgraphicsview MACOSX_BUNDLE ${SRCS})

TARGET_LINK_LIBRARIES( qtgraphicsview
QVTK
${QT_LIBRARIES}
vtkRendering
vtkGraphics
vtkIO
vtkCommon
)

find_package(Qt4 REQUIRED)
set(QT_USE_QTOPENGL 1)
set(QT_USE_QTWEBKIT 1)
include(${QT_USE_FILE})

find_package(OpenGL)

if(NOT VTK_BINARY_DIR)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
endif(NOT VTK_BINARY_DIR)

if(NOT VTK_USE_QVTK_QTOPENGL)
message(WARNING "VTK isn't configured to use QtOpenGL. GraphicsView example is disabled.")
else(NOT VTK_USE_QVTK_QTOPENGL)

qt4_wrap_cpp(mocs
OpenGLScene.hpp
QBoolAnimation.h
WebView.h
)

add_executable(qtgraphicsview
main.cpp
OpenGLScene.cpp
TreeRingViewItem.cpp
GraphLayoutViewItem.cpp
WebView.cpp
${mocs}
)

target_link_libraries(qtgraphicsview QVTK vtkRendering)
target_link_libraries(qtgraphicsview ${QT_LIBRARIES})

endif(NOT VTK_USE_QVTK_QTOPENGL)
83 changes: 83 additions & 0 deletions Examples/GUI/Qt/GraphicsView/GraphLayoutViewItem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

#include "GraphLayoutViewItem.h"
#include "vtkGraphLayoutView.h"
#include "vtkGenericOpenGLRenderWindow.h"
#include "vtkgl.h"
#include "QVTKInteractor.h"
#include "vtkXMLTreeReader.h"
#include "vtkRenderedTreeAreaRepresentation.h"
#include "vtkStringArray.h"
#include "vtkIdTypeArray.h"
#include "vtkDataSetAttributes.h"
#include "vtkRenderedGraphRepresentation.h"
#include "vtkStringToNumeric.h"
#include "vtkViewTheme.h"
#include "vtkTextProperty.h"

GraphLayoutViewItem::GraphLayoutViewItem(QGLContext* ctx, QGraphicsItem* p)
: QVTKGraphicsItem(ctx, p)
{
GraphLayoutView.TakeReference(vtkGraphLayoutView::New());
GraphLayoutView->SetInteractor(this->GetInteractor());
GraphLayoutView->SetRenderWindow(this->GetRenderWindow());

QString f1 = "/home/cjstimp/vtk/VTKData/Data/treetest.xml";


vtkSmartPointer<vtkXMLTreeReader> reader = vtkSmartPointer<vtkXMLTreeReader>::New();
reader->SetFileName(f1.toAscii().data());
reader->SetMaskArrays(true);
reader->Update();
vtkTree* t = reader->GetOutput();
vtkSmartPointer<vtkStringArray> label = vtkSmartPointer<vtkStringArray>::New();
label->SetName("edge label");
vtkSmartPointer<vtkIdTypeArray> dist = vtkSmartPointer<vtkIdTypeArray>::New();
dist->SetName("distance");
for (vtkIdType i = 0; i < t->GetNumberOfEdges(); i++)
{
dist->InsertNextValue(i);
switch (i % 3)
{
case 0:
label->InsertNextValue("a");
break;
case 1:
label->InsertNextValue("b");
break;
case 2:
label->InsertNextValue("c");
break;
}
}
t->GetEdgeData()->AddArray(dist);
t->GetEdgeData()->AddArray(label);

vtkSmartPointer<vtkStringToNumeric> numeric = vtkSmartPointer<vtkStringToNumeric>::New();
numeric->SetInput(t);

GraphLayoutView->DisplayHoverTextOn();
GraphLayoutView->SetLayoutStrategyToCircular();
GraphLayoutView->SetVertexLabelArrayName("name");
GraphLayoutView->VertexLabelVisibilityOn();
GraphLayoutView->SetVertexColorArrayName("size");
GraphLayoutView->ColorVerticesOn();
GraphLayoutView->SetRepresentationFromInputConnection(numeric->GetOutputPort());
GraphLayoutView->SetEdgeColorArrayName("distance");
GraphLayoutView->ColorEdgesOn();
GraphLayoutView->SetEdgeLabelArrayName("edge label");
GraphLayoutView->EdgeLabelVisibilityOn();
vtkRenderedGraphRepresentation* rep =
vtkRenderedGraphRepresentation::SafeDownCast(GraphLayoutView->GetRepresentation());
rep->SetVertexHoverArrayName("name");
rep->SetEdgeHoverArrayName("edge label");

GraphLayoutView->SetHideVertexLabelsOnInteraction(1);
GraphLayoutView->SetHideEdgeLabelsOnInteraction(1);

GraphLayoutView->ResetCamera();

}

GraphLayoutViewItem::~GraphLayoutViewItem()
{
}
18 changes: 18 additions & 0 deletions Examples/GUI/Qt/GraphicsView/GraphLayoutViewItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#ifndef GraphLayoutViewItem_h
#define GraphLayoutViewItem_h

#include "QVTKGraphicsItem.h"
class vtkGraphLayoutView;

class GraphLayoutViewItem : public QVTKGraphicsItem
{
public:
GraphLayoutViewItem(QGLContext* ctx, QGraphicsItem* p=0);
~GraphLayoutViewItem();

protected:
vtkSmartPointer<vtkGraphLayoutView> GraphLayoutView;
};

#endif
61 changes: 61 additions & 0 deletions Examples/GUI/Qt/GraphicsView/GraphicsView.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

#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
}
~GraphicsView()
{
}

protected:

void drawBackground(QPainter* p, const QRectF& vtkNotUsed(r))
{
p->beginNativePainting();
mWidget->GetRenderWindow()->PushState();
mWidget->GetRenderWindow()->Render();
mWidget->GetRenderWindow()->PopState();
p->endNativePainting();
}

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
Loading

0 comments on commit 07a2db7

Please sign in to comment.