From 1c98dba72a4f7659dfd3f3b71aa8bcfe0ca50f7b Mon Sep 17 00:00:00 2001 From: denizdiktas Date: Tue, 13 Jun 2023 16:23:40 +0300 Subject: [PATCH] small code refactor --- .../demo/earth/mainwidget.cpp | 32 +++++----------- .../demo/earth/mainwidget.h | 37 ++++++++++--------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp index 9b789e5f800..685234cfae4 100644 --- a/Arrangement_on_surface_2/demo/earth/mainwidget.cpp +++ b/Arrangement_on_surface_2/demo/earth/mainwidget.cpp @@ -1,5 +1,3 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "mainwidget.h" @@ -17,18 +15,23 @@ MainWidget::~MainWidget() doneCurrent(); } -void MainWidget::mousePressEvent(QMouseEvent *e) + +void MainWidget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag) { switch (e->button()) { case Qt::LeftButton: - m_left_mouse_button_down = true; + m_left_mouse_button_down = flag; break; case Qt::MiddleButton: - m_middle_mouse_button_down = true; + m_middle_mouse_button_down = flag; break; } +} +void MainWidget::mousePressEvent(QMouseEvent *e) +{ + set_mouse_button_pressed_flag(e, true); m_last_mouse_pos = QVector2D(e->position()); } void MainWidget::mouseMoveEvent(QMouseEvent* e) @@ -41,10 +44,9 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e) const float rotation_scale_factor = 0.1f; const float theta_around_x = rotation_scale_factor * diff.y(); const float theta_around_y = rotation_scale_factor * diff.x(); - m_camera.rotate(theta_around_x, theta_around_y); } - else + else if(m_middle_mouse_button_down) { const float zoom_scale_factor = 0.01f; const auto distance = zoom_scale_factor * diff.y(); @@ -53,23 +55,9 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e) m_last_mouse_pos = current_mouse_pos; } -void MainWidget::wheelEvent(QWheelEvent* e) -{ - auto distance = e->angleDelta(); - m_camera.move_forward(distance.x()); -} void MainWidget::mouseReleaseEvent(QMouseEvent *e) { - switch (e->button()) - { - case Qt::LeftButton: - m_left_mouse_button_down = false; - break; - - case Qt::MiddleButton: - m_middle_mouse_button_down = false; - break; - } + set_mouse_button_pressed_flag(e, false); } void MainWidget::timerEvent(QTimerEvent *) { diff --git a/Arrangement_on_surface_2/demo/earth/mainwidget.h b/Arrangement_on_surface_2/demo/earth/mainwidget.h index 86c3d7334b3..e29160be4cc 100644 --- a/Arrangement_on_surface_2/demo/earth/mainwidget.h +++ b/Arrangement_on_surface_2/demo/earth/mainwidget.h @@ -22,31 +22,32 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase { - Q_OBJECT + Q_OBJECT public: - using QOpenGLWidget::QOpenGLWidget; - ~MainWidget(); + using QOpenGLWidget::QOpenGLWidget; + ~MainWidget(); protected: - void mousePressEvent(QMouseEvent *e) override; - void mouseMoveEvent(QMouseEvent* e) override; - void wheelEvent(QWheelEvent* event) override; - void mouseReleaseEvent(QMouseEvent *e) override; - void timerEvent(QTimerEvent *e) override; - - void initializeGL() override; - void resizeGL(int w, int h) override; - void paintGL() override; + void set_mouse_button_pressed_flag(QMouseEvent* e, bool flag); + void mousePressEvent(QMouseEvent *e) override; + void mouseMoveEvent(QMouseEvent* e) override; + void mouseReleaseEvent(QMouseEvent *e) override; + void timerEvent(QTimerEvent *e) override; - void add_shader(GLuint the_program, - const char* shader_code, - GLenum shader_type); + void initializeGL() override; + void resizeGL(int w, int h) override; + void paintGL() override; + + + void add_shader(GLuint the_program, + const char* shader_code, + GLenum shader_type); - void init_camera(); - void init_geometry(); - void init_shader_program(); + void init_camera(); + void init_geometry(); + void init_shader_program(); private: std::unique_ptr m_sphere;