]> git.sur5r.net Git - minitube/blob - src/loadingwidget.cpp
Imported Upstream version 2.1.5
[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     layout->addWidget(descriptionLabel);
49
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);
58
59     setMouseTracking(true);
60 }
61
62 void LoadingWidget::setVideo(Video *video) {
63
64     QFont titleFont;
65     titleFont.setStyleName("Light");
66 #ifdef APP_MAC
67     titleFont.setFamily("Helvetica Neue");
68 #endif
69 #ifdef APP_WIN
70     titleFont.setFamily("Segoe UI Light");
71 #endif
72     int smallerDimension = qMin(height(), width());
73     titleFont.setPixelSize(smallerDimension / 12);
74     titleFont.setHintingPreference(QFont::PreferNoHinting);
75     QFontMetrics fm(titleFont);
76     int textHeightInPixels = fm.height();
77     int spacing = textHeightInPixels / 2;
78     layout()->setSpacing(spacing);
79     layout()->setMargin(spacing);
80
81     QString title = video->title();
82     // enhance legibility by splitting the title
83     title = title.replace(" - ", "<p>");
84     title = title.replace("] ", "]<p>");
85     title = title.replace(" [", "<p>[");
86     titleLabel->setText(title);
87     titleLabel->setVisible(window()->height() > 100);
88     titleLabel->setFont(titleFont);
89
90     static const int maxDescLength = 256;
91     QString videoDesc = video->description();
92     if (videoDesc.length() > maxDescLength) {
93         videoDesc.truncate(maxDescLength-1);
94         videoDesc.append("...");
95     }
96     QFont descFont(titleFont);
97     descFont.setPixelSize(descFont.pixelSize() / 2);
98     descFont.setHintingPreference(QFont::PreferNoHinting);
99     descriptionLabel->setFont(descFont);
100     descriptionLabel->setText(videoDesc);
101     bool hiddenDesc = height() < 400;
102     if (hiddenDesc)
103         titleLabel->setAlignment(Qt::AlignCenter);
104     else
105         titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
106     descriptionLabel->setVisible(!hiddenDesc);
107
108     // progressBar->hide();
109     progressBar->setValue(0);
110     startTime.start();
111 }
112
113 void LoadingWidget::setError(QString message) {
114     titleLabel->setText(tr("Error"));
115     descriptionLabel->setText(message);
116     // progressBar->hide();
117     progressBar->setValue(0);
118 }
119
120 void LoadingWidget::bufferStatus(int percent) {
121     // qDebug() << percent;
122
123     /*
124     if (progressBar->isHidden() && percent > 0) {
125         progressBar->show();
126         QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity");
127         animation->setDuration(1000);
128         animation->setStartValue(0.0);
129         animation->setEndValue(1.0);
130         animation->start();
131     }*/
132     // progressBar->setShown(percent > 0);
133     if (startTime.elapsed() < 1000) return;
134     if (progressBar->value() == 0 && percent > 80) return;
135     progressBar->setValue(percent);
136 }
137
138 void LoadingWidget::clear() {
139     titleLabel->clear();
140     descriptionLabel->clear();
141     // progressBar->hide();
142     progressBar->setValue(0);
143 }