]> git.sur5r.net Git - minitube/blob - src/loadingwidget.cpp
13e7cb6c882e68c6f175e772ef4fa67a54f127bc
[minitube] / src / loadingwidget.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "loadingwidget.h"
22 #include "fontutils.h"
23
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);
30     setPalette(p);
31     setAutoFillBackground(true);
32
33     QBoxLayout *layout = new QVBoxLayout(this);
34
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);
43
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);
52
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);
58 }
59
60 void LoadingWidget::setVideo(Video *video) {
61     adjustFontSize();
62
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);
77
78     const int maxDescLength = 400;
79
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("…");
88     }
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;
93     if (hiddenDesc)
94         titleLabel->setAlignment(Qt::AlignCenter);
95     else
96         titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
97     descriptionLabel->setVisible(!hiddenDesc);
98
99     progressBar->setValue(0);
100     startTime.start();
101 }
102
103 void LoadingWidget::setError(const QString &message) {
104     titleLabel->setText(tr("Error"));
105     descriptionLabel->setText(message);
106     progressBar->setValue(0);
107 }
108
109 void LoadingWidget::bufferStatus(int percent) {
110     if (startTime.elapsed() > 2000 && percent > progressBar->value())
111         progressBar->setValue(percent);
112 }
113
114 void LoadingWidget::adjustFontSize() {
115     QFont f = titleLabel->font();
116     int smallerDimension = qMin(height(), width());
117     f.setPixelSize(smallerDimension / 12);
118     QFontMetrics fm(f);
119     int textHeightInPixels = fm.height();
120     int spacing = textHeightInPixels / 2;
121     layout()->setSpacing(spacing);
122     layout()->setMargin(spacing);
123     titleLabel->setFont(f);
124
125     f.setPixelSize(f.pixelSize() / 2);
126     descriptionLabel->setFont(f);
127 }
128
129 void LoadingWidget::clear() {
130     titleLabel->clear();
131     descriptionLabel->clear();
132     progressBar->setValue(0);
133 }
134
135 void LoadingWidget::resizeEvent(QResizeEvent *e) {
136     Q_UNUSED(e);
137     if (isVisible()) adjustFontSize();
138 }