添加地图上移动围栏的功能

This commit is contained in:
hm
2022-05-18 18:04:15 +08:00
parent 0574b2d49e
commit af95cccf82
12 changed files with 418 additions and 29 deletions
+115 -12
View File
@@ -10,9 +10,13 @@ geoFencecircle::geoFencecircle(int group, bool inclusion, int version, internals
QGraphicsEllipseItem(map),m_center(center), coord(center), m_brush(color), m_map(map),dashed(dashed),lineWidth(width)
{
this->setAcceptHoverEvents(true);
this->setZValue(3);
this->setFlag(QGraphicsItem::ItemIsMovable, true);
this->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
//this->setFlag(QGraphicsItem::ItemIsSelectable, true);//这个有bug
m_version = version;
m_inclusion = inclusion;
m_radius = radius;
@@ -21,6 +25,9 @@ geoFencecircle::geoFencecircle(int group, bool inclusion, int version, internals
m_longitide = coord.Lng();//6
connect(map, SIGNAL(childRefreshPosition()), this, SLOT(refreshLocations()));
refreshLocations();
}
@@ -54,16 +61,21 @@ void geoFencecircle::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
}
//painter->translate(-line.length(), -line.length());
if(m_inclusion)
if(enter)
{
painter->setBrush(Qt::NoBrush);
painter->setOpacity(0.3);
painter->setBrush(QColor("#FFFFFF"));
}
else
{
painter->setBrush(m_brush);
if(m_inclusion)
{
painter->setBrush(Qt::NoBrush);
}
else
{
painter->setBrush(m_brush);
}
}
@@ -86,8 +98,11 @@ void geoFencecircle::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
void geoFencecircle::refreshLocations()
{
QPointF p(m_map->FromLatLngToLocal(this->coord).X(),
m_map->FromLatLngToLocal(this->coord).Y());
QPointF p(m_map->FromLatLngToLocal(coord).X(),
m_map->FromLatLngToLocal(coord).Y());
internals::PointLatLng C;
C = coord;
@@ -95,11 +110,17 @@ void geoFencecircle::refreshLocations()
line = QLineF(p.x(),p.y(),p.x(),m_map->FromLatLngToLocal(C).Y());
rectangle.setRect(-line.length(),-line.length(), 2 * line.length(), 2 * line.length());
//qDebug() << line << rectangle << C.Lat() << C.Lng() << m_radius;
//rectangle.setRect(p.x() -line.length(),p.y() -line.length(), 2 * line.length(), 2 * line.length());
rectangle.setRect(p.x(),p.y(), 2 * line.length(), 2 * line.length());
setPos(p);
setRect(rectangle);
/*
qDebug() << p;
setRect(p.x() - line.length(),p.y() - line.length(), 2 * line.length(), 2 * line.length());
*/
update();
}
@@ -113,7 +134,89 @@ void geoFencecircle::setOpacitySlot(qreal opacity)
setOpacity(opacity);
}
void geoFencecircle::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
}
}
void geoFencecircle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
isDragging = true;
qDebug() << "circle press";
}
QGraphicsItem::mousePressEvent(event);
update();
}
void geoFencecircle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
isDragging = false;
qDebug() << "circle release";
}
QGraphicsItem::mouseReleaseEvent(event);
update();
}
void geoFencecircle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (isDragging) {
emit localPositionChanged(this->pos(), this);
//更新一下points
QPointF p = mapToScene(rectangle.center());
m_latitude = m_map->FromLocalToLatLng(p.x(),p.y()).Lat();
m_longitide = m_map->FromLocalToLatLng(p.x(),p.y()).Lng();
/*
coord.SetLat(m_latitude);
coord.SetLng(m_longitide);
m_center = coord;
*/
emit updateFenceCircle(m_group,m_radius,m_inclusion,m_latitude,m_longitide);
// qDebug() << rectangle << pos() << p << m_center.Lat() << m_center.Lng();
//refreshLocations();
}
QGraphicsItem::mouseMoveEvent(event);
update();
}
void geoFencecircle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
qDebug() << "circle enter";
enter = true;
update();
}
void geoFencecircle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
qDebug() << "circle leave";
enter = false;
update();
}
}
+17 -1
View File
@@ -8,6 +8,8 @@
#include <QObject>
#include "mapgraphicitem.h"
#include "geoFenceitem.h"
#include <QGraphicsSceneMouseEvent>
namespace mapcontrol {
@@ -121,16 +123,30 @@ private:
QRectF rectangle;
bool isDragging = false;
bool enter = false;
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
public slots:
void refreshLocations();
void waypointdeleted();
void setOpacitySlot(qreal opacity);
signals:
void updateFenceCircle(int group,qreal radius, bool inclusion, qreal lat, qreal lng);
void localPositionChanged(QPointF point, geoFencecircle *p);
};
}
#endif // GEOFENCECIRCLE_H
+65 -12
View File
@@ -1,13 +1,20 @@
#include "geoFenceitem.h"
#include <QDateTime>
#include <QGraphicsSceneMouseEvent>
namespace mapcontrol {
geoFenceitem::geoFenceitem(int group, bool inclusion, int version, internals::PointLatLng const & coord, QBrush color, MapGraphicItem *map, bool dashed, int width) :
QGraphicsPolygonItem(map), number(0) , coord(coord), m_brush(color), m_map(map), dashed(dashed), lineWidth(width)
{
this->setAcceptHoverEvents(true);
this->setZValue(3);
this->setFlag(QGraphicsItem::ItemIsMovable, true);
this->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
//this->setFlag(QGraphicsItem::ItemIsSelectable, true);//这个有bug
connect(map, SIGNAL(childRefreshPosition()), this, SLOT(RefreshPos()));
@@ -41,13 +48,22 @@ void geoFenceitem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
myPen.setDashPattern(dashes);
}
if(m_inclusion)
if(enter)
{
painter->setBrush(Qt::NoBrush);
painter->setOpacity(0.3);
painter->setBrush(QColor("#FFFFFF"));
}
else
{
painter->setBrush(m_brush);
if(m_inclusion)
{
painter->setBrush(Qt::NoBrush);
}
else
{
painter->setBrush(m_brush);
}
}
painter->drawPoint(0, 0);
@@ -98,8 +114,50 @@ void geoFenceitem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
void geoFenceitem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (isDragging) {
//ElevationTrig();
emit localPositionChanged(this->pos(), this);
//更新一下points
QPolygonF poly = mapToScene(polygon());
QList<internals::PointLatLng> latlng;
QList<QPointF> pointlist;
latlng.clear();
pointlist.clear();
qreal lat_sum = 0;
qreal lng_sum = 0;
for(QPointF p:poly)
{
latlng.push_back(m_map->FromLocalToLatLng(p.x(),p.y()));
pointlist.push_back(QPointF(m_map->FromLocalToLatLng(p.x(),p.y()).Lat(),m_map->FromLocalToLatLng(p.x(),p.y()).Lng()));
lat_sum += m_map->FromLocalToLatLng(p.x(),p.y()).Lat();
lng_sum += m_map->FromLocalToLatLng(p.x(),p.y()).Lng();
}
if(latlng.size() > 0)
{
m_latitude = lat_sum/latlng.size();
m_longitide = lng_sum/latlng.size();
coord.SetLat(m_latitude);
coord.SetLng(m_longitide);
setPoints(latlng);
emit updateFencePolygon(m_group,m_Vertex,m_inclusion,pointlist);
qDebug() << polygon() << pointlist;
}
}
QGraphicsItem::mouseMoveEvent(event);
update();
@@ -109,20 +167,20 @@ void geoFenceitem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
void geoFenceitem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
enter = true;
update();
}
void geoFenceitem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
enter = false;
update();
}
void geoFenceitem::setPosSLOT()
{
//qDebug() << "virtual set pos";
QPolygonF polygon;
for(internals::PointLatLng LatLng:points)
{
@@ -134,8 +192,6 @@ void geoFenceitem::setPosSLOT()
}
setPolygon(polygon);
setPos(m_map->FromLatLngToLocal(this->coord).X(), m_map->FromLatLngToLocal(this->coord).Y());
emit localPositionChanged(this->pos(),this);
update();
@@ -152,9 +208,6 @@ void geoFenceitem::setPoints(QList<internals::PointLatLng> p)
m_Vertex = p.size();
RefreshPos();
}
}
+7 -1
View File
@@ -7,6 +7,7 @@
#include "pointlatlng.h"
#include <QObject>
#include "mapgraphicitem.h"
#include <QGraphicsSceneMouseEvent>
namespace mapcontrol {
@@ -21,7 +22,7 @@ class geoFenceitem : public QObject, public QGraphicsPolygonItem {
public:
enum { Type = UserType + 5 };
geoFenceitem(int group,bool inclusion,int version,internals::PointLatLng const & coord, QBrush color, MapGraphicItem *map, bool dashed = false, int width = 3);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
int type() const;
internals::PointLatLng coord;
int number;
@@ -94,6 +95,7 @@ public:
}
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
@@ -122,12 +124,16 @@ private:
bool isDragging = false;
bool enter = false;
public slots:
void setPosSLOT();
void RefreshPos();
signals:
void updateFencePolygon(int group,qreal vertex,bool inclusion,QList<QPointF> latlng);
void localPositionChanged(QPointF point, geoFenceitem *p);
void aboutToBeDeleted(geoFenceitem *);
+25
View File
@@ -2090,6 +2090,9 @@ void OPMapWidget::addCircle()
//生成一个⚪
geoFencecircle *c = new geoFencecircle(fenceCount,inclusion,version,internals::PointLatLng(lat,lng),radius,QColor("#FF8000"),map);
emit createFenceCircle(fenceCount++,radius,inclusion,lat,lng);
connect(c,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)),
this,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)));
}
void OPMapWidget::addPolygon()
@@ -2126,10 +2129,15 @@ void OPMapWidget::addPolygon()
geoFenceitem *polyitem = new geoFenceitem(fenceCount,inclusion,version,center,QColor("#FF8000"),map);
polyitem->setPoints(points);
emit createFencePolygon(fenceCount++,latlng.size(),inclusion,latlng);
connect(polyitem,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)),
this,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)));
}
void OPMapWidget::delFence(int group)
@@ -2226,6 +2234,9 @@ void OPMapWidget::WPLoad(QString path)//带文件目录参数
polyitem->setPoints(points);
emit createFencePolygon(fenceCount++,points.size(),inclusion,latlng);
connect(polyitem,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)),
this,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)));
}
QJsonArray circlesArray = json.value("geoFence").toObject().value("circles").toArray();
@@ -2246,6 +2257,9 @@ void OPMapWidget::WPLoad(QString path)//带文件目录参数
geoFencecircle *c = new geoFencecircle(fenceCount,inclusion,version,internals::PointLatLng(lat,lng),radius,QColor("#FF8000"),map);
emit createFenceCircle(fenceCount++,radius,inclusion,lat,lng);
connect(c,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)),
this,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)));
}
@@ -2941,6 +2955,9 @@ void OPMapWidget::receivedPoint(float param1,float param2,float param3,float par
emit createFencePolygon(fenceCount++,PolygonPoints.size(),inclusion,latlng);
connect(polyitem,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)),
this,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)));
PolygonCount = 0;
PolygonPoints.clear();
}
@@ -2985,6 +3002,9 @@ void OPMapWidget::receivedPoint(float param1,float param2,float param3,float par
emit createFencePolygon(fenceCount++,PolygonPoints.size(),inclusion,latlng);
connect(polyitem,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)),
this,SIGNAL(updateFencePolygon(int,qreal,bool,QList<QPointF>)));
PolygonCount = 0;
PolygonPoints.clear();
}
@@ -2997,6 +3017,8 @@ void OPMapWidget::receivedPoint(float param1,float param2,float param3,float par
geoFencecircle *c = new geoFencecircle(fenceCount++,inclusion,0,internals::PointLatLng(x * 10e-8,y * 10e-8),radius,QColor("#FF8000"),map);
emit createFenceCircle(0,radius,inclusion,x * 10e-8,y * 10e-8);
connect(c,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)),
this,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)));
}
else if(command == MAV_CMD_NAV_FENCE_CIRCLE_EXCLUSION )
{
@@ -3005,6 +3027,9 @@ void OPMapWidget::receivedPoint(float param1,float param2,float param3,float par
geoFencecircle *c = new geoFencecircle(fenceCount++,inclusion,0,internals::PointLatLng(x * 10e-8,y * 10e-8),radius,QColor("#FF8000"),map);
emit createFenceCircle(0,radius,inclusion,x * 10e-8,y * 10e-8);
connect(c,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)),
this,SIGNAL(updateFenceCircle(int,qreal,bool,qreal,qreal)));
}
else if(command == MAV_CMD_NAV_RALLY_POINT )
{
+5
View File
@@ -651,6 +651,9 @@ signals:
void createFenceCircle(int group,qreal radius,bool inclusion,qreal lat,qreal lng);
void createFencePolygon(int group,qreal vertex,bool inclusion,QList<QPointF> latlng);
void updateFenceCircle(int group,qreal radius, bool inclusion, qreal lat, qreal lng);
void updateFencePolygon(int group,qreal vertex,bool inclusion,QList<QPointF> latlng);
void sendFence(qreal Vertex,
qreal group,
qreal Lat,
@@ -750,6 +753,8 @@ public slots:
void setFenceCircle(int group,qreal radius, bool inclusion, qreal lat, qreal lng);
void setFencePolygon(int group,qreal vertex,bool inclusion,QList<QPointF> latlng);
void addCircle();
void addPolygon();
void delFence(int group);