initial commit

This commit is contained in:
kazu
2016-05-24 16:55:19 +02:00
commit 13a89df8d6
77 changed files with 7454 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
#ifndef MV2DELEMENT_H
#define MV2DELEMENT_H
#include <QKeyEvent>
#include "../MapView2D.h"
#include "../Painter.h"
#include <Indoor/geo/BBox2.h>
#include <Indoor/geo/Line2.h>
/**
* represents one drawable, selectable, editable, ...
* element shown within the MapView2D
*/
class MV2DElement {
private:
bool _focused = false;
public:
/** dtor */
virtual ~MV2DElement() {;}
/** get the element's 2D bounding box */
virtual BBox2 getBoundingBox() const = 0;
/** get the element's minimal distance (nearest whatsoever) to the given point */
virtual float getMinDistanceXY(const Point2 p) const = 0;
/** repaint me */
virtual void paint(Painter& p) = 0;
/** got focus */
void focus() {
_focused = true;
onFocus();
}
/** lost focus */
void unfocus() {
_focused = false;
onUnfocus();
}
bool hasFocus() {return _focused;}
/** mouse pressed at the given point */
virtual void mousePressed(MapView2D* v, const Point2 p) = 0;
/** mouse moved to the given point */
virtual void mouseMove(MapView2D* v, const Point2 p) = 0;
/** mouse released */
virtual void mouseReleased(MapView2D* v, const Point2 p) = 0;
/** key pressed. return true when consumed. */
virtual bool keyPressEvent(MapView2D* v, QKeyEvent* e) = 0;
protected:
virtual void onFocus() = 0;
virtual void onUnfocus() = 0;
};
#endif // MV2DELEMENT_H

View File

@@ -0,0 +1,97 @@
#ifndef MV2DELEMENTACCESSPOINT_H
#define MV2DELEMENTACCESSPOINT_H
#include "MV2DElement.h"
#include "MapViewElementHelper.h"
#include <Indoor/floorplan/v2/Floorplan.h>
class MV2DElementAccessPoint : public MV2DElement {
private:
bool sel = false;
Floorplan::AccessPoint* ap;
public:
/** ctor with the AP to render/edit */
MV2DElementAccessPoint(Floorplan::AccessPoint* ap) : ap(ap) {;}
/** get the element's 3D bounding box */
BBox2 getBoundingBox() const override {
BBox2 bbox;
bbox.add(Point2(ap->pos.x, ap->pos.y));
bbox.grow(Point2(0.1, 0.1));
return bbox;
}
/** get the element's minimal distance (nearest whatsoever) to the given point */
float getMinDistanceXY(const Point2 p) const override {
return p.getDistance(ap->pos.xy());
}
/** repaint me */
void paint(Painter& p) override {
if (sel) {
p.setPenBrush(Qt::black, CFG::SEL_COLOR);
p.drawCircle(ap->pos.xy());
} else if (hasFocus()) {
p.setPenBrush(Qt::black, Qt::NoBrush);
p.drawCircle(ap->pos.xy());
} else {
p.setPenBrush(Qt::gray, Qt::NoBrush);
p.drawCircle(ap->pos.xy());
}
// label
if (p.getScaler().getScale() >= 25) {
p.p->drawText(p.getScaler().xms(ap->pos.x) + 10, p.getScaler().yms(ap->pos.y) + 5, ap->mac.c_str());
}
}
/** mouse pressed at the given point */
virtual void mousePressed(MapView2D* v, const Point2 p) override {
(void) v;
(void) p;
}
/** mouse moved to the given point */
virtual void mouseMove(MapView2D* v, const Point2 _p) override {
(void) v;
if (sel) {
const Point2 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
ap->pos.x = p.x;
ap->pos.y = p.y;
}
}
/** mouse released */
virtual void mouseReleased(MapView2D* v, const Point2 _p) override {
(void) v;
(void) _p;
sel = true;
}
virtual bool keyPressEvent(MapView2D* v, QKeyEvent *e) override {
(void) v;
(void) e;
return false;
}
virtual void onFocus() override {
}
virtual void onUnfocus() override {
sel = false;
}
};
#endif // MV2DELEMENTACCESSPOINT_H

