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