217 lines
4.8 KiB
C++
217 lines
4.8 KiB
C++
#include "geoFenceitem.h"
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
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(QGraphicsPolygonItem::ItemIsMovable, true);
|
|
//this->setFlag(QGraphicsPolygonItem::ItemIgnoresTransformations, true);
|
|
//this->setFlag(QGraphicsItem::ItemIsSelectable, true);//这个有bug
|
|
|
|
|
|
connect(map, SIGNAL(childRefreshPosition()), this, SLOT(RefreshPos()));
|
|
|
|
|
|
m_version = version;
|
|
m_inclusion = inclusion;
|
|
//m_Vertex = count;//1
|
|
m_group = group; //2
|
|
m_latitude = coord.Lat();//5
|
|
m_longitide = coord.Lng();//6
|
|
|
|
|
|
RefreshPos();
|
|
}
|
|
|
|
void geoFenceitem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
{
|
|
Q_UNUSED(option);
|
|
Q_UNUSED(widget);
|
|
|
|
painter->setOpacity(0.5);
|
|
|
|
QPen myPen = painter->pen();
|
|
myPen.setWidth(lineWidth);
|
|
myPen.setColor(m_brush.color());
|
|
painter->setPen(myPen);
|
|
|
|
|
|
if (dashed) {
|
|
QVector<qreal> dashes;
|
|
dashes << 4 << 8;
|
|
myPen.setDashPattern(dashes);
|
|
}
|
|
|
|
|
|
if(enter)
|
|
{
|
|
painter->setOpacity(0.3);
|
|
painter->setBrush(QColor("#FFFFFF"));
|
|
}
|
|
else
|
|
{
|
|
if(m_inclusion)
|
|
{
|
|
painter->setBrush(Qt::NoBrush);
|
|
}
|
|
else
|
|
{
|
|
painter->setBrush(m_brush);
|
|
}
|
|
}
|
|
|
|
painter->drawPoint(0, 0);
|
|
painter->drawPolygon(polygon());
|
|
|
|
|
|
QFont font;
|
|
font.setWeight(QFont::ExtraBold);
|
|
font.setFamily("Arial");//非衬线
|
|
font.setPixelSize(40);
|
|
painter->setFont(font);
|
|
|
|
myPen.setWidth(lineWidth);
|
|
myPen.setColor(QColor("#FFFFFF"));
|
|
painter->setPen(myPen);
|
|
painter->drawText(boundingRect(),Qt::AlignCenter,QString::number(m_group));
|
|
|
|
}
|
|
|
|
int geoFenceitem::type() const
|
|
{
|
|
return Type;
|
|
}
|
|
|
|
void geoFenceitem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
}
|
|
}
|
|
|
|
void geoFenceitem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::LeftButton) {
|
|
isDragging = true;
|
|
}
|
|
QGraphicsItem::mousePressEvent(event);
|
|
update();
|
|
}
|
|
void geoFenceitem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::LeftButton) {
|
|
isDragging = false;
|
|
}
|
|
QGraphicsItem::mouseReleaseEvent(event);
|
|
update();
|
|
}
|
|
void geoFenceitem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
if (isDragging) {
|
|
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();
|
|
}
|
|
|
|
|
|
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()
|
|
{
|
|
QPolygonF polygon;
|
|
for(internals::PointLatLng LatLng:points)
|
|
{
|
|
QPointF pf;
|
|
pf.setX(m_map->FromLatLngToLocal(LatLng).X() - m_map->FromLatLngToLocal(this->coord).X());
|
|
pf.setY(m_map->FromLatLngToLocal(LatLng).Y() - m_map->FromLatLngToLocal(this->coord).Y());
|
|
|
|
polygon.append(pf);
|
|
}
|
|
setPolygon(polygon);
|
|
|
|
setPos(m_map->FromLatLngToLocal(this->coord).X(), m_map->FromLatLngToLocal(this->coord).Y());
|
|
emit localPositionChanged(this->pos(),this);
|
|
update();
|
|
}
|
|
|
|
void geoFenceitem::RefreshPos()
|
|
{
|
|
setPosSLOT();
|
|
}
|
|
|
|
void geoFenceitem::setPoints(QList<internals::PointLatLng> p)
|
|
{
|
|
points = p;
|
|
m_Vertex = p.size();
|
|
RefreshPos();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|