From db44c58d0746ecc9dcb1330265d27f8b76eb44c9 Mon Sep 17 00:00:00 2001 From: Flavio Date: Sun, 10 Jun 2012 16:20:36 +0200 Subject: [PATCH] Adaptive font size --- src/loadingwidget.cpp | 41 ++++++++++++++++++++++++++++++----------- src/videoareawidget.cpp | 2 +- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/loadingwidget.cpp b/src/loadingwidget.cpp index 03f5fd3..2b04b23 100644 --- a/src/loadingwidget.cpp +++ b/src/loadingwidget.cpp @@ -9,22 +9,13 @@ LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) { 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); titleLabel = new QLabel(this); titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom); titleLabel->setPalette(p); titleLabel->setForegroundRole(QPalette::Text); titleLabel->setWordWrap(true); - titleLabel->setFont(bigFont); titleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); layout->addWidget(titleLabel); @@ -36,7 +27,6 @@ LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) { descriptionLabel->setPalette(p); descriptionLabel->setForegroundRole(QPalette::Text); descriptionLabel->setWordWrap(true); - descriptionLabel->setFont(biggerFont); descriptionLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); layout->addWidget(descriptionLabel); @@ -55,13 +45,42 @@ LoadingWidget::LoadingWidget(QWidget *parent) : QWidget(parent) { } void LoadingWidget::setVideo(Video *video) { + + QFont titleFont; + int smallerDimension = qMin(height(), width()); + titleFont.setPixelSize(smallerDimension / 12); + QFontMetrics fm(titleFont); + int textHeightInPixels = fm.height(); + int spacing = textHeightInPixels / 2; + layout()->setSpacing(spacing); + layout()->setMargin(spacing); + QString title = video->title(); // enhance legibility by splitting the title title = title.replace(" - ", "

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

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

["); titleLabel->setText(title); - descriptionLabel->setText(video->description()); + titleLabel->setVisible(window()->height() > 100); + titleLabel->setFont(titleFont); + + static const int maxVideoLength = 256; + QString videoDesc = video->description(); + if (videoDesc.length() > maxVideoLength) { + videoDesc.truncate(maxVideoLength-1); + videoDesc.append("..."); + } + QFont descFont(titleFont); + descFont.setPixelSize(descFont.pixelSize() / 2); + descriptionLabel->setFont(descFont); + descriptionLabel->setText(videoDesc); + bool hiddenDesc = height() < 400; + if (hiddenDesc) + titleLabel->setAlignment(Qt::AlignCenter); + else + titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom); + descriptionLabel->setVisible(!hiddenDesc); + // progressBar->hide(); progressBar->setValue(0); startTime.start(); diff --git a/src/videoareawidget.cpp b/src/videoareawidget.cpp index a8432ac..bf2ae03 100644 --- a/src/videoareawidget.cpp +++ b/src/videoareawidget.cpp @@ -56,8 +56,8 @@ void VideoAreaWidget::showError(QString message) { } void VideoAreaWidget::showLoading(Video *video) { - this->loadingWidget->setVideo(video); stackedLayout->setCurrentWidget(loadingWidget); + this->loadingWidget->setVideo(video); messageLabel->hide(); messageLabel->clear(); } -- 2.39.5