//*
//* Permission is hereby granted, free of charge, to any person obtaining a copy
//* of this software and associated documentation files (the "Software"), to deal
//* in the Software without restriction, including without limitation the rights
//* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//* copies of the Software, and to permit persons to whom the Software is
//* furnished to do so, subject to the following conditions:
//*Created by Wang Lei,Shanghai University
//* 自定义了两个类MainWindow和ViewerQT,界面设计和事件响应完全在类MainWindow中,
//*ViewerQT的对象与OSG中Viewer类的对象功能相同
//*/
#pragma once
#ifndef QTTEST_H
#define QTTEST_H
#ifndef USE_QT4
#define USE_QT4 1
#endif
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#if USE_QT4
#include <QtCore/QString>
#include <QtCore/QTimer>
#include <QtGui/QKeyEvent>
#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
#include <QtGui/QMainWindow>
#include <QtGui/QMdiSubWindow>
#include <QtGui/QMdiArea>
#include <QtGui/QPushButton>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QVBoxLayout>
#include<QtGui/QMessageBox>
using Qt::WindowFlags;
#else
class QWidget;
#include <qtimer.h>
#include <qgl.h>
#include <qapplication.h>
#define WindowFlags WFlags
#endif
#include <iostream>
class AdapterWidget : public QGLWidget
{
public:
AdapterWidget( QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0 );
virtual ~AdapterWidget() {}
osgViewer::GraphicsWindow* getGraphicsWindow() { return _gw.get(); }
const osgViewer::GraphicsWindow* getGraphicsWindow() const { return _gw.get(); }
protected:
void init();
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;
};
AdapterWidget::AdapterWidget( QWidget * parent, const char * name, const QGLWidget * shareWidget, WindowFlags f):
#if USE_QT4
QGLWidget(parent, shareWidget, f)
#else
QGLWidget(parent, name, shareWidget, f)
#endif
{
_gw = new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());
#if USE_QT4
setFocusPolicy(Qt::ClickFocus);
#else
setFocusPolicy(QWidget::ClickFocus);
#endif
}
void AdapterWidget::resizeGL( int width, int height )
{
_gw->getEventQueue()->windowResize(0, 0, width, height );
_gw->resized(0,0,width,height);
}
void AdapterWidget::keyPressEvent( QKeyEvent* event )
{
#if USE_QT4
_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
#else
_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
#endif
}
void AdapterWidget::keyReleaseEvent( QKeyEvent* event )
{
#if USE_QT4
_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
#else
_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol) event->ascii() );
#endif
}
void AdapterWidget::mousePressEvent( QMouseEvent* event )
{
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);
}
void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
{
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);
}
void AdapterWidget::mouseMoveEvent( QMouseEvent* event )
{
_gw->getEventQueue()->mouseMotion(event->x(), event->y());
}
class ViewerQT : public osgViewer::Viewer, public AdapterWidget
{
public:
ViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
AdapterWidget( parent, name, shareWidget, f )
{
getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));
getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
getCamera()->setGraphicsContext(getGraphicsWindow());
setThreadingModel(osgViewer::Viewer::SingleThreaded);
connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
_timer.start(10);
}
virtual void paintGL()
{
frame();
}
//add interface element
// void createAction();
// void createMenu();
protected:
QTimer _timer;
// QMenu *fileMenu;
// QAction *openAction;
};
//void ViewerQT::createAction()
//{
// openAction=new QAction("&Open",this);
//}
//
//void ViewerQT::createMenu()
//{
// fileMenu=menuBar()->addMenu("&File");
// fileMenu->addAction(openAction);
//}
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent=0);
// {
// viewerWindow=new ViewerQT;
// setCentralWidget(viewerWindow);
// }
void inline createAction();
void inline createMenu();
protected:
ViewerQT* viewerWindow;
QMenu *fileMenu,*fileOptions;
QAction *openAction,*editAction,*saveAction,*saveAsAction,*exitAction,*rainAction,*fogAction,*snowAction;
private slots://处定义事件槽
void open();
void save();
void saveAs();
void exit();
void rain();
void fog();
void snow();
};
MainWindow::MainWindow(QWidget *parent )//界面设置
{
this->resize(400,300);
createAction();
createMenu();
}
void MainWindow::createAction(void)
{
openAction=new QAction("&Open",this);
//QObject::connect(openAction,SIGNAL(triggered()),this,SLOT(message()),Qt::AutoConnection);//事件响应
//editAction=new QAction("&Edit",this);
saveAction=new QAction("&Save",this);
saveAsAction=new QAction("&SaveAs",this);
exitAction=new QAction("&Exit",this);
rainAction=new QAction("&Rain",this);
fogAction=new QAction("&Fog",this);
snowAction=new QAction("&Snow",this);
}
void MainWindow::createMenu(void)
{
fileMenu=menuBar()->addMenu("&File");
fileMenu->addAction(openAction);
QObject::connect(openAction,SIGNAL(triggered()),this,SLOT(open()),Qt::AutoConnection);
fileMenu->addAction(saveAction);
QObject::connect(saveAction,SIGNAL(triggered()),this,SLOT(save()),Qt::AutoConnection);
fileMenu->addAction(saveAsAction);
QObject::connect(saveAsAction,SIGNAL(triggered()),this,SLOT(saveAs()),Qt::AutoConnection);
fileMenu->addAction(exitAction);
QObject::connect(exitAction,SIGNAL(triggered()),this,SLOT(exit()),Qt::AutoConnection);
fileOptions=menuBar()->addMenu("&Option");
fileOptions->addAction(rainAction);
QObject::connect(rainAction,SIGNAL(triggered()),this,SLOT(rain()),Qt::AutoConnection);
fileOptions->addAction(fogAction);
QObject::connect(fogAction,SIGNAL(triggered()),this,SLOT(fog()),Qt::AutoConnection);
fileOptions->addAction(snowAction);
QObject::connect(snowAction,SIGNAL(triggered()),this,SLOT(snow()),Qt::AutoConnection);
}
//void MainWindow::message()
//{
//QMessageBox msgBox;
//msgBox.setText("OK!");
//msgBox.exec();
//}
void MainWindow::open()
{
//QString fileName = QFileDialog::getOpenFileName(this);
// if (!fileName.isEmpty())
//{
//loadFile(fileName);
//}
}
void MainWindow::save()
{
//QString fileName = QFileDialog::getSaveFileName(this);
//if (fileName.isEmpty())
//{
//return false;
// }
}
void MainWindow::saveAs()
{
QMessageBox msgBox;
msgBox.setText("OK1!");
msgBox.exec();
}
void MainWindow::exit()
{
QMessageBox msgBox;
msgBox.setText("OK2!");
msgBox.exec();
}
void MainWindow::rain()
{
}
void MainWindow::fog()
{
}
void MainWindow::snow()
{
}
//class CompositeViewerQT : public osgViewer::CompositeViewer, public AdapterWidget
//{
// public:
//
// CompositeViewerQT(QWidget * parent = 0, const char * name = 0, const QGLWidget * shareWidget = 0, WindowFlags f = 0):
// AdapterWidget( parent, name, shareWidget, f )
// {
// setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
//
// connect(&_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
// _timer.start(10);
// }
//
// virtual void paintGL()
// {
// frame();
// }
//
// protected:
//
// QTimer _timer;
//};
//int mainAdapterWidget(QApplication& a, osg::ArgumentParser& arguments)
//{
// // load the scene.
// osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
// if (!loadedModel)
// {
// std::cout << arguments[0] <<": No data loaded." << std::endl;
// return 1;
// }
//
// std::cout<<"Using AdapterWidget - QGLWidget subclassed to integrate with osgViewer using its embedded graphics window support."<<std::endl;
//
// if (arguments.read("--CompositeViewer"))
// {
// CompositeViewerQT* viewerWindow = new CompositeViewerQT;
//
// unsigned int width = viewerWindow->width();
// unsigned int height = viewerWindow->height();
//
// {
// osgViewer::View* view1 = new osgViewer::View;
// view1->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
// view1->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
// view1->getCamera()->setViewport(new osg::Viewport(0,0,width,height/2));
// view1->setCameraManipulator(new osgGA::TrackballManipulator);
// view1->setSceneData(loadedModel.get());
//
// viewerWindow->addView(view1);
// }
//
// {
// osgViewer::View* view2 = new osgViewer::View;
// view2->getCamera()->setGraphicsContext(viewerWindow->getGraphicsWindow());
// view2->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width)/static_cast<double>(height/2), 1.0, 1000.0);
// view2->getCamera()->setViewport(new osg::Viewport(0,height/2,width,height/2));
// view2->setCameraManipulator(new osgGA::TrackballManipulator);
// view2->setSceneData(loadedModel.get());
//
// viewerWindow->addView(view2);
// }
//
// viewerWindow->show();
//#if USE_QT4
// }
// else if (arguments.read("--mdi")) {
// std::cout<<"Using ViewerQT MDI version"<<std::endl;
// /*
// Following problems are found here:
// - miminize causes loaded model to disappear (some problem with Camera matrix? - clampProjectionMatrix is invalid)
// */
// ViewerQT* viewerWindow = new ViewerQT;
// viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
// viewerWindow->setSceneData(loadedModel.get());
//
// QMainWindow* mw = new QMainWindow();
// QMdiArea* mdiArea = new QMdiArea(mw);
// mw->setCentralWidget(mdiArea);
//
// QMdiSubWindow *subWindow = mdiArea->addSubWindow(viewerWindow);
// subWindow->showMaximized();
// subWindow->setWindowTitle("New Window");
// /*
//
// QHBoxLayout *mainLayout = new QHBoxLayout;
// mainLayout->addWidget(mw);
// mainLayout->
// QPushButton *btn1 = new QPushButton("Normal Button");
//// mainLayout->addWidget(subWindow);
// mainLayout->addWidget(btn1);
// */
// mw->show();
//#endif // USE_QT4
// } else {
// ViewerQT* viewerWindow = new ViewerQT;
//
// viewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
// viewerWindow->setSceneData(loadedModel.get());
///*
// QHBoxLayout *mainLayout = new QHBoxLayout;
// mainLayout->addWidget(viewerWindow);
//
// QPushButton *btn1 = new QPushButton("Normal Button");
// mainLayout->addWidget(btn1);
//// setLayout(mainLayout); //inherted from QWidget
//*/
// viewerWindow->show();
// }
//
//
// a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) );
//
// return a.exec();
//}
/*EOF*/
#endif
有些是注释过的~`以前也是这个程序,运行没问题,现在总会出现这样的bug~· 用得是WIN7系统,是不是属性设置 链接库的问题,还是其他的?~