Files
gcs-nf/opmap/mapwidget/geoFencecircle.cpp
T

225 lines
5.0 KiB
C++

#include "geoFencecircle.h"
#include <QDateTime>
namespace mapcontrol {
/*
geoFencecircle::geoFencecircle(int group, bool inclusion, int version, geoFenceitem *center, double radius, QBrush color, MapGraphicItem *map, bool dashed, int width) :
QGraphicsEllipseItem(map),m_center(center), coord(center->coord), m_brush(color), m_map(map),dashed(dashed),lineWidth(width)
*/
geoFencecircle::geoFencecircle(int group, bool inclusion, int version, internals::PointLatLng const &center, double radius, QBrush color, MapGraphicItem *map, bool dashed, int width) :
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(QGraphicsEllipseItem::ItemIsMovable, true);
//this->setFlag(QGraphicsEllipseItem::ItemIgnoresTransformations, true);
//this->setFlag(QGraphicsItem::ItemIsSelectable, true);//这个有bug
m_version = version;
m_inclusion = inclusion;
m_radius = radius;
m_group = group; //2
m_latitude = coord.Lat();//5
m_longitide = coord.Lng();//6
connect(map, SIGNAL(childRefreshPosition()), this, SLOT(refreshLocations()));
refreshLocations();
}
int geoFencecircle::type() const
{
return Type;
}
void geoFencecircle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setOpacity(0.5);
QPointF p1;
QPointF p2;
p1 = QPointF(line.p1().x(), line.p1().y() + line.length());
p2 = QPointF(line.p1().x(), line.p1().y() - line.length());
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->drawEllipse(rectangle);
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(rectangle,Qt::AlignCenter,QString::number(m_group));
}
void geoFencecircle::refreshLocations()
{
QPointF p(m_map->FromLatLngToLocal(coord).X(),
m_map->FromLatLngToLocal(coord).Y());
internals::PointLatLng C;
C = coord;
C.SetLat(coord.Lat() + m_radius/111133.3333f);
line = QLineF(p.x(),p.y(),p.x(),m_map->FromLatLngToLocal(C).Y());
//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());
setRect(rectangle);
/*
qDebug() << p;
setRect(p.x() - line.length(),p.y() - line.length(), 2 * line.length(), 2 * line.length());
*/
update();
}
void geoFencecircle::waypointdeleted()
{
this->deleteLater();
}
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();
}
}