From Jean-Sebastien Guay, "I've been working in the last few days

to get QWidgetImage to a point where it can fill a need we have: to be
able to use Qt to make HUDs and to display widgets over / inside an OSG
scene.

---------------
Current results
---------------
I've attached what I have at this point. The modified QWidgetImage +
QGraphicsViewAdapter classes can be rendered fullscreen (i.e. the Qt
QGraphicsView's size follows the size of the OSG window) or on a quad in
the scene as before. It will let events go through to OSG if no widget
is under the mouse when they happen (useful when used as a HUD with
transparent parts - a click-focus scheme could be added later too). It
also supercedes Martin Scheffler's submission because it adds a
getter/setter for the QGraphicsViewAdapter's background color (and the
user can set their widget to be transparent using
widget->setAttribute(Qt::WA_TranslucentBackground) themselves).

The included osgQtBrowser example has been modified to serve as a test
bed for these changes. It has lots more command line arguments than
before, some of which can be removed eventually (once things are
tested). Note that it may be interesting to change its name or split it
into two examples. Though if things go well, the specific QWebViewImage
class can be removed completely and we can consolidate to using
QWidgetImage everywhere, and then a single example to demonstrate it
would make more sense, albeit not named osgQtBrowser... You can try this
path by using the --useWidgetImage --useBrowser command line arguments -
this results in an equivalent setup to QWebViewImage, but using
QWidgetImage, and doesn't work completely yet for some unknown reason,
see below.

----------------
Remaining issues
----------------
There are a few issues left to fix, and for these I request the
community's assistance. They are not blockers for me, and with my
limited Qt experience I don't feel like I'm getting any closer to fixing
them, so if someone else could pitch in and see what they can find, it
would be appreciated. It would be really nice to get them fixed, that
way we'd really have a first-class integration of Qt widgets in an OSG
scene. The issues are noted in the osgQtBrowser.cpp source file, but
here they are too:

-------------------------------------------------------------------
  QWidgetImage still has some issues, some examples are:

  1. Editing in the QTextEdit doesn't work. Also when started with
     --useBrowser, editing in the search field on YouTube doesn't
     work. But that same search field when using QWebViewImage
     works... And editing in the text field in the pop-up getInteger
     dialog works too. All these cases use QGraphicsViewAdapter
     under the hood, so why do some work and others don't?

     a) osgQtBrowser --useWidgetImage [--fullscreen] (optional)
     b) Try to click in the QTextEdit and type, or to select text
        and drag-and-drop it somewhere else in the QTextEdit. These
        don't work.
     c) osgQtBrowser --useWidgetImage --sanityCheck
     d) Try the operations in b), they all work.
     e) osgQtBrowser --useWidgetImage --useBrowser [--fullscreen]
     f) Try to click in the search field and type, it doesn't work.
     g) osgQtBrowser
     h) Try the operation in f), it works.

  2. Operations on floating windows (--numFloatingWindows 1 or more).
     Moving by dragging the titlebar, clicking the close button,
     resizing them, none of these work. I wonder if it's because the
     OS manages those functions (they're functions of the window
     decorations) so we need to do something special for that? But
     in --sanityCheck mode they work.

     a) osgQtBrowser --useWidgetImage --numFloatingWindows 1
                     [--fullscreen]
     b) Try to drag the floating window, click the close button, or
        drag its sides to resize it. None of these work.
     c) osgQtBrowser --useWidgetImage --numFloatingWindows 1
                     --sanityCheck
     d) Try the operations in b), all they work.
     e) osgQtBrowser --useWidgetImage [--fullscreen]
     f) Click the button so that the getInteger() dialog is
        displayed, then try to move that dialog or close it with the
        close button, these don't work.
     g) osgQtBrowser --useWidgetImage --sanityCheck
     h) Try the operation in f), it works.

  3. (Minor) The QGraphicsView's scrollbars don't appear when
     using QWidgetImage or QWebViewImage. QGraphicsView is a
     QAbstractScrollArea and it should display scrollbars as soon as
     the scene is too large to fit the view.

     a) osgQtBrowser --useWidgetImage --fullscreen
     b) Resize the OSG window so it's smaller than the QTextEdit.
        Scrollbars should appear but don't.
     c) osgQtBrowser --useWidgetImage --sanityCheck
     d) Try the operation in b), scrollbars appear. Even if you have
        floating windows (by clicking the button or by adding
        --numFloatingWindows 1) and move them outside the view,
        scrollbars appear too. You can't test that case in OSG for
        now because of problem 2 above, but that's pretty cool.

  4. (Minor) In sanity check mode, the widget added to the
     QGraphicsView is centered. With QGraphicsViewAdapter, it is not.

     a) osgQtBrowser --useWidgetImage [--fullscreen]
     b) The QTextEdit and button are not in the center of the image
        generated by the QGraphicsViewAdapter.
     c) osgQtBrowser --useWidgetImage --sanityCheck
     d) The QTextEdit and button are in the center of the
        QGraphicsView.
