Skip to content

Commit

Permalink
Add behavior for zoom in/out buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsunni committed Jun 6, 2019
1 parent 4d9b79d commit a968cf9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/3d/qgscameracontroller.cpp
Expand Up @@ -327,14 +327,18 @@ void QgsCameraController::onPositionChanged( Qt3DInput::QMouseEvent *mouse )
}
else if ( hasRightButton && !hasShift && !hasCtrl )
{
zoom(dy);
}

mMousePos = QPoint( mouse->x(), mouse->y() );
}

void QgsCameraController::zoom(float factor){
// zoom in/out
float dist = mCameraPose.distanceFromCenterPoint();
dist -= dist * dy * 0.01f;
dist -= dist * factor * 0.01f;
mCameraPose.setDistanceFromCenterPoint( dist );
updateCameraFromPose();
}

mMousePos = QPoint( mouse->x(), mouse->y() );
}

void QgsCameraController::onWheel( Qt3DInput::QWheelEvent *wheel )
Expand Down
2 changes: 2 additions & 0 deletions src/3d/qgscameracontroller.h
Expand Up @@ -135,6 +135,8 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
//! Reads camera configuration from the given DOM element
void readXml( const QDomElement &elem );

void zoom(float factor);

private:
void rotateCamera( float diffPitch, float diffYaw );
void updateCameraFromPose( bool centerPointChanged = false );
Expand Down
4 changes: 1 addition & 3 deletions src/app/3d/qgs3dmapcanvas.cpp
Expand Up @@ -41,9 +41,7 @@ Qgs3DMapCanvas::Qgs3DMapCanvas( QWidget *parent )
} );

mContainer = QWidget::createWindowContainer( mEngine->window() );

mNavigationWidget = new Qgs3DNavigationWidget();
mNavigationWidget->setParent(this);
mNavigationWidget = new Qgs3DNavigationWidget(this);

QHBoxLayout *hLayout = new QHBoxLayout( this );
hLayout->setMargin( 0 );
Expand Down
22 changes: 21 additions & 1 deletion src/app/3d/qgs3dnavigationwidget.cpp
@@ -1,23 +1,43 @@
#include <QGridLayout>
#include <QPushButton>
#include <QDial>
#include <QObject>

#include "qgs3dnavigationwidget.h"
#include "qgscameracontroller.h"

Qgs3DNavigationWidget::Qgs3DNavigationWidget(QWidget *parent) : QWidget(parent)
Qgs3DNavigationWidget::Qgs3DNavigationWidget(Qgs3DMapCanvas *parent) : QWidget(parent)
{
// Zoom in button
mZoomInButton = new QPushButton(this);
mZoomInButton->setText(QStringLiteral("+"));
mZoomInButton->setToolTip(QStringLiteral("Zoom In"));
mZoomInButton->setAutoRepeat(true);

QObject::connect(
mZoomInButton,
&QPushButton::clicked,
parent,
[ = ]{
parent->cameraController()->zoom(5);
}
);

// Zoom out button
mZoomOutButton = new QPushButton(this);
mZoomOutButton->setText(QStringLiteral("-"));
mZoomOutButton->setToolTip(QStringLiteral("Zoom Out"));
mZoomOutButton->setAutoRepeat(true);

QObject::connect(
mZoomOutButton,
&QPushButton::clicked,
parent,
[ = ]{
parent->cameraController()->zoom(-5);
}
);

// Tilt up button
mTiltUpButton = new QPushButton(this);
mTiltUpButton->setText(QString::fromUtf8("\u25B3"));
Expand Down
5 changes: 4 additions & 1 deletion src/app/3d/qgs3dnavigationwidget.h
Expand Up @@ -21,11 +21,14 @@
#include <QDial>
#include <QGridLayout>

#include "qgs3dmapcanvas.h"
#include "qgscameracontroller.h"

class Qgs3DNavigationWidget : public QWidget
{
Q_OBJECT
public:
Qgs3DNavigationWidget(QWidget *parent = nullptr);
Qgs3DNavigationWidget(Qgs3DMapCanvas *parent = nullptr);
~Qgs3DNavigationWidget();

signals:
Expand Down

0 comments on commit a968cf9

Please sign in to comment.