赞
踩
#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 // LkGaugePlane_H
#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 //俯仰角一度代表5px 内圆125,可表示俯仰角的正负25度 #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 /*= 0*/) { } 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);//水平 5度数值对应y轴长度150,1度为30 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);//水平 5度数值对应y轴长度150,1度为30 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);//水平 5度数值对应y轴长度150,1度为30 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(); }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。