#include "clock_painter.h"
#define PI 3.1416
Clock_Painter::Clock_Painter(QWidget *parent) : QWidget(parent)
{
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
timer->start(1000);
}
void Clock_Painter::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
Draw_Clock(&painter);
}
void Clock_Painter::Draw_Clock(QPainter *painter)
{
Init_Parameter();
Set_Mask(painter);
Draw_Broad(painter);
Draw_Dial(painter);
Draw_Text(painter);
Draw_Pointer(painter);
}
void Clock_Painter::Init_Parameter()
{
Center_pos = QPoint(this->width()/2,this->height()/2);
R_Edge = this->height()/2;
R_Inside = R_Edge-10;
R_Center = 15;
R_Pointer = 6;
QTime Time = QTime::currentTime();
H=Time.hour();
M=Time.minute();
S=Time.second();
}
void Clock_Painter::Set_Mask(QPainter *painter)
{
painter->save();
QBrush brush = QBrush(Qt::transparent);
painter->setBrush(brush);
painter->fillRect(this->rect(),QColor(0,0,0,0));
painter->restore();
}
void Clock_Painter::Draw_Broad(QPainter *painter)
{
painter->save();
QBrush brush = QBrush(Color_Edge);
painter->setBrush(brush);
painter->drawEllipse(Center_pos,R_Edge,R_Edge);
brush = QBrush(Color_Inside);
painter->setBrush(brush);
painter->drawEllipse(Center_pos,R_Inside,R_Inside);
brush = QBrush(Color_Center);
painter->setBrush(brush);
painter->drawEllipse(Center_pos,R_Center,R_Center);
painter->restore();
}
void Clock_Painter::Draw_Dial(QPainter *painter)
{
painter->save();
for(int Loop = 0; Loop <= Div_Max*Div_Min; Loop++)
{
float Angle = BaseAngle + (360 / (Div_Max * Div_Min))*Loop;
int R = R_Inside-1;
int x_start = Center_pos.x() + R * cos((Angle / 180) * PI);
int y_start = Center_pos.y() + R * sin((Angle / 180) * PI);
if(Loop % Div_Min == 0)
{
QPen pen(Qt::black);
pen.setWidth(4);
painter->setPen(pen);
R = R_Inside-20;
}
else
{
QPen pen(Qt::black);
pen.setWidth(2);
painter->setPen(pen);
R = R_Inside-15;
}
int x_end = Center_pos.x() + R * cos((Angle / 180) * PI);
int y_end = Center_pos.y() + R * sin((Angle / 180) * PI);
painter->drawLine(QPoint(x_start,y_start),QPoint(x_end,y_end));
}
painter->restore();
}
void Clock_Painter::Draw_Text(QPainter *painter)
{
painter->save();
QPen qPen(Qt::black);
qPen.setWidth(5); //设置画笔的粗细
painter->setPen(qPen);
QFont qFont("楷体",28,QFont::Bold,false);
painter->setFont(qFont);
int Dial_Text = 12;
for(int Loop = 0;Loop < Div_Max;Loop++)
{
if(Dial_Text >12 )
Dial_Text = 1;
int R = R_Inside-60;
float Angle = BaseAngle + (360 / Div_Max )*Loop;
int x = Center_pos.x() + R * cos((Angle / 180) * PI);
int y = Center_pos.y() + R * sin((Angle / 180) * PI);
painter->drawText(QRect(x-20,y-20,80,80),QString::number(Dial_Text++));
}
painter->drawText(Center_pos.x()-60,Center_pos.y()+60,"劳力士");
painter->restore();
}
void Clock_Painter::Draw_Pointer(QPainter *painter)
{
painter->save();
QBrush qBrush = QBrush(QColor(Qt::black));
painter->setBrush(qBrush);
QPen qPen(Qt::black);
qPen.setWidth(2); //设置画笔的粗细
painter->setPen(qPen);
float Angle = BaseAngle + (360 / (Div_Max * Div_Min))*S;
float RightAngle = Angle + 90;//右侧角度
float LeftAngle = Angle - 90;//左侧角度
int R = R_Inside-1;
int x_start = Center_pos.x() + R * cos((Angle / 180) * PI);
int y_start = Center_pos.y() + R * sin((Angle / 180) * PI);
R = R_Pointer-1;
int x_end1 = Center_pos.x() + R * cos(RightAngle * 3.14 / 180);//过圆心的右侧切点
int y_end1 = Center_pos.y() + R * sin(RightAngle * 3.14 / 180);
int x_end2 = Center_pos.x() + R * cos(LeftAngle * 3.14 / 180);//过圆心的左侧切点
int y_end2 = Center_pos.y() + R * sin(LeftAngle * 3.14 / 180);
QPointF qTriangle_S[3] = {QPoint(x_start,y_start),QPoint(x_end1,y_end1),QPoint(x_end2,y_end2)};
painter->drawPolygon(qTriangle_S,3);
//**************************************************************************************************
Angle = BaseAngle + (360 / (Div_Max * Div_Min))*M;
RightAngle = Angle + 90;//右侧角度
LeftAngle = Angle - 90;//左侧角度
R = R_Inside-60;
x_start = Center_pos.x() + R * cos((Angle / 180) * PI);
y_start = Center_pos.y() + R * sin((Angle / 180) * PI);
R = R_Pointer-1;
x_end1 = Center_pos.x() + R * cos(RightAngle * 3.14 / 180);//过圆心的右侧切点
y_end1 = Center_pos.y() + R * sin(RightAngle * 3.14 / 180);
x_end2 = Center_pos.x() + R * cos(LeftAngle * 3.14 / 180);//过圆心的左侧切点
y_end2 = Center_pos.y() + R * sin(LeftAngle * 3.14 / 180);
QPointF qTriangle_M[3] = {QPoint(x_start,y_start),QPoint(x_end1,y_end1),QPoint(x_end2,y_end2)};
painter->drawPolygon(qTriangle_M,3);
//**************************************************************************************************
Angle = BaseAngle + (360 / Div_Max)*H;
RightAngle = Angle + 90;//右侧角度
LeftAngle = Angle - 90;//左侧角度
R = R_Inside-120;
x_start = Center_pos.x() + R * cos((Angle / 180) * PI);
y_start = Center_pos.y() + R * sin((Angle / 180) * PI);
R = R_Pointer-1;
x_end1 = Center_pos.x() + R * cos(RightAngle * 3.14 / 180);//过圆心的右侧切点
y_end1 = Center_pos.y() + R * sin(RightAngle * 3.14 / 180);
x_end2 = Center_pos.x() + R * cos(LeftAngle * 3.14 / 180);//过圆心的左侧切点
y_end2 = Center_pos.y() + R * sin(LeftAngle * 3.14 / 180);
QPointF qTriangle_H[3] = {QPoint(x_start,y_start),QPoint(x_end1,y_end1),QPoint(x_end2,y_end2)};
painter->drawPolygon(qTriangle_H,3);
painter->restore();
}