| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_QTOPENGL_GLWIDGET_H |
| 7 |
#define AVOGADRO_QTOPENGL_GLWIDGET_H |
| 8 |
|
| 9 |
#include "avogadroqtopenglexport.h" |
| 10 |
|
| 11 |
#include <avogadro/qtgui/scenepluginmodel.h> |
| 12 |
#include <avogadro/qtgui/toolplugin.h> |
| 13 |
#include <avogadro/rendering/glrenderer.h> |
| 14 |
|
| 15 |
#include <QPointer> |
| 16 |
|
| 17 |
#if QT_VERSION >= 0x060000 |
| 18 |
#include <QtOpenGLWidgets/QOpenGLWidget> |
| 19 |
#else |
| 20 |
#include <QOpenGLWidget> |
| 21 |
#endif |
| 22 |
|
| 23 |
class QTimer; |
| 24 |
|
| 25 |
namespace Avogadro { |
| 26 |
|
| 27 |
namespace QtGui { |
| 28 |
class Molecule; |
| 29 |
} |
| 30 |
|
| 31 |
namespace QtOpenGL { |
| 32 |
|
| 33 |
/** |
| 34 |
* @class GLWidget glwidget.h <avogadro/qtopengl/glwidget.h> |
| 35 |
* @brief QOpenGLGLWidget derived object for displaying 3D molecular geometry. |
| 36 |
* @author Marcus D. Hanwell |
| 37 |
* |
| 38 |
* This class creates the GL context, and contains a renderer to render the |
| 39 |
* 3D molecular geometry. |
| 40 |
* |
| 41 |
* The GLWidget also manages a collection of ToolPlugins that are used to |
| 42 |
* respond to user input events. Use setTools() or addTool() to add tools to the |
| 43 |
* widget. Use setActiveTool() to indicate which tool is active. The active tool |
| 44 |
* will be given the opportunity to handle input events first. If the active |
| 45 |
* tool does not handle the event, the default tool will be used. If the default |
| 46 |
* tool also ignores the event, it will be passed to QOpenGLWidget's handlers. |
| 47 |
*/ |
| 48 |
|
| 49 |
class AVOGADROQTOPENGL_EXPORT GLWidget : public QOpenGLWidget |
| 50 |
{ |
| 51 |
Q_OBJECT |
| 52 |
|
| 53 |
public: |
| 54 |
explicit GLWidget(QWidget* parent = nullptr); |
| 55 |
~GLWidget() override; |
| 56 |
|
| 57 |
/** Set the molecule the widget will render. */ |
| 58 |
void setMolecule(QtGui::Molecule* molecule); |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the molecule being rendered by the widget. |
| 62 |
* @{ |
| 63 |
*/ |
| 64 |
QtGui::Molecule* molecule(); |
| 65 |
const QtGui::Molecule* molecule() const; |
| 66 |
/** @}*/ |
| 67 |
|
| 68 |
/** Get a reference to the renderer for the widget. */ |
| 69 |
Rendering::GLRenderer& renderer() { return m_renderer; } |
| 70 |
|
| 71 |
/** |
| 72 |
* @return A list of the ToolPlugins owned by the GLWidget. |
| 73 |
*/ |
| 74 |
QList<QtGui::ToolPlugin*> tools() const { return m_tools; } |
| 75 |
|
| 76 |
/** |
| 77 |
* @return The active tool. |
| 78 |
*/ |
| 79 |
QtGui::ToolPlugin* activeTool() const { return m_activeTool; } |
| 80 |
|
| 81 |
/** |
| 82 |
* @return The default tool. |
| 83 |
*/ |
| 84 |
QtGui::ToolPlugin* defaultTool() const { return m_defaultTool; } |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the GLWidget's ScenePluginModel, used to add, delete and modify the |
| 88 |
* scene plugin items. |
| 89 |
* @{ |
| 90 |
*/ |
| 91 |
QtGui::ScenePluginModel& sceneModel() { return m_scenePlugins; } |
| 92 |
const QtGui::ScenePluginModel& sceneModel() const { return m_scenePlugins; } |
| 93 |
/** @}*/ |
| 94 |
|
| 95 |
/** |
| 96 |
* Check if the GLWidget was able to acquire a context, and set up the |
| 97 |
* renderer correctly. If not valid, the error method may provide more |
| 98 |
* information. |
| 99 |
* @return true if value, false if not. |
| 100 |
*/ |
| 101 |
bool isValid() const { return m_renderer.isValid(); } |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the error(s), if any, encountered when setting up the GLWidget. |
| 105 |
* @return A free form string containing errors encountered. |
| 106 |
*/ |
| 107 |
QString error() const { return m_renderer.error().c_str(); } |
| 108 |
|
| 109 |
signals: |
| 110 |
void rendererInvalid(); |
| 111 |
|
| 112 |
public slots: |
| 113 |
/** |
| 114 |
* Update the scene plugins for the widget, this will generate geometry in |
| 115 |
* the scene etc. |
| 116 |
*/ |
| 117 |
void updateScene(); |
| 118 |
|
| 119 |
/** |
| 120 |
* Request update of molecule properties (e.g., dipole moment) |
| 121 |
*/ |
| 122 |
void updateMolecule(); |
| 123 |
|
| 124 |
/** |
| 125 |
* Clear the contents of the scene. |
| 126 |
*/ |
| 127 |
void clearScene(); |
| 128 |
|
| 129 |
/** Reset the view to fit the entire scene. */ |
| 130 |
void resetCamera(); |
| 131 |
|
| 132 |
/** Reset the geometry when the molecule etc changes. */ |
| 133 |
void resetGeometry(); |
| 134 |
|
| 135 |
/** |
| 136 |
* Make the tools in toolList available to the GLWidget. The GLWidget takes |
| 137 |
* ownership of the tools. |
| 138 |
*/ |
| 139 |
void setTools(const QList<QtGui::ToolPlugin*>& toolList); |
| 140 |
|
| 141 |
/** |
| 142 |
* Make tool available to the GLWidget. The GLWidget takes ownership of the |
| 143 |
* tool. |
| 144 |
*/ |
| 145 |
void addTool(QtGui::ToolPlugin* tool); |
| 146 |
|
| 147 |
/** |
| 148 |
* Set the active tool. This is the tool that will be used to handle input |
| 149 |
* events first. |
| 150 |
* @{ |
| 151 |
*/ |
| 152 |
void setActiveTool(const QString& name); |
| 153 |
void setActiveTool(QtGui::ToolPlugin* tool); |
| 154 |
/**@}*/ |
| 155 |
|
| 156 |
/** |
| 157 |
* Set the default tool. This is the tool that will be used to handle input |
| 158 |
* events that are ignored by the active tool. |
| 159 |
* @{ |
| 160 |
*/ |
| 161 |
void setDefaultTool(const QString& name); |
| 162 |
void setDefaultTool(QtGui::ToolPlugin* tool); |
| 163 |
/**@}*/ |
| 164 |
|
| 165 |
/** |
| 166 |
* Request an update, this will by default initiate a timer that will trigger |
| 167 |
* in a specified time, enabling us to compress multiple events such as |
| 168 |
* camera moves to maintain interactivity. |
| 169 |
*/ |
| 170 |
void requestUpdate(); |
| 171 |
|
| 172 |
protected slots: |
| 173 |
/** |
| 174 |
* Perform the update of the render, this should only be called by the timer. |
| 175 |
*/ |
| 176 |
void updateTimeout(); |
| 177 |
|
| 178 |
protected: |
| 179 |
/** This is where the GL context is initialized. */ |
| 180 |
void initializeGL() override; |
| 181 |
|
| 182 |
/** Take care of resizing the context. */ |
| 183 |
void resizeGL(int width, int height) override; |
| 184 |
|
| 185 |
/** Main entry point for all GL rendering. */ |
| 186 |
void paintGL() override; |
| 187 |
|
| 188 |
/** Reimplemented from QOpenGLWidget @{ */ |
| 189 |
void mouseDoubleClickEvent(QMouseEvent*) override; |
| 190 |
void mousePressEvent(QMouseEvent*) override; |
| 191 |
void mouseMoveEvent(QMouseEvent*) override; |
| 192 |
void mouseReleaseEvent(QMouseEvent*) override; |
| 193 |
void wheelEvent(QWheelEvent*) override; |
| 194 |
void keyPressEvent(QKeyEvent*) override; |
| 195 |
void keyReleaseEvent(QKeyEvent*) override; |
| 196 |
/** @} */ |
| 197 |
|
| 198 |
private: |
| 199 |
QPointer<QtGui::Molecule> m_molecule; |
| 200 |
QList<QtGui::ToolPlugin*> m_tools; |
| 201 |
QtGui::ToolPlugin* m_activeTool; |
| 202 |
QtGui::ToolPlugin* m_defaultTool; |
| 203 |
Rendering::GLRenderer m_renderer; |
| 204 |
QtGui::ScenePluginModel m_scenePlugins; |
| 205 |
|
| 206 |
QTimer* m_renderTimer; |
| 207 |
}; |
| 208 |
|
| 209 |
} // namespace QtOpenGL |
| 210 |
} // namespace Avogadro |
| 211 |
|
| 212 |
#endif // AVOGADRO_QTOPENGL_GLWIDGET_H |