diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2ea9125b5..0d9488b1d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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() diff --git a/examples/osgQtWidgets/CMakeLists.txt b/examples/osgQtWidgets/CMakeLists.txt new file mode 100644 index 000000000..2266ee4f8 --- /dev/null +++ b/examples/osgQtWidgets/CMakeLists.txt @@ -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) diff --git a/examples/osgQtWidgets/osgQtWidgets.cpp b/examples/osgQtWidgets/osgQtWidgets.cpp new file mode 100644 index 000000000..10518f1c0 --- /dev/null +++ b/examples/osgQtWidgets/osgQtWidgets.cpp @@ -0,0 +1,497 @@ +/* OpenSceneGraph example, osgcompositeviewer. +* +* 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: +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +//#include +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + + +// Thread that runs the viewer's frame loop as we can't run Qt in the background... +class ViewerFrameThread : public OpenThreads::Thread +{ + public: + + ViewerFrameThread(osgViewer::ViewerBase* viewerBase, bool doQApplicationExit): + _viewerBase(viewerBase), + _doQApplicationExit(doQApplicationExit) {} + + ~ViewerFrameThread() + { + cancel(); + while(isRunning()) + { + OpenThreads::Thread::YieldCurrentThread(); + } + } + + int cancel() + { + _viewerBase->setDone(true); + return 0; + } + + void run() + { + int result = _viewerBase->run(); + + if (_doQApplicationExit) QApplication::exit(result); + } + + osg::ref_ptr _viewerBase; + bool _doQApplicationExit; +}; + + +class MyPushButton : public QPushButton +{ +public: + MyPushButton(const QString& text) : QPushButton(text) {} + +protected: + virtual void mousePressEvent(QMouseEvent* e) + { + bool ok = false; + int val = QInputDialog::getInteger(this, "Get integer", "Please enter an integer between 0 and pi", 0, 0, 3, 1, &ok); + std::cout << "Ok was " << (ok ? "" : "not") << " pressed, val is " << val << std::endl; + } +}; + + +//We would need to document the following somewhere in order to guide people on +//what they need to use... +// +//---------------------------------------------- +//There are two angles to consider. +// +//1. If someone wants a widget in their Qt app to be an OSG-rendered scene, they +//need GraphicsWindowQt (in the osgViewerQtContext example) or QOSGWidget (in the +//osgViewerQt example). These two allow both OSG and Qt to manage their threads +//in a way which is optimal to them. We've used QOSGWidget in the past and had +//trouble when Qt tried to overlay other widgets over the QOSGWidget (since OSG +//did its rendering independently of Qt, it would overwrite what Qt had drawn). I +//haven't tried GraphicsWindowQt yet, but I expect since it uses QGLWidget, it +//will result in Qt knowing when OSG has drawn and be able to do overlays at the +//right time. Eventually GraphicsWindowQt can be brought into osgViewer I imagine... +// +//2. If someone wants to bring Qt widgets inside their OSG scene (to do HUDs or +//an interface on a computer screen which is inside the 3D scene, or even +//floating Qt widgets, for example). That's where QGraphicsViewAdapter + +//QWidgetImage will be useful. +//---------------------------------------------- + + +int main(int argc, char **argv) +{ + // Qt requires that we construct the global QApplication before creating any widgets. + QApplication app(argc, argv); + + // use an ArgumentParser object to manage the program arguments. + osg::ArgumentParser arguments(&argc,argv); + + // true = run osgViewer in a separate thread than Qt + // false = interleave osgViewer and Qt in the main thread + bool useFrameLoopThread = true; + if (arguments.read("--noFrameThread")) useFrameLoopThread = false; + + // true = use QWidgetImage + // false = use QWebViewImage + bool useWidgetImage = false; + if (arguments.read("--useWidgetImage")) useWidgetImage = true; + + // true = use QWebView in a QWidgetImage to compare to QWebViewImage + // false = make an interesting widget + bool useBrowser = false; + if (arguments.read("--useBrowser")) useBrowser = true; + + // true = use a QLabel for text + // false = use a QTextEdit for text + // (only applies if useWidgetImage == true and useBrowser == false) + bool useLabel = false; + if (arguments.read("--useLabel")) useLabel = true; + + // true = make a Qt window with the same content to compare to + // QWebViewImage/QWidgetImage + // false = use QWebViewImage/QWidgetImage (depending on useWidgetImage) + bool sanityCheck = false; + if (arguments.read("--sanityCheck")) sanityCheck = true; + + // Add n floating windows inside the QGraphicsScene. + int numFloatingWindows = 0; + while (arguments.read("--numFloatingWindows", numFloatingWindows)); + + // true = Qt widgets will be displayed on a quad inside the 3D scene + // false = Qt widgets will be an overlay over the scene (like a HUD) + bool inScene = true; + if (arguments.read("--fullscreen")) { inScene = false; } + + + osg::ref_ptr root = new osg::Group; + + if (!useWidgetImage) + { + //------------------------------------------------------------------- + // QWebViewImage test + //------------------------------------------------------------------- + // Note: When the last few issues with QWidgetImage are fixed, + // QWebViewImage and this if() {} section can be removed since + // QWidgetImage can display a QWebView just like QWebViewImage. Use + // --useWidgetImage --useBrowser to see that in action. + + if (!sanityCheck) + { + osg::ref_ptr image = new osgQt::QWebViewImage; + + if (arguments.argc()>1) image->navigateTo((arguments[1])); + else image->navigateTo("http://www.youtube.com/"); + + osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f), + osg::Vec3(1.0f,0.0f,0.0f), + osg::Vec3(0.0f,0.0f,1.0f), + osg::Vec4(1.0f,1.0f,1.0f,1.0f), + osgWidget::GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO); + + osg::ref_ptr browser = new osgWidget::Browser; + browser->assign(image.get(), hints); + + root->addChild(browser.get()); + } + else + { + // Sanity check, do the same thing as QGraphicsViewAdapter but in + // a separate Qt window. + QWebPage* webPage = new QWebPage; + webPage->settings()->setAttribute(QWebSettings::JavascriptEnabled, true); + webPage->settings()->setAttribute(QWebSettings::PluginsEnabled, true); + + QWebView* webView = new QWebView; + webView->setPage(webPage); + + if (arguments.argc()>1) webView->load(QUrl(arguments[1])); + else webView->load(QUrl("http://www.youtube.com/")); + + QGraphicsScene* graphicsScene = new QGraphicsScene; + graphicsScene->addWidget(webView); + + QGraphicsView* graphicsView = new QGraphicsView; + graphicsView->setScene(graphicsScene); + + QMainWindow* mainWindow = new QMainWindow; + //mainWindow->setLayout(new QVBoxLayout); + mainWindow->setCentralWidget(graphicsView); + mainWindow->setGeometry(50, 50, 1024, 768); + mainWindow->show(); + mainWindow->raise(); + } + } + else + { + //------------------------------------------------------------------- + // QWidgetImage test + //------------------------------------------------------------------- + // 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. + + + QWidget* widget = 0; + if (useBrowser) + { + QWebPage* webPage = new QWebPage; + webPage->settings()->setAttribute(QWebSettings::JavascriptEnabled, true); + webPage->settings()->setAttribute(QWebSettings::PluginsEnabled, true); + + QWebView* webView = new QWebView; + webView->setPage(webPage); + + if (arguments.argc()>1) webView->load(QUrl(arguments[1])); + else webView->load(QUrl("http://www.youtube.com/")); + + widget = webView; + } + else + { + widget = new QWidget; + widget->setLayout(new QVBoxLayout); + + QString text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque velit turpis, euismod ac ultrices et, molestie non nisi. Nullam egestas dignissim enim, quis placerat nulla suscipit sed. Donec molestie elementum risus sit amet sodales. Nunc consectetur congue neque, at viverra massa pharetra fringilla. Integer vitae mi sem. Donec dapibus semper elit nec sollicitudin. Vivamus egestas ultricies felis, in mollis mi facilisis quis. Nam suscipit bibendum eros sed cursus. Suspendisse mollis suscipit hendrerit. Etiam magna eros, convallis non congue vel, faucibus ac augue. Integer ante ante, porta in ornare ullamcorper, congue nec nibh. Etiam congue enim vitae enim sollicitudin fringilla. Mauris mattis, urna in fringilla dapibus, ipsum sem feugiat purus, ac hendrerit felis arcu sed sapien. Integer id velit quam, sit amet dignissim tortor. Sed mi tortor, placerat ac luctus id, tincidunt et urna. Nulla sed nunc ante.Sed ut sodales enim. Ut sollicitudin ultricies magna, vel ultricies ante venenatis id. Cras luctus mi in lectus rhoncus malesuada. Sed ac sollicitudin nisi. Nunc venenatis congue quam, et suscipit diam consectetur id. Donec vel enim ac enim elementum bibendum ut quis augue. Nulla posuere suscipit dolor, id convallis tortor congue eu. Vivamus sagittis consectetur dictum. Duis a ante quis dui varius fermentum. In hac habitasse platea dictumst. Nam dapibus dolor eu felis eleifend in scelerisque dolor ultrices. Donec arcu lectus, fringilla ut interdum non, tristique id dolor. Morbi sagittis sagittis volutpat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Duis venenatis ultrices euismod.Nam sit amet convallis libero. Integer lectus urna, eleifend et sollicitudin non, porttitor vel erat. Vestibulum pulvinar egestas leo, a porttitor turpis ullamcorper et. Vestibulum in ornare turpis. Ut nec libero a sem mattis iaculis quis id purus. Praesent ante neque, dictum vitae pretium vel, iaculis luctus dui. Etiam luctus tellus vel nunc suscipit a ullamcorper nisl semper. Nunc dapibus, eros in sodales dignissim, orci lectus egestas felis, sit amet vehicula tortor dolor eu quam. Vivamus pellentesque convallis quam aliquet pellentesque. Phasellus facilisis arcu ac orci fringilla aliquet. Donec sed euismod augue. Duis eget orci sit amet neque tempor fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In hac habitasse platea dictumst. Duis sollicitudin, lacus ac pellentesque lacinia, lacus magna pulvinar purus, pulvinar porttitor est nibh quis augue.Duis eleifend, massa sit amet mattis fringilla, elit turpis venenatis libero, sed convallis turpis diam sit amet ligula. Morbi non dictum turpis. Integer porttitor condimentum elit, sit amet sagittis nibh ultrices sit amet. Mauris ac arcu augue, id aliquet mauris. Donec ultricies urna id enim accumsan at pharetra dui adipiscing. Nunc luctus rutrum molestie. Curabitur libero ipsum, viverra at pulvinar ut, porttitor et neque. Aliquam sit amet dolor et purus sagittis adipiscing. Nam sit amet hendrerit sem. Etiam varius, ligula non ultricies dignissim, sapien dui commodo urna, eu vehicula enim nunc molestie augue. Fusce euismod, erat vitae pharetra tempor, quam eros tincidunt lorem, ut iaculis ligula erat vitae nibh. Aenean eu ultricies dolor. Curabitur suscipit viverra bibendum.Sed egestas adipiscing mi in egestas. Proin in neque in nibh blandit consequat nec quis tortor. Vestibulum sed interdum justo. Sed volutpat velit vitae elit pulvinar aliquam egestas elit rutrum. Proin lorem nibh, bibendum vitae sollicitudin condimentum, pulvinar ut turpis. Maecenas iaculis, mauris in consequat ultrices, ante erat blandit mi, vel fermentum lorem turpis eget sem. Integer ultrices tristique erat sit amet volutpat. In sit amet diam et nunc congue pellentesque at in dolor. Mauris eget orci orci. Integer posuere augue ornare tortor tempus elementum. Quisque iaculis, nunc ac cursus fringilla, magna elit cursus eros, id feugiat diam eros et tellus. Etiam consectetur ultrices erat quis rhoncus. Mauris eu lacinia neque. Curabitur suscipit feugiat tellus in dictum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed aliquam tempus ante a tempor. Praesent viverra erat quis sapien pretium rutrum. Praesent dictum scelerisque venenatis.Proin bibendum lectus eget nisl lacinia porta. Morbi eu erat in sapien malesuada vulputate. Cras non elit quam. Ut dictum urna quis nisl feugiat ac sollicitudin libero luctus. Donec leo mauris, varius at luctus eget, placerat quis arcu. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Etiam tristique, mauris ut lacinia elementum, mauris erat consequat massa, ac gravida nisi tellus vitae purus. Curabitur consectetur ultricies commodo. Cras pulvinar orci nec enim adipiscing tristique. Ut ornare orci id est fringilla sit amet blandit libero pellentesque. Vestibulum tincidunt sapien ut enim venenatis vestibulum ultricies ipsum tristique. Mauris tempus eleifend varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse vitae dui ac quam gravida semper. In ac enim ac ligula rutrum porttitor.Integer dictum sagittis leo, at convallis sapien facilisis eget. Etiam cursus bibendum tortor, faucibus aliquam lectus ullamcorper sed. Nulla pulvinar posuere quam, ut sagittis ligula tincidunt ut. Nulla convallis velit ut enim condimentum pulvinar. Quisque gravida accumsan scelerisque. Proin pellentesque nisi cursus tortor aliquet dapibus. Duis vel eros orci. Sed eget purus ligula. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ullamcorper porta congue. Nunc id velit ut neque malesuada consequat in eu nisi. Nulla facilisi. Quisque pellentesque magna vitae nisl euismod ac accumsan tellus feugiat.Nulla facilisi. Integer quis orci lectus, non aliquam nisi. Vivamus varius porta est, ac porttitor orci blandit mattis. Sed dapibus facilisis dapibus. Duis tincidunt leo ac tortor faucibus hendrerit. Morbi sit amet sapien risus, vel luctus enim. Aliquam sagittis nunc id purus aliquam lobortis. Duis posuere viverra dui, sit amet convallis sem vulputate at. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pellentesque, lectus id imperdiet commodo, diam diam faucibus lectus, sit amet vestibulum tortor lacus viverra eros.Maecenas nec augue lectus. Duis nec arcu eget lorem tempus sollicitudin suscipit vitae arcu. Nullam vitae mauris lectus. Vivamus id risus neque, dignissim vehicula diam. Cras rhoncus velit sed velit iaculis ac dignissim turpis luctus. Suspendisse potenti. Sed vitae ligula a ligula ornare rutrum sit amet ut quam. Duis tincidunt, nibh vitae iaculis adipiscing, dolor orci cursus arcu, vel congue tortor quam eget arcu. Suspendisse tellus felis, blandit ac accumsan vitae, fringilla id lorem. Duis tempor lorem mollis est congue ut imperdiet velit laoreet. Nullam interdum cursus mollis. Pellentesque non mauris accumsan elit laoreet viverra ut at risus. Proin rutrum sollicitudin sem, vitae ultricies augue sagittis vel. Cras quis vehicula neque. Aliquam erat volutpat. Aliquam erat volutpat. Praesent non est erat, accumsan rutrum lacus. Pellentesque tristique molestie aliquet. Cras ullamcorper facilisis faucibus. In non lorem quis velit lobortis pulvinar.Phasellus non sem ipsum. Praesent ut libero quis turpis viverra semper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In hac habitasse platea dictumst. Donec at velit tellus. Fusce commodo pharetra tincidunt. Proin lacus enim, fringilla a fermentum ut, vestibulum ut nibh. Duis commodo dolor vel felis vehicula at egestas neque bibendum. Phasellus malesuada dictum ante in aliquam. Curabitur interdum semper urna, nec placerat justo gravida in. Praesent quis mauris massa. Pellentesque porttitor lacinia tincidunt. Phasellus egestas viverra elit vel blandit. Sed dapibus nisi et lectus pharetra dignissim. Mauris hendrerit lectus nec purus dapibus condimentum. Sed ac eros nulla. Aenean semper sapien a nibh aliquam lobortis. Aliquam elementum euismod sapien, in dapibus leo dictum et. Pellentesque augue neque, ultricies non viverra eu, tincidunt ac arcu. Morbi ut porttitor lectus."); + + if (useLabel) + { + QLabel* label = new QLabel(text); + label->setWordWrap(true); + label->setTextInteractionFlags(Qt::TextEditorInteraction); + + QPalette palette = label->palette(); + palette.setColor(QPalette::Highlight, Qt::darkBlue); + palette.setColor(QPalette::HighlightedText, Qt::white); + label->setPalette(palette); + + QScrollArea* scrollArea = new QScrollArea; + scrollArea->setWidget(label); + + widget->layout()->addWidget(scrollArea); + } + else + { + QTextEdit* textEdit = new QTextEdit(text); + textEdit->setReadOnly(false); + textEdit->setTextInteractionFlags(Qt::TextEditable); + + QPalette palette = textEdit->palette(); + palette.setColor(QPalette::Highlight, Qt::darkBlue); + palette.setColor(QPalette::HighlightedText, Qt::white); + textEdit->setPalette(palette); + + widget->layout()->addWidget(textEdit); + } + + QPushButton* button = new MyPushButton("Button"); + widget->layout()->addWidget(button); + + widget->setGeometry(0, 0, 800, 600); + } + + QGraphicsScene* graphicsScene = 0; + + if (!sanityCheck) + { + osg::ref_ptr widgetImage = new osgQt::QWidgetImage(widget); + widgetImage->getQWidget()->setAttribute(Qt::WA_TranslucentBackground); + widgetImage->getQGraphicsViewAdapter()->setBackgroundColor(QColor(0, 0, 0, 0)); + //widgetImage->getQGraphicsViewAdapter()->resize(800, 600); + graphicsScene = widgetImage->getQGraphicsViewAdapter()->getQGraphicsScene(); + + osg::Camera* camera = 0; // Will stay NULL in the inScene case. + osg::Geometry* quad = osg::createTexturedQuadGeometry(osg::Vec3(0,0,0), osg::Vec3(1,0,0), osg::Vec3(0,1,0), 1, 1); + osg::Geode* geode = new osg::Geode; + geode->addDrawable(quad); + + osg::MatrixTransform* mt = new osg::MatrixTransform; + if (inScene) + { + mt->setMatrix(osg::Matrix::rotate(osg::Vec3(0,1,0), osg::Vec3(0,0,1))); + mt->addChild(geode); + } + else // fullscreen + { + // The HUD camera's viewport needs to follow the size of the + // window. MyInteractiveImageHandler will make sure of this. + // As for the quad and the camera's projection, setting the + // projection resize policy to FIXED takes care of them, so + // they can stay the same: (0,1,0,1) with a quad that fits. + + // Set the HUD camera's projection and viewport to match the screen. + camera = new osg::Camera; + camera->setProjectionResizePolicy(osg::Camera::FIXED); + camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1)); + camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); + camera->setViewMatrix(osg::Matrix::identity()); + camera->setClearMask(GL_DEPTH_BUFFER_BIT); + camera->setRenderOrder(osg::Camera::POST_RENDER); + camera->addChild(geode); + camera->setViewport(0, 0, 1024, 768); + + mt->addChild(camera); + } + + mt->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); + mt->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); + mt->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); + mt->getOrCreateStateSet()->setAttribute(new osg::Program); + + osg::Texture2D* texture = new osg::Texture2D(widgetImage.get()); + texture->setResizeNonPowerOfTwoHint(false); + texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); + texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); + texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); + mt->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); + + osg::Group* overlay = new osg::Group; + overlay->addChild(mt); + + root->addChild(overlay); + + osgViewer::InteractiveImageHandler* handler = new osgViewer::InteractiveImageHandler(widgetImage.get(), camera); + quad->setEventCallback(handler); + quad->setCullCallback(handler); + } + else + { + // Sanity check, do the same thing as QWidgetImage and + // QGraphicsViewAdapter but in a separate Qt window. + + graphicsScene = new QGraphicsScene; + graphicsScene->addWidget(widget); + + QGraphicsView* graphicsView = new QGraphicsView; + graphicsView->setScene(graphicsScene); + + QMainWindow* mainWindow = new QMainWindow; + mainWindow->setCentralWidget(graphicsView); + mainWindow->setGeometry(50, 50, 1024, 768); + mainWindow->show(); + mainWindow->raise(); + } + + // Add numFloatingWindows windows to the graphicsScene. + for (unsigned int i = 0; i < (unsigned int)numFloatingWindows; ++i) + { + QWidget* window = new QWidget(0, Qt::Window); + window->setWindowTitle(QString("Window %1").arg(i)); + window->setLayout(new QVBoxLayout); + window->layout()->addWidget(new QLabel(QString("This window %1").arg(i))); + window->layout()->addWidget(new MyPushButton(QString("Button in window %1").arg(i))); + window->setGeometry(100, 100, 300, 300); + + QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(0, Qt::Window); + proxy->setWidget(window); + proxy->setFlag(QGraphicsItem::ItemIsMovable, true); + + graphicsScene->addItem(proxy); + } + + } + + root->addChild(osgDB::readNodeFile("cow.osg.(15,0,5).trans.(0.1,0.1,0.1).scale")); + + osg::ref_ptr viewer = new osgViewer::Viewer(arguments); + viewer->setSceneData(root.get()); + viewer->setCameraManipulator(new osgGA::TrackballManipulator()); + viewer->addEventHandler(new osgGA::StateSetManipulator(root->getOrCreateStateSet())); + viewer->addEventHandler(new osgViewer::StatsHandler); + viewer->addEventHandler(new osgViewer::WindowSizeHandler); + + viewer->setUpViewInWindow(50, 50, 1024, 768); + viewer->getEventQueue()->windowResize(0, 0, 1024, 768); + + if (useFrameLoopThread) + { + // create a thread to run the viewer's frame loop + ViewerFrameThread viewerThread(viewer.get(), true); + viewerThread.startThread(); + + // now start the standard Qt event loop, then exists when the viewerThead sends the QApplication::exit() signal. + return QApplication::exec(); + + } + else + { + // run the frame loop, interleaving Qt and the main OSG frame loop + while(!viewer->done()) + { + // process Qt events - this handles both events and paints the browser image + QCoreApplication::processEvents(QEventLoop::AllEvents, 100); + + viewer->frame(); + } + + return 0; + } +} diff --git a/include/osgQt/QGraphicsViewAdapter b/include/osgQt/QGraphicsViewAdapter index 91c111c26..faf0a21af 100644 --- a/include/osgQt/QGraphicsViewAdapter +++ b/include/osgQt/QGraphicsViewAdapter @@ -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 KeyMap; KeyMap _keyMap; @@ -86,6 +100,7 @@ class OSGQT_EXPORT QGraphicsViewAdapter : public QObject private slots: void repaintRequestedSlot(const QList ®ions); + void repaintRequestedSlot(const QRectF ®ion); }; diff --git a/include/osgQt/QWidgetImage b/include/osgQt/QWidgetImage index bd6f88ca4..0217e7551 100644 --- a/include/osgQt/QWidgetImage +++ b/include/osgQt/QWidgetImage @@ -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); diff --git a/include/osgViewer/ViewerEventHandlers b/include/osgViewer/ViewerEventHandlers index 7d23b9290..526a11ffb 100644 --- a/include/osgViewer/ViewerEventHandlers +++ b/include/osgViewer/ViewerEventHandlers @@ -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 _image; - bool _handleKeyboardEvents; - bool _handledOnKeyboardEvents; - bool _handleMouseEvents; - bool _handledOnMouseEvents; + osg::observer_ptr _image; + + bool _fullscreen; + osg::observer_ptr _camera; }; diff --git a/src/osgQt/QGraphicsViewAdapter.cpp b/src/osgQt/QGraphicsViewAdapter.cpp index a5f4154c9..a545f6f4e 100644 --- a/src/osgQt/QGraphicsViewAdapter.cpp +++ b/src/osgQt/QGraphicsViewAdapter.cpp @@ -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 &)), this, SLOT(repaintRequestedSlot(const QList &))); + connect(_graphicsScene, SIGNAL(sceneRectChanged(const QRectF &)), + this, SLOT(repaintRequestedSlot(const QRectF &))); assignImage(0); } @@ -109,6 +113,12 @@ void QGraphicsViewAdapter::repaintRequestedSlot(const QList&) render(); } +void QGraphicsViewAdapter::repaintRequestedSlot(const QRectF&) +{ + // OSG_NOTICE<<"QGraphicsViewAdapter::repaintRequestedSlot"<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("<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<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 << ")" <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("<update(_graphicsScene->sceneRect()); } } diff --git a/src/osgQt/QWidgetImage.cpp b/src/osgQt/QWidgetImage.cpp index 9a72b183c..823180e4b 100644 --- a/src/osgQt/QWidgetImage.cpp +++ b/src/osgQt/QWidgetImage.cpp @@ -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); diff --git a/src/osgViewer/ViewerEventHandlers.cpp b/src/osgViewer/ViewerEventHandlers.cpp index 0e9a39ed7..ef3b006eb 100644 --- a/src/osgViewer/ViewerEventHandlers.cpp +++ b/src/osgViewer/ViewerEventHandlers.cpp @@ -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(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;