View File

@@ -0,0 +1,94 @@
#ifndef MV2DELEMENTBEACON_H
#define MV2DELEMENTBEACON_H
#include "MV2DElement.h"
#include "MapViewElementHelper.h"
#include <Indoor/floorplan/v2/Floorplan.h>
class MV2DElementBeacon : public MV2DElement {
private:
bool sel = false;
Floorplan::Beacon* b;
public:
/** ctor with the Beacon to render/edit */
MV2DElementBeacon(Floorplan::Beacon* b) : b(b) {;}
/** get the element's 3D bounding box */
BBox2 getBoundingBox() const override {
BBox2 bbox;
bbox.add(Point2(b->pos.x, b->pos.y));
bbox.grow(Point2(0.1, 0.1));
return bbox;
}
/** get the element's minimal distance (nearest whatsoever) to the given point */
float getMinDistanceXY(const Point2 p) const override {
return p.getDistance(b->pos.xy());
}
/** repaint me */
void paint(Painter& p) override {
if (sel) {
p.setPenBrush(Qt::black, CFG::SEL_COLOR);
p.drawCircle(b->pos.xy());
} else if (hasFocus()) {
p.setPenBrush(Qt::black, Qt::NoBrush);
p.drawCircle(b->pos.xy());
} else {
p.setPenBrush(Qt::gray, Qt::NoBrush);
p.drawCircle(b->pos.xy());
}
// label
if (p.getScaler().getScale() >= 25) {
p.p->drawText(p.getScaler().xms(b->pos.x) + 10, p.getScaler().yms(b->pos.y) + 5, b->mac.c_str());
}
}
virtual void onFocus() override {
;
}
virtual void onUnfocus() override {
sel = false;
}
/** mouse pressed at the given point */
virtual void mousePressed(MapView2D* v, const Point2 p) override {
(void) v;
(void) p;
}
/** mouse moved to the given point */
virtual void mouseMove(MapView2D* v, const Point2 _p) override {
(void) v;
if (sel) {
const Point2 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
b->pos.x = p.x;
b->pos.y = p.y;
}
}
/** mouse released */
virtual void mouseReleased(MapView2D* v, const Point2 p) override {
(void) v;
(void) p;
sel = true;
}
virtual bool keyPressEvent(MapView2D* v, QKeyEvent *e) override {
(void) v;
(void) e;
return false;
}
};
#endif // MV2DELEMENTBEACON_H

View File

