Gazebo Gui

API Reference

6.9.0
MinimalScene.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef GZ_GUI_PLUGINS_MINIMALSCENE_HH_
19 #define GZ_GUI_PLUGINS_MINIMALSCENE_HH_
20 
21 #include <string>
22 #include <memory>
23 
24 #include <gz/common/KeyEvent.hh>
25 #include <gz/common/MouseEvent.hh>
26 #include <gz/math/Color.hh>
27 #include <gz/math/Pose3.hh>
28 #include <gz/math/Vector2.hh>
29 #include <gz/utils/ImplPtr.hh>
30 
31 #include "gz/gui/Plugin.hh"
32 
33 namespace ignition
34 {
35 namespace gui
36 {
37 namespace plugins
38 {
71  class MinimalScene : public Plugin
72  {
73  Q_OBJECT
74 
76  Q_PROPERTY(
78  READ LoadingError
79  WRITE SetLoadingError
80  NOTIFY LoadingErrorChanged
81  )
82 
83 
84  public: MinimalScene();
85 
89  public slots: void OnHovered(int _mouseX, int _mouseY);
90 
93  public slots: void OnFocusWindow();
94 
99  public slots: void OnDropped(const QString &_drop,
100  int _mouseX, int _mouseY);
101 
102  // Documentation inherited
103  public: virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem)
104  override;
105 
108  public: Q_INVOKABLE QString LoadingError() const;
109 
112  public: Q_INVOKABLE void SetLoadingError(const QString &_loadingError);
113 
115  signals: void LoadingErrorChanged();
116 
118  public: QString loadingError;
119 
122  IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
123  };
124 
125  class RenderSync;
126 
134  {
136  public: IgnRenderer();
137 
140  public: void Render(RenderSync *_renderSync);
141 
145  public: std::string Initialize();
146 
148  public: void Destroy();
149 
152  public: void NewMouseEvent(const common::MouseEvent &_e);
153 
156  public: void NewHoverEvent(const math::Vector2i &_hoverPos);
157 
161  public: void NewDropEvent(const std::string &_dropText,
162  const math::Vector2i &_dropPos);
163 
166  public: void HandleKeyPress(const common::KeyEvent &_e);
167 
170  public: void HandleKeyRelease(const common::KeyEvent &_e);
171 
173  private: void HandleMouseEvent();
174 
176  private: void BroadcastHoverPos();
177 
179  private: void BroadcastDrag();
180 
182  private: void BroadcastLeftClick();
183 
185  private: void BroadcastRightClick();
186 
188  private: void BroadcastMousePress();
189 
191  private: void BroadcastScroll();
192 
194  private: void BroadcastKeyRelease();
195 
197  private: void BroadcastDrop();
198 
200  private: void BroadcastKeyPress();
201 
206  private: math::Vector3d ScreenToScene(const math::Vector2i &_screenPos)
207  const;
208 
212  public: GLuint textureId;
213 
215  public: std::string engineName = "ogre";
216 
218  public: std::string sceneName = "scene";
219 
221  public: math::Pose3d cameraPose = math::Pose3d(0, 0, 2, 0, 0.4, 0);
222 
224  public: double cameraNearClip = 0.01;
225 
227  public: double cameraFarClip = 1000.0;
228 
230  public: unsigned int cameraAntiAliasing = 8;
231 
233  public: math::Color backgroundColor = math::Color::Black;
234 
236  public: math::Color ambientLight = math::Color(0.3f, 0.3f, 0.3f, 1.0f);
237 
239  public: bool initialized = false;
240 
242  public: QSize textureSize = QSize(1024, 1024);
243 
245  public: bool textureDirty = false;
246 
248  public: bool skyEnable = false;
249 
251  public: math::Angle cameraHFOV = math::Angle(M_PI * 0.5);
252 
254  public: std::string cameraViewController{""};
255 
258  IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
259  };
260 
262  class RenderThread : public QThread
263  {
264  Q_OBJECT
265 
267  public: RenderThread();
268 
272  public slots: void RenderNext(RenderSync *_renderSync);
273 
275  public slots: void ShutDown();
276 
278  public slots: void SizeChanged();
279 
284  signals: void TextureReady(uint _id, const QSize &_size);
285 
288  public: void SetErrorCb(std::function<void(const QString &)> _cb);
289 
291  public: std::function<void(const QString &)> errorCb;
292 
294  public: QOffscreenSurface *surface = nullptr;
295 
297  public: QOpenGLContext *context = nullptr;
298 
301  };
302 
304  class RenderWindowItem : public QQuickItem
305  {
306  Q_OBJECT
307 
310  public: explicit RenderWindowItem(QQuickItem *_parent = nullptr);
311 
312  public: ~RenderWindowItem();
313 
316  public: void SetBackgroundColor(const math::Color &_color);
317 
320  public: void SetAmbientLight(const math::Color &_ambient);
321 
324  public: void SetEngineName(const std::string &_name);
325 
328  public: void SetSceneName(const std::string &_name);
329 
332  public: void SetCameraPose(const math::Pose3d &_pose);
333 
336  public: void SetCameraNearClip(double _near);
337 
340  public: void SetCameraFarClip(double _far);
341 
344  public: void SetAntiAliasing(unsigned int _aa);
345 
349  public: void OnHovered(const gz::math::Vector2i &_hoverPos);
350 
354  public: void OnDropped(const QString &_drop,
355  const gz::math::Vector2i &_dropPos);
356 
359  public: void SetSkyEnabled(const bool &_sky);
360 
363  public: void SetCameraHFOV(const math::Angle &_fov);
364 
367  public: void SetCameraViewController(const std::string &_view_controller);
368 
370  public Q_SLOTS: void Ready();
371 
374  public: void HandleKeyPress(const common::KeyEvent &_e);
375 
378  public: void HandleKeyRelease(const common::KeyEvent &_e);
379 
382  public: void SetErrorCb(std::function<void(const QString &)> _cb);
383 
385  public: void StopRendering();
386 
387  // Documentation inherited
388  protected: virtual void mousePressEvent(QMouseEvent *_e) override;
389 
390  // Documentation inherited
391  protected: virtual void mouseReleaseEvent(QMouseEvent *_e) override;
392 
393  // Documentation inherited
394  protected: virtual void mouseMoveEvent(QMouseEvent *_e) override;
395 
396  // Documentation inherited
397  protected: virtual void keyPressEvent(QKeyEvent *_e) override;
398 
399  // Documentation inherited
400  protected: virtual void keyReleaseEvent(QKeyEvent *_e) override;
401 
402  // Documentation inherited
403  protected: virtual void wheelEvent(QWheelEvent *_e) override;
404 
411  private: QSGNode *updatePaintNode(QSGNode *_oldNode,
412  QQuickItem::UpdatePaintNodeData *_data) override;
413 
416  IGN_UTILS_UNIQUE_IMPL_PTR(dataPtr)
417  };
418 
420  class TextureNode : public QObject, public QSGSimpleTextureNode
421  {
422  Q_OBJECT
423 
428  public: explicit TextureNode(QQuickWindow *_window,
429  RenderSync &_renderSync);
430 
432  public: ~TextureNode() override;
433 
438  public slots: void NewTexture(uint _id, const QSize &_size);
439 
442  public slots: void PrepareNode();
443 
445  signals: void TextureInUse(RenderSync *_renderSync);
446 
449  signals: void PendingNewTexture();
450 
452  public: uint id = 0;
453 
455  public: QSize size = QSize(0, 0);
456 
458  public: QMutex mutex;
459 
461  public: RenderSync &renderSync;
462 
464  public: QSGTexture *texture = nullptr;
465 
467  public: QQuickWindow *window = nullptr;
468  };
469 }
470 }
471 }
472 
473 #endif
Q_INVOKABLE void SetLoadingError(const QString &_loadingError)
Set the loading error message.
IgnRenderer ignRenderer
Ign-rendering renderer.
Definition: MinimalScene.hh:300
Creates a Gazebo rendering scene and user camera. It is possible to orbit the camera around the scene...
Definition: MinimalScene.hh:71
STL class.
std::function< void(const QString &)> errorCb
Function to be called if there are errors.
Definition: MinimalScene.hh:291
void OnFocusWindow()
Callback when the mouse enters the render window to focus the window for mouse/key events.
GLuint textureId
Render texture id.
Definition: MinimalScene.hh:212
void OnHovered(int _mouseX, int _mouseY)
Callback when the mouse hovers to a new position.
static const Color Black
Pose3< double > Pose3d
Texture node for displaying the render texture from gz-renderer.
Definition: MinimalScene.hh:420
Base class for Gazebo GUI plugins.
Definition: gz/gui/Plugin.hh:54
Q_INVOKABLE QString LoadingError() const
Get the loading error string.
RenderSync & renderSync
See RenderSync.
Definition: MinimalScene.hh:461
void OnDropped(const QString &_drop, int _mouseX, int _mouseY)
Callback when receives a drop event.
QString loadingError
Loading error message.
Definition: MinimalScene.hh:81
QMutex mutex
Mutex to protect the texture variables.
Definition: MinimalScene.hh:458
Rendering thread.
Definition: MinimalScene.hh:262
A QQUickItem that manages the render window.
Definition: MinimalScene.hh:304
Ign-rendering renderer. All ign-rendering calls should be performed inside this class as it makes sur...
Definition: MinimalScene.hh:133
Definition: gz/gui/Application.hh:35
virtual void LoadConfig(const tinyxml2::XMLElement *_pluginElem) override
Load the plugin with a configuration file. Override this on custom plugins to handle custom configura...
void LoadingErrorChanged()
Notify that loading error has changed.