OpenSceneGraph/examples/osgviewerQT/osgviewerQT.cpp

167 lines
4.4 KiB
C++
Raw Normal View History

2006-10-01 00:26:53 +08:00
// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
#include <osgViewer/Viewer>
#include <osgViewer/StatsHandler>
2006-10-01 00:26:53 +08:00
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#if USE_QT4
#include <QtCore/QTimer>
#include <QtGui/QKeyEvent>
#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
using Qt::WFlags;
#else
class QWidget;
#include <qtimer.h>
#include <qgl.h>
#include <qapplication.h>
#endif
2006-10-01 00:26:53 +08:00
#include <iostream>
class AdapterWidget : public QGLWidget
2006-10-01 00:26:53 +08:00
{
2007-06-04 22:47:16 +08:00
public:
2006-10-01 00:26:53 +08:00
2007-06-04 22:47:16 +08:00
AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0 );
2007-06-04 22:47:16 +08:00
virtual ~AdapterWidget() {}
2006-10-01 00:26:53 +08:00
2007-06-04 22:47:16 +08:00
osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
2006-10-01 00:26:53 +08:00
2007-06-04 22:47:16 +08:00
protected:
2007-06-04 22:47:16 +08:00
void init();
2006-10-01 00:26:53 +08:00
2007-06-04 22:47:16 +08:00
virtual void resizeGL( int width, int height );
virtual void keyPressEvent( QKeyEvent* event );
virtual void keyReleaseEvent( QKeyEvent* event );
virtual void mousePressEvent( QMouseEvent* event );
virtual void mouseReleaseEvent( QMouseEvent* event );
virtual void mouseMoveEvent( QMouseEvent* event );
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
2006-10-01 00:26:53 +08:00
};
AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WFlags f):
2006-10-01 00:26:53 +08:00
QGLWidget(parent, name, shareWidget, f)
{
_gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
2006-10-01 00:26:53 +08:00
}
void AdapterWidget::resizeGL( int width, int height )
2006-10-01 00:26:53 +08:00
{
_gw->getEventQueue()->windowResize(0, 0, width, height );
_gw->resized(0,0,width,height);
2006-10-01 00:26:53 +08:00
}
void AdapterWidget::keyPressEvent( QKeyEvent* event )
2006-10-01 00:26:53 +08:00
{
_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
2006-10-01 00:26:53 +08:00
}
void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
2006-10-01 00:26:53 +08:00
{
_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
2006-10-01 00:26:53 +08:00
}
void AdapterWidget::mousePressEvent( QMouseEvent* event )
2006-10-01 00:26:53 +08:00
{
int button = 0;
switch(event->button())
{
case(Qt::LeftButton): button = 1; break;
case(Qt::MidButton): button = 2; break;
case(Qt::RightButton): button = 3; break;
case(Qt::NoButton): button = 0; break;
default: button = 0; break;
}
_gw->getEventQueue()->mouseButtonPress(event->x(), event->y(), button);
2006-10-01 00:26:53 +08:00
}
void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
2006-10-01 00:26:53 +08:00
{
int button = 0;
switch(event->button())
{
case(Qt::LeftButton): button = 1; break;
case(Qt::MidButton): button = 2; break;
case(Qt::RightButton): button = 3; break;
case(Qt::NoButton): button = 0; break;
default: button = 0; break;
}
_gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
2006-10-01 00:26:53 +08:00
}
void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
2006-10-01 00:26:53 +08:00
{
_gw->getEventQueue()->mouseMotion(event->x(), event->y());
2006-10-01 00:26:53 +08:00
}
2006-10-15 19:39:35 +08:00
2007-06-04 22:47:16 +08:00
class ViewerQT : public osgViewer::Viewer, public AdapterWidget
2007-06-04 23:11:46 +08:00
{
2006-10-15 19:39:35 +08:00
public:
ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WFlags f = 0):
AdapterWidget( parent, name, shareWidget, f )
{
getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
getCamera()->setGraphicsContext(getGraphicsWindow());
setThreadingModel(osgViewer::Viewer::SingleThreaded);
2006-10-15 19:39:35 +08:00
2007-06-04 22:47:16 +08:00
connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
_timer.start(10);
2006-10-15 19:39:35 +08:00
}
virtual void paintGL()
{
frame();
}
2007-06-04 22:47:16 +08:00
protected:
2006-10-15 19:39:35 +08:00
2007-06-04 22:47:16 +08:00
QTimer _timer;
2006-10-15 19:39:35 +08:00
};
2006-10-01 00:26:53 +08:00
int main( int argc, char **argv )
{
QApplication a( argc, argv );
2006-10-01 04:05:36 +08:00
if (argc<2)
2006-10-01 00:26:53 +08:00
{
std::cout << argv[0] <<": requires filename argument." << std::endl;
return 1;
}
// load the scene.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
if (!loadedModel)
{
std::cout << argv[0] <<": No data loaded." << std::endl;
return 1;
}
ViewerQT* viewerWindow = new ViewerQT;
2006-10-01 00:26:53 +08:00
viewerWindow->setSceneData(loadedModel.get());
viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
viewerWindow->addEventHandler(new osgViewer::StatsHandler);
2006-10-01 00:26:53 +08:00
viewerWindow->show();
a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
return a.exec();
}
/*EOF*/