#include "radar.h"
Radar::Radar(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
init();
}
Radar::~Radar()
{
}
void Radar::setRadarType(RadarType radartype)
{
m_radartype = radartype;
}
void Radar::setPointTpye(PointTpye pointstype)
{
m_pointstype = pointstype;
}
void Radar::init()
{
m_box1 = new QComboBox();
m_box2 = new QComboBox();
m_box1->addItem(QStringLiteral("Radars"));
m_box1->addItem(QStringLiteral("Spiders"));
m_box2->addItem(QStringLiteral("ScatterPoints"));
m_box2->addItem(QStringLiteral("LinePoints"));
m_box1->setGeometry(0, 0, 10, 8);
m_box2->setGeometry(50, 50, 10, 8);
//setRadarType(Radar);
setRadarType(Radars);
setPointTpye(LinePoints);
m_cylindernum = 5;//默认6圈
m_lineangle = 30; //默认30度
m_showAxisLabel = true;
m_Axis_min = 0;
m_Axis_max = 100;
m_AxisLabelList << "0" << "20" << "40" << "60" << "80" << "100";
m_colorlist << QColor(136, 68, 255);
m_colorlist << QColor(102, 170, 238);
m_colorlist << QColor(146, 39, 143);
m_colorlist << QColor(238, 187, 34);
m_colorlist << QColor(238, 187, 34);
m_colorlist << QColor(238, 187, 34);
m_colorlist << QColor(141, 198, 63);
m_colorlist << QColor(255, 242, 0);
for (int i = 0; i < 360; i += m_lineangle)
{
m_labelList << QString("%1").arg(i);
}
RadarData tlist1, tlist2;
QMap<qreal, qreal> datamap, datamap2;
for (int i = 0; i < 10; ++i)
{
datamap2.insert(i * 20, i * 10);
datamap.insert(i * 30, i * 5);
}
tlist2.color = QColor(136, 68, 255);;
tlist1.color = QColor(146, 39, 143);;
tlist1.transitiondatmap = datamap;
tlist2.transitiondatmap = datamap2;
m_datamap.insert(0, tlist2);
m_datamap.insert(1, tlist1);
}
void Radar::paintEvent(QPaintEvent *e)
{
QPainter *painter = new QPainter(this);
painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->setRenderHint(QPainter::TextAntialiasing);
//添加背景颜色
// painter->save();
// painter->setBrush(Qt::blue);
// painter->drawRect(0,0,width(),height());
// painter->restore();
//////////////////////////////////////////////////////////////////////////
drawRadarType(painter);
drawAxisLabel(painter);
drawLabel(painter);
drawData(painter);
}
void Radar::drawRadarType(QPainter *painter)
{
switch (m_radartype)
{
case Radars:drawRadar(painter); break;
case Spiders:drawSpider(painter); break;
default:drawRadar(painter); break;
}
}
void Radar::drawRadar(QPainter *painter)
{
painter->save();
QPen pen;
QColor color(Qt::lightGray);
pen.setWidthF(0.4);
//pen.setColor(color/*.lighter(100)*/); //添加线的颜色
painter->setPen(pen);
QPainterPath looppath;
int w = width();
int h = height();
int count = m_cylindernum;
int radius = qMin(w, h);
int step = radius / (count + 1);//加1是为了四周留出空间,写标签
int x = w*0.2;
int y = h*0.2;
QRectF outrect;
for (int i = 1; i < count + 1; ++i)
{
radius = step*i;
x= w / 2 - radius / 2;
y = h / 2 - radius / 2;
QRectF rect(x, y, radius, radius);
looppath.addEllipse(rect);
outrect = rect;
}
painter->drawPath(looppath);
//////////////////////////////////////////////////////////////////////////
if (m_lineangle == 0)
{
m_lineangle = 90;
}
int linecount = 360 / m_lineangle;
int x0 = w / 2;
int y0 = h / 2;
int newradius = outrect.height() / 2;
for (int i = 0; i < linecount; ++i)
{
int x1 = x0 + newradius*qSin(PI * 2 / linecount*i);
int y1 = y0 + newradius*qCos(PI * 2 / linecount*i);
painter->drawLine(x0, y0, x1, y1);
}
painter->restore();
}
void Radar::drawSpider(QPainter *painter)
{
painter->save();
QPen pen;
QColor color(Qt::lightGray);
pen.setWidthF(0.4);
//pen.setColor(color/*.lighter(100)*/); //添加线的颜色
painter->setPen(pen);
QPainterPath looppath;
int w = width();
int h = height();
if (m_lineangle == 0)
{
m_lineangle = 90;
}
int laycount = m_cylindernum;//默认是几层
int count = 360 / m_lineangle;
int radius = qMin(w, h) / 2;
int x0 = w / 2;
int y0 = h / 2;
for (int j = 1; j < laycount + 1; ++j)
{
int newradius = radius*j / (laycount + 1);
for (int i = 0; i < count; ++i)
{
int x1 = x0 + newradius*qSin(PI * 2 / count*i);
int y1 = y0 + newradius*qCos(PI * 2 / count*i);
painter->drawLine(x0, y0, x1, y1);
int x2;
int y2;
if (i == count * 2 - 1)
{
x2 = x0 + newradius*qSin(0);
y2 = y0 + newradius*qCos(0);
}
else
{
x2 = x0 + newradius*qSin(PI * 2 / count*(i + 1));
y2 = y0 + newradius*qCos(PI * 2 / count*(i + 1));
}
painter->drawLine(x1, y1, x2, y2);
}
}
painter->drawPath(looppath);
painter->restore();
}
void Radar::drawAxisLabel(QPainter *painter)
{
if (!m_showAxisLabel) return;
painter->save();
//QPen pen;
//QColor color(Qt::lightGray);
//pen.setColor(color/*.lighter(100)*/); //添加线的颜色
//painter->setPen(pen);
int laycount = m_cylindernum;//默认是几层
int w = width();
int h = height();
int count = 360 / m_lineangle;
int radius = qMin(w, h) / 2;
int x0 = w / 2;
int y0 = h / 2;
QFont font;
int fontsize = 10;
if (h > 500)
fontsize = 15;
font.setPointSize(fontsize);
painter->setFont(font);
for (int j = 0; j < laycount + 1; ++j)
{
int newradius = radius*j / (laycount + 1);
int x1 = x0 + newradius*qSin(PI);
int y1 = y0 + newradius*qCos(PI) + 20;
if (j < m_AxisLabelList.count())
{
painter->drawText(x1, y1, m_AxisLabelList.at(j));
}
}
painter->restore();
}
void Radar::drawLabel(QPainter *painter)
{
if (!m_showLabel) return;
painter->save();
//QPen pen;
//QColor color(Qt::lightGray);
//pen.setColor(color/*.lighter(100)*/); //添加线的颜色
//painter->setPen(pen);
int laycount = m_cylindernum;//默认是几层
int w = width();
int h = height();
int count = 360 / m_lineangle;
int radius = qMin(w, h) / 2;
int x0 = w / 2;
int y0 = h / 2;
QFont font;
int fontsize = 10;
if (h > 500)
fontsize = 15;
font.setPointSize(fontsize);
painter->setFont(font);
for (int i = 0; i < count; ++i)
{
int newradius = radius*laycount / (laycount + 1) + 10;
newradius = newradius + 10 * qSin(PI * 2 / count*i);
int x1 = x0 - newradius*qSin(PI * 2 / count*i);
int y1 = y0 - newradius*qCos(PI * 2 / count*i);
if (y0 < y1 && x0 < x1)
{
y1 -= fontsize*qSin(PI * 2 / count*i);
}
else
{
if (x1 < x0)
x1 -= fontsize;
y1 += fontsize*qSin(PI * 2 / count*i);
}
if (i < m_labelList.count())
{
painter->drawText(x1, y1, m_labelList.at(i));
}
}
painter->restore();
}
void Radar::drawData(QPainter *painter)
{
switch (m_pointstype)
{
case ScatterPoints:drawScatterPoints(painter); break;
case LinePoints:drawLinePoints(painter); break;
default:drawLinePoints(painter); break;
}
}
void Radar::drawScatterPoints(QPainter *painter)
{
painter->save();
int w = width();
int h = height();
int count = 360 / m_lineangle;
int radius = qMin(w, h) / 2 * m_cylindernum / (m_cylindernum + 1);
QList<int> keyilist = m_datamap.keys();
qreal step = 1.0*radius / (m_Axis_max - m_Axis_min);
foreach(qreal key, keyilist)
{
RadarData rdata = m_datamap.value(key);
QColor color = rdata.color;
painter->setPen(color.darker(200));
painter->setBrush(color.darker(200));
foreach(qreal angle, rdata.transitiondatmap.keys())
{
qreal length = step*rdata.transitiondatmap.value(angle);
angle += 90; //因为转90
QPointF certp(w / 2 + length*qCos(angle*PI / 180), h / 2 - length*qSin(angle*PI / 180));
painter->drawEllipse(certp, 2, 2);
}
}
painter->restore();
}
void Radar::drawLinePoints(QPainter *painter)
{
painter->save();
int w = width();
int h = height();
int count = 360 / m_lineangle;
int radius = qMin(w, h) / 2 * m_cylindernum / (m_cylindernum + 1);
QList<int> keyilist = m_datamap.keys();
qreal step = 1.0*radius / (m_Axis_max - m_Axis_min);
foreach(qreal key, keyilist)
{
RadarData rdata = m_datamap.value(key);
QPainterPath path;
int i = 0;
QPointF s