@@ -0,0 +1,108 @@
#ifndef MV2DELEMENTFLOOROBSTACLECIRCLE_H
#define MV2DELEMENTFLOOROBSTACLECIRCLE_H
#include "MV2DElement.h"
#include "MapViewElementHelper.h"
#include <Indoor/floorplan/v2/Floorplan.h>
class MV2DElementFloorObstacleCircle : public MV2DElement {
private:
int selPoint = -1;
Floorplan::FloorObstacleCircle* c;
public:
/** ctor */
MV2DElementFloorObstacleCircle(Floorplan::FloorObstacleCircle* c) : c(c) {;}
/** get the element's 3D bounding box */
BBox2 getBoundingBox() const override {
BBox2 bbox;
bbox.add(c->center);
bbox.grow(Point2(c->radius, c->radius));
return bbox;
}
/** get the element's minimal distance (nearest whatsoever) to the given point */
float getMinDistanceXY(const Point2 p) const override {
return std::min(p.getDistance(getSelPoints()[0]), p.getDistance(getSelPoints()[1]));
}
/** repaint me */
void paint(Painter& p) override {
QPen pen = MapElementHelper::getPen(c->material, c->type, hasFocus());
p.setPenBrush(pen, Qt::NoBrush);
p.drawCircle(c->center, c->radius);
//QBrush brush = MapElementHelper::getBru(c->material, c->type, _focused);
// selected endpoints?
if (hasFocus()) {
p.setPenBrush(Qt::NoPen, CFG::SEL_COLOR);
if (selPoint == 0) {p.drawCircle(getSelPoints()[0]);}
if (selPoint == 1) {p.drawCircle(getSelPoints()[1]);}
}
// available endpoints
if (hasFocus()) {
p.setPenBrush(Qt::black, Qt::NoBrush);
p.drawCircle(getSelPoints()[0]);
p.drawCircle(getSelPoints()[1]);
}
}
std::vector<Point2> getSelPoints() const {
return {c->center, (c->center + Point2(c->radius,0))};
}
virtual void onFocus() override {
;
}
virtual void onUnfocus() override {
selPoint = -1;
}
/** mouse pressed at the given point */
virtual void mousePressed(MapView2D* v, const Point2 p) override {
(void) v;
(void) p;
}
/** mouse moved to the given point */
virtual void mouseMove(MapView2D* v, const Point2 _p) override {
(void) v;
if (selPoint == -1) {return;}
const Point2 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
if (selPoint == 0) {c->center = p;}
if (selPoint == 1) {c->radius = p.getDistance(c->center);}
}
/** mouse released */
virtual void mouseReleased(MapView2D* v, const Point2 _p) override {
const float t = v->getScaler().sm(CFG::SEL_THRESHOLD_SIZE_PX);
const float l1 = _p.getDistance(getSelPoints()[0]);
const float l2 = _p.getDistance(getSelPoints()[1]);
if (l1 < l2 && l1 <= t) {selPoint = 0;}
else if (l2 < l1 && l2 <= t) {selPoint = 1;}
else {selPoint = -1;}
}
virtual bool keyPressEvent(MapView2D* v, QKeyEvent *e) override {
(void) v;
(void) e;
return false;
}
};
#endif // MV2DELEMENTFLOOROBSTACLECIRCLE_H

View File

@@ -0,0 +1,108 @@
#ifndef MV2DELEMENTFLOOROBSTACLELINE_H
#define MV2DELEMENTFLOOROBSTACLELINE_H
#include "MV2DElement.h"
#include "MapViewElementHelper.h"
#include <Indoor/floorplan/v2/Floorplan.h>
class MV2DElementFloorObstacleLine : public MV2DElement {
private:
int selPoint = -1;
Floorplan::FloorObstacleLine* fo;
public:
/** ctor */
MV2DElementFloorObstacleLine(Floorplan::FloorObstacleLine* fo) : fo(fo) {;}
/** get the element's 3D bounding box */
BBox2 getBoundingBox() const override {
BBox2 bbox;
bbox.add(fo->from);
bbox.add(fo->to);
return bbox;
}
/** get the element's minimal distance (nearest whatsoever) to the given point */
float getMinDistanceXY(const Point2 p) const override {
return MapElementHelper::getLineDistanceXY(fo->from, fo->to, p);
}
/** repaint me */
void paint(Painter& p) override {
// selected endpoints?
if (hasFocus()) {
p.setPenBrush(Qt::NoPen, CFG::SEL_COLOR);
if (selPoint == 0) {p.drawCircle(fo->from);}
if (selPoint == 1) {p.drawCircle(fo->to);}
}
// line
p.setPenBrush(MapElementHelper::getPen(fo->material, fo->type, hasFocus()), Qt::NoBrush);
p.drawLine(fo->from, fo->to);
// available endpoints
if (hasFocus()) {
p.setPenBrush(Qt::black, Qt::NoBrush);
p.drawCircle(fo->from);
p.drawCircle(fo->to);
}
}
virtual void onFocus() override {
;
}
virtual void onUnfocus() override {
selPoint = -1; // clear selection
}
/** mouse pressed at the given point */
virtual void mousePressed(MapView2D* v, const Point2 p) override {
(void) v;
(void) p;
}
/** mouse moved to the given point */
virtual void mouseMove(MapView2D* v, const Point2 _p) override {
(void) v;
if (selPoint == -1) {return;}
const Point2 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
if (selPoint == 0) {fo->from.x = p.x; fo->from.y = p.y;}
if (selPoint == 1) {fo->to.x = p.x; fo->to.y = p.y;}
}
/** mouse released */
virtual void mouseReleased(MapView2D* v, const Point2 _p) override {
// (void) v;
// if (selPoint == -1) {return;}
// const Point3 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
// if (selPoint == 0) {fo.from.x = p.x; fo.from.y = p.y;}
// if (selPoint == 1) {fo.to.x = p.x; fo.to.y = p.y;}
// select a new point on mouse-release (more robust than on mouse-press)
const float t = v->getScaler().sm(CFG::SEL_THRESHOLD_SIZE_PX);
const float l1 = _p.getDistance(fo->from);
const float l2 = _p.getDistance(fo->to);
if (l1 < l2 && l1 <= t) {selPoint = 0;}
else if (l2 < l1 && l2 <= t) {selPoint = 1;}
else {selPoint = -1;}
}
virtual bool keyPressEvent(MapView2D* v, QKeyEvent *e) override {
(void) v;
(void) e;
return false;
}
};
#endif // MV2DELEMENTFLOOROBSTACLELINE_H

