mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-15 03:28:49 -07:00
Fix wrong position and scaling of svg pixmaps when zoomed out (#5563)
* fix scaling for player icon profile pic * fix scaling for other svgs
This commit is contained in:
parent
0503fe589c
commit
218ed726b6
2 changed files with 27 additions and 15 deletions
|
|
@ -19,11 +19,12 @@
|
||||||
* Loads in an svg from file and scales it without affecting image quality.
|
* Loads in an svg from file and scales it without affecting image quality.
|
||||||
*
|
*
|
||||||
* @param svgPath The path to the svg file. Automatically appends ".svg" to the end if not present
|
* @param svgPath The path to the svg file. Automatically appends ".svg" to the end if not present
|
||||||
* @param size The desired size of the pixmap
|
* @param size The desired size of the pixmap.
|
||||||
|
* @param expandOnly If true, then keep the size of the initial pixmap to at least the svg size.
|
||||||
*
|
*
|
||||||
* @return The svg loaded into a Pixmap with the given size, or an empty Pixmap if the loading failed.
|
* @return The svg loaded into a Pixmap with the given size, or an empty Pixmap if the loading failed.
|
||||||
*/
|
*/
|
||||||
static QPixmap loadSvg(const QString &svgPath, const QSize &size)
|
static QPixmap loadSvg(const QString &svgPath, const QSize &size, bool expandOnly = false)
|
||||||
{
|
{
|
||||||
QString path = svgPath;
|
QString path = svgPath;
|
||||||
if (!path.endsWith(".svg")) {
|
if (!path.endsWith(".svg")) {
|
||||||
|
|
@ -37,16 +38,21 @@ static QPixmap loadSvg(const QString &svgPath, const QSize &size)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Makes sure the pixmap is at least as large as the svg, so that we don't lose any detail.
|
// If expandOnly, make sure the pixmap is at least as large as the svg, so that we don't lose any detail.
|
||||||
// QIcon.pixmap(size) will automatically scale down the image, but it won't scale it up.
|
// QIcon.pixmap(size) will automatically scale down the image, but it won't scale it up.
|
||||||
QPixmap pix(svgRenderer.defaultSize().expandedTo(size));
|
QSize pixmapSize = expandOnly ? svgRenderer.defaultSize().expandedTo(size) : size;
|
||||||
|
QPixmap pix(pixmapSize);
|
||||||
pix.fill(Qt::transparent);
|
pix.fill(Qt::transparent);
|
||||||
|
|
||||||
QPainter pixPainter(&pix);
|
QPainter pixPainter(&pix);
|
||||||
svgRenderer.render(&pixPainter);
|
svgRenderer.render(&pixPainter);
|
||||||
|
|
||||||
// Converting the pixmap to a QIcon and back is the easiest way to scale down a svg without affecting image quality
|
// Converting the pixmap to a QIcon and back is the easiest way to scale down a svg without affecting image quality
|
||||||
return QIcon(pix).pixmap(size);
|
if (expandOnly) {
|
||||||
|
return QIcon(pix).pixmap(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pix;
|
||||||
}
|
}
|
||||||
|
|
||||||
QMap<QString, QPixmap> PhasePixmapGenerator::pmCache;
|
QMap<QString, QPixmap> PhasePixmapGenerator::pmCache;
|
||||||
|
|
@ -129,7 +135,7 @@ QPixmap CountryPixmapGenerator::generatePixmap(int height, const QString &countr
|
||||||
return pmCache.value(key);
|
return pmCache.value(key);
|
||||||
|
|
||||||
int width = height * 2;
|
int width = height * 2;
|
||||||
QPixmap pixmap = loadSvg("theme:countries/" + countryCode.toLower(), QSize(width, height));
|
QPixmap pixmap = loadSvg("theme:countries/" + countryCode.toLower(), QSize(width, height), true);
|
||||||
|
|
||||||
QPainter painter(&pixmap);
|
QPainter painter(&pixmap);
|
||||||
painter.setPen(Qt::black);
|
painter.setPen(Qt::black);
|
||||||
|
|
@ -321,7 +327,7 @@ QPixmap LockPixmapGenerator::generatePixmap(int height)
|
||||||
if (pmCache.contains(key))
|
if (pmCache.contains(key))
|
||||||
return pmCache.value(key);
|
return pmCache.value(key);
|
||||||
|
|
||||||
QPixmap pixmap = loadSvg("theme:icons/lock", QSize(height, height));
|
QPixmap pixmap = loadSvg("theme:icons/lock", QSize(height, height), true);
|
||||||
pmCache.insert(key, pixmap);
|
pmCache.insert(key, pixmap);
|
||||||
return pixmap;
|
return pixmap;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,7 @@ void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o
|
||||||
const QString cacheKey = "avatar" + QString::number(translatedSize.width()) + "_" +
|
const QString cacheKey = "avatar" + QString::number(translatedSize.width()) + "_" +
|
||||||
QString::number(info->user_level()) + "_" + QString::number(fullPixmap.cacheKey());
|
QString::number(info->user_level()) + "_" + QString::number(fullPixmap.cacheKey());
|
||||||
if (!QPixmapCache::find(cacheKey, &cachedPixmap)) {
|
if (!QPixmapCache::find(cacheKey, &cachedPixmap)) {
|
||||||
|
qDebug() << "TRACK cache miss" << cacheKey;
|
||||||
cachedPixmap = QPixmap(translatedSize.width(), translatedSize.height());
|
cachedPixmap = QPixmap(translatedSize.width(), translatedSize.height());
|
||||||
|
|
||||||
QPainter tempPainter(&cachedPixmap);
|
QPainter tempPainter(&cachedPixmap);
|
||||||
|
|
@ -101,16 +102,21 @@ void PlayerTarget::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*o
|
||||||
grad.setColorAt(0, QColor(180, 180, 180));
|
grad.setColorAt(0, QColor(180, 180, 180));
|
||||||
tempPainter.fillRect(QRectF(0, 0, translatedSize.width(), translatedSize.height()), grad);
|
tempPainter.fillRect(QRectF(0, 0, translatedSize.width(), translatedSize.height()), grad);
|
||||||
|
|
||||||
QPixmap tempPixmap;
|
if (fullPixmap.isNull()) {
|
||||||
if (fullPixmap.isNull())
|
int sideLength = translatedSize.height();
|
||||||
tempPixmap = UserLevelPixmapGenerator::generatePixmap(
|
QPixmap tempPixmap = UserLevelPixmapGenerator::generatePixmap(
|
||||||
translatedSize.height(), UserLevelFlags(info->user_level()), info->pawn_colors(), false,
|
sideLength, UserLevelFlags(info->user_level()), info->pawn_colors(), false,
|
||||||
QString::fromStdString(info->privlevel()));
|
QString::fromStdString(info->privlevel()));
|
||||||
else
|
int x = (translatedSize.width() - sideLength) / 2;
|
||||||
tempPixmap = fullPixmap.scaled(translatedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
int y = 0;
|
||||||
|
tempPainter.drawPixmap(x, y, tempPixmap);
|
||||||
|
} else {
|
||||||
|
QPixmap tempPixmap = fullPixmap.scaled(translatedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||||
|
int x = (translatedSize.width() - tempPixmap.width()) / 2;
|
||||||
|
int y = (translatedSize.height() - tempPixmap.height()) / 2;
|
||||||
|
tempPainter.drawPixmap(x, y, tempPixmap);
|
||||||
|
}
|
||||||
|
|
||||||
tempPainter.drawPixmap((translatedSize.width() - tempPixmap.width()) / 2,
|
|
||||||
(translatedSize.height() - tempPixmap.height()) / 2, tempPixmap);
|
|
||||||
QPixmapCache::insert(cacheKey, cachedPixmap);
|
QPixmapCache::insert(cacheKey, cachedPixmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue