/
StoryBoard.cpp
173 lines (135 loc) · 4.88 KB
/
StoryBoard.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
#include "StoryBoard.h"
#include "Project.h"
#include <cmath>
#include <TimeLineItem.h>
#include <QHBoxLayout>
#include <QPushButton>
using namespace WTS;
StoryBoard::StoryBoard(QWidget *parent)
: TimeLineWidget(parent)
, m_videoWidth(640)
, m_videoHeight(360)
, m_selectedThumb(0)
, m_dragItem(0)
{
connect(m_mainWindow,SIGNAL(storyBoardChanged()),SLOT(updateSnapshots()));
QFrame * box = new QFrame();
box->setFrameShadow( QFrame::Raised );
box->setFrameShape( QFrame::Panel );
QHBoxLayout * layout = new QHBoxLayout(box);
layout->setMargin(0);
layout->setSpacing(0);
QPushButton * b = new QPushButton("<");
b->setFlat(true);
b->setMaximumSize(QSize(16,16));
layout->addWidget(b);
b = new QPushButton("x");
b->setFlat(true);
b->setMaximumSize(QSize(16,16));
layout->addWidget(b);
b = new QPushButton(">");
b->setFlat(true);
b->setMaximumSize(QSize(16,16));
layout->addWidget(b);
}
void StoryBoard::drawBackground ( QPainter * painter, const QRectF & rect )
{
TimeLineWidget::drawBackground(painter, rect);
painter->setFont( QFont("Monaco", 9) );
int maxLines = width() / 5;
float totalMin = (float)project()->duration() / 60000.0f;
float N[] = { 60, 30, 15, 12, 6, 5, 4, 3, 2 };
int Ncount = sizeof(N)/sizeof(N[0]);
int i;
for(i = 0; i<(Ncount-1) && ((totalMin * N[i]) > maxLines) ; ++i) {}
float dx = 1.0f / (N[i] * totalMin);
painter->setPen(QColor(50,60,50));
for(int j = 0; j<maxLines; ++j) {
float x = dx * j;
int rem = j % (int)N[i];
float y = 1.0 - (rem ? 0.5 * s_marginBottom : s_marginBottom);
painter->drawLine(QPointF(x,y),QPointF(x,1.0));
if (!rem) {
QString text = QString("%1:00").arg( floor(j / N[i]) );
double x_pix = x * width() + 3;
double y_pix = y * height() - 2;
painter->setMatrixEnabled(false);
painter->drawText(QPointF(x_pix,y_pix), text);
painter->setMatrixEnabled(true);
}
}
}
void StoryBoard::updateSnapshots()
{
float tt = project()->duration();
foreach(QGraphicsItem * item, m_msToItem) {
scene()->removeItem(item);
}
m_msToItem.clear();
m_dragItem = 0;
m_selectedThumb = 0;
int n = 0;
foreach(Project::Marker * m,
project()->getMarkers(Project::ANY, false)) {
float x = (float)m->at() / tt - 0.5 * m_thumbWidth;
int gapCount = s_levelCount - 1;
float y = s_marginY
// zig zag function
+ fabs(n++ % (2*gapCount) - gapCount )
// fit centers between two margins + two halves
* (1.0 - s_marginY - s_marginBottom - m_thumbHeight) / (float)gapCount;
TimeLineItem * tli = new TimeLineItem(m, scene());
tli->setEditModeOnly(false);
QGraphicsItem * frameItem = scene()->addRect(
QRectF(x - m_marginX,
y - s_marginY,
m_thumbWidth + 2.0*m_marginX,
m_thumbHeight + s_marginY*2.0),
QPen(Qt::NoPen),QBrush(QColor(255,255,255,150)));
frameItem->setParentItem(tli);
QGraphicsPixmapItem * gpi = scene()->addPixmap( m->snapshot() );
gpi->setPos(x,y);
gpi->scale(m_thumbScale/(float)width(), m_thumbScale/(float)height());
gpi->setParentItem(frameItem);
m_msToItem[m->at()] = tli;
}
}
void StoryBoard::resizeEvent ( QResizeEvent * event )
{
TimeLineWidget::resizeEvent(event);
m_thumbHeight = 0.55;
float pixH = (float)height() * m_thumbHeight;
float pixW = pixH * (float)m_videoWidth / (float)m_videoHeight;
m_thumbWidth = pixW / (float)width();
m_thumbScale = pixH/(float) m_videoHeight;
m_marginX = s_marginY * (float)height() / (float)width();
updateSnapshots();
}
void StoryBoard::setCurrentTime(qint64 time)
{
TimeLineWidget::setCurrentTime(time);
if (m_msToItem.size() == 0)
return; // no thumbs to select
// find the last marker / item that we've passed
QMap<qint64, QGraphicsItem *>::iterator iter = m_msToItem.upperBound(time);
if (iter != m_msToItem.begin())
iter--;
QGraphicsItem * lastItem = iter.value();
if (lastItem && lastItem != m_selectedThumb) {
if (m_selectedThumb) {
m_selectedThumb->setZValue(0.0);
QGraphicsRectItem * ri = dynamic_cast<QGraphicsRectItem *>(m_selectedThumb);
if (ri)
ri->setPen(Qt::NoPen);
}
m_selectedThumb = lastItem;
m_selectedThumb->setZValue(1.0);
QGraphicsRectItem * ri = dynamic_cast<QGraphicsRectItem *>(m_selectedThumb);
if (ri) ri->setPen(QPen(Qt::blue));
}
}
void StoryBoard::setProject(Project *project)
{
TimeLineWidget::setProject(project);
connect(project, SIGNAL(storyBoardChanged()), SLOT(updateSnapshots()));
}