QGraphicsViewAdapter 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef QGRAPHICSVIEWADAPTER
  14. #define QGRAPHICSVIEWADAPTER
  15. #include <QGLWidget>
  16. #include <osg/Image>
  17. #include <osg/observer_ptr>
  18. #include <osgQt/Export>
  19. #include <QPointer>
  20. #include <QGraphicsScene>
  21. #include <QGraphicsView>
  22. #include <QApplication>
  23. #include <QPainter>
  24. #include <QtEvents>
  25. namespace osgQt
  26. {
  27. extern OSGQT_EXPORT QCoreApplication* getOrCreateQApplication();
  28. class OSGQT_EXPORT QGraphicsViewAdapter : public QObject
  29. {
  30. Q_OBJECT
  31. public:
  32. QGraphicsViewAdapter(osg::Image* image, QWidget* widget);
  33. void setUpKeyMap();
  34. bool sendPointerEvent(int x, int y, int buttonMask);
  35. bool sendKeyEvent(int key, bool keyDown);
  36. void setFrameLastRendered(const osg::FrameStamp* frameStamp);
  37. void clearWriteBuffer();
  38. bool requiresRendering() const { return _requiresRendering; }
  39. void render();
  40. void assignImage(unsigned int i);
  41. void resize(int width, int height);
  42. void setBackgroundColor(QColor color) { _backgroundColor = color; }
  43. QColor getBackgroundColor() const { return _backgroundColor; }
  44. /** The 'background widget' will ignore mouse/keyboard events and let following handlers handle them
  45. It is mainly used for integrating scene graph and full-screen UIs
  46. */
  47. void setBackgroundWidget(QWidget* w) { _backgroundWidget = w; }
  48. QWidget* getBackgroundWidget() { return _backgroundWidget; }
  49. QGraphicsView* getQGraphicsView() { return _graphicsView; }
  50. QGraphicsScene* getQGraphicsScene() { return _graphicsScene; }
  51. protected:
  52. bool handlePointerEvent(int x, int y, int buttonMask);
  53. bool handleKeyEvent(int key, bool keyDown);
  54. QWidget* getWidgetAt(const QPoint& pos);
  55. osg::observer_ptr<osg::Image> _image;
  56. QWidget* _backgroundWidget;
  57. int _previousButtonMask;
  58. int _previousMouseX;
  59. int _previousMouseY;
  60. int _previousQtMouseX;
  61. int _previousQtMouseY;
  62. bool _previousSentEvent;
  63. bool _requiresRendering;
  64. int _width;
  65. int _height;
  66. typedef std::map<int, Qt::Key> KeyMap;
  67. KeyMap _keyMap;
  68. Qt::KeyboardModifiers _qtKeyModifiers;
  69. QColor _backgroundColor;
  70. QPointer<QGraphicsView> _graphicsView;
  71. QPointer<QGraphicsScene> _graphicsScene;
  72. QPointer<QWidget> _widget;
  73. OpenThreads::Mutex _qimagesMutex;
  74. OpenThreads::Mutex _qresizeMutex;
  75. unsigned int _previousFrameNumber;
  76. bool _newImageAvailable;
  77. unsigned int _currentRead;
  78. unsigned int _currentWrite;
  79. unsigned int _previousWrite;
  80. QImage _qimages[3];
  81. virtual void customEvent ( QEvent * event ) ;
  82. private slots:
  83. void repaintRequestedSlot(const QList<QRectF> &regions);
  84. void repaintRequestedSlot(const QRectF &region);
  85. };
  86. }
  87. #endif