]> git.sur5r.net Git - minitube/blob - src/videosourcewidget.cpp
Update watchfile
[minitube] / src / videosourcewidget.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "videosourcewidget.h"
22 #include "videosource.h"
23 #include "video.h"
24 #include "fontutils.h"
25
26 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
27     : GridWidget(parent),
28       videoSource(videoSource) {
29
30     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
31
32     connect(videoSource, SIGNAL(gotVideos(QList<Video*>)),
33             SLOT(previewVideo(QList<Video*>)), Qt::UniqueConnection);
34     videoSource->loadVideos(1, 1);
35
36     connect(this, SIGNAL(activated()), SLOT(activate()));
37 }
38
39 void VideoSourceWidget::activate() {
40     emit activated(videoSource);
41 }
42
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();
50 }
51
52 void VideoSourceWidget::setPixmapData(QByteArray bytes) {
53     video->deleteLater();
54     video = 0;
55     pixmap.loadFromData(bytes);
56     update();
57 }
58
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);
65     QPolygon polygon;
66     polygon << QPoint(padding, padding)
67             << QPoint(s - padding, s / 2)
68             << QPoint(padding, s - padding);
69     painter.setRenderHints(QPainter::Antialiasing, true);
70
71     // QColor color = pressed ? Qt::black : Qt::white;
72     QColor color = Qt::white;
73     painter.setBrush(color);
74     QPen pen;
75     pen.setColor(color);
76     pen.setWidth(10);
77     pen.setJoinStyle(Qt::RoundJoin);
78     pen.setCapStyle(Qt::RoundCap);
79     painter.setPen(pen);
80     painter.drawPolygon(polygon);
81     return playIcon;
82 }
83
84 void VideoSourceWidget::paintEvent(QPaintEvent *event) {
85     GridWidget::paintEvent(event);
86     if (pixmap.isNull()) return;
87
88     QPainter p(this);
89
90     const int w = width();
91     const int h = height();
92
93     int xOffset = 0;
94     int xOrigin = 0;
95     int wDiff = pixmap.width() - w;
96     if (wDiff > 0) xOffset = wDiff / 2;
97     else xOrigin = -wDiff / 2;
98     int yOffset = 0;
99     int yOrigin = 0;
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);
104
105     if (hovered) {
106         QPixmap play = playPixmap();
107         p.save();
108         p.setOpacity(.5);
109         p.drawPixmap(
110                     (w - play.width()) / 2,
111                     (h * 2/3 - play.height()) / 2,
112                     play
113                     );
114         p.restore();
115     }
116
117     QRect nameBox = rect();
118     nameBox.adjust(0, 0, 0, -h*2/3);
119     nameBox.translate(0, h - nameBox.height());
120     p.save();
121     p.setPen(Qt::NoPen);
122     p.setBrush(QColor(0, 0, 0, 128));
123     p.drawRect(nameBox);
124     p.restore();
125
126     QString name = videoSource->getName();
127     bool tooBig = false;
128     p.save();
129     p.setFont(FontUtils::medium());
130     QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
131     if (textBox.height() > nameBox.height()) {
132         p.setFont(font());
133         textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
134         if (textBox.height() > nameBox.height()) {
135             p.setClipRect(nameBox);
136             tooBig = true;
137         }
138     }
139     p.setPen(Qt::white);
140     if (tooBig)
141         p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
142     else
143         p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
144     p.restore();
145
146     if (hasFocus()) {
147         p.save();
148         QPen pen;
149         pen.setBrush(palette().highlight());
150         pen.setWidth(2);
151         p.setPen(pen);
152         p.drawRect(rect());
153         p.restore();
154     }
155 }