#include <QDesktopWidget>
#include <QDebug>
#include <QPainter>
#include "videsscan.h"
#include "ui_videsscan.h"
#define VIEWFINDER_WIDTH 260
#define VIEWFINDER_HEIGHT 260
#define GRADUAL_WIDTH 6
#define TRANSPARENCY 200
videSscan::videSscan(QWidget *parent) :
QWidget(parent),
ui(new Ui::videSscan)
{
ui->setupUi(this);
setStyleSheet("QWidget#videSscan{border-image:url(:/new/prefix1/images/lena.jpg)}");
QPixmap pix = grab(QRect(QPoint(0, 0), this->size()));
pix.save("./lena.jpg","JPG");
labelY = (height() - VIEWFINDER_HEIGHT)/2;
//
setLabelProperty();
connect(&mTimer,&QTimer::timeout,this,&videSscan::scanGradualSlot);
mTimer.start(10);
}
videSscan::~videSscan()
{
delete ui;
}
void videSscan::setLabelProperty()
{
label = new QLabel(this);
label->setGeometry((width() - VIEWFINDER_WIDTH)/2,(height() + VIEWFINDER_HEIGHT)/2,VIEWFINDER_WIDTH,GRADUAL_WIDTH);
QImage mainimg=QImage(2*VIEWFINDER_WIDTH,GRADUAL_WIDTH,QImage::Format_ARGB32);
QPainter painter(&mainimg);
painter.setRenderHint(QPainter::Antialiasing, true);
// QRadialGradient radialGradient(mainimg.width()/2,mainimg.height()/2,100,mainimg.width()/2,mainimg.height()/2);
// radialGradient.setColorAt(0,QColor(105, 164, 53,TRANSPARENCY));
// radialGradient.setColorAt(1,QColor(200, 228, 173,TRANSPARENCY));
// painter.setBrush(radialGradient);
painter.setPen(QPen(QColor(105, 164, 53), 1));
painter.setBrush(QBrush(QColor(105, 164, 53)));
painter.drawEllipse(0,0,label->width(),5);
QPixmap pix = QPixmap::fromImage(mainimg);
label->setPixmap(pix);
}
void videSscan::scanGradualSlot()
{
label->setGeometry((width() - VIEWFINDER_WIDTH)/2,labelY,VIEWFINDER_WIDTH,GRADUAL_WIDTH);
labelY++;
if(labelY == (height() + VIEWFINDER_HEIGHT)/2)
{
labelY = (height() - VIEWFINDER_HEIGHT)/2;
}
}
void videSscan::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setPen(QPen(QColor(127, 194, 65), 6));
//左上
painter.drawLine(QPointF((width() - VIEWFINDER_WIDTH)/2,(height() - VIEWFINDER_HEIGHT)/2),
QPointF((width() - VIEWFINDER_WIDTH)/2 + 5,(height() - VIEWFINDER_HEIGHT)/2));
painter.drawLine(QPointF((width() - VIEWFINDER_WIDTH)/2,(height() - VIEWFINDER_HEIGHT)/2),
QPointF((width() - VIEWFINDER_WIDTH)/2 ,(height() - VIEWFINDER_HEIGHT)/2 + 5));
//右上
painter.drawLine(QPointF((width()+VIEWFINDER_WIDTH)/2,(height() - VIEWFINDER_HEIGHT)/2),
QPointF((width()+VIEWFINDER_WIDTH)/2 - 5,(height() - VIEWFINDER_HEIGHT)/2));
painter.drawLine(QPointF((width()+VIEWFINDER_WIDTH)/2,(height() - VIEWFINDER_HEIGHT)/2),
QPointF((width()+VIEWFINDER_WIDTH)/2,(height() - VIEWFINDER_HEIGHT)/2 + 5));
//左下
painter.drawLine(QPointF((width() - VIEWFINDER_WIDTH)/2,(height() + VIEWFINDER_HEIGHT)/2),
QPointF((width() - VIEWFINDER_WIDTH)/2 + 5,(height() + VIEWFINDER_HEIGHT)/2));
painter.drawLine(QPointF((width() - VIEWFINDER_WIDTH)/2,(height() + VIEWFINDER_HEIGHT)/2),
QPointF((width() - VIEWFINDER_WIDTH)/2 ,(height() + VIEWFINDER_HEIGHT)/2 - 5));
//右下
painter.drawLine(QPointF((width()+VIEWFINDER_WIDTH)/2,(height() + VIEWFINDER_HEIGHT)/2),
QPointF((width()+VIEWFINDER_WIDTH)/2 - 5,(height() + VIEWFINDER_HEIGHT)/2));
painter.drawLine(QPointF((width()+VIEWFINDER_WIDTH)/2,(height() + VIEWFINDER_HEIGHT)/2),
QPointF((width()+VIEWFINDER_WIDTH)/2,(height() + VIEWFINDER_HEIGHT)/2 - 5));
//画周边阴影
painter.setCompositionMode( QPainter::CompositionMode_Darken );
QColor darkenColor(0, 0, 0, 100);
painter.fillRect( 5, 5, width(), (height() - VIEWFINDER_HEIGHT)/2 - 5, darkenColor );
painter.fillRect( 5, (height() - VIEWFINDER_HEIGHT)/2, (width() - VIEWFINDER_WIDTH)/2 - 5, + VIEWFINDER_HEIGHT, darkenColor );
painter.fillRect( (width()+VIEWFINDER_WIDTH)/2, (height() - VIEWFINDER_HEIGHT)/2, (width() - VIEWFINDER_WIDTH)/2 , + VIEWFINDER_HEIGHT, darkenColor);
painter.fillRect( 5, (height() + VIEWFINDER_HEIGHT)/2, width() , (height() + VIEWFINDER_HEIGHT)/2, darkenColor);
//painter.drawPixmap(0,0,width(),height(),QPixmap("./lena.jpg").scaled(width(),height()));
}