| 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 |
#include "glwidget.h" |
| 7 |
|
| 8 |
#include "qttextrenderstrategy.h" |
| 9 |
|
| 10 |
#include <avogadro/qtgui/molecule.h> |
| 11 |
#include <avogadro/qtgui/rwmolecule.h> |
| 12 |
#include <avogadro/qtgui/sceneplugin.h> |
| 13 |
#include <avogadro/qtgui/scenepluginmodel.h> |
| 14 |
#include <avogadro/qtgui/toolplugin.h> |
| 15 |
|
| 16 |
#include <avogadro/rendering/camera.h> |
| 17 |
|
| 18 |
#include <QAction> |
| 19 |
#include <QtCore/QTimer> |
| 20 |
#include <QtGui/QKeyEvent> |
| 21 |
#include <QtGui/QMouseEvent> |
| 22 |
#include <QtGui/QWheelEvent> |
| 23 |
#include <QtGui/QWindow> |
| 24 |
#include <QtWidgets/QApplication> |
| 25 |
|
| 26 |
namespace Avogadro::QtOpenGL { |
| 27 |
|
| 28 |
GLWidget::GLWidget(QWidget* p) |
| 29 |
: QOpenGLWidget(p), m_activeTool(nullptr), m_defaultTool(nullptr), |
| 30 |
m_renderTimer(nullptr) |
| 31 |
{ |
| 32 |
setFocusPolicy(Qt::ClickFocus); |
| 33 |
connect(&m_scenePlugins, &QtGui::ScenePluginModel::pluginStateChanged, this, |
| 34 |
&GLWidget::updateScene); |
| 35 |
connect(&m_scenePlugins, &QtGui::ScenePluginModel::pluginConfigChanged, this, |
| 36 |
&GLWidget::updateScene); |
| 37 |
m_renderer.setTextRenderStrategy(new QtTextRenderStrategy); |
| 38 |
} |
| 39 |
|
| 40 |
GLWidget::~GLWidget() {} |
| 41 |
|
| 42 |
void GLWidget::setMolecule(QtGui::Molecule* mol) |
| 43 |
{ |
| 44 |
clearScene(); |
| 45 |
if (m_molecule) |
| 46 |
disconnect(m_molecule, nullptr, nullptr, nullptr); |
| 47 |
m_molecule = mol; |
| 48 |
foreach (QtGui::ToolPlugin* tool, m_tools) |
| 49 |
tool->setMolecule(m_molecule); |
| 50 |
|
| 51 |
if (m_molecule != nullptr) { |
| 52 |
// update properties like dipole rendering |
| 53 |
QTimer::singleShot(500, m_molecule, &QtGui::Molecule::update); |
| 54 |
} |
| 55 |
|
| 56 |
connect(m_molecule, &QtGui::Molecule::changed, this, &GLWidget::updateScene); |
| 57 |
} |
| 58 |
|
| 59 |
QtGui::Molecule* GLWidget::molecule() |
| 60 |
{ |
| 61 |
return m_molecule; |
| 62 |
} |
| 63 |
|
| 64 |
const QtGui::Molecule* GLWidget::molecule() const |
| 65 |
{ |
| 66 |
return m_molecule; |
| 67 |
} |
| 68 |
|
| 69 |
void GLWidget::updateMolecule() |
| 70 |
{ |
| 71 |
if (m_molecule != nullptr) { |
| 72 |
// update properties like dipole rendering |
| 73 |
QTimer::singleShot(500, m_molecule, &QtGui::Molecule::update); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
void GLWidget::updateScene() |
| 78 |
{ |
| 79 |
// Build up the scene with the scene plugins, creating the appropriate nodes. |
| 80 |
QtGui::Molecule* mol = m_molecule; |
| 81 |
if (!mol) |
| 82 |
mol = new QtGui::Molecule(this); |
| 83 |
if (mol) { |
| 84 |
Rendering::GroupNode& node = m_renderer.scene().rootNode(); |
| 85 |
node.clear(); |
| 86 |
auto* moleculeNode = new Rendering::GroupNode(&node); |
| 87 |
|
| 88 |
foreach (QtGui::ScenePlugin* scenePlugin, |
| 89 |
m_scenePlugins.activeScenePlugins()) { |
| 90 |
auto* engineNode = new Rendering::GroupNode(moleculeNode); |
| 91 |
scenePlugin->process(*mol, *engineNode); |
| 92 |
} |
| 93 |
|
| 94 |
// Let the tools perform any drawing they need to do. |
| 95 |
foreach (QtGui::ToolPlugin* tool, m_tools) { |
| 96 |
auto* toolNode = new Rendering::GroupNode(moleculeNode); |
| 97 |
tool->draw(*toolNode); |
| 98 |
} |
| 99 |
|
| 100 |
m_renderer.resetGeometry(); |
| 101 |
update(); |
| 102 |
} |
| 103 |
if (mol != m_molecule) |
| 104 |
delete mol; |
| 105 |
} |
| 106 |
|
| 107 |
void GLWidget::clearScene() |
| 108 |
{ |
| 109 |
m_renderer.scene().clear(); |
| 110 |
} |
| 111 |
|
| 112 |
void GLWidget::resetCamera() |
| 113 |
{ |
| 114 |
m_renderer.resetCamera(); |
| 115 |
update(); |
| 116 |
} |
| 117 |
|
| 118 |
void GLWidget::resetGeometry() |
| 119 |
{ |
| 120 |
m_renderer.resetGeometry(); |
| 121 |
} |
| 122 |
|
| 123 |
void GLWidget::setTools(const QList<QtGui::ToolPlugin*>& toolList) |
| 124 |
{ |
| 125 |
foreach (QtGui::ToolPlugin* tool, toolList) |
| 126 |
addTool(tool); |
| 127 |
} |
| 128 |
|
| 129 |
void GLWidget::addTool(QtGui::ToolPlugin* tool) |
| 130 |
{ |
| 131 |
if (m_tools.contains(tool)) |
| 132 |
return; |
| 133 |
|
| 134 |
connect(tool, &QtGui::ToolPlugin::updateRequested, this, |
| 135 |
&GLWidget::requestUpdate); |
| 136 |
tool->setParent(this); |
| 137 |
tool->setGLWidget(this); |
| 138 |
tool->setActiveWidget(this); |
| 139 |
tool->setMolecule(m_molecule); |
| 140 |
tool->setGLRenderer(&m_renderer); |
| 141 |
m_tools << tool; |
| 142 |
} |
| 143 |
|
| 144 |
void GLWidget::setActiveTool(const QString& name) |
| 145 |
{ |
| 146 |
foreach (QtGui::ToolPlugin* tool, m_tools) { |
| 147 |
QAction* toolAction = tool->activateAction(); |
| 148 |
if (tool->objectName() == name || |
| 149 |
(toolAction && toolAction->text() == name)) { |
| 150 |
setActiveTool(tool); |
| 151 |
return; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
void GLWidget::setActiveTool(QtGui::ToolPlugin* tool) |
| 157 |
{ |
| 158 |
if (tool == m_activeTool) |
| 159 |
return; |
| 160 |
|
| 161 |
if (m_activeTool && m_activeTool != m_defaultTool) { |
| 162 |
disconnect(m_activeTool, &QtGui::ToolPlugin::drawablesChanged, this, |
| 163 |
&GLWidget::updateScene); |
| 164 |
} |
| 165 |
|
| 166 |
if (tool) |
| 167 |
addTool(tool); |
| 168 |
m_activeTool = tool; |
| 169 |
|
| 170 |
if (m_activeTool && m_activeTool != m_defaultTool) { |
| 171 |
connect(m_activeTool, &QtGui::ToolPlugin::drawablesChanged, this, |
| 172 |
&GLWidget::updateScene); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void GLWidget::setDefaultTool(const QString& name) |
| 177 |
{ |
| 178 |
foreach (QtGui::ToolPlugin* tool, m_tools) { |
| 179 |
QAction* toolAction = tool->activateAction(); |
| 180 |
|
| 181 |
if (tool->objectName() == name || tool->name() == name || |
| 182 |
(toolAction && toolAction->text() == name)) { |
| 183 |
setDefaultTool(tool); |
| 184 |
return; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void GLWidget::setDefaultTool(QtGui::ToolPlugin* tool) |
| 190 |
{ |
| 191 |
if (tool == m_defaultTool) |
| 192 |
return; |
| 193 |
|
| 194 |
if (m_defaultTool && m_activeTool != m_defaultTool) { |
| 195 |
disconnect(m_defaultTool, &QtGui::ToolPlugin::drawablesChanged, this, |
| 196 |
&GLWidget::updateScene); |
| 197 |
} |
| 198 |
|
| 199 |
if (tool) |
| 200 |
addTool(tool); |
| 201 |
m_defaultTool = tool; |
| 202 |
|
| 203 |
if (m_defaultTool && m_activeTool != m_defaultTool) { |
| 204 |
connect(m_defaultTool, &QtGui::ToolPlugin::drawablesChanged, this, |
| 205 |
&GLWidget::updateScene); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void GLWidget::requestUpdate() |
| 210 |
{ |
| 211 |
if (!m_renderTimer) { |
| 212 |
m_renderTimer = new QTimer(this); |
| 213 |
connect(m_renderTimer, &QTimer::timeout, this, &GLWidget::updateTimeout); |
| 214 |
m_renderTimer->setSingleShot(1000 / 30); // 30 fps |
| 215 |
m_renderTimer->start(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
void GLWidget::updateTimeout() |
| 220 |
{ |
| 221 |
if (m_renderTimer) { |
| 222 |
m_renderTimer->deleteLater(); |
| 223 |
m_renderTimer = nullptr; |
| 224 |
} |
| 225 |
update(); |
| 226 |
} |
| 227 |
|
| 228 |
void GLWidget::initializeGL() |
| 229 |
{ |
| 230 |
m_renderer.initialize(); |
| 231 |
if (!m_renderer.isValid()) |
| 232 |
emit rendererInvalid(); |
| 233 |
} |
| 234 |
|
| 235 |
void GLWidget::resizeGL(int width_, int height_) |
| 236 |
{ |
| 237 |
float pixelRatio = window()->windowHandle()->devicePixelRatio(); |
| 238 |
m_renderer.setPixelRatio(pixelRatio); |
| 239 |
m_renderer.resize(width_, height_); |
| 240 |
} |
| 241 |
|
| 242 |
void GLWidget::paintGL() |
| 243 |
{ |
| 244 |
m_renderer.render(); |
| 245 |
} |
| 246 |
|
| 247 |
void GLWidget::mouseDoubleClickEvent(QMouseEvent* e) |
| 248 |
{ |
| 249 |
e->ignore(); |
| 250 |
|
| 251 |
if (m_activeTool) |
| 252 |
m_activeTool->mouseDoubleClickEvent(e); |
| 253 |
|
| 254 |
if (m_defaultTool && !e->isAccepted()) |
| 255 |
m_defaultTool->mouseDoubleClickEvent(e); |
| 256 |
|
| 257 |
if (!e->isAccepted()) |
| 258 |
QOpenGLWidget::mouseDoubleClickEvent(e); |
| 259 |
} |
| 260 |
|
| 261 |
void GLWidget::mousePressEvent(QMouseEvent* e) |
| 262 |
{ |
| 263 |
e->ignore(); |
| 264 |
|
| 265 |
if (m_activeTool) |
| 266 |
m_activeTool->mousePressEvent(e); |
| 267 |
|
| 268 |
if (m_defaultTool && !e->isAccepted()) |
| 269 |
m_defaultTool->mousePressEvent(e); |
| 270 |
|
| 271 |
if (!e->isAccepted()) |
| 272 |
QOpenGLWidget::mousePressEvent(e); |
| 273 |
} |
| 274 |
|
| 275 |
void GLWidget::mouseMoveEvent(QMouseEvent* e) |
| 276 |
{ |
| 277 |
e->ignore(); |
| 278 |
|
| 279 |
if (m_activeTool) |
| 280 |
m_activeTool->mouseMoveEvent(e); |
| 281 |
|
| 282 |
if (m_defaultTool && !e->isAccepted()) |
| 283 |
m_defaultTool->mouseMoveEvent(e); |
| 284 |
|
| 285 |
if (!e->isAccepted()) |
| 286 |
QOpenGLWidget::mouseMoveEvent(e); |
| 287 |
} |
| 288 |
|
| 289 |
void GLWidget::mouseReleaseEvent(QMouseEvent* e) |
| 290 |
{ |
| 291 |
e->ignore(); |
| 292 |
|
| 293 |
if (m_activeTool) |
| 294 |
m_activeTool->mouseReleaseEvent(e); |
| 295 |
|
| 296 |
if (m_defaultTool && !e->isAccepted()) |
| 297 |
m_defaultTool->mouseReleaseEvent(e); |
| 298 |
|
| 299 |
if (!e->isAccepted()) |
| 300 |
QOpenGLWidget::mouseReleaseEvent(e); |
| 301 |
} |
| 302 |
|
| 303 |
void GLWidget::wheelEvent(QWheelEvent* e) |
| 304 |
{ |
| 305 |
e->ignore(); |
| 306 |
|
| 307 |
if (m_activeTool) |
| 308 |
m_activeTool->wheelEvent(e); |
| 309 |
|
| 310 |
if (m_defaultTool && !e->isAccepted()) |
| 311 |
m_defaultTool->wheelEvent(e); |
| 312 |
|
| 313 |
if (!e->isAccepted()) |
| 314 |
QOpenGLWidget::wheelEvent(e); |
| 315 |
} |
| 316 |
|
| 317 |
void GLWidget::keyPressEvent(QKeyEvent* e) |
| 318 |
{ |
| 319 |
e->ignore(); |
| 320 |
|
| 321 |
if (m_activeTool) |
| 322 |
m_activeTool->keyPressEvent(e); |
| 323 |
|
| 324 |
if (m_defaultTool && !e->isAccepted()) |
| 325 |
m_defaultTool->keyPressEvent(e); |
| 326 |
|
| 327 |
if (!e->isAccepted()) |
| 328 |
QOpenGLWidget::keyPressEvent(e); |
| 329 |
} |
| 330 |
|
| 331 |
void GLWidget::keyReleaseEvent(QKeyEvent* e) |
| 332 |
{ |
| 333 |
e->ignore(); |
| 334 |
|
| 335 |
if (m_activeTool) |
| 336 |
m_activeTool->keyReleaseEvent(e); |
| 337 |
|
| 338 |
if (m_defaultTool && !e->isAccepted()) |
| 339 |
m_defaultTool->keyReleaseEvent(e); |
| 340 |
|
| 341 |
if (!e->isAccepted()) |
| 342 |
QOpenGLWidget::keyReleaseEvent(e); |
| 343 |
} |
| 344 |
|
| 345 |
} // namespace Avogadro::QtOpenGL |