3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "loadingwidget.h"
23 LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) {
25 QPalette p = palette();
26 p.setBrush(backgroundRole(), Qt::black);
27 p.setBrush(QPalette::Text, Qt::white);
30 setAutoFillBackground(true);
32 QBoxLayout *layout = new QVBoxLayout(this);
34 titleLabel = new QLabel(this);
35 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
36 titleLabel->setPalette(p);
37 titleLabel->setForegroundRole(QPalette::Text);
38 titleLabel->setWordWrap(true);
39 titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
40 layout->addWidget(titleLabel);
42 descriptionLabel = new QLabel(this);
43 descriptionLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
44 descriptionLabel->setPalette(p);
45 descriptionLabel->setForegroundRole(QPalette::Text);
46 descriptionLabel->setWordWrap(true);
47 descriptionLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Expanding);
48 layout->addWidget(descriptionLabel);
50 progressBar = new QProgressBar(this);
51 progressBar->setAutoFillBackground(false);
52 progressBar->setBackgroundRole(QPalette::Window);
53 progressBar->setPalette(p);
54 // progressBar->hide();
55 progressBar->setStyleSheet("QProgressBar {max-height:3px; background:black; border:0} QProgressBar::chunk {background:white}");
56 progressBar->setTextVisible(false);
57 layout->addWidget(progressBar);
59 setMouseTracking(true);
62 void LoadingWidget::setVideo(Video *video) {
65 titleFont.setStyleName("Light");
67 titleFont.setFamily("Helvetica Neue");
70 titleFont.setFamily("Segoe UI Light");
72 int smallerDimension = qMin(height(), width());
73 titleFont.setPixelSize(smallerDimension / 12);
74 titleFont.setHintingPreference(QFont::PreferNoHinting);
75 QFontMetrics fm(titleFont);
76 int textHeightInPixels = fm.height();
77 int spacing = textHeightInPixels / 2;
78 layout()->setSpacing(spacing);
79 layout()->setMargin(spacing);
81 QString title = video->title();
82 // enhance legibility by splitting the title
83 title = title.replace(" - ", "<p>");
84 title = title.replace("] ", "]<p>");
85 title = title.replace(" [", "<p>[");
86 titleLabel->setText(title);
87 titleLabel->setVisible(window()->height() > 100);
88 titleLabel->setFont(titleFont);
90 static const int maxDescLength = 256;
91 QString videoDesc = video->description();
92 if (videoDesc.length() > maxDescLength) {
93 videoDesc.truncate(maxDescLength-1);
94 videoDesc.append("...");
96 QFont descFont(titleFont);
97 descFont.setPixelSize(descFont.pixelSize() / 2);
98 descFont.setHintingPreference(QFont::PreferNoHinting);
99 descriptionLabel->setFont(descFont);
100 descriptionLabel->setText(videoDesc);
101 bool hiddenDesc = height() < 400;
103 titleLabel->setAlignment(Qt::AlignCenter);
105 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
106 descriptionLabel->setVisible(!hiddenDesc);
108 // progressBar->hide();
109 progressBar->setValue(0);
113 void LoadingWidget::setError(QString message) {
114 titleLabel->setText(tr("Error"));
115 descriptionLabel->setText(message);
116 // progressBar->hide();
117 progressBar->setValue(0);
120 void LoadingWidget::bufferStatus(int percent) {
121 // qDebug() << percent;
124 if (progressBar->isHidden() && percent > 0) {
126 QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity");
127 animation->setDuration(1000);
128 animation->setStartValue(0.0);
129 animation->setEndValue(1.0);
132 // progressBar->setShown(percent > 0);
133 if (startTime.elapsed() < 1000) return;
134 if (progressBar->value() == 0 && percent > 80) return;
135 progressBar->setValue(percent);
138 void LoadingWidget::clear() {
140 descriptionLabel->clear();
141 // progressBar->hide();
142 progressBar->setValue(0);