Extract mouse handling events to it's own class

This commit is contained in:
Luis Ángel San Martín 2025-05-03 15:10:16 +02:00
parent 9e37947479
commit 7db47f9147
5 changed files with 136 additions and 65 deletions

View File

@ -80,6 +80,7 @@ HEADERS += ../common/comic.h \
goto_dialog.h \ goto_dialog.h \
magnifying_glass.h \ magnifying_glass.h \
main_window_viewer.h \ main_window_viewer.h \
mouse_handler.h \
viewer.h \ viewer.h \
goto_flow.h \ goto_flow.h \
options_dialog.h \ options_dialog.h \
@ -119,6 +120,7 @@ SOURCES += ../common/comic.cpp \
goto_dialog.cpp \ goto_dialog.cpp \
magnifying_glass.cpp \ magnifying_glass.cpp \
main_window_viewer.cpp \ main_window_viewer.cpp \
mouse_handler.cpp \
viewer.cpp \ viewer.cpp \
goto_flow.cpp \ goto_flow.cpp \
options_dialog.cpp \ options_dialog.cpp \

View File

@ -0,0 +1,97 @@
#include "mouse_handler.h"
#include <QtWidgets>
#include "configuration.h"
#include "magnifying_glass.h"
#include "render.h"
#include "viewer.h"
#include "goto_flow.h"
#ifndef NO_OPENGL
#include "goto_flow_gl.h"
#else
#include <QtWidgets>
#endif
using namespace YACReader;
YACReader::MouseHandler::MouseHandler(Viewer *viewer)
: viewer(viewer)
{
}
void YACReader::MouseHandler::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
viewer->drag = true;
auto position = event->position();
viewer->yDragOrigin = position.y();
viewer->xDragOrigin = position.x();
viewer->setCursor(Qt::ClosedHandCursor);
event->accept();
return;
}
}
void YACReader::MouseHandler::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
viewer->drag = false;
viewer->setCursor(Qt::OpenHandCursor);
event->accept();
return;
}
if (event->button() == Qt::ForwardButton) {
viewer->right();
event->accept();
return;
}
if (event->button() == Qt::BackButton) {
viewer->left();
event->accept();
return;
}
}
void YACReader::MouseHandler::mouseMoveEvent(QMouseEvent *event)
{
viewer->showCursor();
viewer->hideCursorTimer->start(2500);
auto position = event->position();
if (viewer->magnifyingGlassShown)
viewer->mglass->move(static_cast<int>(position.x() - float(viewer->mglass->width()) / 2), static_cast<int>(position.y() - float(viewer->mglass->height()) / 2));
if (viewer->render->hasLoadedComic()) {
if (viewer->showGoToFlowAnimation->state() != QPropertyAnimation::Running) {
if (Configuration::getConfiguration().getDisableShowOnMouseOver() == false) {
if (viewer->goToFlow->isVisible()) {
QPoint gtfPos = viewer->goToFlow->mapFrom(this->viewer, event->pos());
if (gtfPos.y() < 0 || gtfPos.x() < 0 || gtfPos.x() > viewer->goToFlow->width()) // TODO this extra check is for Mavericks (mouseMove over goToFlowGL seems to be broken)
viewer->animateHideGoToFlow();
// goToFlow->hide();
} else {
int umbral = (viewer->width() - viewer->goToFlow->width()) / 2;
if ((position.y() > viewer->height() - 15) && (position.x() > umbral) && (position.x() < viewer->width() - umbral)) {
viewer->animateShowGoToFlow();
viewer->hideCursorTimer->stop();
}
}
}
}
if (viewer->drag) {
int currentPosY = viewer->verticalScrollBar()->sliderPosition();
int currentPosX = viewer->horizontalScrollBar()->sliderPosition();
viewer->verticalScrollBar()->setSliderPosition(currentPosY + (viewer->yDragOrigin - position.y()));
viewer->horizontalScrollBar()->setSliderPosition(currentPosX + (viewer->xDragOrigin - position.x()));
viewer->yDragOrigin = position.y();
viewer->xDragOrigin = position.x();
}
}
}

23
YACReader/mouse_handler.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef MOUSE_HANDLER_H
#define MOUSE_HANDLER_H
#include <QMouseEvent>
class Viewer;
namespace YACReader {
class MouseHandler
{
public:
MouseHandler(Viewer *viewer);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
Viewer *viewer;
};
}
#endif // MOUSE_HANDLER_H

View File

