3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "videosourcewidget.h"
22 #include "videosource.h"
24 #include "fontutils.h"
26 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
28 videoSource(videoSource) {
30 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
32 connect(videoSource, SIGNAL(gotVideos(QList<Video*>)),
33 SLOT(previewVideo(QList<Video*>)), Qt::UniqueConnection);
34 videoSource->loadVideos(1, 1);
36 connect(this, SIGNAL(activated()), SLOT(activate()));
39 void VideoSourceWidget::activate() {
40 emit activated(videoSource);
43 void VideoSourceWidget::previewVideo(QList<Video*> videos) {
44 videoSource->disconnect();
45 if (videos.isEmpty()) return;
46 video = videos.first();
47 connect(video, SIGNAL(gotMediumThumbnail(QByteArray)),
48 SLOT(setPixmapData(QByteArray)), Qt::UniqueConnection);
49 video->loadMediumThumbnail();
52 void VideoSourceWidget::setPixmapData(QByteArray bytes) {
55 pixmap.loadFromData(bytes);
59 QPixmap VideoSourceWidget::playPixmap() {
60 const int s = height() / 2;
61 const int padding = s / 8;
62 QPixmap playIcon = QPixmap(s, s);
63 playIcon.fill(Qt::transparent);
64 QPainter painter(&playIcon);
66 polygon << QPoint(padding, padding)
67 << QPoint(s - padding, s / 2)
68 << QPoint(padding, s - padding);
69 painter.setRenderHints(QPainter::Antialiasing, true);
71 // QColor color = pressed ? Qt::black : Qt::white;
72 QColor color = Qt::white;
73 painter.setBrush(color);
77 pen.setJoinStyle(Qt::RoundJoin);
78 pen.setCapStyle(Qt::RoundCap);
80 painter.drawPolygon(polygon);
84 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
85 GridWidget::paintEvent(event);
86 if (pixmap.isNull()) return;
90 const int w = width();
91 const int h = height();
95 int wDiff = pixmap.width() - w;
96 if (wDiff > 0) xOffset = wDiff / 2;
97 else xOrigin = -wDiff / 2;
100 int hDiff = pixmap.height() - h;
101 if (hDiff > 0) yOffset = hDiff / 4;
102 else yOrigin = -hDiff / 2;
103 p.drawPixmap(xOrigin, yOrigin, pixmap, xOffset, yOffset, w, h);
106 QPixmap play = playPixmap();
110 (w - play.width()) / 2,
111 (h * 2/3 - play.height()) / 2,
117 QRect nameBox = rect();
118 nameBox.adjust(0, 0, 0, -h*2/3);
119 nameBox.translate(0, h - nameBox.height());
122 p.setBrush(QColor(0, 0, 0, 128));
126 QString name = videoSource->getName();
129 p.setFont(FontUtils::medium());
130 QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
131 if (textBox.height() > nameBox.height()) {
133 textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
134 if (textBox.height() > nameBox.height()) {
135 p.setClipRect(nameBox);
141 p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
143 p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
149 pen.setBrush(palette().highlight());