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"
22 #include "fontutils.h"
24 LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) {
25 QPalette p = palette();
26 p.setColor(QPalette::Window, Qt::black);
27 p.setColor(QPalette::WindowText, Qt::white);
28 p.setColor(QPalette::Base, Qt::black);
29 p.setColor(QPalette::Text, Qt::white);
31 setAutoFillBackground(true);
33 QBoxLayout *layout = new QVBoxLayout(this);
35 titleLabel = new QLabel();
36 titleLabel->setPalette(p);
37 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
38 titleLabel->setWordWrap(true);
39 titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
40 titleLabel->setTextFormat(Qt::RichText);
41 titleLabel->setFont(FontUtils::light(titleLabel->font().pointSize()));
42 layout->addWidget(titleLabel);
44 descriptionLabel = new QLabel();
45 descriptionLabel->setPalette(p);
46 descriptionLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
47 descriptionLabel->setWordWrap(true);
48 descriptionLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Expanding);
49 descriptionLabel->setTextFormat(Qt::RichText);
50 descriptionLabel->setOpenExternalLinks(true);
51 layout->addWidget(descriptionLabel);
53 progressBar = new QProgressBar();
54 progressBar->setStyleSheet("QProgressBar {max-height:3px; background:black; border:0} "
55 "QProgressBar::chunk {background:white}");
56 progressBar->setTextVisible(false);
57 layout->addWidget(progressBar);
60 void LoadingWidget::setVideo(Video *video) {
63 QString title = video->getTitle();
64 // enhance legibility by splitting the title
65 static const QLatin1String p("<p>");
66 title.replace(QLatin1String(" - "), p);
67 title.replace(QLatin1String(" | "), p);
68 title.replace(QLatin1String(" — "), p);
69 title.replace(QLatin1String(": "), p);
70 title.replace(QLatin1String("; "), p);
71 title.replace(QLatin1String("] "), QLatin1String("]<p>"));
72 title.replace(QLatin1String(" ["), QLatin1String("<p>["));
73 title.replace(QLatin1String(" ("), QLatin1String("<p>("));
74 title.replace(QLatin1String(") "), QLatin1String(")<p>"));
75 titleLabel->setText(title);
76 titleLabel->setVisible(window()->height() > 100);
78 const int maxDescLength = 500;
80 QString videoDesc = video->getDescription();
81 if (videoDesc.length() > maxDescLength) {
82 videoDesc.truncate(maxDescLength);
83 videoDesc = videoDesc.trimmed();
84 videoDesc.append("…");
85 } else if (videoDesc.endsWith(QLatin1String(" ..."))) {
86 videoDesc = videoDesc.left(videoDesc.length() - 4);
87 videoDesc.append("…");
89 static const QRegExp linkRE("(https?://\\S+)");
90 videoDesc.replace(linkRE, QStringLiteral("<a style='color:white' href=\"\\1\">\\1</a>"));
91 descriptionLabel->setText(videoDesc);
92 bool hiddenDesc = height() < 400;
94 titleLabel->setAlignment(Qt::AlignCenter);
96 titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
97 descriptionLabel->setVisible(!hiddenDesc);
99 progressBar->setValue(0);
103 void LoadingWidget::setError(const QString &message) {
104 titleLabel->setText(tr("Error"));
105 descriptionLabel->setText(message);
106 progressBar->setValue(0);
109 void LoadingWidget::bufferStatus(qreal value) {
110 int percent = value * 100.;
111 if (startTime.elapsed() > 1000 && percent > progressBar->value())
112 progressBar->setValue(percent);
115 void LoadingWidget::adjustFontSize() {
116 QFont f = titleLabel->font();
117 int smallerDimension = qMin(height(), width());
118 f.setPixelSize(smallerDimension / 12);
120 int textHeightInPixels = fm.height();
121 int spacing = textHeightInPixels / 2;
122 layout()->setSpacing(spacing);
123 layout()->setMargin(spacing);
124 titleLabel->setFont(f);
126 QFont descFont = descriptionLabel->font();
127 descFont.setPixelSize(f.pixelSize() / 2);
128 descriptionLabel->setFont(descFont);
131 void LoadingWidget::clear() {
133 descriptionLabel->clear();
134 progressBar->setValue(0);
137 void LoadingWidget::resizeEvent(QResizeEvent *e) {
139 if (isVisible()) adjustFontSize();