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/Painter.cpp
2018-10-25 12:15:13 +02:00

188 lines
5.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* © 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 "Painter.h"
#include <QPainter>
#include <QBitmap>
#include <Indoor/geo/Point2.h>
#include <Indoor/geo/Point3.h>
#include <Indoor/geo/BBox2.h>
#include "Scaler.h"
Painter::Painter(Scaler& s, QPainter* p, int w, int h) : s(s), p(p), w(w), h(h) {
p->setPen(Qt::black);
p->setBrush(Qt::NoBrush);
}
int Painter::width() {return w;}
int Painter::height() {return h;}
float Painter::radToDeg(const float rad) const {
return rad * 180 / M_PI;
}
bool Painter::isVisible(const Point2 p) {
const float x = s.xms(p.x);
const float y = s.yms(p.y);
return (x >= 0 && y >= 0 && x < w && y < h);
}
bool Painter::isVisible(const BBox2 bb) {
const BBox2 bbt(0,0,w,h);
BBox2 bbs;
bbs.add(s.xms(bb.getMin().x), s.yms(bb.getMin().y)); // y might be inverted! -> use the add() method to address this
bbs.add(s.xms(bb.getMax().x), s.yms(bb.getMax().y)); // y might be inverted! -> use the add() method to address this
return bbt.intersects(bbs);
}
void Painter::drawLine(const Point2 p1, const Point2 p2) {
//p->drawLine(s.xms(p1.x), s.yms(p1.y), s.xms(p2.x), s.yms(p2.y));
p->drawLine(QPointF(s.xms(p1.x), s.yms(p1.y)), QPointF(s.xms(p2.x), s.yms(p2.y)));
}
void Painter::drawLine(const Point3 p1, const Point3 p2) {
//p->drawLine(s.xms(p1.x), s.yms(p1.y), s.xms(p2.x), s.yms(p2.y));
p->drawLine(QPointF(s.xms(p1.x), s.yms(p1.y)), QPointF(s.xms(p2.x), s.yms(p2.y)));
}
void Painter::drawLine(const float x1, const float y1, const float x2, const float y2) {
p->drawLine(QPointF(s.xms(x1), s.yms(y1)), QPointF(s.xms(x2), s.yms(y2)));
}
void Painter::drawArc(const Point2 center, const float radius, const float startAngleRad, const float spanAngleRad) {
const float wh = s.ms(radius) * 2;
const float x1 = s.xms(center.x) - wh/2;
const float y1 = s.yms(center.y) - wh/2;
const QRectF rect(x1, y1, wh, wh);
p->drawArc(rect, radToDeg(startAngleRad)*16, radToDeg(spanAngleRad)*16);
//p->drawArc(x1, y1, wh, wh, radToDeg(startAngleRad)*16, radToDeg(spanAngleRad)*16);
}
/** draw a dot at the given map coordinates */
void Painter::drawDot(const Point2 center) {
//int r = 1;
//p->drawEllipse(QPointF(s.xms(center.x)-r, s.yms(center.y)-r), 2*r, 2*r);
p->drawPoint(QPointF(s.xms(center.x), s.yms(center.y)));
}
void Painter::drawCircle(const Point3 center) {
int r = 5;
p->drawEllipse(QPointF(s.xms(center.x), s.yms(center.y)), r, r);
}
void Painter::drawCircle(const Point2 center) {
int r = 5;
p->drawEllipse(QPointF(s.xms(center.x), s.yms(center.y)), r, r);
}
void Painter::drawCircle(const Point2 center, const float size_m) {
int r = s.ms(size_m);
p->drawEllipse(QPointF(s.xms(center.x), s.yms(center.y)), r, r);
}
void Painter::drawCircle_px(const Point2 center, const float size_px) {
int r = size_px;
p->drawEllipse(QPointF(s.xms(center.x), s.yms(center.y)), r, r);
}
void Painter::drawNode(Point2 pt, bool focused, bool selected) {
float rs = 4;
if (selected) {setPenBrush(Qt::black, Qt::gray); rs*=2;}
else if (focused) {setPenBrush(Qt::black, Qt::white); rs*=2;}
else {setPenBrush(Qt::black, Qt::NoBrush); rs*=1;}
const float s = this->s.getScale();
if (s > 25) {drawCircle_px(pt, rs);} // only at a certain zoom level
else if (s > 10) {drawCircle_px(pt, 1);} // only at a certain zoom level
}
void Painter::drawRect(const Point2 p1, const Point2 p2) {
drawRect(p1.x, p1.y, p2.x, p2.y);
}
void Painter::drawRect(const float x1, const float y1, const float x2, const float y2) {
const float w = x2-x1;
const float h = y1-y2;
const QRectF rect(s.xms(x1), s.yms(y1), s.ms(w), s.ms(h));
p->drawRect(rect);
//p->drawRect(s.xms(x1), s.yms(y1), s.ms(w), s.ms(h));
}
void Painter::drawRect(const Point2 center) {
float r = s.sm(5); // 5 pixel
drawRect(center-Point2(r,r), center+Point2(r,r));
}
void Painter::drawText(const Point2 pos, const std::string& text) {
p->drawText(QPointF(s.xms(pos.x), s.yms(pos.y)), text.c_str());
}
void Painter::drawPolygon(const std::vector<Point2>& points) {
std::vector<QPointF> vec;
for (const Point2 p : points) {
vec.push_back(QPointF(s.xms(p.x), s.yms(p.y)));
}
p->drawPolygon(vec.data(), vec.size());
}
void Painter::drawPolygon(const std::vector<Point3>& points) {
std::vector<QPointF> vec;
for (const Point3 p : points) {
vec.push_back(QPointF(s.xms(p.x), s.yms(p.y)));
}
p->drawPolygon(vec.data(), vec.size());
}
void Painter::drawPixmap(const Point2 pt, const QPixmap& img) {
p->drawPixmap(s.xms(pt.x)-img.width()/2, s.yms(pt.y)-img.height()/2, img);
}
void Painter::drawImage(const Point2 pt, const QImage& img) {
p->drawImage(s.xms(pt.x)-img.width()/2, s.yms(pt.y)-img.height()/2, img);
}
void Painter::drawLength(Point2 p1, Point2 p2, const float len, const float offset) {
if (p1.x < p2.x) {swap(p1, p2);}
const Point2 center_m = (p1 + p2) / 2 - Point2(0.5,0);
Point2 dir_px = (p2 - p1).perpendicular().normalized() * (5+offset);
if (dir_px.x <= 0) {dir_px = -dir_px;}
const Point2 pos_m = center_m + dir_px / getScaler().getScale();
char buf[64]; sprintf(buf, "%.1f", len);
drawText(pos_m, buf);
}
void Painter::setBrush(const QBrush& brush) { p->setBrush(brush); }
void Painter::setBrush(const Qt::BrushStyle& brush) { p->setBrush(brush); }
void Painter::setPen(const QPen& pen) {p->setPen(pen); }
void Painter::setPen(const QColor& pen) {p->setPen(pen); }
void Painter::setPen(const Qt::PenStyle& pen) {p->setPen(pen); }
const QPen& Painter::getPen() {return p->pen();}
const Scaler& Painter::getScaler() {return s;}