-------------------------------------------------------------------

As you can see I've put specific repro steps there too, so it's clear
what I mean by a given problem. The --sanityCheck mode is useful to see
what should happen in a "normal" Qt app that demonstrates the same
situation, so hopefully we can get to a point where it behaves the same
with --sanityCheck and without."
This commit is contained in:
Robert Osfield 2010-06-15 13:57:44 +00:00
parent 0c84f2deff
commit 6cbce93aa4
9 changed files with 742 additions and 51 deletions

View File

@ -194,10 +194,12 @@ IF(DYNAMIC_OPENSCENEGRAPH)
IF (BUILD_QT_EXAMPLES AND QT4_FOUND)
ADD_SUBDIRECTORY(osgviewerQtWidget)
ADD_SUBDIRECTORY(osgQtWidgets)
IF (QT_QTWEBKIT_FOUND)
ADD_SUBDIRECTORY(osgQtBrowser)
ENDIF()
ENDIF()
ENDIF()

View File

@ -0,0 +1,16 @@
SET(TARGET_SRC
osgQtWidgets.cpp
)
IF(CMAKE_COMPILER_IS_GNUCXX)
# Remove -pedantic flag as it barfs on Qt headers
STRING(REGEX REPLACE "-pedantic" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ENDIF()
SET(TARGET_EXTERNAL_LIBRARIES ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTOPENGL_LIBRARY} ${QT_QTWEBKIT_LIBRARY})
INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR} ${QT_QTCORE_INCLUDE_DIR})
SET(TARGET_ADDED_LIBRARIES osgWidget osgQt)
#### end var setup ###
SETUP_EXAMPLE(osgQtWidgets)

File diff suppressed because one or more lines are too long

View File

@ -54,6 +54,14 @@ class OSGQT_EXPORT QGraphicsViewAdapter : public QObject
void assignImage(unsigned int i);
void resize(int width, int height);
void setBackgroundColor(QColor color) { _backgroundColor = color; }
QColor getBackgroundColor() const { return _backgroundColor; }
QGraphicsView* getQGraphicsView() { return _graphicsView; }
QGraphicsScene* getQGraphicsScene() { return _graphicsScene; }
protected:
bool handlePointerEvent(int x, int y, int buttonMask);
@ -64,6 +72,12 @@ class OSGQT_EXPORT QGraphicsViewAdapter : public QObject
int _previousButtonMask;
int _previousMouseX;
int _previousMouseY;
int _previousQtMouseX;
int _previousQtMouseY;
bool _previousSentEvent;
int _width;
int _height;
typedef std::map<int, Qt::Key> KeyMap;
KeyMap _keyMap;
@ -86,6 +100,7 @@ class OSGQT_EXPORT QGraphicsViewAdapter : public QObject
private slots:
void repaintRequestedSlot(const QList<QRectF> &regions);
void repaintRequestedSlot(const QRectF &region);
};

View File

