ImageItem high DPI support.
This commit is contained in:
parent
58593015c1
commit
6d30e9d682
|
@ -1,8 +1,10 @@
|
||||||
#include "imageitem.h"
|
#include "imageitem.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
#include <QBitmap>
|
#include <QBitmap>
|
||||||
#include <QGraphicsOpacityEffect>
|
#include <QGraphicsOpacityEffect>
|
||||||
#include <QRect>
|
#include <QRect>
|
||||||
|
#include <QScreen>
|
||||||
|
|
||||||
ImageItem::ImageItem(QQuickItem *parent) : QQuickPaintedItem(parent) {}
|
ImageItem::ImageItem(QQuickItem *parent) : QQuickPaintedItem(parent) {}
|
||||||
|
|
||||||
|
@ -16,19 +18,22 @@ inline static QString stringtoColor(QString string) {
|
||||||
return colour;
|
return colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static QImage getImageFromPaintable(QPointer<Paintable> p, QRectF b) {
|
inline static QImage getImageFromPaintable(QPointer<Paintable> p, int width,
|
||||||
|
int height) {
|
||||||
if (p.isNull()) return {};
|
if (p.isNull()) return {};
|
||||||
QImage image(p->image(int(b.width()), int(b.height())));
|
qreal dpi = QApplication::primaryScreen()->devicePixelRatio();
|
||||||
|
QImage image(p->image(width * dpi, height * dpi));
|
||||||
if (image.isNull()) return {};
|
if (image.isNull()) return {};
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImageItem::paint(QPainter *painter) {
|
void ImageItem::paint(QPainter *painter) {
|
||||||
QRectF bounding_rect = boundingRect();
|
QRectF bounding_rect(boundingRect());
|
||||||
|
|
||||||
painter->setRenderHint(QPainter::Antialiasing, true);
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
||||||
|
|
||||||
QImage image(getImageFromPaintable(m_paintable, bounding_rect));
|
QImage image(getImageFromPaintable(m_paintable, bounding_rect.width(),
|
||||||
|
bounding_rect.height()));
|
||||||
|
|
||||||
if (image.isNull()) {
|
if (image.isNull()) {
|
||||||
painter->setPen(Qt::NoPen);
|
painter->setPen(Qt::NoPen);
|
||||||
|
@ -37,11 +42,9 @@ void ImageItem::paint(QPainter *painter) {
|
||||||
else
|
else
|
||||||
painter->setBrush(QColor(m_color));
|
painter->setBrush(QColor(m_color));
|
||||||
if (m_round)
|
if (m_round)
|
||||||
painter->drawEllipse(0, 0, int(bounding_rect.width()),
|
painter->drawEllipse(bounding_rect);
|
||||||
int(bounding_rect.height()));
|
|
||||||
else
|
else
|
||||||
painter->drawRect(0, 0, int(bounding_rect.width()),
|
painter->drawRect(bounding_rect);
|
||||||
int(bounding_rect.height()));
|
|
||||||
painter->setPen(QPen(Qt::white, 2));
|
painter->setPen(QPen(Qt::white, 2));
|
||||||
QFont font;
|
QFont font;
|
||||||
font.setStyleHint(QFont::SansSerif);
|
font.setStyleHint(QFont::SansSerif);
|
||||||
|
@ -49,25 +52,15 @@ void ImageItem::paint(QPainter *painter) {
|
||||||
font.setPixelSize(int(bounding_rect.width() / 2));
|
font.setPixelSize(int(bounding_rect.width() / 2));
|
||||||
font.setBold(true);
|
font.setBold(true);
|
||||||
painter->setFont(font);
|
painter->setFont(font);
|
||||||
painter->drawText(
|
painter->drawText(bounding_rect, Qt::AlignCenter, m_hint.at(0).toUpper());
|
||||||
QRect(0, 0, int(bounding_rect.width()), int(bounding_rect.height())),
|
|
||||||
Qt::AlignCenter, m_hint.at(0).toUpper());
|
|
||||||
} else {
|
} else {
|
||||||
QImage scaled = image.scaled(
|
|
||||||
int(bounding_rect.width()) + 1, int(bounding_rect.height()) + 1,
|
|
||||||
Qt::KeepAspectRatioByExpanding, Qt::FastTransformation);
|
|
||||||
|
|
||||||
QPointF center = bounding_rect.center() - scaled.rect().center();
|
|
||||||
|
|
||||||
if (m_round) {
|
if (m_round) {
|
||||||
QPainterPath clip;
|
QPainterPath clip;
|
||||||
clip.addEllipse(
|
clip.addEllipse(bounding_rect); // this is the shape we want to clip to
|
||||||
0, 0, bounding_rect.width(),
|
|
||||||
bounding_rect.height()); // this is the shape we want to clip to
|
|
||||||
painter->setClipPath(clip);
|
painter->setClipPath(clip);
|
||||||
}
|
}
|
||||||
|
|
||||||
painter->drawImage(center, scaled);
|
painter->drawImage(bounding_rect, image);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue