#include "View.h" #include #include #include namespace Map { ////////////////////////////////////////// /////////// PUBLIC METHODS /////////////// ////////////////////////////////////////// View::View(Scene* scene) : QGraphicsView(scene) { // If the next lines are removed, scale() uses // the center of the view as the anchor, but the translate() // method does not work and the scrollbars are visible. // However, if we keep the next lines of code, the translate() // method works, but the scale() method does not work. // Remove scrollbars from view setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // Disconnect the scrollbars to prevent interference on view's positioning horizontalScrollBar()->disconnect(); verticalScrollBar()->disconnect(); } ////////////////////////////////////////// /////////// PRIVATE METHODS ////////////// ////////////////////////////////////////// void View::mousePressEvent(QMouseEvent* event) { if (event->buttons() == Qt::LeftButton) { mouseOrigin = event->pos(); } } void View::mouseMoveEvent(QMouseEvent* event) { if (event->buttons() == Qt::LeftButton) { QPointF oldPoint = mapToScene(mouseOrigin); QPointF newPoint = mapToScene(event->pos()); // Invert the mouse movement for translation QPointF translation = -(oldPoint - newPoint); translate(translation.x(), translation.y()); mouseOrigin = event->pos(); } } void View::wheelEvent(QWheelEvent* event) { QPoint degrees = event->angleDelta() / WHEEL_UNIT_PER_DEGREES; if (!degrees.isNull()) { double steps = degrees.y() / WHEEL_STEPS_PER_DEGREE; double scaleFactor = 1.0; if (steps < 0) { scaleFactor = std::abs(steps) * ZOOM_OUT_FACTOR; testDouble -= 0.1; } else { scaleFactor = std::abs(steps) * ZOOM_IN_FACTOR; testDouble += 0.1; } scale(scaleFactor, scaleFactor); } } }