@ -34,6 +34,10 @@ class OSGQT_EXPORT QWidgetImage : public osg::Image
void render();
/// Overridden scaleImage used to catch cases where the image is
/// fullscreen and the window is resized.
virtual void scaleImage(int s,int t,int r, GLenum newDataType);
virtual bool sendFocusHint(bool focus);
virtual bool sendPointerEvent(int x, int y, int buttonMask);

View File

@ -407,8 +407,8 @@ class OSGVIEWER_EXPORT InteractiveImageHandler : public osgGA::GUIEventHandler,
{
public:
InteractiveImageHandler(osg::Image* image):
_image(image) {}
InteractiveImageHandler(osg::Image* image, osg::Camera* camera = 0):
_image(image), _fullscreen(camera != 0), _camera(camera) {}
META_Object(osgViewer, InteractiveImageHandler);
@ -426,12 +426,12 @@ protected:
osg::Object(), osgGA::GUIEventHandler(), osg::Drawable::CullCallback() {}
bool mousePosition(osgViewer::View* view, osg::NodeVisitor* nv, const osgGA::GUIEventAdapter& ea, int& x, int &y) const;
bool computeIntersections(osgViewer::View* view, float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask = 0xffffffff) const;
osg::observer_ptr<osg::Image> _image;
bool _handleKeyboardEvents;
bool _handledOnKeyboardEvents;
bool _handleMouseEvents;
bool _handledOnMouseEvents;
osg::observer_ptr<osg::Image> _image;
bool _fullscreen;
osg::observer_ptr<osg::Camera> _camera;
};

View File

