当前位置:   article > 正文

自绘无人机仪表盘

自绘无人机仪表盘
#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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
#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();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/659668
推荐阅读
相关标签
  

闽ICP备14008679号