]> git.sur5r.net Git - minitube/blobdiff - src/loadingwidget.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / loadingwidget.cpp
index 79efe0d3d0431477d1d84527781f54536dad9d1b..66ec79d2a1eafcb9f4a48ac1d559a8a696677ec6 100644 (file)
@@ -19,83 +19,75 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "loadingwidget.h"
+#include "fontutils.h"
 
 LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) {
-
     QPalette p = palette();
-    p.setBrush(backgroundRole(), 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);
 
     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->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+    titleLabel->setTextFormat(Qt::RichText);
+    titleLabel->setFont(FontUtils::light(titleLabel->font().pointSize()));
     layout->addWidget(titleLabel);
 
-    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->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);
 }
 
 void LoadingWidget::setVideo(Video *video) {
+    adjustFontSize();
 
-    QFont titleFont;
-#ifdef APP_MAC
-    titleFont.setFamily("Helvetica");
-#endif
-#ifdef APP_WIN
-    titleFont.setFamily("Segoe UI Light");
-#endif
-    int smallerDimension = qMin(height(), width());
-    titleFont.setPixelSize(smallerDimension / 12);
-    titleFont.setHintingPreference(QFont::PreferNoHinting);
-    QFontMetrics fm(titleFont);
-    int textHeightInPixels = fm.height();
-    int spacing = textHeightInPixels / 2;
-    layout()->setSpacing(spacing);
-    layout()->setMargin(spacing);
-
-    QString title = video->title();
+    QString title = video->getTitle();
     // enhance legibility by splitting the title
-    title = title.replace(" - ", "<p>");
-    title = title.replace("] ", "]<p>");
-    title = title.replace(" [", "<p>[");
+    static const QLatin1String p("<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("]<p>"));
+    title.replace(QLatin1String(" ["), QLatin1String("<p>["));
+    title.replace(QLatin1String(" ("), QLatin1String("<p>("));
+    title.replace(QLatin1String(") "), QLatin1String(")<p>"));
     titleLabel->setText(title);
     titleLabel->setVisible(window()->height() > 100);
-    titleLabel->setFont(titleFont);
 
-    static const int maxDescLength = 256;
-    QString videoDesc = video->description();
+    const int maxDescLength = 500;
+
+    QString videoDesc = video->getDescription();
     if (videoDesc.length() > maxDescLength) {
-        videoDesc.truncate(maxDescLength-1);
-        videoDesc.append("...");
+        videoDesc.truncate(maxDescLength);
+        videoDesc = videoDesc.trimmed();
+        videoDesc.append("…");
+    } else if (videoDesc.endsWith(QLatin1String(" ..."))) {
+        videoDesc = videoDesc.left(videoDesc.length() - 4);
+        videoDesc.append("…");
     }
-    QFont descFont(titleFont);
-    descFont.setPixelSize(descFont.pixelSize() / 2);
-    descFont.setHintingPreference(QFont::PreferNoHinting);
-    descriptionLabel->setFont(descFont);
+    static const QRegExp linkRE("(https?://\\S+)");
+    videoDesc.replace(linkRE, QStringLiteral("<a style='color:white' href=\"\\1\">\\1</a>"));
     descriptionLabel->setText(videoDesc);
     bool hiddenDesc = height() < 400;
     if (hiddenDesc)
@@ -104,39 +96,45 @@ void LoadingWidget::setVideo(Video *video) {
         titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
     descriptionLabel->setVisible(!hiddenDesc);
 
-    // progressBar->hide();
     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();
+}