]> git.sur5r.net Git - minitube/blob - src/videosourcewidget.cpp
Imported Upstream version 2.0
[minitube] / src / videosourcewidget.cpp
1 #include "videosourcewidget.h"
2 #include "videosource.h"
3 #include "video.h"
4 #include "fontutils.h"
5
6 VideoSourceWidget::VideoSourceWidget(VideoSource *videoSource, QWidget *parent)
7     : QWidget(parent),
8       videoSource(videoSource),
9       hovered(false),
10       pressed(false) {
11
12     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
13     setCursor(Qt::PointingHandCursor);
14     setFocusPolicy(Qt::StrongFocus);
15
16     connect(videoSource, SIGNAL(gotVideos(QList<Video*>)),
17             SLOT(previewVideo(QList<Video*>)), Qt::UniqueConnection);
18     videoSource->loadVideos(1, 1);
19 }
20
21 void VideoSourceWidget::activate() {
22     emit activated(videoSource);
23 }
24
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();
32 }
33
34 void VideoSourceWidget::setPixmapData(QByteArray bytes) {
35     video->deleteLater();
36     video = 0;
37     pixmap.loadFromData(bytes);
38     update();
39 }
40
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);
47     QPolygon polygon;
48     polygon << QPoint(padding, padding)
49             << QPoint(s - padding, s / 2)
50             << QPoint(padding, s - padding);
51     painter.setRenderHints(QPainter::Antialiasing, true);
52
53     // QColor color = pressed ? Qt::black : Qt::white;
54     QColor color = Qt::white;
55     painter.setBrush(color);
56     QPen pen;
57     pen.setColor(color);
58     pen.setWidth(10);
59     pen.setJoinStyle(Qt::RoundJoin);
60     pen.setCapStyle(Qt::RoundCap);
61     painter.setPen(pen);
62     painter.drawPolygon(polygon);
63     return playIcon;
64 }
65
66 void VideoSourceWidget::paintEvent(QPaintEvent *) {
67     if (pixmap.isNull()) return;
68
69     QPainter p(this);
70
71     const int w = width();
72     const int h = height();
73
74     int xOffset = 0;
75     int xOrigin = 0;
76     int wDiff = pixmap.width() - w;
77     if (wDiff > 0) xOffset = wDiff / 2;
78     else xOrigin = -wDiff / 2;
79     int yOffset = 0;
80     int yOrigin = 0;
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);
85
86     if (hovered) {
87         QPixmap play = playPixmap();
88         p.save();
89         p.setOpacity(.5);
90         p.drawPixmap(
91                     (w - play.width()) / 2,
92                     (h * 2/3 - play.height()) / 2,
93                     play
94                     );
95         p.restore();
96     }
97
98     QRect nameBox = rect();
99     nameBox.adjust(0, 0, 0, -h*2/3);
100     nameBox.translate(0, h - nameBox.height());
101     p.save();
102     p.setPen(Qt::NoPen);
103     p.setBrush(QColor(0, 0, 0, 128));
104     p.drawRect(nameBox);
105     p.restore();
106
107     QString name = videoSource->getName();
108     bool tooBig = false;
109     p.save();
110     p.setFont(FontUtils::medium());
111     QRect textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
112     if (textBox.height() > nameBox.height()) {
113         p.setFont(font());
114         textBox = p.boundingRect(nameBox, Qt::AlignCenter | Qt::TextWordWrap, name);
115         if (textBox.height() > nameBox.height()) {
116             p.setClipRect(nameBox);
117             tooBig = true;
118         }
119     }
120     p.setPen(Qt::white);
121     if (tooBig)
122         p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
123     else
124         p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
125     p.restore();
126
127     if (hasFocus()) {
128         p.save();
129         QPen pen;
130         pen.setBrush(palette().highlight());
131         pen.setWidth(2);
132         p.setPen(pen);
133         p.drawRect(rect());
134         p.restore();
135     }
136 }
137
138 void VideoSourceWidget::mouseMoveEvent (QMouseEvent *event) {
139     QWidget::mouseMoveEvent(event);
140     hovered = rect().contains(event->pos());
141 }
142
143 void VideoSourceWidget::mousePressEvent(QMouseEvent *event) {
144     QWidget::mousePressEvent(event);
145     if (event->button() != Qt::LeftButton) return;
146     pressed = true;
147     update();
148 }
149
150 void VideoSourceWidget::mouseReleaseEvent(QMouseEvent *event) {
151     QWidget::mouseReleaseEvent(event);
152     if (event->button() != Qt::LeftButton) return;
153     pressed = false;
154     if (hovered) emit activated(videoSource);
155 }
156
157 void VideoSourceWidget::leaveEvent(QEvent *event) {
158     QWidget::leaveEvent(event);
159     hovered = false;
160     update();
161 }
162
163 void VideoSourceWidget::enterEvent(QEvent *event) {
164     QWidget::enterEvent(event);
165     hovered = true;
166     update();
167 }
168
169 void VideoSourceWidget::keyReleaseEvent(QKeyEvent *event) {
170     if (event->key() == Qt::Key_Return)
171         emit activated(videoSource);
172 }