170 lines
3.8 KiB
C++
170 lines
3.8 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#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);
|
||
|
||
|
||
|
||
|
||
std::vector<MV2DElement*> normal;
|
||
std::vector<MV2DElement*> focused;
|
||
|
||
// get all visible elements that can be rendered
|
||
// group them whether they currently have focus or not
|
||
for (MapModelElement* el : getModel()->getVisibleElements()) {
|
||
if (el->getMV2D()) {
|
||
if (el->getMV2D()->hasFocus()) {
|
||
focused.push_back(el->getMV2D());
|
||
} else {
|
||
normal.push_back(el->getMV2D());
|
||
}
|
||
}
|
||
}
|
||
|
||
qp.setRenderHint( QPainter::Antialiasing, true );
|
||
|
||
// 2 layer rendering (paint, paintAfter), only UNFOCUSED elements
|
||
for (MV2DElement* e : normal) {e->paint(p);}
|
||
for (MV2DElement* e : normal) {e->paintAfter(p);}
|
||
|
||
// 2 layer rendering (paint, paintAfter), only FOCUSED elements
|
||
for (MV2DElement* e : focused) {e->paint(p);}
|
||
for (MV2DElement* e : focused) {e->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();
|
||
}
|