]> git.sur5r.net Git - minitube/blob - src/loadingwidget.cpp
Refresh patches
[minitube] / src / loadingwidget.cpp
1 #include "loadingwidget.h"
2
3 LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) {
4
5     QPalette p = palette();
6     p.setBrush(backgroundRole(), Qt::black);
7     p.setBrush(QPalette::Text, Qt::white);
8     setPalette(p);
9
10     setAutoFillBackground(true);
11
12     QBoxLayout *layout = new QVBoxLayout();
13
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);
21
22     QFont biggerFont;
23     biggerFont.setPointSize(biggerFont.pointSize()*2);
24
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);
32
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);
41
42     setMouseTracking(true);
43
44     setLayout(layout);
45 }
46
47 void LoadingWidget::setVideo(Video *video) {
48
49     QFont titleFont;
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);
57
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);
66
67     static const int maxVideoLength = 256;
68     QString videoDesc = video->description();
69     if (videoDesc.length() > maxVideoLength) {
70         videoDesc.truncate(maxVideoLength-1);
71         videoDesc.append("...");
72     }
73     QFont descFont(titleFont);
74     descFont.setPixelSize(descFont.pixelSize() / 2);
75     descriptionLabel->setFont(descFont);
76     descriptionLabel->setText(videoDesc);
77     bool hiddenDesc = height() < 400;
78     if (hiddenDesc)
79         titleLabel->setAlignment(Qt::AlignCenter);
80     else
81         titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
82     descriptionLabel->setVisible(!hiddenDesc);
83
84     // progressBar->hide();
85     progressBar->setValue(0);
86     startTime.start();
87 }
88
89 void LoadingWidget::setError(QString message) {
90     titleLabel->setText(tr("Error"));
91     descriptionLabel->setText(message);
92     // progressBar->hide();
93     progressBar->setValue(0);
94 }
95
96 void LoadingWidget::bufferStatus(int percent) {
97     // qDebug() << percent;
98
99     /*
100     if (progressBar->isHidden() && percent > 0) {
101         progressBar->show();
102         QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity");
103         animation->setDuration(1000);
104         animation->setStartValue(0.0);
105         animation->setEndValue(1.0);
106         animation->start();
107     }*/
108     // progressBar->setShown(percent > 0);
109     if (startTime.elapsed() < 1000) return;
110     if (progressBar->value() == 0 && percent > 80) return;
111     progressBar->setValue(percent);
112 }
113
114 void LoadingWidget::clear() {
115     titleLabel->clear();
116     descriptionLabel->clear();
117     // progressBar->hide();
118     progressBar->setValue(0);
119 }