#ifndef LkGaugePlane_H
#define LkGaugePlane_H
#include <QWidget>
#include <QOpenGLWidget>
class LkGaugePlane : public QWidget
{
Q_OBJECT
public:
LkGaugePlane(QWidget *parent = 0);
~LkGaugePlane() {}
inline void setUav(float _pitch, float _yaw, float _roll) {
m_pitch = _pitch;
m_yaw = _yaw;
m_roll = _roll;
update();
};
protected:
void resizeEvent(QResizeEvent *event);
void paintEvent(QPaintEvent *);
QString getPitchValuePic(int value);
void drawRollBg(QPainter *painter);
void drawPitchBg(QPainter *painter);
void drawPitchScale(QPainter *painter);
void drawPixmapYaw(QPainter *painter);
private:
float m_yaw = 0.0;
float m_pitch = 0.0;
float m_roll = 0.0;
};
#endif
#include "LkGaugePlane.h"
#include <QPainter>
#include <QTimer>
#include <QPixmap>
#define LkGaugePlane_Width 250
#define LkGaugePlane_Height 250
#define BorderIn_Radius 125.0 * (float)250.0 / (float)320.0
#define Unit_Px 3
#define Pitch_Width 30
#define Pitch_Value_Space 30
#define Pixmap_Width 12
#define Pixmap_Height 12
LkGaugePlane::LkGaugePlane(QWidget *parent )
{
}
void LkGaugePlane::resizeEvent(QResizeEvent *event)
{
}
void LkGaugePlane::paintEvent(QPaintEvent *)
{
int width = this->width();
int height = this->height();
int side = qMin(width, height);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.translate(width / 2, height / 2);
painter.scale(side / LkGaugePlane_Width, side / LkGaugePlane_Height);
drawPitchBg(&painter);
drawRollBg(&painter);
drawPixmapYaw(&painter);
drawPitchScale(&painter);
}
QString LkGaugePlane::getPitchValuePic(int value)
{
switch (abs(value))
{
case 0:
return QString(":/PlanControlUI/Resources/Image/pfd/0.png");
case 10:
return QString(":/PlanControlUI/Resources/Image/pfd/10.png");
case 20:
return QString(":/PlanControlUI/Resources/Image/pfd/20.png");
case 30:
return QString(":/PlanControlUI/Resources/Image/pfd/30.png");
case 40:
return QString(":/PlanControlUI/Resources/Image/pfd/40.png");
default:
break;
}
return "";
}
void LkGaugePlane::drawRollBg(QPainter *painter)
{
painter->save();
QImage image(":/PlanControlUI/Resources/Image/pfd/roll.png");
image = image.scaled(QSize(LkGaugePlane_Width, LkGaugePlane_Height));
painter->drawPixmap(QPoint(-LkGaugePlane_Width / 2, -LkGaugePlane_Height / 2), QPixmap::fromImage(image));
painter->restore();
}
void LkGaugePlane::drawPitchBg(QPainter *painter)
{
painter->save();
painter->rotate(-m_roll);
if (m_pitch >= 0) {
painter->setBrush(QBrush(QColor(76, 76, 153)));
QRectF rectUp(-BorderIn_Radius, -BorderIn_Radius, BorderIn_Radius * 2, BorderIn_Radius * 2);
painter->drawEllipse(rectUp);
int hUp = m_pitch * Unit_Px;
int width = std::sqrt(BorderIn_Radius * BorderIn_Radius - hUp * hUp);
int hDown = BorderIn_Radius - hUp;
painter->setBrush(QBrush(QColor(170, 157, 107)));
QRectF rectDown(-width, BorderIn_Radius - hDown * 2, width * 2, hDown * 2);
painter->drawPie(rectDown, 180 * 16, 180 * 16);
}
else{
painter->setBrush(QBrush(QColor(170, 157, 107)));
QRectF rectUp(-BorderIn_Radius, -BorderIn_Radius, BorderIn_Radius * 2, BorderIn_Radius * 2);
painter->drawEllipse(rectUp);
int hUp = abs(m_pitch * Unit_Px);
int width = std::sqrt(BorderIn_Radius * BorderIn_Radius - hUp * hUp);
int hPie = BorderIn_Radius - hUp;
painter->setBrush(QBrush(QColor(76, 76, 153)));
QRectF rectDown(-width, -BorderIn_Radius, width * 2, hPie * 2);
painter->drawPie(rectDown, 0, 180 * 16);
}
painter->restore();
}
void LkGaugePlane::drawPixmapYaw(QPainter *painter)
{
painter->save();
painter->rotate(-m_yaw);
QImage image(":/PlanControlUI/Resources/Image/pfd/yaw.png");
image = image.scaled(QSize(LkGaugePlane_Width, LkGaugePlane_Height));
painter->drawPixmap(QPoint(-LkGaugePlane_Width / 2, -LkGaugePlane_Height / 2), QPixmap::fromImage(image));
painter->restore();
}
void LkGaugePlane::drawPitchScale(QPainter *painter)
{
int iPitch = m_pitch;
QPen pen;
pen.setColor(Qt::white);
pen.setWidth(2);
painter->setPen(pen);
painter->save();
painter->rotate(-m_roll);
int upValue1, upValue2, downValue1, downValue2;
int up_y1, up_y2, down_y1, down_y2;
if (iPitch % 10 == 0)
{
upValue1 = (iPitch / 10 + 1) * 10;
downValue1 = (iPitch / 10 - 1) * 10;
}
else if (iPitch > 0)
{
upValue1 = (iPitch / 10 + 1) * 10;
downValue1 = iPitch / 10 * 10;
}
else if (iPitch < 0)
{
upValue1 = (iPitch / 10) * 10;
downValue1 = (iPitch / 10 - 1) * 10;
}
upValue2 = upValue1 + 10;
downValue2 = downValue1 - 10;
if (iPitch % 10 == 0)
{
up_y1 = -(10 - (iPitch % 10 + m_pitch - iPitch)) * Unit_Px;
down_y1 = (iPitch % 10 + m_pitch - iPitch + 10) * Unit_Px;
}
else if (iPitch > 0)
{
up_y1 = -(10 - (iPitch % 10 + m_pitch - iPitch)) * Unit_Px;
down_y1 = (iPitch % 10 + m_pitch - iPitch) * Unit_Px;
}
else if (iPitch < 0)
{
up_y1 = (iPitch % 10 + m_pitch - iPitch) * Unit_Px;
down_y1 = (10 + iPitch % 10 + m_pitch - iPitch) * Unit_Px;
}
up_y2 = up_y1 - 10 * Unit_Px;
down_y2 = down_y1 + 10 * Unit_Px;
painter->drawLine(-Pitch_Width, up_y2, Pitch_Width, up_y2);
painter->drawPixmap(-Pitch_Width - Pitch_Value_Space, up_y2 - 10, QPixmap(getPitchValuePic(upValue2)).scaled(20,20));
painter->drawPixmap(Pitch_Value_Space + 5, up_y2 - 10, QPixmap(getPitchValuePic(upValue2)).scaled(20, 20));
painter->drawLine(-Pitch_Width, up_y1, Pitch_Width, up_y1);
painter->drawPixmap(-Pitch_Width - Pitch_Value_Space, up_y1 - 10, QPixmap(getPitchValuePic(upValue1)).scaled(20, 20));
painter->drawPixmap(Pitch_Value_Space + 5, up_y1 - 10, QPixmap(getPitchValuePic(upValue1)).scaled(20, 20));
if (iPitch % 10 == 0)
{
painter->drawLine(-Pitch_Width, (m_pitch - iPitch) * 5, Pitch_Width, (m_pitch - iPitch) * 5);
painter->drawPixmap(-Pitch_Width - Pitch_Value_Space, 0 - 10, QPixmap(getPitchValuePic(m_pitch)).scaled(20, 20));
painter->drawPixmap(Pitch_Value_Space + 5, 0 - 10, QPixmap(getPitchValuePic(m_pitch)).scaled(20, 20));
}
painter->drawLine(-Pitch_Width, down_y1, Pitch_Width, down_y1);
painter->drawPixmap(-Pitch_Width - Pitch_Value_Space, down_y1 - 10, QPixmap(getPitchValuePic(downValue1)).scaled(20, 20));
painter->drawPixmap(Pitch_Value_Space + 5, down_y1 - 10, QPixmap(getPitchValuePic(downValue1)).scaled(20, 20));
painter->drawLine(-Pitch_Width, down_y2, Pitch_Width, down_y2);
painter->drawPixmap(-Pitch_Width - Pitch_Value_Space, down_y2 - 10, QPixmap(getPitchValuePic(downValue2)).scaled(20, 20));
painter->drawPixmap(Pitch_Value_Space + 5, down_y2 - 10, QPixmap(getPitchValuePic(downValue2)).scaled(20, 20));
QPixmap pixmap(":/PlanControlUI/Resources/Image/pfd/arraw.png");
pixmap = pixmap.scaled(20, 20);
painter->drawPixmap(QRect(-9, -BorderIn_Radius + 25, 20, 20), pixmap);
painter->restore();
}