#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
initVariables();
}
void Dialog::initVariables() /*初始化*/
{
m_outerRadius=width()>height() ? height()/4: width()/4; /*外径*/
m_innerRadius=m_outerRadius/8*7; /*内径*/
m_coverCircleRadius=m_outerRadius/8*4+10;
m_colorCircleRadius=m_outerRadius/8*5; /*颜色*/
// m_center=rect().center();
m_value=0;
//m_currentValue=0;
singleTimer=new QTimer(this); /*定时器*/
connect(singleTimer,SIGNAL(timeout()),this,SLOT(update()));
singleTimer->start();
}
void Dialog::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing);
resetVariables(&painter);
drawMark(&painter);
drawIndicator(&painter);
drawCoverBall(&painter);
painter.end();
}
void Dialog::drawMark(QPainter *painter)
{
painter->save();
painter->setPen(Qt::red);
painter->translate(m_center);
qreal dAngle=(qreal)0.3;
qreal startAngle=0;
int value=0;
QString strValue;
for(int i=0;i<=700;i++) /*表盘上的最大刻度*/
{
painter->save();
painter->rotate(startAngle);
if(i%100==0)
{
strValue=tr("%1").arg(value);
qreal textWidth=fontMetrics().width(strValue);
qreal textHeight=fontMetrics().height();
QPointF bottomPot(0,m_colorCircleRadius+SPACE+OK);
QPointF topPot(0,m_colorCircleRadius+SPACE-OK);
painter->drawLine(bottomPot,topPot);
value+=100;
painter->save();
QPointF textPot(0-textWidth/2,m_colorCircleRadius+LONG-textHeight);
painter->translate(textPot);
painter->rotate(180);
painter->drawText(QPointF(-textWidth,textHeight/2),strValue);
painter->restore();
}else if(i%20==0)
{
QPointF bottomPot(0,m_colorCircleRadius+SPACE);
QPointF topPot(0,m_colorCircleRadius+SPACE+OK);
painter->drawLine(bottomPot,topPot);
}
painter->restore();
startAngle+=dAngle;
}
painter->restore();
}
void Dialog::drawCoverBall(QPainter *painter)
{
painter->save();
qreal ballRadius=m_outerRadius/10;
m_coverBallRadius=ballRadius;
QRadialGradient ballGradient(m_center,ballRadius,m_center);
ballGradient.setColorAt(0.0,QColor(236,187,62));
ballGradient.setColorAt(0.7,QColor(220,147,0));
ballGradient.setColorAt(1.0,QColor(236,187,62));
painter->setBrush(ballGradient);
painter->setPen(Qt::NoPen);
painter->drawEllipse(m_center,ballRadius,ballRadius);
painter->restore();
}
void Dialog::drawIndicator(QPainter *painter)
{
painter->save();
int A=2; /*指针所指的表盘上最小格的数目,改变此参数,指针就有所变化*/
painter->translate(m_center);
qreal increment=(qreal)210/35;
qreal changedAngle=increment*A;
painter->rotate(changedAngle);
QPointF topPot(0,m_colorCircleRadius+LONG);
QPointF bottomLeftPot(-m_coverBallRadius/3,0);
QPointF bottomRightPot(m_coverBallRadius/3,0);
painter->setPen(Qt::NoPen);
QLinearGradient indicatorGradient(topPot,bottomLeftPot);
indicatorGradient.setColorAt(0.0,QColor(0,170,255));
indicatorGradient.setColorAt(0.5,QColor(0,170,255));
indicatorGradient.setColorAt(1.0,QColor(0,170,255));
painter->setBrush(indicatorGradient);
QVector<QPointF> potVec;
potVec.push_back(topPot);
potVec.push_back(bottomLeftPot);
potVec.push_back(bottomRightPot);
painter->drawPolygon(potVec);
painter->restore();
}
void Dialog::resetVariables(QPainter *painter)
{
m_outerRadius=width()>height() ? height()/4: width()/4;
m_innerRadius=m_outerRadius/8*7;
m_coverCircleRadius=m_outerRadius/8*4+10;
m_colorCircleRadius=m_outerRadius/8*5;
int a=200,b=250;
painter->translate(a,b);
// m_center=rect().center();
}
Dialog::~Dialog()
{
delete ui;
}