From Wang Rui, QWidgetImage class that enables QWidgets to be used as an interactive osg::Image that can be assigned to textures.

This commit is contained in:
Robert Osfield
2010-03-10 13:24:53 +00:00
parent a5c33886da
commit 6da42d9cd6
4 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef QWIDGETIMAGE
#define QWIDGETIMAGE
#include <osg/Image>
#include <osgQt/QGraphicsViewAdapter>
namespace osgQt
{
class OSGQT_EXPORT QWidgetImage : public osg::Image
{
public:
QWidgetImage( QWidget* widget=0 );
QWidget* getQWidget() { return _widget; }
QGraphicsViewAdapter* getQGraphicsViewAdapter() { return _adapter; }
void focusWidget(bool focus);
void clearWriteBuffer();
void render();
virtual void setFrameLastRendered(const osg::FrameStamp* frameStamp);
virtual bool sendPointerEvent(int x, int y, int buttonMask);
virtual bool sendKeyEvent(int key, bool keyDown);
protected:
QPointer<QGraphicsViewAdapter> _adapter;
QPointer<QWidget> _widget;
};
}
#endif

View File

@@ -20,6 +20,7 @@ SET(LIB_PUBLIC_HEADERS
${HEADER_PATH}/QFontImplementation
${HEADER_PATH}/QGraphicsViewAdapter
${HEADER_PATH}/QWebViewImage
${HEADER_PATH}/QWidgetImage
)
# FIXME: For OS X, need flag for Framework or dylib
@@ -29,6 +30,7 @@ ADD_LIBRARY(${LIB_NAME}
QFontImplementation.cpp
QGraphicsViewAdapter.cpp
QWebViewImage.cpp
QWidgetImage.cpp
${SOURCES_H_MOC}
${OPENSCENEGRAPH_VERSIONINFO_RC}
)

View File

@@ -12,6 +12,7 @@
*/
#include <osgQt/QGraphicsViewAdapter>
#include <osgQt/QWidgetImage>
#include <osgQt/QWebViewImage>
#include <QtOpenGL/QGLWidget>
@@ -309,6 +310,9 @@ bool QGraphicsViewAdapter::handlePointerEvent(int x, int y, int buttonMask)
if (eventType==QEvent::MouseButtonPress)
{
QWidgetImage* qwidgetImage = dynamic_cast<QWidgetImage*>(_image.get());
if (qwidgetImage) qwidgetImage->focusWidget(true);
QWebViewImage* qwebViewImage = dynamic_cast<QWebViewImage*>(_image.get());
if (qwebViewImage) qwebViewImage->focusBrowser(true);
}

View File

@@ -0,0 +1,65 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgQt/QWidgetImage>
#include <QtGui/QLayout>
namespace osgQt
{
QWidgetImage::QWidgetImage( QWidget* widget )
{
// make sure we have a valid QApplication before we start creating widgets.
getOrCreateQApplication();
QVBoxLayout* layout = new QVBoxLayout;
if (widget) layout->addWidget(widget);
_widget = new QWidget;
_widget->setLayout(layout);
_widget->setGeometry(0, 0, 800, 600); // FIXME: a different size leads to unexpected result
_adapter = new QGraphicsViewAdapter(this, _widget.data());
}
void QWidgetImage::focusWidget(bool focus)
{
QFocusEvent event(focus ? QEvent::FocusIn : QEvent::FocusOut, Qt::OtherFocusReason);
QCoreApplication::sendEvent(_widget, &event);
}
void QWidgetImage::clearWriteBuffer()
{
_adapter->clearWriteBuffer();
}
void QWidgetImage::render()
{
_adapter->render();
}
void QWidgetImage::setFrameLastRendered(const osg::FrameStamp* frameStamp)
{
_adapter->setFrameLastRendered(frameStamp);
}
bool QWidgetImage::sendPointerEvent(int x, int y, int buttonMask)
{
return _adapter->sendPointerEvent(x,y,buttonMask);
}
bool QWidgetImage::sendKeyEvent(int key, bool keyDown)
{
return _adapter->sendKeyEvent(key, keyDown);
}
}