-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
169 lines (142 loc) · 4.41 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&grabFramesTimer, SIGNAL(timeout()), this, SLOT(on_grabFrame()));
connect(&updateGuiTimer, SIGNAL(timeout()), this, SLOT(on_updateGui()));
updateGuiTimer.start(500);
}
//Clean memory
MainWindow::~MainWindow()
{
if(videoWidget!=NULL)
delete videoWidget;
delete ui;
}
//Open a window to browse to the video source and then start reading the video
void MainWindow::on_browseButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, "Select video", QDir::homePath(), "Video files (*.*)");
std::string videoPath=fileName.toStdString();
if(videoPath=="") //if the user pressed "Cancel"
return;
if(!cap.open(videoPath)){
QMessageBox::information (0, "Error", QString("Invalid path"));
return;
}
cap.read(imgSrc);
frameDurationMs=1000/cap.get(CV_CAP_PROP_FPS);
if(videoWidget!=NULL)
delete videoWidget;
videoWidget=new GLWidget(ui->viewerGroupBox, imgSrc.size());
ui->viewerGroupBox->layout()->addWidget(videoWidget);
videoWidget->show();
ellapsedTimeMs=0;
on_satSlider_valueChanged(ui->satSlider->value());
on_brightSlider_valueChanged(ui->brightSlider->value());
grabFramesTimer.stop();
grabFramesTimer.start(frameDurationMs);
}
//Update progress bar & time label
void MainWindow::on_updateGui()
{
if(!cap.isOpened())
return;
//Saturation & brightness labels
// ui->satLabel->setText("Saturation: "+QString::number((float)ui->satSlider->value()/10));
// ui->brightLabel->setText("Brightness: "+QString::number((float)ui->brightSlider->value()/10));
//Time Label
int minutes=ellapsedTimeMs/60000;
int seconds=((int)ellapsedTimeMs%60000)/1000;
QString textTime=((minutes<10)?"0":"")+QString::number(minutes)+":"+((seconds<10)?"0":"")+QString::number(seconds);
ui->timeLabel->setText(textTime);
//Progress bar
int videoLenght=cap.get(CV_CAP_PROP_FRAME_COUNT)*frameDurationMs;
int progress= ui->progressSlider->maximum()* ellapsedTimeMs/videoLenght;
ui->progressSlider->setValue(progress);
}
//Reads new frames
void MainWindow::on_grabFrame()
{
if(!cap.isOpened())
return;
if(!cap.read(imgSrc))
{
ellapsedTimeMs=0;
cap.set(CV_CAP_PROP_POS_MSEC,ellapsedTimeMs);
}
ellapsedTimeMs+=frameDurationMs;
if(imgSrc.data)
videoWidget->updateImage(imgSrc.ptr());
}
//Rewind 10sec
void MainWindow::on_backButton_clicked()
{
if(cap.isOpened())
{
ellapsedTimeMs-=10000;
if(ellapsedTimeMs<0)
ellapsedTimeMs=0;
cap.set(CV_CAP_PROP_POS_MSEC,ellapsedTimeMs);
}
}
//Go forward 10sec
void MainWindow::on_forwardButton_clicked()
{
if(cap.isOpened())
{
ellapsedTimeMs+=10000;
cap.set(CV_CAP_PROP_POS_MSEC,ellapsedTimeMs);
}
}
//Toggle Pause / Play
void MainWindow::on_pauseButton_clicked()
{
if(grabFramesTimer.isActive())
grabFramesTimer.stop();
else
grabFramesTimer.start();
}
//Scroll video with progress bar
void MainWindow::on_progressSlider_sliderMoved(int position)
{
if(cap.isOpened())
{
float lenght=cap.get(CV_CAP_PROP_FRAME_COUNT)*frameDurationMs;
ellapsedTimeMs= position*lenght/ui->progressSlider->maximum();
cap.set(CV_CAP_PROP_POS_MSEC,ellapsedTimeMs);
}
else
{
ellapsedTimeMs=0;
ui->progressSlider->setValue(0);
return;
}
}
//Change Saturation
void MainWindow::on_satSlider_valueChanged(int position)
{
if(videoWidget!=NULL){
float val=(float)position/10;
ui->satLabel->setText("Saturation: "+QString::number(val));
videoWidget->setSaturation(val);
if(imgSrc.data) //update the image in case the video is paused
videoWidget->updateImage(imgSrc.ptr());
}
else
ui->satSlider->setValue(10);
}
//Change Brightness
void MainWindow::on_brightSlider_valueChanged(int position)
{
if(videoWidget!=NULL){
float val=(float)position/10;
ui->brightLabel->setText("Brightness: "+QString::number(val));
videoWidget->setBrightness(val);
if(imgSrc.data) //update the image in case the video is paused
videoWidget->updateImage(imgSrc.ptr());
}
else
ui->brightSlider->setValue(10);
}