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);
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);

View file

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

View file

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

View file

@ -423,6 +423,9 @@ TimelineItem::markReceived()
void
TimelineItem::generateBody(const QString &body)
{
if (body.isEmpty())
return;
QString content("<span>%1</span>");
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);

View file

@ -72,6 +72,8 @@ AudioItem::init()
update();
}
});
setFixedHeight(Height);
}
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
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_));

View file

@ -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<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
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_));