#include "qtantanscanning.h"
#include <QTimer>
#include <QPainter>
#include <QVector>
#include <QPair>
#include <QDebug>
#include <QRectF>
#include <QMouseEvent>
#include <QPainterPath>
#include <QRegion>
#include <QSequentialAnimationGroup>
#include <QPropertyAnimation>
QTanTanScanning::QTanTanScanning(QWidget *parent) : QWidget(parent)
{
ringMaxR = 95;
ringColor = QColor(255, 0, 0);
scannigColor = QColor(255, 0, 0);
m_pressflag = false;
centerRingR = 40;
centerR = 40;
ringPenWidthStep = 0.01;
ringsRStep = 1;
ringsAlphaStep = 255 / ringMaxR;
timer = new QTimer(this);
timer->setInterval(10);
// connect(timer, SIGNAL(timeout()), this, SLOT(timerWork()));
// timer->start();
ringsTimer = new QTimer(this);
ringsTimer->setInterval(10);
connect(ringsTimer, SIGNAL(timeout()), this, SLOT(ringsTimerWork()));
ringsTimer->start();
QPropertyAnimation *anim1 = new QPropertyAnimation(this, "");
anim1->setStartValue(40);
anim1->setEndValue(50);
anim1->setDuration(300);
QPropertyAnimation *anim2 = new QPropertyAnimation(this, "");
anim2->setStartValue(50);
anim2->setEndValue(30);
anim2->setDuration(300);
QPropertyAnimation *anim3 = new QPropertyAnimation(this, "");
anim3->setStartValue(30);
anim3->setEndValue(40);
anim3->setDuration(300);
animationGroup = new QSequentialAnimationGroup() ;
animationGroup->addAnimation(anim1);
animationGroup->addAnimation(anim2);
animationGroup->addAnimation(anim3);
connect(anim1, SIGNAL(valueChanged(QVariant)), this, SLOT(updateRadius(QVariant)));
connect(anim2, SIGNAL(valueChanged(QVariant)), this, SLOT(updateRadius(QVariant)));
connect(anim3, SIGNAL(valueChanged(QVariant)), this, SLOT(updateRadius(QVariant)));
}
void QTanTanScanning::updateRadius(QVariant radius)
{
//如果鼠标悬停则逐渐变小,鼠标移开则逐渐变大直到恢复
centerR = radius.toInt();
update();
}
void QTanTanScanning::ringsTimerWork()
{
QVector<struct RingData>::iterator iter = rings.begin();
while(iter != rings.end())
{
struct RingData currRing = *iter;
if(currRing.radius < ringMaxR)
{
currRing.radius += ringsRStep;
currRing.alpha = currRing.radius * (255 / ringMaxR);
currRing.penWidth = 3.0 - currRing.radius * (3.0 / ringMaxR);
*iter = currRing;
iter++;
}
else
{
rings.erase(iter);
}
}
update();
}
void QTanTanScanning::mousePressEvent(QMouseEvent *event)
{
if(!m_pressflag)
{
QPoint pressedPoint = event->pos();
QPointF realPos = pressedPoint - rect().center();
QMatrix qMatrix;
int width = this->width();
int height = this->height();
int side = qMin(width, height);
qMatrix.scale(side / 200.0, side / 200.0);
QRegion mEllipse = QRegion(-centerRingR, -centerRingR, centerRingR * 2, centerRingR * 2, QRegion::Ellipse);
mEllipse = qMatrix.map(mEllipse);
if( mEllipse.contains(realPos.toPoint()))
{
m_pressflag = true;
animationGroup->stop();
update();
}
}
}
void QTanTanScanning::mouseReleaseEvent(QMouseEvent* event)
{
if(m_pressflag)
{
m_pressflag = false;
struct RingData ring;
ring.radius = centerRingR;
ring.alpha = 255;
ring.penWidth = 3;
rings.push_back(ring);
animationGroup->start();
update();
}
}
void QTanTanScanning::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 / 200.0, side / 200.0);
//drawRings(&painter);
drawCenterRing(&painter);
}
void QTanTanScanning::drawRings(QPainter *painter)
{
painter->save();
painter->setBrush(Qt::NoBrush);
QVector<struct RingData>::iterator iter = rings.begin();
while(iter != rings.end())
{
struct RingData currRing = *iter;
int radius = currRing.radius;
//绘制圆弧方法绘制圆环
float penWidth = currRing.penWidth;
QRectF rect(-radius + penWidth / 2, -radius + penWidth / 2, radius * 2 - penWidth, radius * 2 - penWidth);
//可以自行修改画笔的后三个参数,形成各种各样的效果,例如Qt::FlatCap改为Qt::RoundCap可以产生圆角效果
ringColor.setAlpha(255 - currRing.alpha);
QPen pen(ringColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);
//绘制总范围角度圆弧
pen.setColor(ringColor);
painter->setPen(pen);
painter->drawArc(rect, 0 * 16, 360 * 16);
iter++;
}
painter->restore();
}
void QTanTanScanning::drawCenterRing(QPainter *painter)
{
painter->save();
QPixmap pixmap(":/image/bg.jpg");
QPainterPath path;
path.addEllipse(QPoint(0, 0), centerR, centerR);
painter->setClipPath(path);
QRect rect(-centerR, -centerR, centerR * 2, centerR * 2);
painter->drawPixmap(rect, pixmap);
int penWidth = 3;
//可以自行修改画笔的后三个参数,形成各种各样的效果,例如Qt::FlatCap改为Qt::RoundCap可以产生圆角效果
QPen pen(Qt::white, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);
painter->setPen(pen);
painter->drawArc(rect, 0, 360 * 16);
painter->restore();
}
QSize QTanTanScanning::sizeHint() const
{
return QSize(100, 100);
}
QSize QTanTanScanning::minimumSizeHint() const
{
return QSize(50, 50);
}