-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
133 lines (118 loc) · 4.09 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
rubberBand(NULL)
{
myPlayer = new Player();
progressBar = new QProgressDialog(tr("Chargement de la vidéo..."), tr("Annuler"), 0, 100, this);
progressBar->setWindowModality(Qt::WindowModal);
progressBar->setCancelButton(0);
progressBar->setWindowTitle(tr("Chargement..."));
QObject::connect(myPlayer, SIGNAL(processedImage(QImage)),
this, SLOT(updatePlayerUI(QImage)));
QObject::connect(myPlayer->video().get(), SIGNAL(loadedFrame(long)), this, SLOT(update_progress_bar(long)));
ui->setupUi(this);
QObject::connect(ui->label, SIGNAL(Mouse_Press(QMouseEvent*)), this, SLOT(startDrawRubberBand(QMouseEvent*)));
QObject::connect(ui->label, SIGNAL(Mouse_Move(QMouseEvent*)), this, SLOT(drawRubberBand(QMouseEvent*)));
ui->pushButton_2->setEnabled(false);
ui->horizontalSlider->setEnabled(false);
}
void MainWindow::updatePlayerUI(QImage img)
{
if (!img.isNull())
{
ui->label->setAlignment(Qt::AlignCenter);
ui->label->setPixmap(QPixmap::fromImage(img).scaled(ui->label->size(),
Qt::KeepAspectRatio, Qt::FastTransformation));
ui->horizontalSlider->setValue(myPlayer->getCurrentFrame());
ui->label_2->setText( getFormattedTime( (int)myPlayer->getCurrentFrame()/(int)myPlayer->getFrameRate()));
}
}
MainWindow::~MainWindow()
{
delete myPlayer;
delete ui;
delete rubberBand;
delete progressBar;
}
void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open Video"), ".",
tr("Video Files (*.avi *.mpg *.mp4)"));
QFileInfo name = filename;
if (!filename.isEmpty()){
progressBar->show();
if (!myPlayer->loadVideo(filename.toStdString().data()))
{
QMessageBox msgBox;
msgBox.setText("The selected video could not be opened!");
msgBox.exec();
}
else{
this->setWindowTitle(name.fileName());
ui->pushButton_2->setEnabled(true);
ui->horizontalSlider->setEnabled(true);
ui->horizontalSlider->setMaximum(myPlayer->getNumberOfFrames());
ui->label_3->setText( getFormattedTime( (int)myPlayer->getNumberOfFrames()/(int)myPlayer->getFrameRate()) );
}
progressBar->hide();
}
}
void MainWindow::on_pushButton_2_clicked()
{
if (myPlayer->isStopped())
{
myPlayer->Play();
ui->pushButton_2->setText(tr("Stop"));
}else
{
myPlayer->Stop();
ui->pushButton_2->setText(tr("Play"));
}
}
QString MainWindow::getFormattedTime(int timeInSeconds){
int seconds = (int) (timeInSeconds) % 60 ;
int minutes = (int) ((timeInSeconds / 60) % 60);
int hours = (int) ((timeInSeconds / (60*60)) % 24);
QTime t(hours, minutes, seconds);
if (hours == 0 )
return t.toString("mm:ss");
else
return t.toString("h:mm:ss");
}
void MainWindow::startDrawRubberBand(QMouseEvent *event)
{
origin = event->pos();
if (!rubberBand){
rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->label);
} else {
rubberBand->hide();
}
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}
void MainWindow::drawRubberBand(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}
void MainWindow::on_horizontalSlider_sliderPressed()
{
myPlayer->Stop();
}
void MainWindow::on_horizontalSlider_sliderReleased()
{
myPlayer->Play();
}
void MainWindow::update_progress_bar(long value)
{
int newValue = (100*value)/myPlayer->video()->getTotalFrameNumber();
progressBar->setValue(newValue);
}
void MainWindow::on_horizontalSlider_sliderMoved(int position)
{
myPlayer->setCurrentFrame(position);
ui->label_2->setText( getFormattedTime( position/(int)myPlayer->getFrameRate()) );
}