View File

@@ -0,0 +1,152 @@
#ifndef MV2DELEMENTFLOOROUTLINEPOLYGON_H
#define MV2DELEMENTFLOOROUTLINEPOLYGON_H
#include "MV2DElement.h"
#include "MapViewElementHelper.h"
#include <Indoor/floorplan/v2/Floorplan.h>
class MV2DElementFloorOutlinePolygon : public MV2DElement {
private:
int selPoint = -1;
Floorplan::FloorOutlinePolygon& fo;
public:
/** ctor */
MV2DElementFloorOutlinePolygon(Floorplan::FloorOutlinePolygon& fo) : fo(fo) {;}
/** get the element's 3D bounding box */
BBox2 getBoundingBox() const override {
BBox2 bbox;
for (const Point2 p : fo.poly.points) { bbox.add(p); }
return bbox;
}
/** get the element's minimal distance (nearest whatsoever) to the given point */
float getMinDistanceXY(const Point2 p) const override {
float min = 999999;
for (int i = 0; i < (int)fo.poly.points.size()-1; ++i) {
const Point2 p1 = fo.poly.points[i];
const Point2 p2 = fo.poly.points[i+1];
const float dst = MapElementHelper::getLineDistanceXY(p1, p2, p);
if (dst < min) {min = dst;}
}
return min;
}
virtual void onFocus() override {
}
virtual void onUnfocus() override {
selPoint = -1; // clear selection
}
void paint(Painter& p) override {
QBrush brush;
// fill-style (depends on the mode)
switch (fo.method) {
case Floorplan::OutlineMethod::ADD:
brush.setStyle(Qt::BrushStyle::SolidPattern);
brush.setColor(QColor(0,0,0,24));
break;
case Floorplan::OutlineMethod::REMOVE:
brush.setStyle(Qt::BrushStyle::DiagCrossPattern);
brush.setColor(QColor(0,0,0));
break;
default:
brush.setStyle(Qt::BrushStyle::SolidPattern);
brush.setColor(QColor(255,0,0));
}
// outline + filled area
p.setPenBrush(Qt::black, brush);
p.drawPolygon(fo.poly.points);
// selected endpoints?
if (hasFocus() && selPoint != -1) {
p.setPenBrush(Qt::NoPen, CFG::SEL_COLOR);
p.drawCircle(fo.poly.points[selPoint]);
}
// available endpoints
if (hasFocus()) {
p.setPenBrush(Qt::black, Qt::NoBrush);
for (const Point2 pt : fo.poly.points) {
p.drawCircle(pt);
}
}
}
virtual void mousePressed(MapView2D* v, const Point2 p) override {
(void) v;
(void) p;
}
virtual void mouseMove(MapView2D* v, const Point2 _p) override {
(void) v;
if (selPoint == -1) {return;}
const Point2 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
fo.poly.points[selPoint].x = p.x;
fo.poly.points[selPoint].y = p.y;
}
virtual void mouseReleased(MapView2D* v, const Point2 _p) override {
(void) v;
// if (selPoint != -1) {
// const Point3 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
// fo.poly.points[selPoint].x = p.x;
// fo.poly.points[selPoint].y = p.y;
// }
// select a new point on mouse-release (more robust than on mouse-press)
const float t = v->getScaler().sm(CFG::SEL_THRESHOLD_SIZE_PX);
auto comp = [&] (const Point2 a, const Point2 b) {return a.getDistance(_p) < b.getDistance(_p);};
auto it = std::min_element(fo.poly.points.begin(), fo.poly.points.end(), comp);
if (it == fo.poly.points.end()) {selPoint = -1;} // none found -> skip
else if ((*it).getDistance(_p) > t) {selPoint = -1;} // nearest distance is above threshold -> skip
else {selPoint = it - fo.poly.points.begin();}
}
virtual bool keyPressEvent(MapView2D* v, QKeyEvent *e) override {
(void) v;
if (e->key() == Qt::Key_Delete) {
// delete the currently selected vertex?
if (selPoint != -1) {
fo.poly.points.erase(fo.poly.points.begin() + selPoint);
selPoint = -1;
return true;
}
} else if (e->key() == Qt::Key_Plus && selPoint != -1) {
int idx1 = selPoint;
int idx2 = (selPoint + 1) % fo.poly.points.size();
int idxNew = idx2;
Point2 pNew = (fo.poly.points[idx1] + fo.poly.points[idx2]) / 2.0f;
fo.poly.points.insert(fo.poly.points.begin() + idxNew, pNew);
selPoint = idxNew;
return true;
}
// not consumed
return false;
}
};
#endif // MV2DELEMENTFLOOROUTLINEPOLYGON_H

