Fix support for Qt versions < 5.11

This commit is contained in:
Joseph Donofry 2019-07-04 22:58:06 -04:00
parent 2484e0c118
commit 4c0d4f35fe
No known key found for this signature in database
GPG key ID: E8A1D78EF044B0CB
8 changed files with 56 additions and 12 deletions

View file

@ -19,6 +19,7 @@
#include <QDebug>
#include <QMouseEvent>
#include <QPainter>
#include <QtGlobal>
#include "Cache.h"
#include "Config.h"
@ -182,9 +183,12 @@ RoomInfoListItem::paintEvent(QPaintEvent *event)
QFont tsFont;
tsFont.setPointSizeF(tsFont.pointSizeF() * 0.9);
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
const int msgStampWidth = QFontMetrics(tsFont).width(lastMsgInfo_.timestamp) + 4;
#else
const int msgStampWidth =
QFontMetrics(tsFont).horizontalAdvance(lastMsgInfo_.timestamp) + 4;
#endif
// We use the full width of the widget if there is no unread msg bubble.
const int bottomLineWidthLimit = (unreadMsgCount_ > 0) ? msgStampWidth : 0;
@ -212,8 +216,11 @@ RoomInfoListItem::paintEvent(QPaintEvent *event)
p.setFont(QFont{});
p.drawText(QPoint(2 * wm.padding + wm.iconSize, bottom_y), userName);
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
int nameWidth = QFontMetrics(QFont{}).width(userName);
#else
int nameWidth = QFontMetrics(QFont{}).horizontalAdvance(userName);
#endif
p.setFont(QFont{});
// The limit is the space between the end of the username and the start of

View file

@ -2,6 +2,7 @@
#include <QPainter>
#include <QPoint>
#include <QShowEvent>
#include <QtGlobal>
#include "Config.h"
#include "TypingDisplay.h"
@ -69,9 +70,12 @@ TypingDisplay::paintEvent(QPaintEvent *)
text_ = fm.elidedText(text_, Qt::ElideRight, (double)(width() * 0.75));
QPainterPath path;
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
path.addRoundedRect(QRectF(0, 0, fm.width(text_) + 2 * LEFT_PADDING, height()), 3, 3);
#else
path.addRoundedRect(
QRectF(0, 0, fm.horizontalAdvance(text_) + 2 * LEFT_PADDING, height()), 3, 3);
#endif
p.fillPath(path, backgroundColor());
p.drawText(region, Qt::AlignVCenter, text_);
}

View file

@ -21,6 +21,7 @@
#include <QFontDatabase>
#include <QMenu>
#include <QTimer>
#include <QtGlobal>
#include "ChatPage.h"
#include "Config.h"
@ -731,11 +732,13 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam
userName_->setToolTipDuration(1500);
userName_->setAttribute(Qt::WA_Hover);
userName_->setAlignment(Qt::AlignLeft | Qt::AlignTop);
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
// width deprecated in 5.13:
// userName_->setFixedWidth(QFontMetrics(userName_->font()).width(userName_->text()));
userName_->setFixedWidth(QFontMetrics(userName_->font()).width(userName_->text()));
#else
userName_->setFixedWidth(
QFontMetrics(userName_->font()).horizontalAdvance(userName_->text()));
#endif
// Set the user color asynchronously if it hasn't been generated yet,
// otherwise this will just set it.
refreshAuthorColor();

View file

@ -21,6 +21,7 @@
#include <QFileDialog>
#include <QPainter>
#include <QPixmap>
#include <QtGlobal>
#include "Logging.h"
#include "MatrixClient.h"
@ -162,10 +163,14 @@ AudioItem::resizeEvent(QResizeEvent *event)
font.setWeight(QFont::Medium);
QFontMetrics fm(font);
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
const int computedWidth = std::min(
fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth);
#else
const int computedWidth =
std::min(fm.horizontalAdvance(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding,
(double)MaxWidth);
#endif
resize(computedWidth, Height);
event->accept();

View file

@ -21,6 +21,7 @@
#include <QFileDialog>
#include <QPainter>
#include <QPixmap>
#include <QtGlobal>
#include "Logging.h"
#include "MatrixClient.h"
@ -153,10 +154,14 @@ FileItem::resizeEvent(QResizeEvent *event)
font.setWeight(QFont::Medium);
QFontMetrics fm(font);
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
const int computedWidth = std::min(
fm.width(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding, (double)MaxWidth);
#else
const int computedWidth =
std::min(fm.horizontalAdvance(text_) + 2 * IconRadius + VerticalPadding * 2 + TextPadding,
(double)MaxWidth);
#endif
resize(computedWidth, Height);
event->accept();

View file

@ -22,6 +22,7 @@
#include <QPainter>
#include <QPixmap>
#include <QUuid>
#include <QtGlobal>
#include "Config.h"
#include "ImageItem.h"
@ -191,9 +192,11 @@ ImageItem::paintEvent(QPaintEvent *event)
if (image_.isNull()) {
QString elidedText = metrics.elidedText(text_, Qt::ElideRight, max_width_ - 10);
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
setFixedSize(metrics.width(elidedText), fontHeight);
#else
setFixedSize(metrics.horizontalAdvance(elidedText), fontHeight);
#endif
painter.setFont(font);
painter.setPen(QPen(QColor(66, 133, 244)));
painter.drawText(QPoint(0, fontHeight / 2), elidedText);

View file

@ -4,6 +4,7 @@
#include <QDateTime>
#include <QPainter>
#include <QPen>
#include <QtGlobal>
constexpr int VPadding = 6;
constexpr int HPadding = 12;
@ -22,7 +23,13 @@ InfoMessage::InfoMessage(QString msg, QWidget *parent)
initFont();
QFontMetrics fm{font()};
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
// width deprecated in 5.13
width_ = fm.width(msg_) + HPadding * 2;
#else
width_ = fm.horizontalAdvance(msg_) + HPadding * 2;
#endif
height_ = fm.ascent() + 2 * VPadding;
setFixedHeight(height_ + 2 * HMargin);
@ -64,7 +71,12 @@ DateSeparator::DateSeparator(QDateTime datetime, QWidget *parent)
msg_ = datetime.toString(fmt);
QFontMetrics fm{font()};
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
// width deprecated in 5.13
width_ = fm.width(msg_) + HPadding * 2;
#else
width_ = fm.horizontalAdvance(msg_) + HPadding * 2;
#endif
height_ = fm.ascent() + 2 * VPadding;
setFixedHeight(height_ + 2 * HMargin);

View file

@ -3,6 +3,7 @@
#include <QFontMetrics>
#include <QPaintDevice>
#include <QPainter>
#include <QtGlobal>
class Painter : public QPainter
{
@ -21,8 +22,12 @@ public:
{
QFontMetrics m(fontMetrics());
if (textWidth < 0) {
// deprecated in 5.13: textWidth = m.width(text);
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
// deprecated in 5.13:
textWidth = m.width(text);
#else
textWidth = m.horizontalAdvance(text);
#endif
}
drawText((outerw - x - textWidth), y + m.ascent(), text);
}