366 lines
8.9 KiB
C++
366 lines
8.9 KiB
C++
#include "AltitudeItem.h"
|
|
|
|
|
|
namespace mapcontrol {
|
|
|
|
AltitudeItem::AltitudeItem(MapGraphicItem *map, QColor background) : myMap(map), myColor(background)
|
|
{
|
|
|
|
|
|
this->setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
this->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
|
|
this->setFlag(QGraphicsItem::ItemIsSelectable, false);
|
|
|
|
|
|
connect(map, SIGNAL(childRefreshPosition()), this, SLOT(RefreshPos()));
|
|
connect(map, SIGNAL(childSetOpacity(qreal)), this, SLOT(setOpacitySlot(qreal)));
|
|
|
|
this->setZValue(4);
|
|
|
|
|
|
AltColor = "#00ff00";
|
|
EleColor = "#ff0000";
|
|
|
|
|
|
currentPoint = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
QRectF AltitudeItem::boundingRect() const //鼠标可以操作的大小
|
|
{
|
|
return QRectF(0,0,400,200);
|
|
}
|
|
|
|
void AltitudeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
{
|
|
Q_UNUSED(option);
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
QPen ePen;
|
|
ePen.setWidth(1);
|
|
|
|
|
|
QFont font;
|
|
font.setPixelSize(15);//不随着屏幕分辨率改变而改变
|
|
font.setFamily("Arial");//非衬线
|
|
painter->setFont(font);
|
|
|
|
//画底部
|
|
painter->save();
|
|
ePen.setColor(QColor("#a39d9c"));
|
|
painter->setPen(ePen);
|
|
painter->setBrush(QColor("#a39d9c"));
|
|
painter->setOpacity(0.5);
|
|
painter->drawRect(0,0,this->boundingRect().width(),this->boundingRect().height());
|
|
painter->restore();
|
|
|
|
//画坐标
|
|
painter->save();
|
|
ePen.setColor(QColor("#ffffff"));
|
|
painter->setPen(ePen);
|
|
painter->translate(20,180);
|
|
painter->drawLine(0,0,0,-160);
|
|
painter->drawLine(0,0,360,0);
|
|
|
|
//painter->drawLine(0,-160,360,-160);
|
|
//painter->drawLine(360,-160,360,0);
|
|
painter->restore();
|
|
|
|
//查询最大最小值
|
|
|
|
float max = -999999;
|
|
float min = 999999;
|
|
float V_scale = 1;
|
|
|
|
if(!Elevation.isEmpty())
|
|
{
|
|
for (QMap<int, float>::iterator i = Elevation.begin();i != Elevation.end(); ++i)
|
|
{
|
|
if(i.value() > max)
|
|
{
|
|
max = i.value();
|
|
}
|
|
|
|
if(i.value() < min)
|
|
{
|
|
min = i.value();
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!Altitude.isEmpty())
|
|
{
|
|
for (QMap<int, float>::iterator i = Altitude.begin();i != Altitude.end(); ++i)
|
|
{
|
|
if(i.value() > max)
|
|
{
|
|
max = i.value();
|
|
}
|
|
|
|
if(i.value() < min)
|
|
{
|
|
min = i.value();
|
|
}
|
|
}
|
|
}
|
|
|
|
if(max != min)
|
|
{
|
|
V_scale = (float)160.0 / (max - min);
|
|
}
|
|
|
|
|
|
|
|
//画坐标数据
|
|
if((max != -999999)&&(min != 999999))
|
|
{
|
|
painter->save();
|
|
ePen.setColor(QColor("#FFFFFF"));
|
|
painter->setPen(ePen);
|
|
painter->translate(20,180);
|
|
painter->drawText(QRectF(-20,-180,100,20),Qt::AlignVCenter|Qt::AlignLeft, QString::number(max));
|
|
painter->drawText(QRectF(-20, 0,100,20),Qt::AlignVCenter|Qt::AlignLeft, QString::number(min));
|
|
painter->restore();
|
|
}
|
|
|
|
//画图例
|
|
painter->save();
|
|
painter->translate(20,180);
|
|
ePen.setColor(AltColor);
|
|
painter->setPen(ePen);
|
|
painter->drawLine(220,-170,250,-170);
|
|
painter->drawText(QRect(260,-180,40,20),Qt::AlignVCenter|Qt::AlignLeft, tr("mission"));
|
|
|
|
ePen.setColor(EleColor);
|
|
painter->setPen(ePen);
|
|
painter->drawLine(300,-170,330,-170);
|
|
painter->drawText(QRect(340,-180,40,20),Qt::AlignVCenter|Qt::AlignLeft, tr("terrain"));
|
|
painter->restore();
|
|
|
|
|
|
float size = Elevation.size();
|
|
float step = 360/(size - 1);
|
|
|
|
QPainterPath Elepath;
|
|
QPainterPath Altpath;
|
|
|
|
|
|
//画高度
|
|
painter->save();
|
|
ePen.setColor(AltColor);
|
|
painter->setPen(ePen);
|
|
painter->translate(20,180);
|
|
painter->setOpacity(0.8);
|
|
|
|
if(!Altitude.isEmpty())
|
|
{
|
|
float current_x,current_y;
|
|
int stepcount = 0;
|
|
//QPainterPath path;
|
|
Altpath.moveTo(0,0);
|
|
QPointF sp;
|
|
for (QMap<int, float>::iterator i = Altitude.begin();i != Altitude.end(); ++i)
|
|
{
|
|
QPointF ep = QPointF(stepcount * step, (-i.value() + min) * V_scale);
|
|
QPointF c1 = QPointF((sp.x() + ep.x()) / 2, sp.y());
|
|
QPointF c2 = QPointF((sp.x() + ep.x()) / 2, ep.y());
|
|
sp = QPointF(stepcount * step, (-i.value() + min) * V_scale);
|
|
|
|
Altpath.cubicTo(c1, c2, ep);
|
|
|
|
if(i.key() == currentPoint)
|
|
{
|
|
current_x = stepcount * step - 2;
|
|
current_y = (-i.value() + min) * V_scale - 2;
|
|
}
|
|
stepcount++;
|
|
}
|
|
|
|
Altpath.lineTo(360,0);
|
|
Altpath.closeSubpath();
|
|
|
|
painter->fillPath(Altpath,AltColor);
|
|
painter->drawPath(Altpath);
|
|
|
|
//画小圈圈
|
|
painter->setOpacity(2);
|
|
painter->setBrush(QColor("#000000"));
|
|
ePen.setColor("#000000");
|
|
painter->setPen(ePen);
|
|
painter->drawEllipse(current_x, current_y,4,4);
|
|
|
|
|
|
}
|
|
painter->restore();
|
|
|
|
|
|
//画地形
|
|
painter->save();
|
|
ePen.setColor(EleColor);
|
|
painter->setPen(ePen);
|
|
painter->translate(20,180);
|
|
|
|
painter->setOpacity(0.5);
|
|
|
|
if(!Elevation.isEmpty())
|
|
{
|
|
int stepcount = 0;
|
|
//QPainterPath path;
|
|
|
|
Elepath.moveTo(0,0);
|
|
|
|
QPointF sp;
|
|
for (QMap<int, float>::iterator i = Elevation.begin();i != Elevation.end(); ++i)
|
|
{
|
|
QPointF ep = QPointF(stepcount * step, (-i.value() + min) * V_scale);
|
|
QPointF c1 = QPointF((sp.x() + ep.x()) / 2, sp.y());
|
|
QPointF c2 = QPointF((sp.x() + ep.x()) / 2, ep.y());
|
|
sp = QPointF(stepcount * step, (-i.value() + min) * V_scale);
|
|
|
|
Elepath.cubicTo(c1, c2, ep);
|
|
stepcount++;
|
|
}
|
|
Elepath.lineTo(360,0);
|
|
Elepath.closeSubpath();
|
|
|
|
painter->fillPath(Elepath,EleColor);
|
|
painter->drawPath(Elepath);
|
|
}
|
|
painter->restore();
|
|
|
|
|
|
//test
|
|
//Bezier(painter);
|
|
|
|
}
|
|
|
|
|
|
void AltitudeItem::Bezier(QPainter *painter)
|
|
{
|
|
// 曲线上的点
|
|
static QList<QPointF> points = QList<QPointF>() << QPointF(0, 0) << QPointF(100,-100) << QPointF(200,-120)
|
|
<< QPointF(300,-100) << QPointF(330,-90) << QPointF(350, -70);
|
|
QPainterPath path(points[0]);
|
|
|
|
for (int i = 0; i < points.size() - 1; ++i) {
|
|
// 控制点的 x 坐标为 sp 与 ep 的 x 坐标和的一半
|
|
// 第一个控制点 c1 的 y 坐标为起始点 sp 的 y 坐标
|
|
// 第二个控制点 c2 的 y 坐标为结束点 ep 的 y 坐标
|
|
QPointF sp = points[i];
|
|
QPointF ep = points[i+1];
|
|
QPointF c1 = QPointF((sp.x() + ep.x()) / 2, sp.y());
|
|
QPointF c2 = QPointF((sp.x() + ep.x()) / 2, ep.y());
|
|
|
|
path.cubicTo(c1, c2, ep);
|
|
}
|
|
|
|
painter->save();
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
painter->setPen(QPen(Qt::black, 2));
|
|
|
|
// 绘制 path
|
|
painter->translate(40, 130);
|
|
painter->drawPath(path);
|
|
|
|
// 绘制曲线上的点
|
|
painter->setBrush(Qt::gray);
|
|
for (int i = 0; i < points.size(); ++i) {
|
|
painter->drawEllipse(points[i], 4, 4);
|
|
}
|
|
painter->restore();
|
|
}
|
|
|
|
void AltitudeItem::RefreshPos()
|
|
{
|
|
this->setPos(this->x(),
|
|
myMap->boundingRect().height() - this->boundingRect().height());
|
|
}
|
|
|
|
void AltitudeItem::setOpacitySlot(qreal opacity)
|
|
{
|
|
setOpacity(opacity);
|
|
}
|
|
|
|
void AltitudeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
qDebug() << "mousePressEvent";
|
|
if (event->button() == Qt::LeftButton) {
|
|
isDragging = true;
|
|
|
|
pos_old = event->scenePos();
|
|
|
|
}
|
|
|
|
//qDebug() << event;
|
|
|
|
//QGraphicsItem::mousePressEvent(event);
|
|
}
|
|
|
|
void AltitudeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
qDebug() << "mouseReleaseEvent";
|
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
isDragging = false;
|
|
}
|
|
//QGraphicsItem::mouseReleaseEvent(event);
|
|
|
|
}
|
|
void AltitudeItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
}
|
|
}
|
|
void AltitudeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
//qDebug() << event;
|
|
if (isDragging) {
|
|
|
|
}
|
|
//QGraphicsItem::mouseMoveEvent(event);
|
|
}
|
|
|
|
void AltitudeItem::setNumber(int value)
|
|
{
|
|
number = value;
|
|
|
|
update();
|
|
}
|
|
|
|
void AltitudeItem::setElevation(QMap<int,float> value)
|
|
{
|
|
Elevation.clear();
|
|
Elevation = value;
|
|
|
|
update();
|
|
}
|
|
|
|
void AltitudeItem::setAltitude(QMap<int, float> value)
|
|
{
|
|
Altitude.clear();
|
|
Altitude = value;
|
|
|
|
update();
|
|
}
|
|
|
|
void AltitudeItem::setCurrentPoint(int value)
|
|
{
|
|
currentPoint = value;
|
|
update();
|
|
}
|
|
|
|
|
|
float AltitudeItem::max = 1;
|
|
float AltitudeItem::min = 0;
|
|
}
|
|
|
|
|