[PageViewMessage] Resolve icon on the fly

Use `PM_SmallIconSize` for size calculation exclusively and
then have `QPainter` resolve it to the correct device pixel
ratio during paint.
remotes/origin/work/aacid/mobile_latest_qt
Kai Uwe Broulik 4 years ago
parent 29911062e3
commit 8eca2bfa9d
  1. 34
      part/pageviewutils.cpp
  2. 4
      part/pageviewutils.h

@ -225,24 +225,23 @@ void PageViewMessage::display(const QString &message, const QString &details, Ic
m_lineSpacing = 0; m_lineSpacing = 0;
// load icon (if set) // load icon (if set)
m_symbol = QPixmap(); m_symbol = QIcon();
const auto symbolSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
if (icon != None) { if (icon != None) {
switch (icon) { switch (icon) {
case Annotation: case Annotation:
m_symbol = QIcon::fromTheme(QStringLiteral("draw-freehand")).pixmap(symbolSize); m_symbol = QIcon::fromTheme(QStringLiteral("draw-freehand"));
break; break;
case Find: case Find:
m_symbol = QIcon::fromTheme(QStringLiteral("zoom-original")).pixmap(symbolSize); m_symbol = QIcon::fromTheme(QStringLiteral("zoom-original"));
break; break;
case Error: case Error:
m_symbol = QIcon::fromTheme(QStringLiteral("dialog-error")).pixmap(symbolSize); m_symbol = QIcon::fromTheme(QStringLiteral("dialog-error"));
break; break;
case Warning: case Warning:
m_symbol = QIcon::fromTheme(QStringLiteral("dialog-warning")).pixmap(symbolSize); m_symbol = QIcon::fromTheme(QStringLiteral("dialog-warning"));
break; break;
default: default:
m_symbol = QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(symbolSize); m_symbol = QIcon::fromTheme(QStringLiteral("dialog-information"));
break; break;
} }
} }
@ -285,13 +284,15 @@ QRect PageViewMessage::computeTextRect(const QString &message, int extra_width)
void PageViewMessage::computeSizeAndResize() void PageViewMessage::computeSizeAndResize()
{ {
const auto symbolSize = !m_symbol.isNull() ? style()->pixelMetric(QStyle::PM_SmallIconSize) : 0;
// determine text rectangle // determine text rectangle
const QRect textRect = computeTextRect(m_message, m_symbol.width()); const QRect textRect = computeTextRect(m_message, symbolSize);
int width = textRect.width(), height = textRect.height(); int width = textRect.width(), height = textRect.height();
if (!m_details.isEmpty()) { if (!m_details.isEmpty()) {
// determine details text rectangle // determine details text rectangle
const QRect detailsRect = computeTextRect(m_details, m_symbol.width()); const QRect detailsRect = computeTextRect(m_details, symbolSize);
width = qMax(width, detailsRect.width()); width = qMax(width, detailsRect.width());
height += detailsRect.height(); height += detailsRect.height();
@ -302,8 +303,8 @@ void PageViewMessage::computeSizeAndResize()
// update geometry with icon information // update geometry with icon information
if (!m_symbol.isNull()) { if (!m_symbol.isNull()) {
width += 2 + m_symbol.width(); width += 2 + symbolSize;
height = qMax(height, m_symbol.height()); height = qMax(height, symbolSize);
} }
// resize widget // resize widget
@ -331,21 +332,22 @@ bool PageViewMessage::eventFilter(QObject *obj, QEvent *event)
void PageViewMessage::paintEvent(QPaintEvent * /* e */) void PageViewMessage::paintEvent(QPaintEvent * /* e */)
{ {
const QRect textRect = computeTextRect(m_message, m_symbol.width()); const auto symbolSize = !m_symbol.isNull() ? style()->pixelMetric(QStyle::PM_SmallIconSize) : 0;
const QRect textRect = computeTextRect(m_message, symbolSize);
QRect detailsRect; QRect detailsRect;
if (!m_details.isEmpty()) { if (!m_details.isEmpty()) {
detailsRect = computeTextRect(m_details, m_symbol.width()); detailsRect = computeTextRect(m_details, symbolSize);
} }
int textXOffset = 0, int textXOffset = 0,
// add 2 to account for the reduced drawRoundedRect later // add 2 to account for the reduced drawRoundedRect later
textYOffset = (geometry().height() - textRect.height() - detailsRect.height() - m_lineSpacing + 2) / 2, iconXOffset = 0, iconYOffset = !m_symbol.isNull() ? (geometry().height() - m_symbol.height()) / 2 : 0, shadowOffset = 1; textYOffset = (geometry().height() - textRect.height() - detailsRect.height() - m_lineSpacing + 2) / 2, iconXOffset = 0, iconYOffset = !m_symbol.isNull() ? (geometry().height() - symbolSize) / 2 : 0, shadowOffset = 1;
if (layoutDirection() == Qt::RightToLeft) if (layoutDirection() == Qt::RightToLeft)
iconXOffset = 2 + textRect.width(); iconXOffset = 2 + textRect.width();
else else
textXOffset = 2 + m_symbol.width(); textXOffset = 2 + symbolSize;
// draw background // draw background
QPainter painter(this); QPainter painter(this);
@ -357,7 +359,7 @@ void PageViewMessage::paintEvent(QPaintEvent * /* e */)
// draw icon if present // draw icon if present
if (!m_symbol.isNull()) if (!m_symbol.isNull())
painter.drawPixmap(5 + iconXOffset, iconYOffset, m_symbol, 0, 0, m_symbol.width(), m_symbol.height()); painter.drawPixmap(5 + iconXOffset, iconYOffset, m_symbol.pixmap(symbolSize));
const int xStartPoint = 5 + textXOffset; const int xStartPoint = 5 + textXOffset;
const int yStartPoint = textYOffset; const int yStartPoint = textYOffset;

@ -11,7 +11,7 @@
#define _PAGEVIEW_UTILS_H_ #define _PAGEVIEW_UTILS_H_
#include <QHash> #include <QHash>
#include <QPixmap> #include <QIcon>
#include <QRect> #include <QRect>
#include <qwidget.h> #include <qwidget.h>
@ -118,7 +118,7 @@ private:
void computeSizeAndResize(); void computeSizeAndResize();
QString m_message; QString m_message;
QString m_details; QString m_details;
QPixmap m_symbol; QIcon m_symbol;
QTimer *m_timer; QTimer *m_timer;
int m_lineSpacing; int m_lineSpacing;
}; };

Loading…
Cancel
Save