Properly resize Audio & File widgets

Remove empty space between username & timeline widgets
This commit is contained in:
Konstantinos Sideris 2018-05-26 17:05:57 +03:00
parent 90595beb16
commit b371c15b5f
6 changed files with 57 additions and 23 deletions

View file

@ -232,7 +232,7 @@ TimelineItem::setupLocalWidgetLayout(Widget *widget, const QString &userid, bool
generateTimestamp(timestamp); generateTimestamp(timestamp);
widgetLayout_ = new QHBoxLayout; widgetLayout_ = new QHBoxLayout;
widgetLayout_->setMargin(0); widgetLayout_->setContentsMargins(0, 2, 0, 2);
widgetLayout_->addWidget(widget); widgetLayout_->addWidget(widget);
widgetLayout_->addStretch(1); widgetLayout_->addStretch(1);
@ -278,7 +278,7 @@ TimelineItem::setupWidgetLayout(Widget *widget, const Event &event, bool withSen
generateTimestamp(timestamp); generateTimestamp(timestamp);
widgetLayout_ = new QHBoxLayout(); widgetLayout_ = new QHBoxLayout();
widgetLayout_->setMargin(0); widgetLayout_->setContentsMargins(0, 2, 0, 2);
widgetLayout_->addWidget(widget); widgetLayout_->addWidget(widget);
widgetLayout_->addStretch(1); widgetLayout_->addStretch(1);

View file

@ -66,6 +66,7 @@ public:
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
private: private:

View file

@ -55,6 +55,7 @@ public:
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
private: private:
void openUrl(); void openUrl();

View file

@ -423,6 +423,9 @@ TimelineItem::markReceived()
void void
TimelineItem::generateBody(const QString &body) TimelineItem::generateBody(const QString &body)
{ {
if (body.isEmpty())
return;
QString content("<span>%1</span>"); QString content("<span>%1</span>");
body_ = new TextLabel(content.arg(replaceEmoji(body)), this); body_ = new TextLabel(content.arg(replaceEmoji(body)), this);
@ -512,7 +515,10 @@ TimelineItem::setupAvatarLayout(const QString &userName)
headerLayout_->setMargin(0); headerLayout_->setMargin(0);
headerLayout_->setSpacing(conf::timeline::headerSpacing); headerLayout_->setSpacing(conf::timeline::headerSpacing);
if (userName_)
headerLayout_->addWidget(userName_); headerLayout_->addWidget(userName_);
if (body_)
headerLayout_->addWidget(body_); headerLayout_->addWidget(body_);
} }
@ -593,7 +599,7 @@ TimelineItem::addAvatar()
// Restore widget's layout. // Restore widget's layout.
if (widget) { if (widget) {
widgetLayout_ = new QHBoxLayout(); widgetLayout_ = new QHBoxLayout();
widgetLayout_->setMargin(0); widgetLayout_->setContentsMargins(0, 2, 0, 2);
widgetLayout_->addWidget(widget); widgetLayout_->addWidget(widget);
widgetLayout_->addStretch(1); widgetLayout_->addStretch(1);

View file

@ -72,6 +72,8 @@ AudioItem::init()
update(); update();
} }
}); });
setFixedHeight(Height);
} }
AudioItem::AudioItem(const mtx::events::RoomEvent<mtx::events::msg::Audio> &event, QWidget *parent) AudioItem::AudioItem(const mtx::events::RoomEvent<mtx::events::msg::Audio> &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 void
AudioItem::paintEvent(QPaintEvent *event) AudioItem::paintEvent(QPaintEvent *event)
{ {
@ -162,17 +180,14 @@ AudioItem::paintEvent(QPaintEvent *event)
QPainter painter(this); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
QFont font("Open Sans"); QFont font;
font.setPixelSize(12); font.setPixelSize(12);
font.setWeight(80); font.setWeight(80);
QFontMetrics fm(font); QFontMetrics fm(font);
int computedWidth = std::min(
fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth);
QPainterPath path; 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.setPen(Qt::NoPen);
painter.fillPath(path, backgroundColor_); painter.fillPath(path, backgroundColor_);
@ -203,10 +218,8 @@ AudioItem::paintEvent(QPaintEvent *event)
const int textStartY = VerticalPadding + fm.ascent() / 2; const int textStartY = VerticalPadding + fm.ascent() / 2;
// Draw the filename. // Draw the filename.
QString elidedText = QString elidedText = fm.elidedText(
fm.elidedText(text_, text_, Qt::ElideRight, width() - HorizontalPadding * 2 - TextPadding - 2 * IconRadius);
Qt::ElideRight,
computedWidth - HorizontalPadding * 2 - TextPadding - 2 * IconRadius);
painter.setFont(font); painter.setFont(font);
painter.setPen(QPen(textColor_)); painter.setPen(QPen(textColor_));

View file

@ -58,6 +58,8 @@ FileItem::init()
QString media_params = url_parts[1]; QString media_params = url_parts[1];
url_ = QString("%1/_matrix/media/r0/download/%2") url_ = QString("%1/_matrix/media/r0/download/%2")
.arg(http::client()->getHomeServer().toString(), media_params); .arg(http::client()->getHomeServer().toString(), media_params);
setFixedHeight(Height);
} }
FileItem::FileItem(const mtx::events::RoomEvent<mtx::events::msg::File> &event, QWidget *parent) FileItem::FileItem(const mtx::events::RoomEvent<mtx::events::msg::File> &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 void
FileItem::paintEvent(QPaintEvent *event) FileItem::paintEvent(QPaintEvent *event)
{ {
@ -150,17 +168,14 @@ FileItem::paintEvent(QPaintEvent *event)
QPainter painter(this); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
QFont font("Open Sans"); QFont font;
font.setPixelSize(12); font.setPixelSize(12);
font.setWeight(80); font.setWeight(80);
QFontMetrics fm(font); QFontMetrics fm(font);
int computedWidth = std::min(
fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth);
QPainterPath path; 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.setPen(Qt::NoPen);
painter.fillPath(path, backgroundColor_); painter.fillPath(path, backgroundColor_);
@ -185,10 +200,8 @@ FileItem::paintEvent(QPaintEvent *event)
const int textStartY = VerticalPadding + fm.ascent() / 2; const int textStartY = VerticalPadding + fm.ascent() / 2;
// Draw the filename. // Draw the filename.
QString elidedText = QString elidedText = fm.elidedText(
fm.elidedText(text_, text_, Qt::ElideRight, width() - HorizontalPadding * 2 - TextPadding - 2 * IconRadius);
Qt::ElideRight,
computedWidth - HorizontalPadding * 2 - TextPadding - 2 * IconRadius);
painter.setFont(font); painter.setFont(font);
painter.setPen(QPen(textColor_)); painter.setPen(QPen(textColor_));