#include "../../fixC11.h" #include "MapView2D.h" #include #include #include #include #include #include "../model/MapModelElement.h" #include "../model/MapModel.h" MapView2D::MapView2D(QWidget* parent) : QOpenGLWidget(parent) { // openGL params QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); format.setSamples(1); // format.setVersion(3, 2); format.setProfile(QSurfaceFormat::CoreProfile); setFormat(format); // receive mouse-move even when mouse is not down setMouseTracking(true); setFocusPolicy(Qt::StrongFocus); // defaults s.setScale(5); // android setAttribute(Qt::WA_AcceptTouchEvents); grabGesture(Qt::PanGesture); grabGesture(Qt::PinchGesture); } void MapView2D::paintGL() { QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glClear(GL_COLOR_BUFFER_BIT); QPainter qp(this); Painter p(s, &qp, width(), height()); s.setScreenSize(width(), height()); // background tools tools.paintBefore(this, p); // render all visible elements qp.setRenderHint( QPainter::Antialiasing, true ); for (MapModelElement* el : getModel()->getVisibleElements()) { if (el->getMV2D()) {el->getMV2D()->paint(p);} } qp.setRenderHint( QPainter::Antialiasing, false ); // foreground tools tools.paintAfter(this, p); } void MapView2D::initializeGL() { QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f); } void MapView2D::resizeGL() { // TODO ? } void MapView2D::keyPressEvent(QKeyEvent *e) { tools.keyPressEvent(this, e); update(); } void MapView2D::wheelEvent(QWheelEvent* e) { tools.wheelEvent(this, e); update(); } void MapView2D::mousePressEvent(QMouseEvent* e) { tools.mousePressEvent(this, e); update(); } void MapView2D::mouseMoveEvent(QMouseEvent* e) { tools.mouseMoveEvent(this, e); update(); } void MapView2D::mouseReleaseEvent(QMouseEvent* e) { tools.mouseReleaseEvent(this, e); update(); } // android bool MapView2D::event(QEvent* event) { if (event->type() == QEvent::Gesture) { return gestureEvent(static_cast(event)); } else { return QWidget::event(event); } } bool MapView2D::gestureEvent(QGestureEvent* event) { if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) { //swipeTriggered(static_cast(swipe)); } else if (QGesture *pan = event->gesture(Qt::PanGesture)) panTriggered(static_cast(pan)); return true; if (QGesture *pinch = event->gesture(Qt::PinchGesture)) { pinchTriggered(static_cast(pinch)); return true; } return false; } void MapView2D::pinchTriggered(QPinchGesture* gesture) { tools.pinchTriggered(this, gesture); update(); } void MapView2D::panTriggered(QPanGesture* gesture) { tools.panTriggered(this, gesture); update(); }