215 lines
5.9 KiB
C++
215 lines
5.9 KiB
C++
#include "circlepointitem.h"
|
|
#include "math.h"
|
|
#include "qgraphicssceneevent.h"
|
|
#include "qgraphicsview.h"
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
|
|
int CirclePointItem::number = 0;//组计数从1开始
|
|
|
|
CirclePointItem::CirclePointItem(mapcontrol::MapGraphicItem *map, QPointF const &p) :
|
|
QGraphicsEllipseItem(map)
|
|
{
|
|
this->setAcceptHoverEvents(true);
|
|
_parentMap = map;
|
|
setPos(p);
|
|
|
|
number++;
|
|
|
|
privatenumber = number;
|
|
|
|
//参数框
|
|
paramItem = new WayPointParamDialog;
|
|
QPointF screenPos = this->screenPos(pos() );
|
|
paramItem->move(screenPos.x() + 10, screenPos.y() );
|
|
|
|
//外圆
|
|
outside = new QGraphicsEllipseItem(-25, -25, 50, 50, this);
|
|
QPen pen(QColor(255, 128, 0) );
|
|
pen.setWidth(2);
|
|
outside->setPen(pen);
|
|
|
|
setBrush(QColor("#0174DF"));
|
|
QPen pen1 = this->pen();
|
|
pen1.setColor(QColor("#0174DF"));
|
|
setPen(pen1);
|
|
setRect(-10, -10, 20, 20);
|
|
|
|
|
|
|
|
|
|
setAcceptDrops(true);
|
|
setEnabled(true);
|
|
this->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
|
|
// this->setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|
// this->setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
|
|
connect(paramItem, &WayPointParamDialog::ok, this, &CirclePointItem::acceptedSlot, Qt::DirectConnection );
|
|
connect(paramItem, SIGNAL(ok() ), this, SIGNAL(accepted() ), Qt::DirectConnection );
|
|
connect(paramItem, SIGNAL(cancel() ), this, SIGNAL(rejected() ), Qt::DirectConnection );
|
|
connect(paramItem, &WayPointParamDialog::remove,
|
|
this, [this](){ this->isRemove = true;}, Qt::DirectConnection );
|
|
|
|
connect(map, &mapcontrol::MapGraphicItem::childRefreshPosition, this, &CirclePointItem::refreshPos);
|
|
connect(map, &mapcontrol::MapGraphicItem::childRefreshPosition, this, &CirclePointItem::refreshOutSideEllipse);
|
|
|
|
|
|
}
|
|
|
|
CirclePointItem::~CirclePointItem()
|
|
{
|
|
delete paramItem;
|
|
}
|
|
|
|
|
|
|
|
|
|
void CirclePointItem::autoParamDialogExex() const
|
|
{
|
|
// QScreen *screen = qApp->primaryScreen();
|
|
QScreen *screen = paramItem->screen();
|
|
QPointF wayScreenPos(screenPos(mapToParent(QPointF(0, 0) ) ) );
|
|
QPoint showPos(wayScreenPos.x(), wayScreenPos.y() );
|
|
|
|
if( ((int)showPos.y() + this->paramItem->size().height() ) > screen->size().height() )
|
|
{
|
|
showPos.setY(showPos.y() - paramItem->size().height() - 30);
|
|
}
|
|
|
|
|
|
|
|
paramItem->move(showPos);
|
|
paramItem->exec();
|
|
}
|
|
|
|
QPointF CirclePointItem::screenPos(QPointF const &pos) const
|
|
{
|
|
QGraphicsItem *map = parentItem(); //获取map
|
|
QPointF scenePos = map->mapToScene(pos ); //获取pos在scene中的位置
|
|
QList<QGraphicsView *> views = map->scene()->views();
|
|
QPointF viewPos = views[0]->mapFromScene(scenePos); //获取航点在view中的位置
|
|
|
|
return views[0]->mapToGlobal(QPoint(viewPos.x(), viewPos.y() ) ); //返回pos在屏幕中的位置
|
|
}
|
|
|
|
int CirclePointItem::radiusPixelLength(double angle) const
|
|
{
|
|
double const arc = 6371393;
|
|
|
|
//根据角度计算加上距离之后的经纬度
|
|
internals::PointLatLng temp = latLng;
|
|
double a = outsideRadius * qSin(angle) / (arc * qCos(latLng.Lat() ) * 2 * M_PI / 360);
|
|
double b = outsideRadius * qCos(angle) / (arc * 2 * M_PI / 360);
|
|
temp.SetLng(temp.Lng() + a);
|
|
temp.SetLat(temp.Lat() + b);
|
|
|
|
//把经纬度转换成map中的坐标系
|
|
QPointF origin(_parentMap->FromLatLngToLocal(latLng).X(), _parentMap->FromLatLngToLocal(latLng).Y() );
|
|
QPointF end(_parentMap->FromLatLngToLocal(temp).X(), _parentMap->FromLatLngToLocal(temp).Y() );
|
|
|
|
// //把map坐标系转换成屏幕坐标系
|
|
// origin = screenPos(origin); //错误,当外圆半径太大时,无法计算
|
|
// end = screenPos(end);
|
|
|
|
QLineF line(origin, end);
|
|
|
|
return line.length();
|
|
}
|
|
|
|
void CirclePointItem::refreshOutSideEllipse()
|
|
{
|
|
int rad = radiusPixelLength(0); //获取外圆的像素半径
|
|
outside->setRect(-rad, -rad, rad * 2, rad * 2); //重新调整大小
|
|
outside->update(); //重新绘制圆
|
|
}
|
|
|
|
void CirclePointItem::setCircleEdit(bool v)
|
|
{
|
|
this->isCircleEdit = v;
|
|
// this->setFlag(QGraphicsItem::ItemIsSelectable, v);
|
|
// setFlag(QGraphicsItem::ItemIsMovable, v);
|
|
|
|
}
|
|
|
|
void CirclePointItem::setLngLat(double lng, double lat)
|
|
{
|
|
this->latLng.SetLat(lat);
|
|
this->latLng.SetLng(lng);
|
|
|
|
paramItem->setlngLat(lng, lat);
|
|
}
|
|
|
|
void CirclePointItem::acceptedSlot()
|
|
{
|
|
qDebug() << u8"circlePointItem: acceptedSlot";
|
|
|
|
height = paramItem->height; //获取高度
|
|
outsideRadius = paramItem->outsideRadius * 1000; //获取外圆半径
|
|
direction = paramItem->direction; //获取方向
|
|
//获取经纬度
|
|
latLng.SetLat(paramItem->latitude);
|
|
latLng.SetLng(paramItem->longitude);
|
|
QPointF mapPos(_parentMap->FromLatLngToLocal(latLng).X(), _parentMap->FromLatLngToLocal(latLng).Y() ); //把经纬度转换成在map中的位置
|
|
setPos(mapPos);
|
|
|
|
refreshOutSideEllipse();
|
|
}
|
|
|
|
void CirclePointItem::refreshPos()
|
|
{
|
|
core::Point point = _parentMap->FromLatLngToLocal(this->latLng);
|
|
setPos(point.X(), point.Y() );
|
|
}
|
|
|
|
void CirclePointItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
{
|
|
if(isCircleEdit)
|
|
{
|
|
autoParamDialogExex();
|
|
}
|
|
}
|
|
|
|
void CirclePointItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
{
|
|
QGraphicsEllipseItem::paint(painter, option, widget);
|
|
|
|
painter->save();
|
|
|
|
QPen myPen;
|
|
QFont font;
|
|
font.setWeight(QFont::ExtraLight);
|
|
font.setFamily("Arial");//非衬线
|
|
font.setPixelSize(12);
|
|
painter->setFont(font);
|
|
|
|
|
|
myPen.setWidth(1);
|
|
myPen.setColor(QColor("#FFFFFF"));
|
|
painter->setPen(myPen);
|
|
painter->drawText(boundingRect(),Qt::AlignCenter,QString::number(privatenumber));
|
|
|
|
painter->restore();
|
|
|
|
|
|
}
|
|
|
|
void CirclePointItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
qDebug() << "CirclePointItem: press";
|
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
emit cmd_long( 0,0,getRadius(),0, latLng.Lat(), latLng.Lng(),0,192,0);
|
|
|
|
|
|
}
|
|
//qgr::mousePressEvent(event);
|
|
|
|
|
|
update();
|
|
}
|
|
|
|
|
|
|