#include "clock.h"
#include <QLabel>
#include <QPainter>
#include <QStyleOption>
#include <QPixmap>
#include <qDebug>
CClock::CClock(QWidget *parent) : QWidget(parent)
{
m_angle = 90;
m_preStr = "25";
m_str = "26";
m_bgColor = QColor(0 , 0 , 0);
m_textColor = QColor(255 , 255 , 255);
m_lineColor = QColor(255 , 0 , 0);
connect(&m_timer , SIGNAL(timeout()) , this , SLOT(onTimer()));
}
CClock::~CClock()
{
}
void CClock::onTimer()
{
m_angle -= 10;
setAngle(m_angle);
}
void CClock::paintEvent(QPaintEvent *e)
{
//设置绘制图片的大小
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
//设置反走样,防止抗锯齿
p.setRenderHint(QPainter::Antialiasing , true);
p.setBrush(m_bgColor);
//先绘制一个数字
p.setPen(m_textColor);
p.drawLine( 0 , height() / 2 , width() , height() / 2);
int iFontSize = width() > height() ? height()/2 : width()/2 ;
QFont font("Helvetica", iFontSize);
QFontMetrics fm(font);
p.setFont(font);
QRect fontRect(rect().x() + (rect().width() - fm.width("25")) / 2 ,
rect().y() + (rect().height() - fm.height()) / 2 , fm.width("25") , fm.height());
//文字居中
p.drawRect(fontRect);
if(m_angle == 90)
{
QPainterPath path;
path.addText(rect().x() + (rect().width() - fm.width("25")) / 2 ,
rect().y() + (rect().height() - fm.height()) / 2 + fm.height() ,
font , m_preStr);
p.setPen(Qt::NoPen);
p.setBrush(m_textColor);
p.drawPath(path);
}
else
{
//绘制上半截的数字(新数字)
QPainterPath path1 , path2 , pathTop , pathBottom , pathRes;
QFontMetrics fm(font);
path1.addText(rect().x() + (rect().width() - fm.width("25")) / 2 ,
rect().y() + (rect().height() - fm.height()) / 2 + fm.height() ,
font , m_str);
pathBottom.addRect(rect().x() ,
rect().y() + (rect().height() - fm.height()) / 2 + fm.height() / 2 ,
rect().width() ,
fm.height() / 2);
pathTop.addRect(rect().x() + (width() - fm.width("25")) / 2 ,
rect().y() + (rect().height() - fm.height()) / 2 ,
fm.width("25") ,
fm.height() / 2 + 1);
pathRes = path1 - pathBottom;
p.setPen(Qt::NoPen);
p.setBrush(m_bgColor);
// p.drawPath(pathTop);
p.setBrush(m_textColor);
p.drawPath(pathRes);
//绘制下半截数字(旧数字)
path2.addText(rect().x() + (rect().width() - fm.width("25")) / 2 ,
rect().y() + (rect().height() - fm.height()) / 2 + fm.height() ,
font , m_preStr);
pathRes = path2 - pathTop;
p.drawPath(pathRes);
}
//绘制需要旋转的界面
QTransform transform;
transform.translate(rect().width() / 2 , rect().height() / 2);
transform.rotate(m_angle , Qt::XAxis);
p.setTransform(transform);
QPainterPath path , pathTop , pathRes , pathBottom;
path.addText( -fm.width("26") / 2,
fm.height() / 2,
font, m_str);
pathTop.addRect(-fm.width("26") / 2 , -fm.height() / 2 , fm.width("26"), fm.height() / 2);
pathRes = path - pathTop;
//绘制背景
p.setBrush(m_bgColor);
pathBottom.addRect(-fm.width("26") / 2 , 0 , fm.width("26") , fm.height() / 2);
p.drawPath(pathBottom);
//绘制文字
p.setBrush(m_textColor);
p.drawPath(pathRes);
p.resetTransform();
return QWidget::paintEvent(e);
}
void CClock::setAngle(int angle)
{
//这个度数0-90度进行变化
if(angle <= 90 && angle >= 0)
{
m_angle = angle;
update();
}
else
{
m_angle = 90;
m_timer.stop();
}
}
void CClock::alterStr(QString str)
{
m_preStr = m_str;
m_str = str;
m_timer.start(50);
//update();
}