Files
2022-07-29 01:43:16 +08:00

203 lines
5.4 KiB
C++

/**
******************************************************************************
*
* @file MeasureLine.cpp
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
* @brief A graphicsItem representing a line connecting 2 waypoints
* @see The GNU Public License (GPL) Version 3
* @defgroup OPMapWidget
* @{
*
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "measureline.h"
#include <math.h>
#include "homeitem.h"
namespace mapcontrol {
MeasureLine::MeasureLine(internals::PointLatLng from, internals::PointLatLng to, MapGraphicItem *map, QColor color, bool dashed, int width) : QGraphicsLineItem(map),
source(from), destination(to), my_map(map), myColor(color), dashed(dashed), lineWidth(width)
{
this->setLine(map->FromLatLngToLocal(from).X(), map->FromLatLngToLocal(from).Y(), map->FromLatLngToLocal(to).X(), map->FromLatLngToLocal(to).Y());
connect(map, SIGNAL(childRefreshPosition()), this, SLOT(refreshLocations()));
connect(map, SIGNAL(childRefreshPosition()), this, SLOT(refreshLocations()));
//connect(from, SIGNAL(aboutToBeDeleted(WayPointItem *)), this, SLOT(waypointdeleted()));
//connect(to, SIGNAL(aboutToBeDeleted(WayPointItem *)), this, SLOT(waypointdeleted()));
this->setZValue(7);
connect(map, SIGNAL(childSetOpacity(qreal)), this, SLOT(setOpacitySlot(qreal)));
isEdit = true;
}
int MeasureLine::type() const
{
// Enable the use of qgraphicsitem_cast with this item.
return Type;
}
QPainterPath MeasureLine::shape() const
{
QPainterPath path = QGraphicsLineItem::shape();
path.addPolygon(arrowHead);
return path;
}
void MeasureLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QPen myPen = pen();
if(isEdit == false)
{
myPen.setColor(myColor);
}
else
{
myPen.setColor("#FF00FF");
}
painter->setPen(myPen);
if (dashed) {
QVector<qreal> dashes;
dashes << 4 << 8;
myPen.setDashPattern(dashes);
}
if (lineWidth == -1) {
if (myColor == Qt::red) {
myPen.setWidth(3);
} else if (myColor == Qt::yellow) {
myPen.setWidth(2);
} else if (myColor == Qt::green) {
myPen.setWidth(1);
}
} else {
myPen.setWidth(lineWidth);
}
painter->setPen(myPen);
painter->drawLine(line());
QFont font;
font.setPixelSize(15);//不随着屏幕分辨率改变而改变
font.setFamily("黑体");//非衬线
painter->setFont(font);
painter->drawText(line().p2().x(),line().p2().y() - 5,QString("%1km %2°").arg(QString::number(distance * 0.001,'f',3)).arg(QString::number(bearing * 57.3,'f',1)));
painter->save();
painter->setOpacity(0.8);
painter->setPen(Qt::NoPen);
painter->setBrush(QColor("#FFFFFF"));
painter->drawEllipse(line().p1(),5,5);
painter->drawEllipse(line().p2(),5,5);
painter->restore();
//画字
painter->save();
painter->setOpacity(1);
font.setWeight(QFont::ExtraBold);
font.setFamily("Arial");//非衬线
font.setPixelSize(8);
painter->setFont(font);
myPen.setWidth(1);
myPen.setColor(QColor("#000000"));
painter->setPen(myPen);
painter->drawText(QRect(line().p1().x() - 20,line().p1().y()-5,40,10),Qt::AlignCenter,QString::number(1));
painter->drawText(QRect(line().p2().x() - 20,line().p2().y()-5,40,10),Qt::AlignCenter,QString::number(2));
painter->restore();
}
void MeasureLine::setEdit(bool value)
{
isEdit = value;
}
void MeasureLine::refreshLocations()
{
this->setLine(my_map->FromLatLngToLocal(source).X(), my_map->FromLatLngToLocal(source).Y(), my_map->FromLatLngToLocal(destination).X(), my_map->FromLatLngToLocal(destination).Y());
update();
}
void MeasureLine::waypointdeleted()
{
this->deleteLater();
}
void MeasureLine::WPLinedelete(WayPointItem *from, WayPointItem *to)
{
Q_UNUSED(from)
Q_UNUSED(to)
this->deleteLater();
}
void MeasureLine::setOpacitySlot(qreal opacity)
{
setOpacity(opacity);
}
void MeasureLine::setFrom(internals::PointLatLng p)
{
source.SetLat(p.Lat());
source.SetLng(p.Lng());
my_map->Projection()->offSetFromLatLngs(source,destination,distance,bearing);
emit Lineinfo(distance,bearing);
refreshLocations();
update();
}
void MeasureLine::setTo(internals::PointLatLng p)
{
destination.SetLat(p.Lat());
destination.SetLng(p.Lng());
my_map->Projection()->offSetFromLatLngs(source,destination,distance,bearing);
emit Lineinfo(distance,bearing);
refreshLocations();
update();
}
}