@ -58,10 +58,16 @@ struct MyQPointerEvent : public QEvent
unsigned int _buttonMask;
};
const QImage::Format s_imageFormat = QImage::Format_ARGB32_Premultiplied;
QGraphicsViewAdapter::QGraphicsViewAdapter(osg::Image* image, QWidget* widget):
_image(image),
_previousQtMouseX(-1),
_previousQtMouseY(-1),
_previousSentEvent(false),
_qtKeyModifiers(Qt::NoModifier),
_backgroundColor(255,255,255)
_backgroundColor(255, 255, 255)
{
// make sure we have a valid QApplication before we start creating widgets.
getOrCreateQApplication();
@ -69,27 +75,23 @@ QGraphicsViewAdapter::QGraphicsViewAdapter(osg::Image* image, QWidget* widget):
setUpKeyMap();
_graphicsScene = new QGraphicsScene();
_graphicsView = new QGraphicsView;
_graphicsScene = new QGraphicsScene;
_graphicsScene->addWidget(widget);
_graphicsView = new QGraphicsView;
_graphicsView->setScene(_graphicsScene);
_graphicsView->viewport()->setParent(0);
#if (QT_VERSION_CHECK(4, 5, 0) <= QT_VERSION)
_graphicsScene->setStickyFocus(true);
#endif
_graphicsView->viewport()->setParent(0);
int width = _graphicsScene->width();
int height = _graphicsScene->height();
_width = _graphicsScene->width();
_height = _graphicsScene->height();
_qimages[0] = QImage(QSize(width, height), QImage::Format_ARGB32);
_qimages[0].fill(_backgroundColor.rgba());
_qimages[0] = QGLWidget::convertToGLFormat(_qimages[0]);
_qimages[1] = QImage(QSize(width, height), QImage::Format_ARGB32);
_qimages[1].fill(_backgroundColor.rgba());
_qimages[2] = QImage(QSize(width, height), QImage::Format_ARGB32);
_qimages[2].fill(_backgroundColor.rgba());
_qimages[0] = QImage(QSize(_width, _height), s_imageFormat);
_qimages[1] = QImage(QSize(_width, _height), s_imageFormat);
_qimages[2] = QImage(QSize(_width, _height), s_imageFormat);
_currentRead = 0;
_currentWrite = 1;
@ -99,6 +101,8 @@ QGraphicsViewAdapter::QGraphicsViewAdapter(osg::Image* image, QWidget* widget):
connect(_graphicsScene, SIGNAL(changed(const QList<QRectF> &)),
this, SLOT(repaintRequestedSlot(const QList<QRectF> &)));
connect(_graphicsScene, SIGNAL(sceneRectChanged(const QRectF &)),
this, SLOT(repaintRequestedSlot(const QRectF &)));
assignImage(0);
}
@ -109,6 +113,12 @@ void QGraphicsViewAdapter::repaintRequestedSlot(const QList<QRectF>&)
render();
}
void QGraphicsViewAdapter::repaintRequestedSlot(const QRectF&)
{
// OSG_NOTICE<<"QGraphicsViewAdapter::repaintRequestedSlot"<<std::endl;
render();
}
void QGraphicsViewAdapter::customEvent ( QEvent * event )
{
if (event->type()==MYQKEYEVENT)
@ -259,15 +269,30 @@ void QGraphicsViewAdapter::setUpKeyMap()
bool QGraphicsViewAdapter::sendPointerEvent(int x, int y, int buttonMask)
{
QCoreApplication::postEvent(this, new MyQPointerEvent(x,y,buttonMask));
return true;
_previousQtMouseX = x;
_previousQtMouseY = _graphicsView->size().height() - y;
QPoint pos(_previousQtMouseX, _previousQtMouseY);
if (_graphicsView->itemAt(pos) ||
_graphicsView->childAt(pos) ||
(_previousSentEvent && buttonMask != 0))
{
QCoreApplication::postEvent(this, new MyQPointerEvent(x,y,buttonMask));
OSG_INFO<<"sendPointerEvent("<<x<<", "<<y<<") sent"<<std::endl;
_previousSentEvent = true;
return true;
}
OSG_INFO<<"sendPointerEvent("<<x<<", "<<y<<") not sent"<<std::endl;
_previousSentEvent = false;
return false;
}
bool QGraphicsViewAdapter::handlePointerEvent(int x, int y, int buttonMask)
{
OSG_INFO<<"dispatchPointerEvent("<<x<<", "<<y<<", "<<buttonMask<<")"<<std::endl;
y = _graphicsScene->height()-y;
y = _graphicsView->size().height()-y;
bool leftButtonPressed = (buttonMask & osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)!=0;
bool middleButtonPressed = (buttonMask & osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)!=0;
@ -286,9 +311,11 @@ bool QGraphicsViewAdapter::handlePointerEvent(int x, int y, int buttonMask)
(middleButtonPressed ? Qt::MidButton : Qt::NoButton) |
(rightButtonPressed ? Qt::RightButton : Qt::NoButton);
const QRect viewportGeometry = _graphicsView->viewport()->geometry();
const QPoint pos(x, y);
if (buttonMask != _previousButtonMask)
{
Qt::MouseButton qtButton = Qt::NoButton;
QEvent::Type eventType = QEvent::None;
if (leftButtonPressed != prev_leftButtonPressed)
@ -312,15 +339,14 @@ bool QGraphicsViewAdapter::handlePointerEvent(int x, int y, int buttonMask)
_image->sendFocusHint(true);
}
QMouseEvent event(eventType, QPoint(x, y), qtButton, qtMouseButtons, 0);
QMouseEvent event(eventType, pos, qtButton, qtMouseButtons, 0);
QCoreApplication::sendEvent(_graphicsView->viewport(), &event );
_previousButtonMask = buttonMask;
}
else if (x != _previousMouseX || y != _previousMouseY)
{
QMouseEvent event(QEvent::MouseMove, QPoint(x, y), Qt::NoButton, qtMouseButtons, 0);
QMouseEvent event(QEvent::MouseMove, pos, Qt::NoButton, qtMouseButtons, 0);
QCoreApplication::sendEvent(_graphicsView->viewport(), &event);
_previousMouseX = x;
@ -332,8 +358,15 @@ bool QGraphicsViewAdapter::handlePointerEvent(int x, int y, int buttonMask)
bool QGraphicsViewAdapter::sendKeyEvent(int key, bool keyDown)
{
QCoreApplication::postEvent(this, new MyQKeyEvent(key,keyDown));
return true;
QPoint pos(_previousQtMouseX, _previousQtMouseY);
if (_graphicsView->itemAt(pos) ||
_graphicsView->childAt(pos))
{
QCoreApplication::postEvent(this, new MyQKeyEvent(key,keyDown));
return true;
}
return false;
}
bool QGraphicsViewAdapter::handleKeyEvent(int key, bool keyDown)
@ -419,20 +452,47 @@ void QGraphicsViewAdapter::render()
OSG_INFO<<"Current write = "<<_currentWrite<<std::endl;
QImage& image = _qimages[_currentWrite];
// If we got a resize, act on it, first by resizing the view, then the current image
if (_graphicsView->size().width() != _width || _graphicsView->size().height() != _height)
{
_graphicsView->setGeometry(0, 0, _width, _height);
_graphicsView->viewport()->setGeometry(0, 0, _width, _height);
}
if (image.width() != _width || image.height() != _height)
{
_qimages[_currentWrite] = QImage(_width, _height, s_imageFormat);
image = _qimages[_currentWrite];
}
OSG_INFO << "render image " << _currentWrite << " with size (" << _width << "," << _height << ")" <<std::endl;
#if 1
// paint the image with the graphics view
QPainter painter(&image);
QRectF destinationRect(0, 0, image.width(), image.height());
QRect sourceRect(0, 0, _graphicsScene->width(), _graphicsScene->height());
_graphicsView->render(&painter, destinationRect, sourceRect);
// Clear the image otherwise there are artifacts for some widgets that overpaint.
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(0, 0, _width, _height, _backgroundColor);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
QRectF destinationRect(0, 0, _width, _height);
QRect sourceRect(0, 0, _width, _height);
_graphicsView->render(&painter, destinationRect, sourceRect, Qt::IgnoreAspectRatio);
painter.end();
#elif 0
QPixmap pixmap(QPixmap::grabWidget(_graphicsView.data(), QRect(0, 0, _width, _height)));
image = pixmap.toImage();
#else
// paint the image with the graphics view
QPixmap pixmap(image.width(), image.height());
QPixmap pixmap(_width, _height);
// Clear the image otherwise there are artifacts for some widgets that overpaint.
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
QRectF destinationRect(0, 0, image.width(), image.height());
QRect sourceRect(0, 0, _graphicsScene->width(), _graphicsScene->height());
_graphicsView->render(&painter, destinationRect, sourceRect);
QRectF destinationRect(0, 0, _width, _height);
QRect sourceRect(0, 0, _width, _height);
_graphicsView->render(&painter, destinationRect, _graphicsView->viewport()->rect());
painter.end();
image = pixmap.toImage();
@ -452,13 +512,23 @@ void QGraphicsViewAdapter::assignImage(unsigned int i)
QImage& image = _qimages[i];
unsigned char* data = image.bits();
OSG_INFO<<"assigImage("<<i<<") image = "<<&image<<" data = "<<(void*)data<<std::endl;
OSG_INFO<<"assignImage("<<i<<") image = "<<&image<<" size = ("<<image.width()<<","<<image.height()<<") data = "<<(void*)data<<std::endl;
_image->setImage(image.width(),image.height(),1,
4,
GL_RGBA,GL_UNSIGNED_BYTE,
data,
osg::Image::NO_DELETE, 1);
_image->setImage(image.width(), image.height(), 1,
4, GL_RGBA, GL_UNSIGNED_BYTE,
data, osg::Image::NO_DELETE, 1);
}
void QGraphicsViewAdapter::resize(int width, int height)
{
OSG_INFO << "resize to (" << width << "," << height << ")" <<std::endl;
// Save the new width and height which will take effect on the next render() (in the Qt thread).
_width = width;
_height = height;
// Force an update so render() will be called.
_graphicsScene->update(_graphicsScene->sceneRect());
}
}

