QQ:609162385
承接各种软件,插件,控件制作,程序代写,毕业设计,图像处理等。
#pragma execution_character_set("utf-8")
/*!
@author chen
@date 2018/9
@brief QQ:609162385
@brief Tell:15826091035
@https https://blog.csdn.net/cqltbe131421
@verbatim
<author> <time> <version> <desc>
chen 2018/9 0.1.0 build this module
@endverbatim
*/
#include "recordwidget.h"
#include "ui_recordwidget.h"
#include <QDateTime>
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
#include <QStyle>
#include <QApplication>
#include <QDesktopWidget>
#include <QDesktopServices>
#include <QScreen>
RecordWidget::RecordWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::RecordWidget)
{
ui->setupUi(this);
setMouseTracking(true);
isLeftPressDown = false;
movie = new QMovie(this);
ui->label_gif->setMovie(movie);
dir = NONE;
this->setAttribute(Qt::WA_TranslucentBackground);
this->setAttribute(Qt::WA_DeleteOnClose);
this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint );
m_screen = QApplication::primaryScreen();
connect(&timer, SIGNAL(timeout()), this, SLOT(SaveImage()));
connect(&countdowntimer, SIGNAL(timeout()), this, SLOT(countdowntimerSlot()));
}
RecordWidget::~RecordWidget()
{
if (nullptr != m_gifWriter)
{
delete m_gifWriter;
m_gifWriter = nullptr;
}
delete ui;
}
void RecordWidget::on_btn_play_clicked()
{
movie->stop();
ui->label_gif->hide();
ui->label_gif->show();
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home/jana", tr("Image Files (*.gif)"));
movie->setFileName(fileName);
movie->start();
}
void RecordWidget::on_btnRecord_clicked()
{
movie->stop();
ui->label_gif->hide();
countdowntimer.start(1000);
}
void RecordWidget::on_btnStop_clicked()
{
movie->stop();
ui->label_gif->hide();
recordnum = 0;
countdownnum = 3;
if (!m_gifWriter) return;
timer.stop();
m_Gif.GifEnd(m_gifWriter);
delete m_gifWriter;
m_gifWriter = nullptr;
ui->labelStatus->setText("录制完成:" + m_strFileName);
ui->label_gif->show();
movie->setFileName(m_strFileName);
movie->start();
}
void RecordWidget::on_btnClose_clicked()
{
movie->stop();
ui->label_gif->hide();
qApp->quit();
}
void RecordWidget::on_btnSave_clicked()
{
movie->stop();
ui->label_gif->hide();
QString str = "";
QString saveFileName = QFileDialog::getSaveFileName(this,"保存文件","dirString","*.gif",&str,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
qDebug()<<"sss"<<saveFileName;
QFile file(saveFileName);
bool copy_error = file.copy( "temp.gif",saveFileName );
}
/**
* @brief RecordWidget::SaveImage
* 保存图像
*/
void RecordWidget::SaveImage()
{
ui->labelStatus->setText(QString("开始录制...%1").arg(recordnum++));
if (!m_gifWriter) return;
QPixmap pix = m_screen->grabWindow(0, this->x() + m_rectRecord.x(),
this->y() + m_rectRecord.y(),
m_rectRecord.width(), m_rectRecord.height());
QImage image = pix.toImage().convertToFormat(QImage::Format_RGBA8888);;
m_Gif.GifWriteFrame(m_gifWriter, image.bits(), m_rectRecord.width(), m_rectRecord.height(), m_Fps);
}
void RecordWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.fillRect(this->rect(), QColor("#2F4F4F"));
int nW = ui->lineEditWidth->text().toInt();
int nH = ui->lineEditHeight->text().toInt();
painter.setCompositionMode( QPainter::CompositionMode_Clear );
m_rectRecord = QRect(2, 32, nW, nH);
painter.fillRect(m_rectRecord, Qt::Dense7Pattern );
}
void RecordWidget::mouseMoveEvent(QMouseEvent *e)
{
QPoint gloPoint = e->globalPos();
QRect rect = this->rect();
QPoint tl = mapToGlobal(rect.topLeft());
QPoint rb = mapToGlobal(rect.bottomRight());
if(!isLeftPressDown) {
this->region(gloPoint);
} else {
if(dir != NONE) {
QRect rMove(tl, rb);
switch(dir) {
case LEFT:
if(rb.x() - gloPoint.x() <= this->minimumWidth())
rMove.setX(tl.x());
else
rMove.setX(gloPoint.x());
break;
case RIGHT:
rMove.setWidth(gloPoint.x() - tl.x());
break;
case UP:
if(rb.y() - gloPoint.y() <= this->minimumHeight())
rMove.setY(tl.y());
else
rMove.setY(gloPoint.y());
break;
case DOWN:
rMove.setHeight(gloPoint.y() - tl.y());
break;
case LEFTTOP:
if(rb.x() - gloPoint.x() <= this->minimumWidth())
rMove.setX(tl.x());
else
rMove.setX(gloPoint.x());
if(rb.y() - gloPoint.y() <= this->minimumHeight())
rMove.setY(tl.y());
else
rMove.setY(gloPoint.y());
break;
case RIGHTTOP:
rMove.setWidth(gloPoint.x() - tl.x());
rMove.setY(gloPoint.y());
break;
case LEFTBOTTOM:
rMove.setX(gloPoint.x());
rMove.setHeight(gloPoint.y() - tl.y());
break;
case RIGHTBOTTOM:
rMove.setWidth(gloPoint.x() - tl.x());
rMove.setHeight(gloPoint.y() - tl.y());
break;
default:
break;
}
this->setGeometry(rMove);
} else {
move(e->globalPos() - dragPosition);
e->accept();
}
}
}
void RecordWidget::mousePressEvent(QMouseEvent *e)
{
switch(e->button()) {
case Qt::LeftButton:
isLeftPressDown = true;
if(dir != NONE) {
this->mouseGrabber();
} else {
dragPosition = e->globalPos() - this->frameGeometry().topLeft();
}
break;
case Qt::RightButton:
this->close();
break;
default:
mousePressEvent(e);
}
if (e->button() == Qt::LeftButton) {
m_mousePressed = true;
mousePoint = e->globalPos() - this->pos();
e->accept();
}
}
void RecordWidget::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
isLeftPressDown = false;
if(dir != NONE) {
this->releaseMouse();
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
m_mousePressed = false;
}
void RecordWidget::region(const QPoint &cursorGlobalPoint)
{
// 获取窗体在屏幕上的位置区域,tl为topleft点,rb为rightbottom点
QRect rect = this->rect();
QPoint tl = mapToGlobal(rect.topLeft());
QPoint rb = mapToGlobal(rect.bottomRight());
int x = cursorGlobalPoint.x();
int y = cursorGlobalPoint.y();
if(tl.x() + PADDING >= x && tl.x() <= x && tl.y() + PADDING >= y && tl.y() <= y) {
// 左上角
dir = LEFTTOP;
this->setCursor(QCursor(Qt::SizeFDiagCursor)); // 设置鼠标形状
} else if(x >= rb.x() - PADDING && x <= rb.x() && y >= rb.y() - PADDING && y <= rb.y()) {
// 右下角
dir = RIGHTBOTTOM;
this->setCursor(QCursor(Qt::SizeFDiagCursor));
} else if(x <= tl.x() + PADDING && x >= tl.x() && y >= rb.y() - PADDING && y <= rb.y()) {
//左下角
dir = LEFTBOTTOM;
this->setCursor(QCursor(Qt::SizeBDiagCursor));
} else if(x <= rb.x() && x >= rb.x() - PADDING && y >= tl.y() && y <= tl.y() + PADDING) {
// 右上角
dir = RIGHTTOP;
this->setCursor(QCursor(Qt::SizeBDiagCursor));
} else if(x <= tl.x() + PADDING && x >= tl.x()) {
// 左边
dir = LEFT;
this->setCursor(QCursor(Qt::SizeHorCursor));
} else if( x <= rb.x() && x >= rb.x() - PADDING) {
// 右边
dir = RIGHT;
this->setCursor(QCursor(Qt::SizeHorCursor));
}else if(y >= tl.y() && y <= tl.y() + PADDING){
// 上边
dir = UP;
this->setCursor(QCursor(Qt::SizeVerCursor));
} else if(y <= rb.y() && y >= rb.y() - PADDING) {
// 下边
dir = DOWN;
this->setCursor(QCursor(Qt::SizeVerCursor));
}else {
// 默认
dir = NONE;
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
/**
* @brief RecordWidget::on_lineEditWidth_editingFinished
*/
void RecordWidget::on_lineEditWidth_editingFinished()
{
int nW = ui->lineEditWidth->text().toInt();
int nH = ui->lineEditHeight->text().toInt();
this->resize(nW + 4, nH + 78);
}
/**
* @brief RecordWidget::on_lineEditHeight_editingFinished
* 修改录制框的高度
*/
void RecordWidget::on_lineEditHeight_editingFinished()
{
int nW = ui->lineEditWidth->text().toInt();
int nH = ui->lineEditHeight->text().toInt();
this->resize(nW + 4, nH + 78);
}
void RecordWidget::countdowntimerSlot()
{
ui->labelStatus->setText(QString("倒计时 %1...").arg(countdownnum));
countdownnum--;
if(countdownnum == -1){
countdowntimer.stop();
m_Fps = ui->lineEditFps->text().toInt();
m_strFileName = "temp.gif";
if (nullptr != m_gifWriter)
{
delete m_gifWriter;
m_gifWriter = nullptr;
}
int nW = ui->lineEditWidth->text().toInt();
int nH = ui->lineEditHeight->text().toInt();
m_gifWriter = new Gif_H::GifWriter;
bool bOk = m_Gif.GifBegin(m_gifWriter, m_strFileName.toLocal8Bit().data(), nW, nH, m_Fps);
if (!bOk)
{
delete m_gifWriter;
m_gifWriter = nullptr;
return;
}
SaveImage();
timer.start(m_Fps * 10);
}
}
void RecordWidget::resizeEvent(QResizeEvent *)
{
int nW = this->width() -4 ;
int nH = this->height() - 78;
ui->lineEditWidth->setText(QString::number(nW));
ui->lineEditHeight->setText(QString::number(nH));
ui->groupBox_2->setGeometry(2,this->height() - 46,this->width(),44);
ui->widgetTop->setGeometry(2,4,this->width(),20);
ui->label_gif->setGeometry(2,24,this->width() - 8,this->height() - 70);
}