X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Floadingwidget.cpp;h=66ec79d2a1eafcb9f4a48ac1d559a8a696677ec6;hb=434d88418722fd7717038e44bd74271ca1d92771;hp=a1c81dd931107ae37392c493d004dad958b8fc56;hpb=c449f3a0f783b74807783ab9618d4b8232e4fae3;p=minitube diff --git a/src/loadingwidget.cpp b/src/loadingwidget.cpp index a1c81dd..66ec79d 100644 --- a/src/loadingwidget.cpp +++ b/src/loadingwidget.cpp @@ -1,100 +1,140 @@ +/* $BEGIN_LICENSE + +This file is part of Minitube. +Copyright 2009, Flavio Tordini + +Minitube is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Minitube is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Minitube. If not, see . + +$END_LICENSE */ + #include "loadingwidget.h" +#include "fontutils.h" LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) { - QPalette p = palette(); - p.setBrush(QPalette::Window, Qt::black); - p.setBrush(QPalette::Text, Qt::white); + p.setColor(QPalette::Window, Qt::black); + p.setColor(QPalette::WindowText, Qt::white); + p.setColor(QPalette::Base, Qt::black); + p.setColor(QPalette::Text, Qt::white); setPalette(p); - setAutoFillBackground(true); - QFont bigFont; - bigFont.setPointSize(bigFont.pointSize()*4); - QFontMetrics fm(bigFont); - int textHeightInPixels = fm.height(); - int spacing = textHeightInPixels / 2; - - QBoxLayout *layout = new QVBoxLayout(); - layout->setSpacing(spacing); - layout->setMargin(spacing); + QBoxLayout *layout = new QVBoxLayout(this); - titleLabel = new QLabel(this); - titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom); + titleLabel = new QLabel(); titleLabel->setPalette(p); - titleLabel->setForegroundRole(QPalette::Text); + titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom); titleLabel->setWordWrap(true); - titleLabel->setFont(bigFont); titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + titleLabel->setTextFormat(Qt::RichText); + titleLabel->setFont(FontUtils::light(titleLabel->font().pointSize())); layout->addWidget(titleLabel); - QFont biggerFont; - biggerFont.setPointSize(biggerFont.pointSize()*2); - - descriptionLabel = new QLabel(this); - descriptionLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop); + descriptionLabel = new QLabel(); descriptionLabel->setPalette(p); - descriptionLabel->setForegroundRole(QPalette::Text); + descriptionLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop); descriptionLabel->setWordWrap(true); - descriptionLabel->setFont(biggerFont); - descriptionLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + descriptionLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Expanding); + descriptionLabel->setTextFormat(Qt::RichText); + descriptionLabel->setOpenExternalLinks(true); layout->addWidget(descriptionLabel); - progressBar = new QProgressBar(this); - progressBar->setAutoFillBackground(false); - progressBar->setBackgroundRole(QPalette::Window); - progressBar->setPalette(p); - // progressBar->hide(); - progressBar->setStyleSheet("QProgressBar {max-height:3px; background:black; border:0} QProgressBar::chunk {background:white}"); + progressBar = new QProgressBar(); + progressBar->setStyleSheet("QProgressBar {max-height:3px; background:black; border:0} " + "QProgressBar::chunk {background:white}"); progressBar->setTextVisible(false); layout->addWidget(progressBar); - - setMouseTracking(true); - - setLayout(layout); } void LoadingWidget::setVideo(Video *video) { - QString title = video->title(); + adjustFontSize(); + + QString title = video->getTitle(); // enhance legibility by splitting the title - title = title.replace(" - ", "

"); - title = title.replace("] ", "]

"); - title = title.replace(" [", "

["); + static const QLatin1String p("

"); + title.replace(QLatin1String(" - "), p); + title.replace(QLatin1String(" | "), p); + title.replace(QLatin1String(" — "), p); + title.replace(QLatin1String(": "), p); + title.replace(QLatin1String("; "), p); + title.replace(QLatin1String("] "), QLatin1String("]

")); + title.replace(QLatin1String(" ["), QLatin1String("

[")); + title.replace(QLatin1String(" ("), QLatin1String("

(")); + title.replace(QLatin1String(") "), QLatin1String(")

")); titleLabel->setText(title); - descriptionLabel->setText(video->description()); - // progressBar->hide(); + titleLabel->setVisible(window()->height() > 100); + + const int maxDescLength = 500; + + QString videoDesc = video->getDescription(); + if (videoDesc.length() > maxDescLength) { + videoDesc.truncate(maxDescLength); + videoDesc = videoDesc.trimmed(); + videoDesc.append("…"); + } else if (videoDesc.endsWith(QLatin1String(" ..."))) { + videoDesc = videoDesc.left(videoDesc.length() - 4); + videoDesc.append("…"); + } + static const QRegExp linkRE("(https?://\\S+)"); + videoDesc.replace(linkRE, QStringLiteral("\\1")); + descriptionLabel->setText(videoDesc); + bool hiddenDesc = height() < 400; + if (hiddenDesc) + titleLabel->setAlignment(Qt::AlignCenter); + else + titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom); + descriptionLabel->setVisible(!hiddenDesc); + progressBar->setValue(0); startTime.start(); } -void LoadingWidget::setError(QString message) { +void LoadingWidget::setError(const QString &message) { titleLabel->setText(tr("Error")); descriptionLabel->setText(message); - // progressBar->hide(); progressBar->setValue(0); } -void LoadingWidget::bufferStatus(int percent) { - // qDebug() << percent; - - /* - if (progressBar->isHidden() && percent > 0) { - progressBar->show(); - QPropertyAnimation *animation = new QPropertyAnimation(progressBar, "opacity"); - animation->setDuration(1000); - animation->setStartValue(0.0); - animation->setEndValue(1.0); - animation->start(); - }*/ - // progressBar->setShown(percent > 0); - if (startTime.elapsed() < 1000) return; - if (progressBar->value() == 0 && percent > 80) return; - progressBar->setValue(percent); +void LoadingWidget::bufferStatus(qreal value) { + int percent = value * 100.; + if (startTime.elapsed() > 1000 && percent > progressBar->value()) + progressBar->setValue(percent); +} + +void LoadingWidget::adjustFontSize() { + QFont f = titleLabel->font(); + int smallerDimension = qMin(height(), width()); + f.setPixelSize(smallerDimension / 12); + QFontMetrics fm(f); + int textHeightInPixels = fm.height(); + int spacing = textHeightInPixels / 2; + layout()->setSpacing(spacing); + layout()->setMargin(spacing); + titleLabel->setFont(f); + + QFont descFont = descriptionLabel->font(); + descFont.setPixelSize(f.pixelSize() / 2); + descriptionLabel->setFont(descFont); } void LoadingWidget::clear() { titleLabel->clear(); descriptionLabel->clear(); - // progressBar->hide(); progressBar->setValue(0); } + +void LoadingWidget::resizeEvent(QResizeEvent *e) { + Q_UNUSED(e); + if (isVisible()) adjustFontSize(); +}