View File

@ -22,12 +22,7 @@ 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
_widget = widget;
_adapter = new QGraphicsViewAdapter(this, _widget.data());
}
@ -48,6 +43,11 @@ void QWidgetImage::render()
_adapter->render();
}
void QWidgetImage::scaleImage(int s,int t,int r, GLenum newDataType)
{
_adapter->resize(s, t);
}
void QWidgetImage::setFrameLastRendered(const osg::FrameStamp* frameStamp)
{
_adapter->setFrameLastRendered(frameStamp);

View File

@ -625,12 +625,90 @@ void LODScaleHandler::getUsage(osg::ApplicationUsage& usage) const
}
bool InteractiveImageHandler::computeIntersections(osgViewer::View* view, float x,float y, const osg::NodePath& nodePath, osgUtil::LineSegmentIntersector::Intersections& intersections,osg::Node::NodeMask traversalMask) const
{
float local_x, local_y = 0.0;
const osg::Camera* camera;
if (_fullscreen)
{
if (!_camera) return false;
camera = _camera.get();
local_x = x;
local_y = y;
}
else
{
if (!view->getCamera() || nodePath.empty()) return false;
camera = view->getCameraContainingPosition(x, y, local_x, local_y);
if (!camera) camera = view->getCamera();
}
osg::ref_ptr< osgUtil::LineSegmentIntersector > picker;
if (_fullscreen)
{
picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, local_x, local_y);
}
else
{
osg::Matrixd matrix;
if (nodePath.size()>1)
{
osg::NodePath prunedNodePath(nodePath.begin(),nodePath.end()-1);
matrix = osg::computeLocalToWorld(prunedNodePath);
}
matrix.postMult(camera->getViewMatrix());
matrix.postMult(camera->getProjectionMatrix());
double zNear = -1.0;
double zFar = 1.0;
if (camera->getViewport())
{
matrix.postMult(camera->getViewport()->computeWindowMatrix());
zNear = 0.0;
zFar = 1.0;
}
osg::Matrixd inverse;
inverse.invert(matrix);
osg::Vec3d startVertex = osg::Vec3d(local_x,local_y,zNear) * inverse;
osg::Vec3d endVertex = osg::Vec3d(local_x,local_y,zFar) * inverse;
picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::MODEL, startVertex, endVertex);
}
osgUtil::IntersectionVisitor iv(picker.get());
iv.setTraversalMask(traversalMask);
if (_fullscreen)
{
const_cast<osg::Camera*>(camera)->accept(iv);
}
else
{
nodePath.back()->accept(iv);
}
if (picker->containsIntersections())
{
intersections = picker->getIntersections();
return true;
}
else
{
intersections.clear();
return false;
}
}
bool InteractiveImageHandler::mousePosition(osgViewer::View* view, osg::NodeVisitor* nv, const osgGA::GUIEventAdapter& ea, int& x, int &y) const
{
osgUtil::LineSegmentIntersector::Intersections intersections;
bool foundIntersection = view==0 ? false :
(nv==0 ? view->computeIntersections(ea.getX(), ea.getY(), intersections) :
view->computeIntersections(ea.getX(), ea.getY(), nv->getNodePath(), intersections));
//view->computeIntersections(ea.getX(), ea.getY(), nv->getNodePath(), intersections));
computeIntersections(view, ea.getX(), ea.getY(), nv->getNodePath(), intersections));
if (foundIntersection)
{
@ -742,6 +820,15 @@ bool InteractiveImageHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUI
return _image->sendKeyEvent(ea.getKey(), ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN);
}
}
case (osgGA::GUIEventAdapter::RESIZE):
{
if (_fullscreen)
{
_camera->setViewport(0, 0, ea.getWindowWidth(), ea.getWindowHeight());
_image->scaleImage(ea.getWindowWidth(), ea.getWindowHeight(), 1);
return true;
}
}
default:
return false;