1 #include "channelwidget.h"
2 #include "videosource.h"
3 #include "ytuser.h"
4 #include "fontutils.h"
5 
ChannelWidget(VideoSource * videoSource,YTUser * user,QWidget * parent)6 ChannelWidget::ChannelWidget(VideoSource *videoSource, YTUser *user, QWidget *parent) :
7     GridWidget(parent) {
8     this->user = user;
9     this->videoSource = videoSource;
10 
11     setMinimumSize(132, 176);
12     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
13 
14     connect(user, SIGNAL(infoLoaded()), SLOT(gotUserInfo()));
15     // user->loadfromAPI();
16 
17     connect(this, SIGNAL(activated()), SLOT(activate()));
18 }
19 
activate()20 void ChannelWidget::activate() {
21     // YTUser.checked(user->getUserId());
22     emit activated(videoSource);
23 }
24 
gotUserInfo()25 void ChannelWidget::gotUserInfo() {
26     connect(user, SIGNAL(thumbnailLoaded(QByteArray)), SLOT(gotUserThumbnail(QByteArray)));
27     // user->loadThumbnail();
28     update();
29 }
30 
paintEvent(QPaintEvent *)31 void ChannelWidget::paintEvent(QPaintEvent *) {
32     if (thumbnail.isNull()) return;
33 
34     const int w = width();
35     const int h = height();
36 
37     QPainter p(this);
38     p.drawPixmap((w - thumbnail.width()) / 2, 0, thumbnail);
39     //(h - thumbnail.height()) / 2
40 
41     QRect nameBox = rect();
42     nameBox.adjust(0, 0, 0, -thumbnail.height() - 10);
43     nameBox.translate(0, h - nameBox.height());
44 
45     QString name = user->getDisplayName();
46     bool tooBig = false;
47     p.save();
48     /*
49     QFont f = font();
50     f.setFamily("Helvetica");
51     p.setFont(f);
52     */
53     QRect textBox = p.boundingRect(nameBox, Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap, name);
54     if (textBox.height() > nameBox.height()) {
55         p.setFont(font());
56         textBox = p.boundingRect(nameBox, Qt::AlignTop | Qt::AlignHCenter | Qt::TextWordWrap, name);
57         if (textBox.height() > nameBox.height()) {
58             p.setClipRect(nameBox);
59             tooBig = true;
60         }
61     }
62     p.setPen(Qt::black);
63     if (tooBig)
64         p.drawText(nameBox, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, name);
65     else
66         p.drawText(textBox, Qt::AlignCenter | Qt::TextWordWrap, name);
67     p.restore();
68 
69     if (hasFocus()) {
70         p.save();
71         QPen pen;
72         pen.setBrush(palette().highlight());
73         pen.setWidth(2);
74         p.setPen(pen);
75         p.drawRect(rect());
76         p.restore();
77     }
78 }
79 
gotUserThumbnail(QByteArray bytes)80 void ChannelWidget::gotUserThumbnail(QByteArray bytes) {
81     thumbnail.loadFromData(bytes);
82     if (thumbnail.width() > 88)
83         thumbnail = thumbnail.scaledToWidth(88, Qt::SmoothTransformation);
84     update();
85 }
86