]> git.sur5r.net Git - minitube/blob - src/loadingwidget.cpp
Imported Upstream version 2.5.1
[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
23 LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) {
24
25     QPalette p = palette();
26     p.setBrush(backgroundRole(), Qt::black);
27     p.setBrush(QPalette::Text, Qt::white);
28     setPalette(p);
29
30     setAutoFillBackground(true);
31
32     QBoxLayout *layout = new QVBoxLayout(this);
33
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);
41
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);
51
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);
60
61     setMouseTracking(true);
62 }
63
64 void LoadingWidget::setVideo(Video *video) {
65
66     QFont titleFont;
67 #ifdef APP_MAC
68     titleFont.setFamily("Helvetica Neue");
69     titleFont.setStyleName("Thin");
70 #elif APP_WIN
71     titleFont.setFamily("Segoe UI Light");
72     titleFont.setStyleName("Light");
73 #else
74     titleFont.setStyleName("Light");
75 #endif
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);
83
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);
96
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("…");
106     }
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;
113     if (hiddenDesc)
114         titleLabel->setAlignment(Qt::AlignCenter);
115     else
116         titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
117     descriptionLabel->setVisible(!hiddenDesc);
118
119     // progressBar->hide();
120     progressBar->setValue(0);
121     startTime.start();
122 }
123
124 void LoadingWidget::setError(const QString &message) {
125     titleLabel->setText(tr("Error"));
126     descriptionLabel->setText(message);
127     // progressBar->hide();
128     progressBar->setValue(0);
129 }
130
131 void LoadingWidget::bufferStatus(int percent) {
132     // qDebug() << percent;
133
134     /*
135     if (progressBar->isHidden() && percent > 0) {
136         progressBar->show();
137         QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity");
138         animation->setDuration(1000);
139         animation->setStartValue(0.0);
140         animation->setEndValue(1.0);
141         animation->start();
142     }*/
143     // progressBar->setShown(percent > 0);
144     if (startTime.elapsed() < 1000) return;
145     if (progressBar->value() == 0 && percent > 80) return;
146     progressBar->setValue(percent);
147 }
148
149 void LoadingWidget::clear() {
150     titleLabel->clear();
151     descriptionLabel->clear();
152     // progressBar->hide();
153     progressBar->setValue(0);
154 }