Skip to content

Commit

Permalink
refactor: added gui_event_handler base class & derived Camera_manip f…
Browse files Browse the repository at this point in the history
…rom this
  • Loading branch information
denizdiktas committed Aug 17, 2023
1 parent f8d58de commit f785ef7
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 73 deletions.
3 changes: 2 additions & 1 deletion Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ file(GLOB source_files_gui
Camera_manip_rot.h Camera_manip_rot.cpp
Camera_manip_rot_bpa.h Camera_manip_rot_bpa.cpp
Camera_manip_zoom.h Camera_manip_zoom.cpp
)
GUI_event_handler.h GUI_event_handler.cpp
)
source_group( "GUI" FILES ${source_files_gui} )


Expand Down
84 changes: 42 additions & 42 deletions Arrangement_on_surface_2/demo/earth/Camera_manip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,46 @@ Camera_manip::Camera_manip(Camera& camera) :
}


void Camera_manip::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
{
switch (e->button())
{
case Qt::LeftButton:
m_left_mouse_button_down = flag;
break;

case Qt::MiddleButton:
m_middle_mouse_button_down = flag;
break;
}
}
void Camera_manip::mousePressEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, true);
m_mouse_press_pos = m_last_mouse_pos = QVector2D(e->position());

// call the function overridden by the derived class
mouse_press_event(e);
}
void Camera_manip::mouseMoveEvent(QMouseEvent* e)
{
m_current_mouse_pos = QVector2D(e->position());
m_diff = m_current_mouse_pos - m_last_mouse_pos;

// call the function overridden by the derived class
mouse_move_event(e);

m_last_mouse_pos = m_current_mouse_pos;
}
void Camera_manip::mouseReleaseEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, false);

// call the function overridden by the derived class
mouse_release_event(e);
}
void Camera_manip::resizeGL(int w, int h)
{
resize(w, h);
}
//void Camera_manip::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
//{
// switch (e->button())
// {
// case Qt::LeftButton:
// m_left_mouse_button_down = flag;
// break;
//
// case Qt::MiddleButton:
// m_middle_mouse_button_down = flag;
// break;
// }
//}
//void Camera_manip::mousePressEvent(QMouseEvent* e)
//{
// set_mouse_button_pressed_flag(e, true);
// m_mouse_press_pos = m_last_mouse_pos = QVector2D(e->position());
//
// // call the function overridden by the derived class
// mouse_press_event(e);
//}
//void Camera_manip::mouseMoveEvent(QMouseEvent* e)
//{
// m_current_mouse_pos = QVector2D(e->position());
// m_diff = m_current_mouse_pos - m_last_mouse_pos;
//
// // call the function overridden by the derived class
// mouse_move_event(e);
//
// m_last_mouse_pos = m_current_mouse_pos;
//}
//void Camera_manip::mouseReleaseEvent(QMouseEvent* e)
//{
// set_mouse_button_pressed_flag(e, false);
//
// // call the function overridden by the derived class
// mouse_release_event(e);
//}
//void Camera_manip::resizeGL(int w, int h)
//{
// resize(w, h);
//}

24 changes: 2 additions & 22 deletions Arrangement_on_surface_2/demo/earth/Camera_manip.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,16 @@
#include <qvector2d.h>

#include "Camera.h"
#include "GUI_event_handler.h"


class Camera_manip
class Camera_manip : public GUI_event_handler
{
public:
Camera_manip(Camera& camera);
virtual ~Camera_manip() {};

void mousePressEvent(QMouseEvent* e);
void mouseMoveEvent(QMouseEvent* e);
void mouseReleaseEvent(QMouseEvent* e);
void resizeGL(int w, int h);

protected:
void set_mouse_button_pressed_flag(QMouseEvent* e, bool flag);

virtual void mouse_press_event(QMouseEvent* e) {}
virtual void mouse_move_event(QMouseEvent* e) {}
virtual void mouse_release_event(QMouseEvent* e) {}
virtual void resize(int w, int h) {}


Camera& m_camera;

bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
QVector2D m_current_mouse_pos;
QVector2D m_last_mouse_pos;
QVector2D m_mouse_press_pos;
QVector2D m_diff;
};


Expand Down
55 changes: 55 additions & 0 deletions Arrangement_on_surface_2/demo/earth/GUI_event_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright(c) 2012, 2020 Tel - Aviv University(Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s): Engin Deniz Diktas <[email protected]>

#include "GUI_event_handler.h"


void GUI_event_handler::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
{
switch (e->button())
{
case Qt::LeftButton:
m_left_mouse_button_down = flag;
break;

case Qt::MiddleButton:
m_middle_mouse_button_down = flag;
break;
}
}
void GUI_event_handler::mousePressEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, true);
m_mouse_press_pos = m_last_mouse_pos = QVector2D(e->position());

// call the function overridden by the derived class
mouse_press_event(e);
}
void GUI_event_handler::mouseMoveEvent(QMouseEvent* e)
{
m_current_mouse_pos = QVector2D(e->position());
m_diff = m_current_mouse_pos - m_last_mouse_pos;

// call the function overridden by the derived class
mouse_move_event(e);

m_last_mouse_pos = m_current_mouse_pos;
}
void GUI_event_handler::mouseReleaseEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, false);

// call the function overridden by the derived class
mouse_release_event(e);
}
void GUI_event_handler::resizeGL(int w, int h)
{
resize(w, h);
}

45 changes: 45 additions & 0 deletions Arrangement_on_surface_2/demo/earth/GUI_event_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright(c) 2012, 2020 Tel - Aviv University(Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s): Engin Deniz Diktas <[email protected]>

#ifndef GUI_EVENT_HANDLER_H
#define GUI_EVENT_HANDLER_H

#include <qevent.h>
#include <qvector2d.h>


class GUI_event_handler
{
public:
virtual ~GUI_event_handler() {};

void mousePressEvent(QMouseEvent* e);
void mouseMoveEvent(QMouseEvent* e);
void mouseReleaseEvent(QMouseEvent* e);
void resizeGL(int w, int h);

protected:
void set_mouse_button_pressed_flag(QMouseEvent* e, bool flag);

virtual void mouse_press_event(QMouseEvent* e) {}
virtual void mouse_move_event(QMouseEvent* e) {}
virtual void mouse_release_event(QMouseEvent* e) {}
virtual void resize(int w, int h) {}


bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
QVector2D m_current_mouse_pos;
QVector2D m_last_mouse_pos;
QVector2D m_mouse_press_pos;
QVector2D m_diff;
};


#endif
8 changes: 0 additions & 8 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,6 @@ void Main_widget::timerEvent(QTimerEvent*)
update();
}

//class GUI_event_handler
//{
//public:
// void keyPressEvent(QKeyEvent* event);
//
//protected:
// virtual void key_press_event(QKeyEvent* event);
//};

void Main_widget::keyPressEvent(QKeyEvent* event)
{
Expand Down

0 comments on commit f785ef7

Please sign in to comment.