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 descriptionLabel->setTextFormat(Qt::RichText);
49 descriptionLabel->setOpenExternalLinks(true);
50 layout->addWidget(descriptionLabel);
52 progressBar = new QProgressBar(this);
53 progressBar->setAutoFillBackground(false);
54 progressBar->setBackgroundRole(QPalette::Window);
55 progressBar->setPalette(p);
56 // progressBar->hide();
57 progressBar->setStyleSheet("QProgressBar {max-height:3px; background:black; border:0} QProgressBar::chunk {background:white}");
58 progressBar->setTextVisible(false);
59 layout->addWidget(progressBar);
61 setMouseTracking(true);
64 void LoadingWidget::setVideo(Video *video) {
68 titleFont.setFamily("Helvetica Neue");
69 titleFont.setStyleName("Thin");
71 titleFont.setFamily("Segoe UI Light");
72 titleFont.setStyleName("Light");
74 titleFont.setStyleName("Light");
76 int smallerDimension = qMin(height(), width());
77 titleFont.setPixelSize(smallerDimension / 12);
78 QFontMetrics fm(titleFont);
79 int textHeightInPixels = fm.height();
80 int spacing = textHeightInPixels / 2;
81 layout()->setSpacing(spacing);
82 layout()->setMargin(spacing);
84 QString title = video->title();
85 // enhance legibility by splitting the title
86 title.replace(QLatin1String(" - "), QLatin1String("<p>"));
87 title.replace(QLatin1String(" | "), QLatin1String("<p>"));
88 title.replace(QLatin1String(" — "), QLatin1String("<p>"));
89 title.replace(QLatin1String("] "), QLatin1String("]<p>"));
90 title.replace(QLatin1String(" ["), QLatin1String("<p>["));
91 title.replace(QLatin1String(" ("), QLatin1String("<p>("));
92 title.replace(QLatin1String(") "), QLatin1String(")<p>"));
93 titleLabel->setText(title);
94 titleLabel->setVisible(window()->height() > 100);
95 titleLabel->setFont(titleFont);
97 static const int maxDescLength = 256;
98 QString videoDesc = video->description();
99 if (videoDesc.length() > maxDescLength) {
100 videoDesc.truncate(maxDescLength-1);
101 videoDesc = videoDesc.trimmed();
102 videoDesc.append("…");
103 } else if (videoDesc.endsWith(QLatin1String(" ..."))) {
104 videoDesc = videoDesc.left(videoDesc.length() - 4);
105 videoDesc.append("…");
107 videoDesc.replace(QRegExp("(https?://\\S+)"), "<a style='color:white' href=\"\\1\">\\1</a>");
108 QFont descFont(titleFont);
109 descFont.setPixelSize(descFont.pixelSize() / 2);
110 descriptionLabel->setFont(descFont);
111 descriptionLabel->setText(videoDesc);
112 bool hiddenDesc = height() < 400;
114 titleLabel->setAlignment(Qt::AlignCenter);
116 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
117 descriptionLabel->setVisible(!hiddenDesc);
119 // progressBar->hide();
120 progressBar->setValue(0);
124 void LoadingWidget::setError(const QString &message) {
125 titleLabel->setText(tr("Error"));
126 descriptionLabel->setText(message);
127 // progressBar->hide();
128 progressBar->setValue(0);
131 void LoadingWidget::bufferStatus(int percent) {
132 // qDebug() << percent;
135 if (progressBar->isHidden() && percent > 0) {
137 QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity");
138 animation->setDuration(1000);
139 animation->setStartValue(0.0);
140 animation->setEndValue(1.0);
143 // progressBar->setShown(percent > 0);
144 if (startTime.elapsed() < 1000) return;
145 if (progressBar->value() == 0 && percent > 80) return;
146 progressBar->setValue(percent);
149 void LoadingWidget::clear() {
151 descriptionLabel->clear();
152 // progressBar->hide();
153 progressBar->setValue(0);