View File

@@ -0,0 +1,136 @@
#ifndef MV2DELEMENTFLOORUNDERLAY_H
#define MV2DELEMENTFLOORUNDERLAY_H
#include "MV2DElement.h"
#include "MapViewElementHelper.h"
#include <Indoor/floorplan/v2/Floorplan.h>
/**
* display e.g. a PNG-file below the map [as reference]
*/
class MV2DElementFloorUnderlay : public MV2DElement {
private:
QImage img;
std::string tmpFile;
Floorplan::UnderlayImage* underlay;
BBox2 bbox;
int selPoint = -1;
public:
/** ctor */
MV2DElementFloorUnderlay(Floorplan::UnderlayImage* underlay) : underlay(underlay) {
missing();
}
void missing() {
//img = QImage(QSize(64, 64), QImage::Format_ARGB32);
//img.fill(0xFF000000);
img = QImage("://res/icons/cross.png");
}
/** get the element's 3D bounding box */
BBox2 getBoundingBox() const override {
return bbox;
}
/** get the element's minimal distance (nearest whatsoever) to the given point */
float getMinDistanceXY(const Point2 p) const override {
(void) p;
return CFG::SEL_THRESHOLD_SIZE_PX; // we do not know the distance from the image
}
virtual void onFocus() override {
}
virtual void onUnfocus() override {
selPoint = -1; // clear selection
}
void paint(Painter& p) override {
(void) p;
if (tmpFile != underlay->filename) {
img = QImage(underlay->filename.c_str());
if (img.size().width() == 0) {
missing();
}
tmpFile = underlay->filename;
}
// map coordinates
float mx1 = underlay->anchor.x;
float my1 = underlay->anchor.y;
float mw = img.width() * underlay->scaleX;
float mh = img.height() * underlay->scaleY;
float mx2 = mx1+mw;
float my2 = my1+mh;
// screen coordinates
float sx1 = p.s.xms(mx1);
float sy1 = p.s.yms(my1);
float sx2 = p.s.xms(mx2);
float sy2 = p.s.yms(my2);
float sw = abs(sx1-sx2);
float sh = abs(sy1-sy2);
bbox = BBox2();
bbox.add(Point2(mx1, my1));
bbox.add(Point2(mx2, my2));
p.p->drawImage(QRectF(sx1, sy1-sh, sw, sh), img, QRectF(0,0,img.width(),img.height()));
// selected endpoint(s)?
if (hasFocus()) {
p.setPenBrush(Qt::NoPen, CFG::SEL_COLOR);
if (selPoint == 0) {p.drawCircle(bbox.getMin());}
}
// just focused?
if (hasFocus()) {
p.setPenBrush(Qt::black, Qt::NoBrush);
p.drawRect(mx1,my1,mx2,my2);
p.drawCircle(bbox.getMin());
}
}
virtual void mousePressed(MapView2D* v, const Point2 p) override {
(void) v;
(void) p;
}
virtual void mouseMove(MapView2D* v, const Point2 _p) override {
(void) v;
if (selPoint == -1) {return;}
const Point2 p = Scaler::snap(_p, CFG::MOVE_SNAP_SIZE_M);
if (selPoint == 0) {underlay->anchor = p;}
}
virtual void mouseReleased(MapView2D* v, const Point2 _p) override {
// select a new point on mouse-release (more robust than on mouse-press)
const float t = v->getScaler().sm(CFG::SEL_THRESHOLD_SIZE_PX);
const float l1 = _p.getDistance(bbox.getMin());
if (l1 <= t) {selPoint = 0;}
else {selPoint = -1;}
}
virtual bool keyPressEvent(MapView2D* v, QKeyEvent *e) override {
(void) v;
(void) e;
return false;
}
};
#endif // MV2DELEMENTFLOORUNDERLAY_H

