mirror of
https://github.com/YACReader/yacreader
synced 2025-05-28 03:10:27 -04:00
Extract mouse handling events to it's own class
This commit is contained in:
parent
9e37947479
commit
7db47f9147
@ -80,6 +80,7 @@ HEADERS += ../common/comic.h \
|
||||
goto_dialog.h \
|
||||
magnifying_glass.h \
|
||||
main_window_viewer.h \
|
||||
mouse_handler.h \
|
||||
viewer.h \
|
||||
goto_flow.h \
|
||||
options_dialog.h \
|
||||
@ -119,6 +120,7 @@ SOURCES += ../common/comic.cpp \
|
||||
goto_dialog.cpp \
|
||||
magnifying_glass.cpp \
|
||||
main_window_viewer.cpp \
|
||||
mouse_handler.cpp \
|
||||
viewer.cpp \
|
||||
goto_flow.cpp \
|
||||
options_dialog.cpp \
|
||||
|
97
YACReader/mouse_handler.cpp
Normal file
97
YACReader/mouse_handler.cpp
Normal 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
23
YACReader/mouse_handler.h
Normal 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
|
@ -1,5 +1,4 @@
|
||||
#include "viewer.h"
|
||||
#include "magnifying_glass.h"
|
||||
#include "configuration.h"
|
||||
#include "magnifying_glass.h"
|
||||
#include "goto_flow.h"
|
||||
@ -38,7 +37,8 @@ Viewer::Viewer(QWidget *parent)
|
||||
shouldOpenNext(false),
|
||||
shouldOpenPrevious(false),
|
||||
magnifyingGlassShown(false),
|
||||
restoreMagnifyingGlass(false)
|
||||
restoreMagnifyingGlass(false),
|
||||
mouseHandler(std::make_unique<YACReader::MouseHandler>(this))
|
||||
{
|
||||
translator = new YACReaderTranslator(this);
|
||||
translator->hide();
|
||||
@ -767,44 +767,6 @@ void Viewer::resizeEvent(QResizeEvent *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
|
||||
{
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||
@ -1093,36 +1055,17 @@ void Viewer::animateHideTranslator()
|
||||
|
||||
void Viewer::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
drag = true;
|
||||
yDragOrigin = event->y();
|
||||
xDragOrigin = event->x();
|
||||
setCursor(Qt::ClosedHandCursor);
|
||||
event->accept();
|
||||
return;
|
||||
}
|
||||
mouseHandler->mousePressEvent(event);
|
||||
}
|
||||
|
||||
void Viewer::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton) {
|
||||
drag = false;
|
||||
setCursor(Qt::OpenHandCursor);
|
||||
event->accept();
|
||||
return;
|
||||
}
|
||||
mouseHandler->mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
if (event->button() == Qt::ForwardButton) {
|
||||
right();
|
||||
event->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->button() == Qt::BackButton) {
|
||||
left();
|
||||
event->accept();
|
||||
return;
|
||||
}
|
||||
void Viewer::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
mouseHandler->mouseMoveEvent(event);
|
||||
}
|
||||
|
||||
void Viewer::updateZoomRatio(int ratio)
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <QSettings>
|
||||
|
||||
#include "scroll_management.h"
|
||||
#include "mouse_handler.h"
|
||||
|
||||
class ComicDB;
|
||||
class Comic;
|
||||
@ -185,6 +186,9 @@ private:
|
||||
int animationDuration() const;
|
||||
void animateScroll(QPropertyAnimation &scroller, const QScrollBar &scrollBar, int delta);
|
||||
|
||||
//! Mouse handler
|
||||
std::unique_ptr<YACReader::MouseHandler> mouseHandler;
|
||||
|
||||
public:
|
||||
Viewer(QWidget *parent = nullptr);
|
||||
~Viewer();
|
||||
@ -213,6 +217,8 @@ signals:
|
||||
void magnifyingGlassZoomIn();
|
||||
void magnifyingGlassZoomOut();
|
||||
void resetMagnifyingGlass();
|
||||
|
||||
friend class YACReader::MouseHandler;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user