1 #include "videosourcewidget.h"
2 #include "videosource.h"
6 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
8 videoSource(videoSource),
12 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
13 setCursor(Qt::PointingHandCursor);
14 setFocusPolicy(Qt::StrongFocus);
16 connect(videoSource, SIGNAL(gotVideos(QList<Video*>)),
17 SLOT(previewVideo(QList<Video*>)), Qt::UniqueConnection);
18 videoSource->loadVideos(1, 1);
21 void VideoSourceWidget::activate() {
22 emit activated(videoSource);
25 void VideoSourceWidget::previewVideo(QList<Video*> videos) {
26 videoSource->disconnect();
27 if (videos.isEmpty()) return;
28 video = videos.first();
29 connect(video, SIGNAL(gotMediumThumbnail(QByteArray)),
30 SLOT(setPixmapData(QByteArray)), Qt::UniqueConnection);
31 video->loadMediumThumbnail();
34 void VideoSourceWidget::setPixmapData(QByteArray bytes) {
37 pixmap.loadFromData(bytes);
41 QPixmap VideoSourceWidget::playPixmap() {
42 const int s = height() / 2;
43 const int padding = s / 8;
44 QPixmap playIcon = QPixmap(s, s);
45 playIcon.fill(Qt::transparent);
46 QPainter painter(&playIcon);
48 polygon << QPoint(padding, padding)
49 << QPoint(s - padding, s / 2)
50 << QPoint(padding, s - padding);
51 painter.setRenderHints(QPainter::Antialiasing, true);
53 // QColor color = pressed ? Qt::black : Qt::white;
54 QColor color = Qt::white;
55 painter.setBrush(color);
59 pen.setJoinStyle(Qt::RoundJoin);
60 pen.setCapStyle(Qt::RoundCap);
62 painter.drawPolygon(polygon);
66 void VideoSourceWidget::paintEvent(QPaintEvent *) {
67 if (pixmap.isNull()) return;
71 const int w = width();
72 const int h = height();
76 int wDiff = pixmap.width() - w;
77 if (wDiff > 0) xOffset = wDiff / 2;
78 else xOrigin = -wDiff / 2;
81 int hDiff = pixmap.height() - h;
82 if (hDiff > 0) yOffset = hDiff / 4;
83 else yOrigin = -hDiff / 2;
84 p.drawPixmap(xOrigin, yOrigin, pixmap, xOffset, yOffset, w, h);
87 QPixmap play = playPixmap();
91 (w - play.width()) / 2,
92 (h * 2/3 - play.height()) / 2,
98 QRect nameBox = rect();
99 nameBox.adjust(0, 0, 0, -h*2/3);
100 nameBox.translate(0, h - nameBox.height());
103 p.setBrush(QColor(0, 0, 0, 128));
107 QString name = videoSource->getName();
110 p.setFont(FontUtils::medium());
111 QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
112 if (textBox.height() > nameBox.height()) {
114 textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
115 if (textBox.height() > nameBox.height()) {
116 p.setClipRect(nameBox);
122 p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
124 p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
130 pen.setBrush(palette().highlight());
138 void VideoSourceWidget::mouseMoveEvent (QMouseEvent *event) {
139 QWidget::mouseMoveEvent(event);
140 hovered = rect().contains(event->pos());
143 void VideoSourceWidget::mousePressEvent(QMouseEvent *event) {
144 QWidget::mousePressEvent(event);
145 if (event->button() != Qt::LeftButton) return;
150 void VideoSourceWidget::mouseReleaseEvent(QMouseEvent *event) {
151 QWidget::mouseReleaseEvent(event);
152 if (event->button() != Qt::LeftButton) return;
154 if (hovered) emit activated(videoSource);
157 void VideoSourceWidget::leaveEvent(QEvent *event) {
158 QWidget::leaveEvent(event);
163 void VideoSourceWidget::enterEvent(QEvent *event) {
164 QWidget::enterEvent(event);
169 void VideoSourceWidget::keyReleaseEvent(QKeyEvent *event) {
170 if (event->key() == Qt::Key_Return)
171 emit activated(videoSource);