View File

@@ -0,0 +1,74 @@
#ifndef MAPELEMENTHELPER_H
#define MAPELEMENTHELPER_H
#include <Indoor/geo/Point2.h>
#include <Indoor/geo/Point3.h>
#include <Indoor/geo/Line2.h>
#include <QColor>
#include <QPen>
#include <QBrush>
#include <Indoor/floorplan/v2/Floorplan.h>
/** configuration */
namespace CFG {
const float MOVE_SNAP_SIZE_M = 0.1f; // in meter (= map-space)
const int SEL_THRESHOLD_SIZE_PX = 15; // in screen-pixels (-> should depend on the current zoom)
const QColor SEL_COLOR = Qt::blue;
}
/**
* contains some common helper-methods
*/
class MapElementHelper {
public:
/**
* get the minimal distance of dst to the line denoted by (p1, p2).
* this will generate line l perpendicular to (p1, p2)
* move l into dst
* and calculate the cut-point between l and (p1, p2)
*/
static float getLineDistanceXY(Point2 p1, Point2 p2, Point2 dst) {
// the line (p1, p2)
const Line2 line(p1, p2);
// 90 degree rotation L of the line (p1, p2)
const Point2 vec = (p1 - p2).perpendicular();
// the line L
const Line2 perb(dst-vec*100, dst+vec*100);
// calculate the cut betwen L and (p1,p2) (if any)
Point2 cut(0,0);
if (line.getSegmentIntersection(perb, cut)) {
// distance between cut-point and mouse
return cut.getDistance(dst);
} else {
// no cut detected
return 9999999;
}
}
static QPen getPen(Floorplan::Material mat, Floorplan::ObstacleType type, bool focus) {
using namespace Floorplan;
QPen pen; pen.setColor(Qt::darkGray);
if (focus) {pen.setColor(Qt::black);}
if (mat == Material::CONCRETE) {pen.setWidth(3);}
if (mat == Material::GLASS) {pen.setStyle(Qt::PenStyle::DotLine);}
if (type == ObstacleType::HANDRAIL) {pen.setStyle(Qt::PenStyle::DashLine);}
return pen;
}
};
#endif // MAPELEMENTHELPER_H