| 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 "navigator.h" |
| 7 |
|
| 8 |
#include <avogadro/core/vector.h> |
| 9 |
|
| 10 |
#include <avogadro/qtopengl/glwidget.h> |
| 11 |
|
| 12 |
#include <avogadro/rendering/camera.h> |
| 13 |
#include <avogadro/rendering/glrenderer.h> |
| 14 |
#include <avogadro/rendering/scene.h> |
| 15 |
|
| 16 |
#include <QAction> |
| 17 |
#include <QDebug> |
| 18 |
#include <QtCore/QSettings> |
| 19 |
#include <QtGui/QGuiApplication> |
| 20 |
#include <QtGui/QKeyEvent> |
| 21 |
#include <QtGui/QMouseEvent> |
| 22 |
#include <QtGui/QWheelEvent> |
| 23 |
#include <QtWidgets/QCheckBox> |
| 24 |
#include <QtWidgets/QVBoxLayout> |
| 25 |
|
| 26 |
#include <Eigen/Geometry> |
| 27 |
|
| 28 |
namespace Avogadro::QtPlugins { |
| 29 |
|
| 30 |
const float ZOOM_SPEED = 0.02f; |
| 31 |
const float ROTATION_SPEED = 0.005f; |
| 32 |
|
| 33 |
Navigator::Navigator(QObject* parent_) |
| 34 |
: QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)), |
| 35 |
m_molecule(nullptr), m_glWidget(nullptr), m_toolWidget(nullptr), |
| 36 |
m_renderer(nullptr), m_pressedButtons(Qt::NoButton), |
| 37 |
m_currentAction(Nothing) |
| 38 |
{ |
| 39 |
QString shortcut = tr("Ctrl+1", "control-key 1"); |
| 40 |
m_activateAction->setText(tr("Navigate")); |
| 41 |
m_activateAction->setToolTip( |
| 42 |
tr("Navigation Tool\t(%1)\n\n" |
| 43 |
"Left Mouse:\tClick and drag to rotate the view.\n" |
| 44 |
"Middle Mouse:\tClick and drag to zoom in or out.\n" |
| 45 |
"Right Mouse:\tClick and drag to move the view.") |
| 46 |
.arg(shortcut)); |
| 47 |
setIcon(); |
| 48 |
QSettings settings; |
| 49 |
m_zoomDirection = settings.value("navigator/zoom", 1).toInt(); |
| 50 |
} |
| 51 |
|
| 52 |
void Navigator::setIcon(bool darkTheme) |
| 53 |
{ |
| 54 |
if (darkTheme) |
| 55 |
m_activateAction->setIcon(QIcon(":/icons/navigator_dark.svg")); |
| 56 |
else |
| 57 |
m_activateAction->setIcon(QIcon(":/icons/navigator_light.svg")); |
| 58 |
} |
| 59 |
|
| 60 |
void Navigator::registerCommands() |
| 61 |
{ |
| 62 |
emit registerCommand("rotateScene", |
| 63 |
tr("Rotate the scene along the x, y, or z axes.")); |
| 64 |
emit registerCommand("zoomScene", tr("Zoom the scene.")); |
| 65 |
emit registerCommand("translateScene", tr("Translate the scene.")); |
| 66 |
} |
| 67 |
|
| 68 |
bool Navigator::handleCommand(const QString& command, |
| 69 |
const QVariantMap& options) |
| 70 |
{ |
| 71 |
if (m_renderer == nullptr) |
| 72 |
return false; // No camera |
| 73 |
|
| 74 |
if (command == "rotateScene") { |
| 75 |
float x = options.value("x").toFloat(); |
| 76 |
float y = options.value("y").toFloat(); |
| 77 |
float z = options.value("z").toFloat(); |
| 78 |
rotate(m_renderer->camera().focus(), x, y, z); |
| 79 |
|
| 80 |
m_glWidget->requestUpdate(); |
| 81 |
} else if (command == "zoomScene") { |
| 82 |
float d = options.value("delta").toFloat(); |
| 83 |
zoom(m_renderer->camera().focus(), d); |
| 84 |
m_glWidget->requestUpdate(); |
| 85 |
} else if (command == "translateScene") { |
| 86 |
float x = options.value("x").toFloat(); |
| 87 |
float y = options.value("y").toFloat(); |
| 88 |
translate(m_renderer->camera().focus(), x, y); |
| 89 |
m_glWidget->requestUpdate(); |
| 90 |
} else { |
| 91 |
qDebug() << "Unknown command: " << command; |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
Navigator::~Navigator() {} |
| 99 |
|
| 100 |
QWidget* Navigator::toolWidget() const |
| 101 |
{ |
| 102 |
if (!m_toolWidget) { |
| 103 |
m_toolWidget = new QWidget(qobject_cast<QWidget*>(parent())); |
| 104 |
auto* layout = new QVBoxLayout; |
| 105 |
|
| 106 |
auto* swapZoom = new QCheckBox(tr("Reverse Direction of Zoom on Scroll")); |
| 107 |
swapZoom->setToolTip( |
| 108 |
tr("Default:\tScroll down to shrink, scroll up to zoom\n" |
| 109 |
"Reversed:\tScroll up to shrink, scroll down to zoom")); |
| 110 |
connect(swapZoom, SIGNAL(toggled(bool)), this, |
| 111 |
SLOT(swapZoomDirection(bool))); |
| 112 |
layout->addWidget(swapZoom); |
| 113 |
|
| 114 |
layout->addStretch(1); |
| 115 |
m_toolWidget->setLayout(layout); |
| 116 |
} |
| 117 |
return m_toolWidget; |
| 118 |
} |
| 119 |
|
| 120 |
void Navigator::swapZoomDirection(bool checked) |
| 121 |
{ |
| 122 |
m_zoomDirection = (checked ? -1 : 1); |
| 123 |
|
| 124 |
QSettings settings; |
| 125 |
settings.setValue("navigator/zoom", m_zoomDirection); |
| 126 |
} |
| 127 |
|
| 128 |
QUndoCommand* Navigator::mousePressEvent(QMouseEvent* e) |
| 129 |
{ |
| 130 |
updatePressedButtons(e, false); |
| 131 |
m_lastMousePosition = e->pos(); |
| 132 |
e->accept(); |
| 133 |
|
| 134 |
// Figure out what type of navigation has been requested. |
| 135 |
if ((e->buttons() & Qt::LeftButton && e->modifiers() == Qt::NoModifier) || |
| 136 |
(e->buttons() & Qt::LeftButton && e->modifiers() == Qt::AltModifier)) { |
| 137 |
m_currentAction = Rotation; |
| 138 |
} else if (e->buttons() & Qt::MiddleButton || |
| 139 |
(e->buttons() & Qt::LeftButton && |
| 140 |
e->modifiers() == Qt::ShiftModifier)) { |
| 141 |
m_currentAction = ZoomTilt; |
| 142 |
} else if (e->buttons() & Qt::RightButton || |
| 143 |
(e->buttons() & Qt::LeftButton && |
| 144 |
(e->modifiers() == Qt::ControlModifier || |
| 145 |
e->modifiers() == Qt::MetaModifier))) { |
| 146 |
m_currentAction = Translation; |
| 147 |
} |
| 148 |
|
| 149 |
return nullptr; |
| 150 |
} |
| 151 |
|
| 152 |
QUndoCommand* Navigator::mouseReleaseEvent(QMouseEvent* e) |
| 153 |
{ |
| 154 |
updatePressedButtons(e, true); |
| 155 |
m_lastMousePosition = QPoint(); |
| 156 |
m_currentAction = Nothing; |
| 157 |
e->accept(); |
| 158 |
return nullptr; |
| 159 |
} |
| 160 |
|
| 161 |
QUndoCommand* Navigator::mouseMoveEvent(QMouseEvent* e) |
| 162 |
{ |
| 163 |
switch (m_currentAction) { |
| 164 |
case Rotation: { |
| 165 |
QPoint delta = e->pos() - m_lastMousePosition; |
| 166 |
rotate(m_renderer->camera().focus(), delta.y(), delta.x(), 0); |
| 167 |
e->accept(); |
| 168 |
break; |
| 169 |
} |
| 170 |
case Translation: { |
| 171 |
Vector2f fromScreen(m_lastMousePosition.x(), m_lastMousePosition.y()); |
| 172 |
Vector2f toScreen(e->localPos().x(), e->localPos().y()); |
| 173 |
translate(m_renderer->camera().focus(), fromScreen, toScreen); |
| 174 |
e->accept(); |
| 175 |
break; |
| 176 |
} |
| 177 |
case ZoomTilt: { |
| 178 |
QPoint delta = e->pos() - m_lastMousePosition; |
| 179 |
// Tilt |
| 180 |
rotate(m_renderer->camera().focus(), 0, 0, delta.x()); |
| 181 |
// Zoom |
| 182 |
// zoom(m_renderer->camera().focus(), delta.y()); |
| 183 |
e->accept(); |
| 184 |
break; |
| 185 |
} |
| 186 |
default:; |
| 187 |
} |
| 188 |
|
| 189 |
m_lastMousePosition = e->pos(); |
| 190 |
|
| 191 |
if (e->isAccepted()) |
| 192 |
emit updateRequested(); |
| 193 |
|
| 194 |
return nullptr; |
| 195 |
} |
| 196 |
|
| 197 |
QUndoCommand* Navigator::mouseDoubleClickEvent(QMouseEvent* e) |
| 198 |
{ |
| 199 |
// Reset |
| 200 |
if (e->button() == Qt::MiddleButton) { |
| 201 |
if (m_glWidget) { |
| 202 |
m_glWidget->resetCamera(); |
| 203 |
e->accept(); |
| 204 |
emit updateRequested(); |
| 205 |
} |
| 206 |
} |
| 207 |
return nullptr; |
| 208 |
} |
| 209 |
|
| 210 |
QUndoCommand* Navigator::wheelEvent(QWheelEvent* e) |
| 211 |
{ |
| 212 |
/// @todo Use scale for orthographic projections |
| 213 |
// Amount to zoom |
| 214 |
float d = 0.0f; |
| 215 |
|
| 216 |
QPoint numPixels = e->pixelDelta(); |
| 217 |
QPoint numDegrees = e->angleDelta() * 0.125; |
| 218 |
|
| 219 |
// see https://doc.qt.io/qt-5/qwheelevent.html#pixelDelta |
| 220 |
if (!numPixels.isNull() && |
| 221 |
QGuiApplication::platformName().toStdString().compare("xcb")) |
| 222 |
d = numPixels.y(); // use pixelDelta() when available, except on X11 |
| 223 |
else if (!numDegrees.isNull()) |
| 224 |
d = numDegrees.y(); // fall back to angleDelta() |
| 225 |
|
| 226 |
zoom(m_renderer->camera().focus(), m_zoomDirection * d); |
| 227 |
|
| 228 |
e->accept(); |
| 229 |
emit updateRequested(); |
| 230 |
return nullptr; |
| 231 |
} |
| 232 |
|
| 233 |
QUndoCommand* Navigator::keyPressEvent(QKeyEvent* e) |
| 234 |
{ |
| 235 |
Vector3f ref = m_renderer->camera().focus(); |
| 236 |
// Alt modifier = bigger changes |
| 237 |
float scale = 1.0f; |
| 238 |
if (e->modifiers() & (Qt::AltModifier)) |
| 239 |
scale = 6.0f; |
| 240 |
|
| 241 |
// Get modifiers without Alt for comparison |
| 242 |
Qt::KeyboardModifiers mods = e->modifiers() & ~(Qt::AltModifier); |
| 243 |
|
| 244 |
switch (e->key()) { |
| 245 |
case Qt::Key_Left: |
| 246 |
case Qt::Key_H: |
| 247 |
case Qt::Key_A: |
| 248 |
if (mods == Qt::NoModifier || mods == Qt::KeypadModifier) |
| 249 |
rotate(ref, 0, -5 * scale, 0); |
| 250 |
else if (mods & Qt::ShiftModifier) |
| 251 |
rotate(ref, 0, 0, -5 * scale); |
| 252 |
else if (mods & Qt::ControlModifier) |
| 253 |
translate(ref, -5 * scale, 0); |
| 254 |
e->accept(); |
| 255 |
break; |
| 256 |
|
| 257 |
case Qt::Key_Right: |
| 258 |
case Qt::Key_L: |
| 259 |
case Qt::Key_D: |
| 260 |
if (mods == Qt::NoModifier || mods == Qt::KeypadModifier) |
| 261 |
rotate(ref, 0, 5 * scale, 0); |
| 262 |
else if (mods & Qt::ShiftModifier) |
| 263 |
rotate(ref, 0, 0, 5 * scale); |
| 264 |
else if (mods & Qt::ControlModifier) |
| 265 |
translate(ref, 5 * scale, 0); |
| 266 |
e->accept(); |
| 267 |
break; |
| 268 |
|
| 269 |
case Qt::Key_Up: |
| 270 |
case Qt::Key_K: |
| 271 |
case Qt::Key_W: |
| 272 |
if (mods == Qt::NoModifier || mods == Qt::KeypadModifier) |
| 273 |
rotate(ref, -5 * scale, 0, 0); |
| 274 |
else if (mods & Qt::ShiftModifier) |
| 275 |
zoom(ref, -2 * scale); |
| 276 |
else if (mods & Qt::ControlModifier) |
| 277 |
translate(ref, 0, -5 * scale); |
| 278 |
e->accept(); |
| 279 |
break; |
| 280 |
|
| 281 |
case Qt::Key_Down: |
| 282 |
case Qt::Key_J: |
| 283 |
case Qt::Key_S: |
| 284 |
if (mods == Qt::NoModifier || mods == Qt::KeypadModifier) |
| 285 |
rotate(ref, 5 * scale, 0, 0); |
| 286 |
else if (mods & Qt::ShiftModifier) |
| 287 |
zoom(ref, 2 * scale); |
| 288 |
else if (mods & Qt::ControlModifier) |
| 289 |
translate(ref, 0, 5 * scale); |
| 290 |
e->accept(); |
| 291 |
break; |
| 292 |
|
| 293 |
default: |
| 294 |
e->ignore(); |
| 295 |
} |
| 296 |
|
| 297 |
emit updateRequested(); |
| 298 |
return nullptr; |
| 299 |
} |
| 300 |
|
| 301 |
QUndoCommand* Navigator::keyReleaseEvent(QKeyEvent* e) |
| 302 |
{ |
| 303 |
/// @todo |
| 304 |
e->ignore(); |
| 305 |
return nullptr; |
| 306 |
} |
| 307 |
|
| 308 |
inline void Navigator::updatePressedButtons(QMouseEvent* e, bool release) |
| 309 |
{ |
| 310 |
/// @todo Use modifier keys on mac |
| 311 |
if (release) |
| 312 |
m_pressedButtons &= e->buttons(); |
| 313 |
else |
| 314 |
m_pressedButtons |= e->buttons(); |
| 315 |
} |
| 316 |
|
| 317 |
inline void Navigator::rotate(const Vector3f& ref, float x, float y, float z) |
| 318 |
{ |
| 319 |
const Eigen::Affine3f& modelView = m_renderer->camera().modelView(); |
| 320 |
Vector3f xAxis = modelView.linear().row(0).transpose().normalized(); |
| 321 |
Vector3f yAxis = modelView.linear().row(1).transpose().normalized(); |
| 322 |
Vector3f zAxis = modelView.linear().row(2).transpose().normalized(); |
| 323 |
m_renderer->camera().translate(ref); |
| 324 |
m_renderer->camera().rotate(x * ROTATION_SPEED, xAxis); |
| 325 |
m_renderer->camera().rotate(y * ROTATION_SPEED, yAxis); |
| 326 |
m_renderer->camera().rotate(z * ROTATION_SPEED, zAxis); |
| 327 |
m_renderer->camera().translate(-ref); |
| 328 |
} |
| 329 |
|
| 330 |
inline void Navigator::zoom(const Vector3f& ref, float d) |
| 331 |
{ |
| 332 |
const Eigen::Affine3f& modelView = m_renderer->camera().modelView(); |
| 333 |
Vector3f transformedCenter = modelView * ref; |
| 334 |
float distance = transformedCenter.norm(); |
| 335 |
float t = d * ZOOM_SPEED; |
| 336 |
float u = 2.0f / distance - 1.0f; |
| 337 |
if (t < u) |
| 338 |
t = u; |
| 339 |
if (m_renderer->camera().projectionType() == Rendering::Perspective) |
| 340 |
m_renderer->camera().preTranslate(transformedCenter * t); |
| 341 |
else |
| 342 |
m_renderer->camera().scale(t + 1.0f); |
| 343 |
} |
| 344 |
|
| 345 |
inline void Navigator::translate(const Vector3f& ref, float x, float y) |
| 346 |
{ |
| 347 |
Vector2f fromScreen(0, 0); |
| 348 |
Vector2f toScreen(x, y); |
| 349 |
translate(ref, fromScreen, toScreen); |
| 350 |
} |
| 351 |
|
| 352 |
inline void Navigator::translate(const Vector3f& ref, const Vector2f& fromScr, |
| 353 |
const Vector2f& toScr) |
| 354 |
{ |
| 355 |
Vector3f from(m_renderer->camera().unProject(fromScr, ref)); |
| 356 |
Vector3f to(m_renderer->camera().unProject(toScr, ref)); |
| 357 |
m_renderer->camera().translate(to - from); |
| 358 |
} |
| 359 |
|
| 360 |
} // namespace Avogadro::QtPlugins |