-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopplayerlabel.cpp
154 lines (123 loc) · 4.24 KB
/
loopplayerlabel.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
#include "loopplayerlabel.h"
#include <QPainter>
LoopPlayerLabel::LoopPlayerLabel(QWidget *parent) : QLabel(parent)
{
this->player = new LoopPlayer();
this->displayingRoi = new QVector<QPoint>();
this->selectedRoi = new QVector<QPoint>();
QObject::connect(this->player, SIGNAL(processedImage(QImage)), this, SLOT(updateLoopPlayerUI(QImage)));
this->updateLayoutValue();
}
LoopPlayer * LoopPlayerLabel::getPlayer() {
return this->player;
}
LoopRoi * LoopPlayerLabel::getLoopRoi() {
return &this->loopRoi;
}
void LoopPlayerLabel::updateLoopPlayerUI(QImage img)
{
if (!img.isNull())
{
this->setAlignment(Qt::AlignCenter);
QPixmap p = QPixmap::fromImage(img);
QPainter painter(&p);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(QColor(0, 0, 255), 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawLines(*this->loopRoi.points);
if (!this->loopRoi.drawing) {
auto prevPoint = *this->loopRoi.points->begin();
auto nextPoint = *(this->loopRoi.points->end()-1);
painter.drawLine(prevPoint, nextPoint);
}
this->setPixmap(p.scaled(this->size(), Qt::KeepAspectRatio, Qt::FastTransformation));
}
}
void LoopPlayerLabel::mouseMoveEvent(QMouseEvent *event)
{
if (this->loopRoi.drawing)
{
int x = event->x();
int y = event->y();
if (x < edgeLeft) x = edgeLeft;
if (x > edgeRight) x = edgeRight;
if (y < edgeTop) y = edgeTop;
if (y > edgeBottom) y = edgeBottom;
x = static_cast<int>((x-edgeLeft) / contentScale);
y = static_cast<int>((y-edgeTop) / contentScale);
QPoint point(x, y);
this->loopRoi.points->append(point);
}
}
void LoopPlayerLabel::mousePressEvent(QMouseEvent *event)
{
auto loopData = this->player->getLoopData();
if (loopData == nullptr) return;
this->loopRoi.drawing = true;
this->loopRoi.points = new QVector<QPoint>();
this->loopRoi.width = this->width();
this->loopRoi.height = this->height();
this->updateLayoutValue();
}
void LoopPlayerLabel::mouseReleaseEvent(QMouseEvent *event)
{
auto player = this->getPlayer();
auto loopData = player->getLoopData();
if (loopData == nullptr) return;
this->loopRoi.drawing = false;
this->updateRoiFrames();
}
void LoopPlayerLabel::updateRoiFrames()
{
auto player = this->getPlayer();
auto loopData = player->getLoopData();
if (loopData == nullptr) return;
player->pause();
auto frames = loopData->loadData();
auto roiFrames = this->loopRoi.generateRoiFrames(frames, *loopRoi.points);
loopData->setFrames(roiFrames);
player->play();
}
void LoopPlayerLabel::resizeEvent(QResizeEvent *event)
{
this->updateLayoutValue();
}
void LoopPlayerLabel::updateLayoutValue()
{
auto loopData = this->player->getLoopData();
if (loopData != nullptr)
{
contentWidth = loopData->getWidth();
contentHeight = loopData->getHeight();
}
contentRatio = static_cast<double>(contentWidth) / static_cast<double>(contentHeight);
screenWidth = this->width();
screenHeight = this->height();
screenRatio = static_cast<double>(screenWidth) / static_cast<double>(screenHeight);
edgeLeft = 0;
edgeRight = screenWidth;
edgeTop = 0;
edgeBottom = screenHeight;
if (contentRatio > screenRatio)
{
contentScale = static_cast<double>(screenWidth) / static_cast<double>(contentWidth);
int edge = static_cast<int>((screenHeight - contentHeight * contentScale) / 2);
edgeTop += edge;
edgeBottom -= edge;
} else if (contentRatio < screenRatio)
{
contentScale = static_cast<double>(screenHeight) / static_cast<double>(contentHeight);
int edge = static_cast<int>((screenWidth - contentWidth * contentScale) / 2);
edgeLeft += edge;
edgeRight -= edge;
}
}
void LoopPlayerLabel::writeFile(std::string filepath) {
auto loopData = this->player->getLoopData();
if (loopData != nullptr)
{
auto frames = loopData->loadData();
auto points = *this->loopRoi.points;
auto roiFrames = loopRoi.generateRoiFrames(frames, points);
loopData->writeFile(roiFrames, filepath);
}
}