-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreviewsWidget.cpp
188 lines (166 loc) · 4.06 KB
/
PreviewsWidget.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <QLabel>
#include <QMessageBox>
#include "PreviewsWidget.h"
/*! \brief Create and setup the previews widget
*
* Create and setup the previews widget
*
* @param parent parent
* @param mainwin pointer to the mainwindow so we can use s&s
* @param buff pointer to the images buffer
*/
PreviewsWidget::PreviewsWidget(
QWidget *parent,
QWidget *mainwin,
ImagesBuffer *buff
) : QWidget(parent), _bmng(buff)
{
// setup the layout
_base = new QHBoxLayout();
setLayout(_base);
QLabel *temp = new QLabel("Previews will be displayed here..");
temp->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
_base->addWidget(temp);
// init vars
_frame_margin_w = _frame_margin_h = 10;
_mid = 0;
_mid_index = 0;
// s&s
connect(this, SIGNAL(updateProgressText(QString)), mainwin, SLOT(updateProgressText(QString)));
}
/*! \brief Destroyer
*
* Destroyer
*/
PreviewsWidget::~PreviewsWidget()
{}
/*! \brief first setup of the entire widget
*
* Setup of the entire widget, must be called only after a video has been
* loaded by the buffer manager.
*
* @return success or not
*/
bool PreviewsWidget::setupPreviews()
{
if (!_bmng->isVideoLoaded()) {
QMessageBox::critical(NULL, "Error", "Can't setup previews, no video loaded.");
return false;
}
_bmng->getDimensions(_frame_ratio);
calculateFrameNumber();
reloadAndDrawPreviews(0);
return true;
}
/*! \brief reload the layout
*
* Recalculate previews dimensions and draw them
*/
void PreviewsWidget::reloadLayout()
{
if (!_bmng->isVideoLoaded())
return;
calculateFrameNumber();
reloadAndDrawPreviews(_mid);
}
/*! \brief reload images and draw
*
* Reload the images from the buffer and draw them
* @param mid middle frame number
*/
void PreviewsWidget::reloadAndDrawPreviews(const qint64 mid)
{
_frames.clear();
emit updateProgressText("Updating previews..");
_bmng->getImagesBuffer(_frames, mid, _frame_num);
_mid = mid;
drawPreviews();
emit updateProgressText("");
}
/*! \brief draw all previews
*
* Draw all previews in the widget
*/
void PreviewsWidget::drawPreviews()
{
clearPreviews(); // clear
// draw
for (int i = 0; i < _frame_num; ++i) {
// init layout and widgets
QWidget *prew = new QWidget();
QVBoxLayout *l = new QVBoxLayout();
l->setSpacing(0);
l->setMargin(0);
l->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
QLabel *img = new QLabel();
l->addWidget(img);
if (i == _mid_index)
prew->setStyleSheet("border-bottom:5px solid #005fb3;");
img->setStyleSheet("border:none;");
// prew->setStyleSheet("background-color:#005fb3;");
// img->setMinimumSize(1, 1);
// if it's a valid frame
if (i < _frames.size()) {
if (_frames[i].num != -1) {
// draw the img
img->setPixmap(
_frames[i].img.scaled(
_frame_w, _frame_h,
Qt::KeepAspectRatio, Qt::SmoothTransformation
)
);
// write frame number
QLabel *lbl = new QLabel(QString::number(_frames[i].num));
lbl->setFixedHeight(18);
lbl->setStyleSheet("border:none;");
l->addWidget(lbl);
}
else {
img->setText("");
}
}
else {
img->setText("Buffer not big enough");
img->setWordWrap(true);
}
// connect all elements
prew->setLayout(l);
_base->addWidget(prew);
}
}
/*! \brief clear the layout
*
* Remove all QWidgets from the layout
*/
void PreviewsWidget::clearPreviews()
{
clear(_base);
}
/*! \brief clear QLayout
*
* Recursively delete a QLayout and all its children
* @param layout layout to clear
*/
void PreviewsWidget::clear(QLayout* layout)
{
QLayoutItem* child;
while (layout->count() != 0) {
child = layout->takeAt(0);
if (child->layout() != 0)
clear(child->layout());
else if (child->widget() != 0)
delete child->widget();
delete child;
}
}
/*! \brief calculate frames number and size
*
* Adapt the number of frames based on the actual size of the widget
*/
void PreviewsWidget::calculateFrameNumber()
{
_frame_h = (height() - _frame_margin_h * 2) - 20; // 20 ~ label height
_frame_w = _frame_ratio * _frame_h; // frame w based on original frame ratio
_frame_num = width() / (_frame_w + _frame_margin_w * 2);
_mid_index = (_frame_num - 1) / 2;
}