Skip to content

Commit

Permalink
Merge pull request #1472 from ghutchis/navigator-tool-scripts
Browse files Browse the repository at this point in the history
Add support for navigator commands - rotate, translate, zoom
  • Loading branch information
ghutchis authored Nov 26, 2023
2 parents a0f8442 + dd3ef4e commit 94f6f98
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
53 changes: 46 additions & 7 deletions avogadro/qtplugins/navigator/navigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
#include <avogadro/rendering/glrenderer.h>
#include <avogadro/rendering/scene.h>

#include <QtGui/QGuiApplication>
#include <QAction>
#include <QDebug>
#include <QtCore/QSettings>
#include <QtGui/QGuiApplication>
#include <QtGui/QKeyEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>
#include <QAction>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QVBoxLayout>

Expand All @@ -42,11 +43,49 @@ Navigator::Navigator(QObject* parent_)
"Left Mouse: \tClick and drag to rotate the view.\n"
"Middle Mouse: \tClick and drag to zoom in or out.\n"
"Right Mouse: \tClick and drag to move the view.\n"));

QSettings settings;
m_zoomDirection = settings.value("navigator/zoom", 1).toInt();
}

void Navigator::registerCommands()
{
emit registerCommand("rotateScene",
tr("Rotate the scene along the x, y, or z axes."));
emit registerCommand("zoomScene", tr("Zoom the scene."));
emit registerCommand("translateScene", tr("Translate the scene."));
}

bool Navigator::handleCommand(const QString& command,
const QVariantMap& options)
{
if (m_renderer == nullptr)
return false; // No camera

if (command == "rotateScene") {
float x = options.value("x").toFloat();
float y = options.value("y").toFloat();
float z = options.value("z").toFloat();
rotate(m_renderer->camera().focus(), x, y, z);

m_glWidget->requestUpdate();
} else if (command == "zoomScene") {
float d = options.value("delta").toFloat();
zoom(m_renderer->camera().focus(), d);
m_glWidget->requestUpdate();
} else if (command == "translateScene") {
float x = options.value("x").toFloat();
float y = options.value("y").toFloat();
translate(m_renderer->camera().focus(), x, y);
m_glWidget->requestUpdate();
} else {
qDebug() << "Unknown command: " << command;
return false;
}

return true;
}

Navigator::~Navigator() {}

QWidget* Navigator::toolWidget() const
Expand All @@ -55,8 +94,7 @@ QWidget* Navigator::toolWidget() const
m_toolWidget = new QWidget(qobject_cast<QWidget*>(parent()));
auto* layout = new QVBoxLayout;

auto* swapZoom =
new QCheckBox(tr("Reverse Direction of Zoom on Scroll"));
auto* swapZoom = new QCheckBox(tr("Reverse Direction of Zoom on Scroll"));
swapZoom->setToolTip(
tr("Default:\t Scroll down to shrink, scroll up to zoom\n"
"Reversed:\t Scroll up to shrink, scroll down to zoom"));
Expand Down Expand Up @@ -169,7 +207,8 @@ QUndoCommand* Navigator::wheelEvent(QWheelEvent* e)
QPoint numDegrees = e->angleDelta() * 0.125;

// see https://doc.qt.io/qt-5/qwheelevent.html#pixelDelta
if (!numPixels.isNull() && QGuiApplication::platformName().toStdString().compare("xcb"))
if (!numPixels.isNull() &&
QGuiApplication::platformName().toStdString().compare("xcb"))
d = numPixels.y(); // use pixelDelta() when available, except on X11
else if (!numDegrees.isNull())
d = numDegrees.y(); // fall back to angleDelta()
Expand Down Expand Up @@ -299,4 +338,4 @@ inline void Navigator::translate(const Vector3f& ref, const Vector2f& fromScr,
m_renderer->camera().translate(to - from);
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins
5 changes: 5 additions & 0 deletions avogadro/qtplugins/navigator/navigator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Navigator : public QtGui::ToolPlugin
QUndoCommand* keyPressEvent(QKeyEvent* e) override;
QUndoCommand* keyReleaseEvent(QKeyEvent* e) override;

bool handleCommand(const QString& command,
const QVariantMap& options) override;

void registerCommands() override;

protected slots:
void swapZoomDirection(bool checked);

Expand Down

0 comments on commit 94f6f98

Please sign in to comment.