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) {
66 titleFont.setFamily("Helvetica");
69 titleFont.setFamily("Segoe UI Light");
71 int smallerDimension = qMin(height(), width());
72 titleFont.setPixelSize(smallerDimension / 12);
73 titleFont.setHintingPreference(QFont::PreferNoHinting);
74 QFontMetrics fm(titleFont);
75 int textHeightInPixels = fm.height();
76 int spacing = textHeightInPixels / 2;
77 layout()->setSpacing(spacing);
78 layout()->setMargin(spacing);
80 QString title = video->title();
81 // enhance legibility by splitting the title
82 title = title.replace(" - ", "<p>");
83 title = title.replace("] ", "]<p>");
84 title = title.replace(" [", "<p>[");
85 titleLabel->setText(title);
86 titleLabel->setVisible(window()->height() > 100);
87 titleLabel->setFont(titleFont);
89 static const int maxDescLength = 256;
90 QString videoDesc = video->description();
91 if (videoDesc.length() > maxDescLength) {
92 videoDesc.truncate(maxDescLength-1);
93 videoDesc.append("...");
95 QFont descFont(titleFont);
96 descFont.setPixelSize(descFont.pixelSize() / 2);
97 descFont.setHintingPreference(QFont::PreferNoHinting);
98 descriptionLabel->setFont(descFont);
99 descriptionLabel->setText(videoDesc);
100 bool hiddenDesc = height() < 400;
102 titleLabel->setAlignment(Qt::AlignCenter);
104 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
105 descriptionLabel->setVisible(!hiddenDesc);
107 // progressBar->hide();
108 progressBar->setValue(0);
112 void LoadingWidget::setError(QString message) {
113 titleLabel->setText(tr("Error"));
114 descriptionLabel->setText(message);
115 // progressBar->hide();
116 progressBar->setValue(0);
119 void LoadingWidget::bufferStatus(int percent) {
120 // qDebug() << percent;
123 if (progressBar->isHidden() && percent > 0) {
125 QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity");
126 animation->setDuration(1000);
127 animation->setStartValue(0.0);
128 animation->setEndValue(1.0);
131 // progressBar->setShown(percent > 0);
132 if (startTime.elapsed() < 1000) return;
133 if (progressBar->value() == 0 && percent > 80) return;
134 progressBar->setValue(percent);
137 void LoadingWidget::clear() {
139 descriptionLabel->clear();
140 // progressBar->hide();
141 progressBar->setValue(0);