root/OpenSceneGraph/trunk/examples/osgQtBrowser/QGraphicsViewAdapter.cpp @ 10768

Revision 10768, 18.6 kB (checked in by robert, 5 years ago)

From Mathias Froechlich, "Attached the collected fixes I needed to compile with all of them.
Most notable the hpux define stuff. The hpux variant seems to be not
defined which resulted in a compile error at this time. Consequently I have
replaced all occurances of
hpux with hpux. And huge surprise: now osg
plugins are found and loaded correctly ...
The next notable one is the MSVC_IDE fix which makes the nmake Makefiles cmake
generator target behave like the ide one. Showed up because I started to do
scripted builds with nmake instead of devenv...
The rest is the usual bunch of stuff that just happens during normal
coding ..."

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include "QGraphicsViewAdapter.h"
15#include "QWebViewImage.h"
16
17#include <QtOpenGL/QGLWidget>
18
19#include <osgGA/GUIEventAdapter>
20
21#include <osg/io_utils>
22
23#define MYQKEYEVENT 2000
24#define MYQPOINTEREVENT 2001
25
26QCoreApplication* getOrCreateQApplication()
27{
28    if (QApplication::instance()==0)
29    {
30        static char** argv = 0;
31        static int argc = 0;
32        static QApplication app(argc,argv);
33    }
34    return QApplication::instance();
35}
36
37class MyQKeyEvent : public QEvent
38{
39public:
40    MyQKeyEvent( int key, bool down ):
41        QEvent( QEvent::Type(MYQKEYEVENT) ),
42        _key(key), _down(down) {}
43
44    int         _key;
45    bool        _down;
46};
47
48struct MyQPointerEvent : public QEvent
49{
50    MyQPointerEvent(int x, int y, unsigned int buttonMask):
51        QEvent( QEvent::Type(MYQPOINTEREVENT) ),
52        _x(x), _y(y),_buttonMask(buttonMask) {}
53
54    int _x, _y;
55    unsigned int _buttonMask;
56};
57
58QGraphicsViewAdapter::QGraphicsViewAdapter(osg::Image* image, QWidget* widget):
59    _image(image),
60    _backgroundColor(255,255,255),
61    _qtKeyModifiers(Qt::NoModifier)
62{
63    // make sure we have a valid QApplication before we start creating widgets.
64    getOrCreateQApplication();
65
66
67    setUpKeyMap();
68
69    _graphicsScene = new QGraphicsScene();
70    _graphicsView = new QGraphicsView;
71    _graphicsScene->addWidget(widget);
72    _graphicsView->setScene(_graphicsScene);
73#if (QT_VERSION_CHECK(4, 5, 0) <= QT_VERSION)
74    _graphicsScene->setStickyFocus(true);
75#endif
76    _graphicsView->viewport()->setParent(0);
77
78    int width = _graphicsScene->width();
79    int height = _graphicsScene->height();
80
81    _qimages[0] = QImage(QSize(width, height), QImage::Format_ARGB32);
82    _qimages[0].fill(_backgroundColor.rgba());
83    _qimages[0] = QGLWidget::convertToGLFormat(_qimages[0]);
84
85    _qimages[1] = QImage(QSize(width, height), QImage::Format_ARGB32);
86    _qimages[1].fill(_backgroundColor.rgba());
87
88    _qimages[2] = QImage(QSize(width, height), QImage::Format_ARGB32);
89    _qimages[2].fill(_backgroundColor.rgba());
90
91    _currentRead = 0;
92    _currentWrite = 1;
93    _previousWrite = 2;
94    _previousFrameNumber = 0;
95    _newImageAvailable = false;
96
97    connect(_graphicsScene, SIGNAL(changed(const QList<QRectF> &)),
98            this, SLOT(repaintRequestedSlot(const QList<QRectF> &)));
99
100    assignImage(0);
101}
102
103void QGraphicsViewAdapter::repaintRequestedSlot(const QList<QRectF>&)
104{
105    // osg::notify(osg::NOTICE)<<"QGraphicsViewAdapter::repaintRequestedSlot"<<std::endl;
106    render();
107}
108
109void QGraphicsViewAdapter::customEvent ( QEvent * event )
110{
111    if (event->type()==MYQKEYEVENT)
112    {
113        MyQKeyEvent* keyEvent = (MyQKeyEvent*)event;
114        handleKeyEvent(keyEvent->_key, keyEvent->_down);
115    }
116    else if (event->type()==MYQPOINTEREVENT)
117    {
118        MyQPointerEvent* pointerEvent = (MyQPointerEvent*)event;
119        handlePointerEvent(pointerEvent->_x, pointerEvent->_y, pointerEvent->_buttonMask);
120    }
121}
122
123
124void QGraphicsViewAdapter::setUpKeyMap()
125{
126    _keyMap[osgGA::GUIEventAdapter::KEY_BackSpace] = Qt::Key_Backspace;
127    _keyMap[osgGA::GUIEventAdapter::KEY_Tab] = Qt::Key_Tab;
128    _keyMap[osgGA::GUIEventAdapter::KEY_Linefeed] = Qt::Key_Return; // No LineFeed in Qt!
129    _keyMap[osgGA::GUIEventAdapter::KEY_Clear] = Qt::Key_Clear;
130    _keyMap[osgGA::GUIEventAdapter::KEY_Return] = Qt::Key_Return;
131    _keyMap[osgGA::GUIEventAdapter::KEY_Pause] = Qt::Key_Pause;
132    _keyMap[osgGA::GUIEventAdapter::KEY_Scroll_Lock] = Qt::Key_ScrollLock;
133    _keyMap[osgGA::GUIEventAdapter::KEY_Sys_Req] = Qt::Key_SysReq;
134    _keyMap[osgGA::GUIEventAdapter::KEY_Escape] = Qt::Key_Escape;
135    _keyMap[osgGA::GUIEventAdapter::KEY_Delete] = Qt::Key_Delete;
136
137    _keyMap[osgGA::GUIEventAdapter::KEY_Home] = Qt::Key_Home;
138    _keyMap[osgGA::GUIEventAdapter::KEY_Left] = Qt::Key_Left;
139    _keyMap[osgGA::GUIEventAdapter::KEY_Up] = Qt::Key_Up;
140    _keyMap[osgGA::GUIEventAdapter::KEY_Right] = Qt::Key_Right;
141    _keyMap[osgGA::GUIEventAdapter::KEY_Down] = Qt::Key_Down;
142    _keyMap[osgGA::GUIEventAdapter::KEY_Prior] = Qt::Key_Left; // no Prior in Qt
143    _keyMap[osgGA::GUIEventAdapter::KEY_Page_Up] = Qt::Key_PageUp;
144    _keyMap[osgGA::GUIEventAdapter::KEY_Next] = Qt::Key_Right; // No Next in Qt
145    _keyMap[osgGA::GUIEventAdapter::KEY_Page_Down] = Qt::Key_PageDown;
146    _keyMap[osgGA::GUIEventAdapter::KEY_End] = Qt::Key_End;
147    _keyMap[osgGA::GUIEventAdapter::KEY_Begin] = Qt::Key_Home; // No Begin in Qt
148
149    _keyMap[osgGA::GUIEventAdapter::KEY_Select] = Qt::Key_Select;
150    _keyMap[osgGA::GUIEventAdapter::KEY_Print] = Qt::Key_Print;
151    _keyMap[osgGA::GUIEventAdapter::KEY_Execute] = Qt::Key_Execute;
152    _keyMap[osgGA::GUIEventAdapter::KEY_Insert] = Qt::Key_Insert;
153    //_keyMap[osgGA::GUIEventAdapter::KEY_Undo] = Qt::Key_; // no Undo
154    //_keyMap[osgGA::GUIEventAdapter::KEY_Redo] = Qt::Key_; // no Redo
155    _keyMap[osgGA::GUIEventAdapter::KEY_Menu] = Qt::Key_Menu;
156    _keyMap[osgGA::GUIEventAdapter::KEY_Find] = Qt::Key_Search; // no Qt Find
157    _keyMap[osgGA::GUIEventAdapter::KEY_Cancel] = Qt::Key_Cancel;
158    _keyMap[osgGA::GUIEventAdapter::KEY_Help] = Qt::Key_Help;
159    _keyMap[osgGA::GUIEventAdapter::KEY_Break] = Qt::Key_Escape; // no break
160    _keyMap[osgGA::GUIEventAdapter::KEY_Mode_switch] = Qt::Key_Mode_switch;
161    _keyMap[osgGA::GUIEventAdapter::KEY_Script_switch] = Qt::Key_Mode_switch; // no Script switch
162    _keyMap[osgGA::GUIEventAdapter::KEY_Num_Lock] = Qt::Key_NumLock;
163
164    _keyMap[osgGA::GUIEventAdapter::KEY_Shift_L] = Qt::Key_Shift;
165    _keyMap[osgGA::GUIEventAdapter::KEY_Shift_R] = Qt::Key_Shift;
166    _keyMap[osgGA::GUIEventAdapter::KEY_Control_L] = Qt::Key_Control;
167    _keyMap[osgGA::GUIEventAdapter::KEY_Control_R] = Qt::Key_Control;
168    _keyMap[osgGA::GUIEventAdapter::KEY_Caps_Lock] = Qt::Key_CapsLock;
169    _keyMap[osgGA::GUIEventAdapter::KEY_Shift_Lock] = Qt::Key_CapsLock;
170
171    _keyMap[osgGA::GUIEventAdapter::KEY_Meta_L] = Qt::Key_Meta; // Qt doesn't have a Meta L
172    _keyMap[osgGA::GUIEventAdapter::KEY_Meta_R] = Qt::Key_Meta; // Qt doesn't have a Meta R
173    _keyMap[osgGA::GUIEventAdapter::KEY_Alt_L] = Qt::Key_Alt; // Qt doesn't have a Alt L
174    _keyMap[osgGA::GUIEventAdapter::KEY_Alt_R] = Qt::Key_Alt; // Qt doesn't have a Alt R
175    _keyMap[osgGA::GUIEventAdapter::KEY_Super_L] = Qt::Key_Super_L;
176    _keyMap[osgGA::GUIEventAdapter::KEY_Super_R] = Qt::Key_Super_R;
177    _keyMap[osgGA::GUIEventAdapter::KEY_Hyper_L] = Qt::Key_Hyper_L;
178    _keyMap[osgGA::GUIEventAdapter::KEY_Hyper_R] = Qt::Key_Hyper_R;
179
180    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Space] = Qt::Key_Space;
181    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Tab] = Qt::Key_Tab;
182    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Enter] = Qt::Key_Enter;
183    _keyMap[osgGA::GUIEventAdapter::KEY_KP_F1] = Qt::Key_F1;
184    _keyMap[osgGA::GUIEventAdapter::KEY_KP_F2] = Qt::Key_F2;
185    _keyMap[osgGA::GUIEventAdapter::KEY_KP_F3] = Qt::Key_F3;
186    _keyMap[osgGA::GUIEventAdapter::KEY_KP_F4] = Qt::Key_F4;
187    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Home] = Qt::Key_Home;
188    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Left] = Qt::Key_Left;
189    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Up] = Qt::Key_Up;
190    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Right] = Qt::Key_Right;
191    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Down] = Qt::Key_Down;
192    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Prior] = Qt::Key_Left;
193    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Page_Up] = Qt::Key_PageUp;
194    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Next] = Qt::Key_Right;
195    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Page_Down] = Qt::Key_PageDown;
196    _keyMap[osgGA::GUIEventAdapter::KEY_KP_End] = Qt::Key_End;
197
198    // _keyMap[osgGA::GUIEventAdapter::KEY_KP_Begin] = Qt::Key_Begin;
199    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Insert] = Qt::Key_Insert;
200    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Delete] = Qt::Key_Delete;
201    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Equal] = Qt::Key_Equal;
202    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Multiply] = Qt::Key_Asterisk;
203    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Add] = Qt::Key_Plus;
204    //_keyMap[osgGA::GUIEventAdapter::KEY_KP_Separator] = Qt::Key_;
205    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Subtract] = Qt::Key_Minus;
206    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Decimal] = Qt::Key_Period;
207    _keyMap[osgGA::GUIEventAdapter::KEY_KP_Divide] = Qt::Key_division;
208    _keyMap[osgGA::GUIEventAdapter::KEY_KP_0] = Qt::Key_0;
209    _keyMap[osgGA::GUIEventAdapter::KEY_KP_1] = Qt::Key_1;
210    _keyMap[osgGA::GUIEventAdapter::KEY_KP_2] = Qt::Key_2;
211    _keyMap[osgGA::GUIEventAdapter::KEY_KP_3] = Qt::Key_3;
212    _keyMap[osgGA::GUIEventAdapter::KEY_KP_4] = Qt::Key_4;
213    _keyMap[osgGA::GUIEventAdapter::KEY_KP_5] = Qt::Key_5;
214    _keyMap[osgGA::GUIEventAdapter::KEY_KP_6] = Qt::Key_6;
215    _keyMap[osgGA::GUIEventAdapter::KEY_KP_7] = Qt::Key_7;
216    _keyMap[osgGA::GUIEventAdapter::KEY_KP_8] = Qt::Key_8;
217    _keyMap[osgGA::GUIEventAdapter::KEY_KP_9] = Qt::Key_9;
218
219    _keyMap[osgGA::GUIEventAdapter::KEY_F1] = Qt::Key_F1;
220    _keyMap[osgGA::GUIEventAdapter::KEY_F2] = Qt::Key_F2;
221    _keyMap[osgGA::GUIEventAdapter::KEY_F3] = Qt::Key_F3;
222    _keyMap[osgGA::GUIEventAdapter::KEY_F4] = Qt::Key_F4;
223    _keyMap[osgGA::GUIEventAdapter::KEY_F5] = Qt::Key_F5;
224    _keyMap[osgGA::GUIEventAdapter::KEY_F6] = Qt::Key_F6;
225    _keyMap[osgGA::GUIEventAdapter::KEY_F7] = Qt::Key_F7;
226    _keyMap[osgGA::GUIEventAdapter::KEY_F8] = Qt::Key_F8;
227    _keyMap[osgGA::GUIEventAdapter::KEY_F9] = Qt::Key_F9;
228    _keyMap[osgGA::GUIEventAdapter::KEY_F10] = Qt::Key_F10;
229    _keyMap[osgGA::GUIEventAdapter::KEY_F11] = Qt::Key_F11;
230    _keyMap[osgGA::GUIEventAdapter::KEY_F12] = Qt::Key_F12;
231    _keyMap[osgGA::GUIEventAdapter::KEY_F13] = Qt::Key_F13;
232    _keyMap[osgGA::GUIEventAdapter::KEY_F14] = Qt::Key_F14;
233    _keyMap[osgGA::GUIEventAdapter::KEY_F15] = Qt::Key_F15;
234    _keyMap[osgGA::GUIEventAdapter::KEY_F16] = Qt::Key_F16;
235    _keyMap[osgGA::GUIEventAdapter::KEY_F17] = Qt::Key_F17;
236    _keyMap[osgGA::GUIEventAdapter::KEY_F18] = Qt::Key_F18;
237    _keyMap[osgGA::GUIEventAdapter::KEY_F19] = Qt::Key_F19;
238    _keyMap[osgGA::GUIEventAdapter::KEY_F20] = Qt::Key_F20;
239    _keyMap[osgGA::GUIEventAdapter::KEY_F21] = Qt::Key_F21;
240    _keyMap[osgGA::GUIEventAdapter::KEY_F22] = Qt::Key_F22;
241    _keyMap[osgGA::GUIEventAdapter::KEY_F23] = Qt::Key_F23;
242    _keyMap[osgGA::GUIEventAdapter::KEY_F24] = Qt::Key_F24;
243    _keyMap[osgGA::GUIEventAdapter::KEY_F25] = Qt::Key_F25;
244    _keyMap[osgGA::GUIEventAdapter::KEY_F26] = Qt::Key_F26;
245    _keyMap[osgGA::GUIEventAdapter::KEY_F27] = Qt::Key_F27;
246    _keyMap[osgGA::GUIEventAdapter::KEY_F28] = Qt::Key_F28;
247    _keyMap[osgGA::GUIEventAdapter::KEY_F29] = Qt::Key_F29;
248    _keyMap[osgGA::GUIEventAdapter::KEY_F30] = Qt::Key_F30;
249    _keyMap[osgGA::GUIEventAdapter::KEY_F31] = Qt::Key_F31;
250    _keyMap[osgGA::GUIEventAdapter::KEY_F32] = Qt::Key_F32;
251    _keyMap[osgGA::GUIEventAdapter::KEY_F33] = Qt::Key_F33;
252    _keyMap[osgGA::GUIEventAdapter::KEY_F34] = Qt::Key_F34;
253    _keyMap[osgGA::GUIEventAdapter::KEY_F35] = Qt::Key_F35;
254
255}
256
257bool QGraphicsViewAdapter::sendPointerEvent(int x, int y, int buttonMask)
258{
259    QCoreApplication::postEvent(this, new MyQPointerEvent(x,y,buttonMask));
260    return true;
261}
262
263bool QGraphicsViewAdapter::handlePointerEvent(int x, int y, int buttonMask)
264{
265    osg::notify(osg::INFO)<<"dispatchPointerEvent("<<x<<", "<<y<<", "<<buttonMask<<")"<<std::endl;
266
267    y = _graphicsScene->height()-y;
268
269    bool leftButtonPressed = (buttonMask & osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)!=0;
270    bool middleButtonPressed = (buttonMask & osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)!=0;
271    bool rightButtonPressed = (buttonMask & osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)!=0;
272
273    bool prev_leftButtonPressed = (_previousButtonMask & osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)!=0;
274    bool prev_middleButtonPressed = (_previousButtonMask & osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)!=0;
275    bool prev_rightButtonPressed = (_previousButtonMask & osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)!=0;
276
277    osg::notify(osg::INFO)<<"leftButtonPressed "<<leftButtonPressed<<std::endl;
278    osg::notify(osg::INFO)<<"middleButtonPressed "<<middleButtonPressed<<std::endl;
279    osg::notify(osg::INFO)<<"rightButtonPressed "<<rightButtonPressed<<std::endl;
280
281    Qt::MouseButtons qtMouseButtons =
282        (leftButtonPressed ? Qt::LeftButton : Qt::NoButton) |
283        (middleButtonPressed ? Qt::MidButton : Qt::NoButton) |
284        (rightButtonPressed ? Qt::RightButton : Qt::NoButton);
285
286    if (buttonMask != _previousButtonMask)
287    {
288
289        Qt::MouseButton qtButton = Qt::NoButton;
290        QEvent::Type eventType = QEvent::None;
291        if (leftButtonPressed != prev_leftButtonPressed)
292        {
293            qtButton = Qt::LeftButton;
294            eventType = leftButtonPressed ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease ;
295        }
296        else if (middleButtonPressed != prev_middleButtonPressed)
297        {
298            qtButton = Qt::MidButton;
299            eventType = middleButtonPressed ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease ;
300        }
301        else if (rightButtonPressed != prev_rightButtonPressed)
302        {
303            qtButton = Qt::RightButton;
304            eventType = rightButtonPressed ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease ;
305        }
306
307        if (eventType==QEvent::MouseButtonPress)
308        {
309            QWebViewImage* qwebViewImage = dynamic_cast<QWebViewImage*>(_image.get());
310            if (qwebViewImage) qwebViewImage->focusBrowser(true);
311        }
312
313        QMouseEvent event(eventType, QPoint(x, y), qtButton, qtMouseButtons, 0);
314        QCoreApplication::sendEvent(_graphicsView->viewport(), &event );
315
316        _previousButtonMask = buttonMask;
317    }
318    else if (x != _previousMouseX || y != _previousMouseY)
319    {
320
321        QMouseEvent event(QEvent::MouseMove, QPoint(x, y), Qt::NoButton, qtMouseButtons, 0);
322        QCoreApplication::sendEvent(_graphicsView->viewport(), &event);
323
324        _previousMouseX = x;
325        _previousMouseY = y;
326    }
327
328    return true;
329}
330
331bool QGraphicsViewAdapter::sendKeyEvent(int key, bool keyDown)
332{
333    QCoreApplication::postEvent(this, new MyQKeyEvent(key,keyDown));
334    return true;
335}
336
337bool QGraphicsViewAdapter::handleKeyEvent(int key, bool keyDown)
338{
339    QEvent::Type eventType = keyDown ? QEvent::KeyPress : QEvent::KeyRelease;
340
341    osg::notify(osg::INFO)<<"sendKeyEvent("<<key<<", "<<keyDown<<")"<<std::endl;
342
343    if (key==osgGA::GUIEventAdapter::KEY_Shift_L || key==osgGA::GUIEventAdapter::KEY_Shift_R)
344    {
345        _qtKeyModifiers = (_qtKeyModifiers & ~Qt::ShiftModifier) | (keyDown ? Qt::ShiftModifier : Qt::NoModifier);
346    }
347
348    if (key==osgGA::GUIEventAdapter::KEY_Control_L || key==osgGA::GUIEventAdapter::KEY_Control_R)
349    {
350        _qtKeyModifiers = (_qtKeyModifiers & ~Qt::ControlModifier) | (keyDown ? Qt::ControlModifier : Qt::NoModifier);
351    }
352
353    if (key==osgGA::GUIEventAdapter::KEY_Alt_L || key==osgGA::GUIEventAdapter::KEY_Alt_R)
354    {
355        _qtKeyModifiers = (_qtKeyModifiers & ~Qt::ControlModifier) | (keyDown ? Qt::ControlModifier : Qt::NoModifier);
356    }
357
358    if (key==osgGA::GUIEventAdapter::KEY_Meta_L || key==osgGA::GUIEventAdapter::KEY_Meta_R)
359    {
360        _qtKeyModifiers = (_qtKeyModifiers & ~Qt::MetaModifier) | (keyDown ? Qt::MetaModifier : Qt::NoModifier);
361    }
362
363    Qt::Key qtkey;
364    QChar input;
365
366    KeyMap::iterator itr = _keyMap.find(key);
367    if (itr != _keyMap.end())
368    {
369        qtkey = itr->second;
370    }
371    else
372    {
373        qtkey = (Qt::Key)key;
374        input = QChar(key);
375    }
376
377    QKeyEvent event(eventType, qtkey, _qtKeyModifiers, input);
378    QCoreApplication::sendEvent(_graphicsScene.data(), &event);
379    return true;
380}
381
382void QGraphicsViewAdapter::setFrameLastRendered(const osg::FrameStamp* frameStamp)
383{
384    osg::notify(osg::INFO)<<"setFrameLastRendered("<<frameStamp->getFrameNumber()<<")"<<std::endl;
385
386    if (_newImageAvailable && _previousFrameNumber!=frameStamp->getFrameNumber())
387    {
388        {
389            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_qimagesMutex);
390
391            // make sure that _previousFrameNumber hasn't been updated by another thread since we entered this branch.
392            if (_previousFrameNumber==frameStamp->getFrameNumber()) return;
393            _previousFrameNumber = frameStamp->getFrameNumber();
394
395            std::swap(_currentRead, _previousWrite);
396            _newImageAvailable = false;
397        }
398
399        assignImage(_currentRead);
400    }
401}
402
403void QGraphicsViewAdapter::clearWriteBuffer()
404{
405    QImage& image = _qimages[_currentWrite];
406    image.fill(_backgroundColor.rgba ());
407    image = QGLWidget::convertToGLFormat(image);
408
409    // swap the write buffers in a thread safe way
410    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_qimagesMutex);
411    std::swap(_currentWrite, _previousWrite);
412    _newImageAvailable = true;
413}
414
415void QGraphicsViewAdapter::render()
416{
417    osg::notify(osg::INFO)<<"Current write = "<<_currentWrite<<std::endl;
418    QImage& image = _qimages[_currentWrite];
419
420#if 1
421    // paint the image with the graphics view
422    QPainter painter(&image);
423    QRectF destinationRect(0, 0, image.width(), image.height());
424    QRect sourceRect(0, 0, _graphicsScene->width(), _graphicsScene->height());
425    _graphicsView->render(&painter, destinationRect, sourceRect);
426    painter.end();
427#else
428    // paint the image with the graphics view
429    QPixmap pixmap(image.width(), image.height());
430    QPainter painter(&pixmap);
431    QRectF destinationRect(0, 0, image.width(), image.height());
432    QRect sourceRect(0, 0, _graphicsScene->width(), _graphicsScene->height());
433    _graphicsView->render(&painter, destinationRect, sourceRect);
434    painter.end();
435
436    image = pixmap.toImage();
437#endif
438
439    // convert into OpenGL format - flipping around the Y axis and swizzling the pixels
440    image = QGLWidget::convertToGLFormat(image);
441
442    // swap the write buffers in a thread safe way
443    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_qimagesMutex);
444    std::swap(_currentWrite, _previousWrite);
445    _newImageAvailable = true;
446}
447
448void QGraphicsViewAdapter::assignImage(unsigned int i)
449{
450    QImage& image = _qimages[i];
451    unsigned char* data = image.bits();
452
453    osg::notify(osg::INFO)<<"assigImage("<<i<<") image = "<<&image<<" data = "<<(void*)data<<std::endl;
454
455    _image->setImage(image.width(),image.height(),1,
456                4,
457                GL_RGBA,GL_UNSIGNED_BYTE,
458                data,
459                osg::Image::NO_DELETE, 1);
460}
Note: See TracBrowser for help on using the browser.