This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
IndoorMap/mapview/2D/MapView2D.cpp
kazu 5d002c3f2b added more cpp files for faster compile speeds
removed many obsolte elements
many improvements and fixes
2018-07-20 15:00:43 +02:00

145 lines
3.1 KiB
C++

#include "../../fixC11.h"
#include "MapView2D.h"
#include <QPainter>
#include <QOpenGLFunctions>
#include <QMouseEvent>
#include <QKeyEvent>
#include <iostream>
#include "../model/MapModelElement.h"
#include "../model/MapModel.h"
MapView2D::MapView2D(QWidget* parent) : QOpenGLWidget(parent) {
// openGL params
QSurfaceFormat format;
format.setDepthBufferSize(8); // 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);
qp.setRenderHint( QPainter::Antialiasing, true );
// render all visible elements. 1st run
for (MapModelElement* el : getModel()->getVisibleElements()) {
if (el->getMV2D()) {el->getMV2D()->paint(p);}
}
// render all visible elements. 2nd run
for (MapModelElement* el : getModel()->getVisibleElements()) {
if (el->getMV2D()) {el->getMV2D()->paintAfter(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(int w, int h) {
(void) w;
(void) h;
// 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<QGestureEvent*>(event));
} else {
return QWidget::event(event);
}
}
bool MapView2D::gestureEvent(QGestureEvent* event) {
if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) {
(void) swipe;
//swipeTriggered(static_cast<QSwipeGesture *>(swipe));
} else if (QGesture *pan = event->gesture(Qt::PanGesture)) {
panTriggered(static_cast<QPanGesture *>(pan));
return true;
}
if (QGesture *pinch = event->gesture(Qt::PinchGesture)) {
pinchTriggered(static_cast<QPinchGesture *>(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();
}