OpenFrames
QtOSGAdapters.hpp
Go to the documentation of this file.
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 
18 #ifndef _OF_QTOSGADAPTERS_
19 #define _OF_QTOSGADAPTERS_
20 
21 // OpenFrames and OSG headers
22 #include <OpenFrames/Export.h>
23 #include <osg/Image>
24 #include <osg/observer_ptr>
25 
26 // Qt headers
27 #include <QObject>
28 #include <QPointer>
29 #include <QWidget> // QPointer does not allow forward decl
30 #include <QGraphicsScene> // QPointer does not allow forward decl
31 #include <QGraphicsView> // QPointer does not allow forward decl
32 #include <QColor>
33 #include <QImage>
34 
35 // Forward
36 QT_FORWARD_DECLARE_CLASS(QCoreApplication)
37 QT_FORWARD_DECLARE_CLASS(QPainter)
38 QT_FORWARD_DECLARE_CLASS(QtEvents)
39 
40 namespace OpenFrames
41 {
42 
43  extern QCoreApplication* getOrCreateQApplication();
44 
45  class OF_EXPORT QGraphicsViewAdapter : public QObject
46  {
47  Q_OBJECT
48 
49  public:
50  QGraphicsViewAdapter(osg::Image* image, QWidget* widget);
51 
52  void setUpKeyMap();
53  bool sendPointerEvent(int x, int y, int buttonMask);
54  bool sendKeyEvent(int key, bool keyDown);
55 
56  void setFrameLastRendered(const osg::FrameStamp* frameStamp);
57  void clearWriteBuffer();
58  void render();
59  void assignImage(unsigned int i);
60 
61  void resize(int width, int height);
62  void setBackgroundColor(QColor color) { _backgroundColor = color; }
63  QColor getBackgroundColor() const { return _backgroundColor; }
64 
67  void setIgnoredWidgets(const std::vector<QWidget*> &w) { _ignoredWidgets = w; }
68  const std::vector<QWidget*> &getIgnoredWidgets() const { return _ignoredWidgets; }
69 
70  QGraphicsView* getQGraphicsView() { return _graphicsView.data(); }
71  QGraphicsScene* getQGraphicsScene() { return _graphicsScene.data(); }
72 
73  protected:
74  bool handlePointerEvent(int x, int y, int buttonMask);
75  bool handleKeyEvent(int key, bool keyDown);
76  QWidget* getWidgetAt(const QPoint& pos);
77 
78  osg::observer_ptr<osg::Image> _image;
79  std::vector<QWidget*> _ignoredWidgets;
80 
81  int _previousButtonMask;
82  int _previousMouseX;
83  int _previousMouseY;
84  int _previousQtMouseX;
85  int _previousQtMouseY;
86  bool _previousSentEvent;
87 
88  int _width;
89  int _height;
90 
91  typedef std::map<int, Qt::Key> KeyMap;
92  KeyMap _keyMap;
93  Qt::KeyboardModifiers _qtKeyModifiers;
94 
95  QColor _backgroundColor;
96  QPointer<QGraphicsView> _graphicsView;
97  QPointer<QGraphicsScene> _graphicsScene;
98  QPointer<QWidget> _widget;
99 
100  OpenThreads::Mutex _qimagesMutex;
101  OpenThreads::Mutex _qresizeMutex;
102  unsigned int _previousFrameNumber;
103  bool _newImageAvailable;
104  unsigned int _currentRead;
105  unsigned int _currentWrite;
106  unsigned int _previousWrite;
107  QImage _qimages[3];
108 
109  virtual void customEvent(QEvent * event);
110 
111  private slots:
112  void repaintRequestedSlot(const QList<QRectF> &regions);
113  void repaintRequestedSlot(const QRectF &region);
114  };
115 
116  class QWidgetImage : public osg::Image
117  {
118  public:
119  QWidgetImage(QWidget* widget = 0);
120 
121  QWidget* getQWidget() { return _widget.data(); }
122  QGraphicsViewAdapter* getQGraphicsViewAdapter() { return _adapter; }
123 
124  virtual bool requiresUpdateCall() const { return true; }
125  virtual void update(osg::NodeVisitor* /*nv*/) { render(); }
126  void clearWriteBuffer();
127  void render();
128 
131  virtual void scaleImage(int s, int t, int r, GLenum newDataType);
132 
133  virtual bool sendFocusHint(bool focus);
134  virtual bool sendPointerEvent(int x, int y, int buttonMask);
135  virtual bool sendKeyEvent(int key, bool keyDown);
136 
137  virtual void setFrameLastRendered(const osg::FrameStamp* frameStamp);
138 
139  protected:
140  QPointer<QGraphicsViewAdapter> _adapter;
141  QPointer<QWidget> _widget;
142  };
143 
144 } // !namespace OpenFrames
145 
146 #endif
void setIgnoredWidgets(const std::vector< QWidget *> &w)
Definition: QtOSGAdapters.hpp:67
virtual void scaleImage(int s, int t, int r, GLenum newDataType)
Definition: QtOSGAdapters.hpp:116
Definition: CoordinateAxes.hpp:29
Definition: QtOSGAdapters.hpp:45