@ -1,5 +1,4 @@
#include "viewer.h" #include "viewer.h"
#include "magnifying_glass.h"
#include "configuration.h" #include "configuration.h"
#include "magnifying_glass.h" #include "magnifying_glass.h"
#include "goto_flow.h" #include "goto_flow.h"
@ -38,7 +37,8 @@ Viewer::Viewer(QWidget *parent)
shouldOpenNext(false), shouldOpenNext(false),
shouldOpenPrevious(false), shouldOpenPrevious(false),
magnifyingGlassShown(false), magnifyingGlassShown(false),
restoreMagnifyingGlass(false) restoreMagnifyingGlass(false),
mouseHandler(std::make_unique<YACReader::MouseHandler>(this))
{ {
translator = new YACReaderTranslator(this); translator = new YACReaderTranslator(this);
translator->hide(); translator->hide();
@ -767,44 +767,6 @@ void Viewer::resizeEvent(QResizeEvent *event)
QScrollArea::resizeEvent(event); QScrollArea::resizeEvent(event);
} }
void Viewer::mouseMoveEvent(QMouseEvent *event)
{
showCursor();
hideCursorTimer->start(2500);
if (magnifyingGlassShown)
mglass->move(static_cast<int>(event->x() - float(mglass->width()) / 2), static_cast<int>(event->y() - float(mglass->height()) / 2));
if (render->hasLoadedComic()) {
if (showGoToFlowAnimation->state() != QPropertyAnimation::Running) {
if (Configuration::getConfiguration().getDisableShowOnMouseOver() == false) {
if (goToFlow->isVisible()) {
QPoint gtfPos = goToFlow->mapFrom(this, event->pos());
if (gtfPos.y() < 0 || gtfPos.x() < 0 || gtfPos.x() > goToFlow->width()) // TODO this extra check is for Mavericks (mouseMove over goToFlowGL seems to be broken)
animateHideGoToFlow();
// goToFlow->hide();
} else {
int umbral = (width() - goToFlow->width()) / 2;
if ((event->y() > height() - 15) && (event->x() > umbral) && (event->x() < width() - umbral)) {
animateShowGoToFlow();
hideCursorTimer->stop();
}
}
}
}
if (drag) {
int currentPosY = verticalScrollBar()->sliderPosition();
int currentPosX = horizontalScrollBar()->sliderPosition();
verticalScrollBar()->setSliderPosition(currentPosY + (yDragOrigin - event->y()));
horizontalScrollBar()->setSliderPosition(currentPosX + (xDragOrigin - event->x()));
yDragOrigin = event->y();
xDragOrigin = event->x();
}
}
}
QPixmap Viewer::pixmap() const QPixmap Viewer::pixmap() const
{ {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
@ -1093,36 +1055,17 @@ void Viewer::animateHideTranslator()
void Viewer::mousePressEvent(QMouseEvent *event) void Viewer::mousePressEvent(QMouseEvent *event)
{ {
if (event->button() == Qt::LeftButton) { mouseHandler->mousePressEvent(event);
drag = true;
yDragOrigin = event->y();
xDragOrigin = event->x();
setCursor(Qt::ClosedHandCursor);
event->accept();
return;
}
} }
void Viewer::mouseReleaseEvent(QMouseEvent *event) void Viewer::mouseReleaseEvent(QMouseEvent *event)
{ {
if (event->button() == Qt::LeftButton) { mouseHandler->mouseReleaseEvent(event);
drag = false; }
setCursor(Qt::OpenHandCursor);
event->accept();
return;
}
if (event->button() == Qt::ForwardButton) { void Viewer::mouseMoveEvent(QMouseEvent *event)
right(); {
event->accept(); mouseHandler->mouseMoveEvent(event);
return;
}
if (event->button() == Qt::BackButton) {
left();
event->accept();
return;
}
} }
void Viewer::updateZoomRatio(int ratio) void Viewer::updateZoomRatio(int ratio)

View File

@ -17,6 +17,7 @@
#include <QSettings> #include <QSettings>
#include "scroll_management.h" #include "scroll_management.h"
#include "mouse_handler.h"
class ComicDB; class ComicDB;
class Comic; class Comic;
@ -185,6 +186,9 @@ private:
int animationDuration() const; int animationDuration() const;
void animateScroll(QPropertyAnimation &scroller, const QScrollBar &scrollBar, int delta); void animateScroll(QPropertyAnimation &scroller, const QScrollBar &scrollBar, int delta);
//! Mouse handler
std::unique_ptr<YACReader::MouseHandler> mouseHandler;
public: public:
Viewer(QWidget *parent = nullptr); Viewer(QWidget *parent = nullptr);
~Viewer(); ~Viewer();
@ -213,6 +217,8 @@ signals:
void magnifyingGlassZoomIn(); void magnifyingGlassZoomIn();
void magnifyingGlassZoomOut(); void magnifyingGlassZoomOut();
void resetMagnifyingGlass(); void resetMagnifyingGlass();
friend class YACReader::MouseHandler;
}; };
#endif #endif