1 #include "loadingwidget.h"
3 LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) {
5 QPalette p = palette();
6 p.setBrush(backgroundRole(), Qt::black);
7 p.setBrush(QPalette::Text, Qt::white);
10 setAutoFillBackground(true);
12 QBoxLayout *layout = new QVBoxLayout();
14 titleLabel = new QLabel(this);
15 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
16 titleLabel->setPalette(p);
17 titleLabel->setForegroundRole(QPalette::Text);
18 titleLabel->setWordWrap(true);
19 titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
20 layout->addWidget(titleLabel);
23 biggerFont.setPointSize(biggerFont.pointSize()*2);
25 descriptionLabel = new QLabel(this);
26 descriptionLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
27 descriptionLabel->setPalette(p);
28 descriptionLabel->setForegroundRole(QPalette::Text);
29 descriptionLabel->setWordWrap(true);
30 descriptionLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
31 layout->addWidget(descriptionLabel);
33 progressBar = new QProgressBar(this);
34 progressBar->setAutoFillBackground(false);
35 progressBar->setBackgroundRole(QPalette::Window);
36 progressBar->setPalette(p);
37 // progressBar->hide();
38 progressBar->setStyleSheet("QProgressBar {max-height:3px; background:black; border:0} QProgressBar::chunk {background:white}");
39 progressBar->setTextVisible(false);
40 layout->addWidget(progressBar);
42 setMouseTracking(true);
47 void LoadingWidget::setVideo(Video *video) {
50 int smallerDimension = qMin(height(), width());
51 titleFont.setPixelSize(smallerDimension / 12);
52 QFontMetrics fm(titleFont);
53 int textHeightInPixels = fm.height();
54 int spacing = textHeightInPixels / 2;
55 layout()->setSpacing(spacing);
56 layout()->setMargin(spacing);
58 QString title = video->title();
59 // enhance legibility by splitting the title
60 title = title.replace(" - ", "<p>");
61 title = title.replace("] ", "]<p>");
62 title = title.replace(" [", "<p>[");
63 titleLabel->setText(title);
64 titleLabel->setVisible(window()->height() > 100);
65 titleLabel->setFont(titleFont);
67 static const int maxVideoLength = 256;
68 QString videoDesc = video->description();
69 if (videoDesc.length() > maxVideoLength) {
70 videoDesc.truncate(maxVideoLength-1);
71 videoDesc.append("...");
73 QFont descFont(titleFont);
74 descFont.setPixelSize(descFont.pixelSize() / 2);
75 descriptionLabel->setFont(descFont);
76 descriptionLabel->setText(videoDesc);
77 bool hiddenDesc = height() < 400;
79 titleLabel->setAlignment(Qt::AlignCenter);
81 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
82 descriptionLabel->setVisible(!hiddenDesc);
84 // progressBar->hide();
85 progressBar->setValue(0);
89 void LoadingWidget::setError(QString message) {
90 titleLabel->setText(tr("Error"));
91 descriptionLabel->setText(message);
92 // progressBar->hide();
93 progressBar->setValue(0);
96 void LoadingWidget::bufferStatus(int percent) {
97 // qDebug() << percent;
100 if (progressBar->isHidden() && percent > 0) {
102 QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity");
103 animation->setDuration(1000);
104 animation->setStartValue(0.0);
105 animation->setEndValue(1.0);
108 // progressBar->setShown(percent > 0);
109 if (startTime.elapsed() < 1000) return;
110 if (progressBar->value() == 0 && percent > 80) return;
111 progressBar->setValue(percent);
114 void LoadingWidget::clear() {
116 descriptionLabel->clear();
117 // progressBar->hide();
118 progressBar->setValue(0);