From b371c15b5ff781615707cfe943b563e41cde0a7b Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 26 May 2018 17:05:57 +0300 Subject: [PATCH] Properly resize Audio & File widgets Remove empty space between username & timeline widgets --- include/timeline/TimelineItem.h | 4 ++-- include/timeline/widgets/AudioItem.h | 1 + include/timeline/widgets/FileItem.h | 1 + src/timeline/TimelineItem.cc | 12 ++++++++--- src/timeline/widgets/AudioItem.cc | 31 ++++++++++++++++++++-------- src/timeline/widgets/FileItem.cc | 31 ++++++++++++++++++++-------- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/include/timeline/TimelineItem.h b/include/timeline/TimelineItem.h index 87e2080e..b72c3591 100644 --- a/include/timeline/TimelineItem.h +++ b/include/timeline/TimelineItem.h @@ -232,7 +232,7 @@ TimelineItem::setupLocalWidgetLayout(Widget *widget, const QString &userid, bool generateTimestamp(timestamp); widgetLayout_ = new QHBoxLayout; - widgetLayout_->setMargin(0); + widgetLayout_->setContentsMargins(0, 2, 0, 2); widgetLayout_->addWidget(widget); widgetLayout_->addStretch(1); @@ -278,7 +278,7 @@ TimelineItem::setupWidgetLayout(Widget *widget, const Event &event, bool withSen generateTimestamp(timestamp); widgetLayout_ = new QHBoxLayout(); - widgetLayout_->setMargin(0); + widgetLayout_->setContentsMargins(0, 2, 0, 2); widgetLayout_->addWidget(widget); widgetLayout_->addStretch(1); diff --git a/include/timeline/widgets/AudioItem.h b/include/timeline/widgets/AudioItem.h index 1020c3e5..b31385d1 100644 --- a/include/timeline/widgets/AudioItem.h +++ b/include/timeline/widgets/AudioItem.h @@ -66,6 +66,7 @@ public: protected: void paintEvent(QPaintEvent *event) override; + void resizeEvent(QResizeEvent *event) override; void mousePressEvent(QMouseEvent *event) override; private: diff --git a/include/timeline/widgets/FileItem.h b/include/timeline/widgets/FileItem.h index 4f590df6..09181d32 100644 --- a/include/timeline/widgets/FileItem.h +++ b/include/timeline/widgets/FileItem.h @@ -55,6 +55,7 @@ public: protected: void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; + void resizeEvent(QResizeEvent *event) override; private: void openUrl(); diff --git a/src/timeline/TimelineItem.cc b/src/timeline/TimelineItem.cc index 166199cd..47bc20ea 100644 --- a/src/timeline/TimelineItem.cc +++ b/src/timeline/TimelineItem.cc @@ -423,6 +423,9 @@ TimelineItem::markReceived() void TimelineItem::generateBody(const QString &body) { + if (body.isEmpty()) + return; + QString content("%1"); body_ = new TextLabel(content.arg(replaceEmoji(body)), this); @@ -512,8 +515,11 @@ TimelineItem::setupAvatarLayout(const QString &userName) headerLayout_->setMargin(0); headerLayout_->setSpacing(conf::timeline::headerSpacing); - headerLayout_->addWidget(userName_); - headerLayout_->addWidget(body_); + if (userName_) + headerLayout_->addWidget(userName_); + + if (body_) + headerLayout_->addWidget(body_); } void @@ -593,7 +599,7 @@ TimelineItem::addAvatar() // Restore widget's layout. if (widget) { widgetLayout_ = new QHBoxLayout(); - widgetLayout_->setMargin(0); + widgetLayout_->setContentsMargins(0, 2, 0, 2); widgetLayout_->addWidget(widget); widgetLayout_->addStretch(1); diff --git a/src/timeline/widgets/AudioItem.cc b/src/timeline/widgets/AudioItem.cc index 5a2bca92..65ca401b 100644 --- a/src/timeline/widgets/AudioItem.cc +++ b/src/timeline/widgets/AudioItem.cc @@ -72,6 +72,8 @@ AudioItem::init() update(); } }); + + setFixedHeight(Height); } AudioItem::AudioItem(const mtx::events::RoomEvent &event, QWidget *parent) @@ -154,6 +156,22 @@ AudioItem::fileDownloaded(const QByteArray &data) } } +void +AudioItem::resizeEvent(QResizeEvent *event) +{ + QFont font; + font.setPixelSize(12); + font.setWeight(80); + + QFontMetrics fm(font); + const int computedWidth = std::min( + fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth); + + resize(computedWidth, Height); + + event->accept(); +} + void AudioItem::paintEvent(QPaintEvent *event) { @@ -162,17 +180,14 @@ AudioItem::paintEvent(QPaintEvent *event) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - QFont font("Open Sans"); + QFont font; font.setPixelSize(12); font.setWeight(80); QFontMetrics fm(font); - int computedWidth = std::min( - fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth); - QPainterPath path; - path.addRoundedRect(QRectF(0, 0, computedWidth, Height), 10, 10); + path.addRoundedRect(QRectF(0, 0, width(), height()), 10, 10); painter.setPen(Qt::NoPen); painter.fillPath(path, backgroundColor_); @@ -203,10 +218,8 @@ AudioItem::paintEvent(QPaintEvent *event) const int textStartY = VerticalPadding + fm.ascent() / 2; // Draw the filename. - QString elidedText = - fm.elidedText(text_, - Qt::ElideRight, - computedWidth - HorizontalPadding * 2 - TextPadding - 2 * IconRadius); + QString elidedText = fm.elidedText( + text_, Qt::ElideRight, width() - HorizontalPadding * 2 - TextPadding - 2 * IconRadius); painter.setFont(font); painter.setPen(QPen(textColor_)); diff --git a/src/timeline/widgets/FileItem.cc b/src/timeline/widgets/FileItem.cc index 7ff60d87..f3906a04 100644 --- a/src/timeline/widgets/FileItem.cc +++ b/src/timeline/widgets/FileItem.cc @@ -58,6 +58,8 @@ FileItem::init() QString media_params = url_parts[1]; url_ = QString("%1/_matrix/media/r0/download/%2") .arg(http::client()->getHomeServer().toString(), media_params); + + setFixedHeight(Height); } FileItem::FileItem(const mtx::events::RoomEvent &event, QWidget *parent) @@ -142,6 +144,22 @@ FileItem::fileDownloaded(const QByteArray &data) } } +void +FileItem::resizeEvent(QResizeEvent *event) +{ + QFont font; + font.setPixelSize(12); + font.setWeight(80); + + QFontMetrics fm(font); + const int computedWidth = std::min( + fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth); + + resize(computedWidth, Height); + + event->accept(); +} + void FileItem::paintEvent(QPaintEvent *event) { @@ -150,17 +168,14 @@ FileItem::paintEvent(QPaintEvent *event) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - QFont font("Open Sans"); + QFont font; font.setPixelSize(12); font.setWeight(80); QFontMetrics fm(font); - int computedWidth = std::min( - fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth); - QPainterPath path; - path.addRoundedRect(QRectF(0, 0, computedWidth, Height), 10, 10); + path.addRoundedRect(QRectF(0, 0, width(), height()), 10, 10); painter.setPen(Qt::NoPen); painter.fillPath(path, backgroundColor_); @@ -185,10 +200,8 @@ FileItem::paintEvent(QPaintEvent *event) const int textStartY = VerticalPadding + fm.ascent() / 2; // Draw the filename. - QString elidedText = - fm.elidedText(text_, - Qt::ElideRight, - computedWidth - HorizontalPadding * 2 - TextPadding - 2 * IconRadius); + QString elidedText = fm.elidedText( + text_, Qt::ElideRight, width() - HorizontalPadding * 2 - TextPadding - 2 * IconRadius); painter.setFont(font); painter.setPen(QPen(textColor_));