#include "form.h"
#include "ui_form.h"
#include <QPainter>
#include <QFileDialog>
#include <QApplication>
#include <QScreen>
#include <QResizeEvent>
#include <QDateTime>
#include <QMessageBox>
#include <QTime>
#include <QDebug>
#include <QProcess>
#include <QTimer>
#include <QInputDialog>
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
mStartResize =false;
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
// this->sizePolicy();
this->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(ui->quit, SIGNAL(clicked()), qApp, SLOT(quit()));
poc = new QProcess(this);
ui->pause->setEnabled(false);
ui->stop->setEnabled(false);
timer.setInterval(50);
timer.start(50);
connect(&timer,SIGNAL(timeout()),this,SLOT(showtime()));
}
Form::~Form()
{
delete ui;
}
void Form::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.fillRect(rect(), Qt::red);
}
void Form::resizeEvent(QResizeEvent *event)
{
QRect rect = this->rect();
rect.adjust(3, 3, -3, -(ui->start->height()+ 5));
mRecordRect = rect;
QRegion region(this->rect());
setMask(region.xored(mRecordRect));
// ui->widget->move(width() - ui->widget->width() - 3, height() - ui->widget->height() - 3);
}
void Form::mousePressEvent(QMouseEvent *event)
{
QRect rect(QPoint(width() - 5, height() - 5), QSize(5, 5));
if (rect.contains(event->pos()) )
{
mStartResize = true;
mStartGeometry = QRect(event->globalPos(), size());
setCursor(Qt::SizeFDiagCursor);
}
else
{
if (event->button() == Qt::LeftButton)
{
mbLeftMousePressed = true;
moLastPos = event->globalPos();
}
}
}
void Form::mouseReleaseEvent(QMouseEvent *event)
{
mStartResize = false;
setCursor(Qt::ArrowCursor);
if (event->button() == Qt::LeftButton)
{
mbLeftMousePressed = false;
moLastPos = event->globalPos();
}
}
void Form::mouseMoveEvent(QMouseEvent *event)
{
QRect rect(QPoint(width() - 5, height() - 5), QSize(5, 5));
if (rect.contains(event->pos()) )
{
// mStartResize = true;
// mStartGeometry = QRect(event->globalPos(), size());
setCursor(Qt::SizeFDiagCursor);
}
else
{
setCursor(Qt::ArrowCursor);
}
if (mStartResize)
{
setCursor(Qt::SizeFDiagCursor);
QPoint ch = event->globalPos() - mStartGeometry.topLeft();
resize(mStartGeometry.size() + QSize(ch.x(), ch.y()));
}
else if (mbLeftMousePressed)
{
QPoint lastpos = pos();
lastpos.setX( lastpos.x() + event->globalX() - moLastPos.x());
lastpos.setY( lastpos.y() + event->globalY() - moLastPos.y());
move(lastpos);
moLastPos = event->globalPos();
}
}
void Form::on_start_clicked()
{
starttime = QTime::currentTime();
lasttime = starttime;
totalseconds =0;
ui->pause->setEnabled(true);
ui->stop->setEnabled(true);
ui->start->setEnabled(false);
filename = QDateTime::currentDateTime().toString("MM_dd_hh_mm_ss");
QString command = QString("ffmpeg -f gdigrab -i desktop -f dshow -i audio=\"virtual-audio-capturer\" -vcodec libx264 -acodec libmp3lame -video_size 600x480 -offset_x 100 -offset_y 60 -r %1 temp/%2.avi").arg(framrate) .arg(filename);
poc->setProcessChannelMode(QProcess::MergedChannels);
// poc->startDetached(command);
poc->start(command);
qDebug()<<command;
ui->pause->setText("暂停");
}
void Form::on_pause_clicked()
{
if(ui->pause->text() == "暂停")
{
ui->pause->setText("继续");
timer.stop();
// lasttime = QTime::currentTime();
totalseconds +=lasttime.secsTo(QTime::currentTime());
}
else
{
ui->pause->setText("暂停");
lasttime = QTime::currentTime();
timer.start(50);
}
}
void Form::on_stop_clicked()
{
ui->start->setEnabled(true);
ui->pause->setText("暂停");
ui->pause->setEnabled(false);
ui->stop->setEnabled(false);
QTimer::singleShot(15000,this,SLOT(realstop()));
}
void Form::realstop()
{
poc->kill();
// QProcess *poc = new QProcess(this);
QTimer::singleShot(2000,this,SLOT(cut()));
}
void Form::cut()
{
QString command = QString("ffmpeg -i temp/%1.avi -vf crop=%2:%3:%4:%5 new%6.avi -y").arg(filename).arg(this->rect().width()-6).arg(this->rect().height()-ui->start->height()-5-5).arg(pos().x()+3).arg(pos().y()+3).arg(filename);
poc->start(command);
qDebug()<<command;
QTimer::singleShot(5000,this,SLOT(removefile()));
// ffmpeg -ss 0 -i input.mp4 -t 10 -c:v copy -c:a copy output.mp4
}
void Form::removefile()
{
// QFile file(QString("%1.avi").arg(filename));
// file.remove();
QFile::remove(QString("temp/%1.avi").arg(filename));
}
void Form::on_configue_clicked()
{
bool ok;
int i = QInputDialog::getInt(this, tr("QInputDialog::getInteger()"),
tr("设置帧率:"), 15, 0, 30, 1, &ok);
if (ok)
framrate = i;
}
void Form::showtime()
{
int num = lasttime.secsTo(QTime::currentTime()) +totalseconds;
QTime tt;tt.setHMS(0,num/60,num%60);
if(!ui->start->isEnabled()&&ui->pause->text()=="暂停")
{
// totalseconds = lasttime.secsTo(QTime::currentTime()) + totalseconds;
QTime tt;tt.setHMS(0,num/60,num%60);
ui->time->setText( tt.toString("hh:mm:ss") );
}
}
void Form::on_play_clicked()
{
QProcess * p = new QProcess(this);
QString str = QString("ffplay new%1.avi").arg(filename);
p->start(str);
}