137 lines
2.7 KiB
C++
137 lines
2.7 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)
|
||
*/
|
||
|
||
#ifndef TOOLMEASURE_H
|
||
#define TOOLMEASURE_H
|
||
|
||
|
||
#include "Tool.h"
|
||
#include "../MapView2D.h"
|
||
|
||
#include "../../model/MapModelElement.h"
|
||
#include "../../model/MapModel.h"
|
||
#include "../MapViewElementHelper.h"
|
||
|
||
|
||
/**
|
||
* this tool allows:
|
||
* - selecting elements within the 2D view (focus/unfocus)
|
||
* - selecting and moving nodes of elements inheriting from HasMoveableNodes
|
||
*
|
||
*/
|
||
class ToolMeasure : public Tool {
|
||
|
||
Q_OBJECT
|
||
|
||
|
||
protected:
|
||
|
||
/** register this tool into the given tools-queue */
|
||
Tools& tools;
|
||
|
||
std::vector<Point2> pts_m;
|
||
|
||
public:
|
||
|
||
/** ctor */
|
||
ToolMeasure(Tools& tools) : tools(tools) {
|
||
resetMe();
|
||
}
|
||
|
||
/** dtor */
|
||
virtual ~ToolMeasure() {
|
||
tools.setMainDefault();
|
||
}
|
||
|
||
const std::string getName() const {
|
||
return "Measure";
|
||
}
|
||
|
||
virtual bool mousePressEvent(MapView2D*, QMouseEvent* e) override {
|
||
if (e->button() == Qt::MouseButton::LeftButton) {
|
||
pts_m.push_back(pts_m.back());
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
virtual bool mouseMoveEvent(MapView2D* m, QMouseEvent* e) override {
|
||
const Point2 onScreen(e->x(), e->y());
|
||
Point2 onMap = m->getScaler().sm(onScreen);
|
||
onMap = m->getScaler().snap(onMap);
|
||
pts_m.back() = onMap;
|
||
return true;
|
||
}
|
||
|
||
virtual bool mouseReleaseEvent(MapView2D*, QMouseEvent* e) override {
|
||
if (e->button() == Qt::MouseButton::LeftButton) {
|
||
return true;
|
||
} else if (e->button() == Qt::MouseButton::RightButton) {
|
||
resetMe();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
virtual bool keyPressEvent(MapView2D* m, QKeyEvent* e) override {
|
||
(void) m;
|
||
if (e->key() == Qt::Key_Escape) {
|
||
disableMe();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
virtual void paintAfter(MapView2D*, Painter& p) override {
|
||
|
||
if (pts_m.size() < 1) {return;}
|
||
|
||
p.setPen(Qt::black);
|
||
|
||
for (const Point2 p_m : pts_m) {
|
||
p.drawCircle(p_m);
|
||
}
|
||
|
||
float totalLen_m = 0;
|
||
for (size_t i = 0; i < pts_m.size() - 1; ++i) {
|
||
const Point2 p1 = pts_m[i];
|
||
const Point2 p2 = pts_m[i+1];
|
||
const float len_m = p1.getDistance(p2);
|
||
p.drawLine(p1, p2);
|
||
p.drawLength(p1, p2, len_m);
|
||
totalLen_m += len_m;
|
||
}
|
||
|
||
if (pts_m.size() > 1) {
|
||
emit onHelpTextChange("total length is: " + QString::number(totalLen_m) + "m | right-click to restart");
|
||
}
|
||
|
||
}
|
||
|
||
protected:
|
||
|
||
|
||
void resetMe() {
|
||
pts_m.resize(1);
|
||
emit onHelpTextChange("select the starting point for measuring");
|
||
}
|
||
|
||
/** finish creating new elements */
|
||
void disableMe() {
|
||
delete this; // see dtor!
|
||
}
|
||
|
||
};
|
||
|
||
|
||
#endif // TOOLMEASURE_H
|