Apply scaling in renderSvgImage() similar to QIcon::pixmap() does

This commit is contained in:
Martchus 2020-07-12 02:38:57 +02:00
parent e10eca68f9
commit 5b2147a874
6 changed files with 15 additions and 12 deletions

View File

@ -104,7 +104,7 @@ void SyncthingFileItemActionStaticData::showAboutDialog()
"icons from <a href=\"https://fontawesome.com\">Font "
"Awesome</a> (see <a href=\"https://fontawesome.com/license\">their license</a>)"),
QStringLiteral(APP_VERSION), CppUtilities::applicationInfo.dependencyVersions, QStringLiteral(APP_URL), QStringLiteral(APP_DESCRIPTION),
renderSvgImage(makeSyncthingIcon()).toImage());
renderSvgImage(makeSyncthingIcon(), QSize(128, 128)).toImage());
aboutDialog->setWindowTitle(tr("About") + QStringLiteral(" - " APP_NAME));
aboutDialog->setWindowIcon(QIcon::fromTheme(QStringLiteral("syncthingtray")));
aboutDialog->setAttribute(Qt::WA_DeleteOnClose);

View File

@ -1,6 +1,6 @@
#include "./syncthingicons.h"
#include <QCoreApplication>
#include <QGuiApplication>
#include <QFile>
#include <QPainter>
#include <QStringBuilder>
@ -113,12 +113,14 @@ QByteArray makeSyncthingIcon(const StatusIconColorSet &colors, StatusEmblem stat
/// \cond
namespace Detail {
template <typename SourceType> QPixmap renderSvgImage(const SourceType &source, const QSize &size, int margin)
template <typename SourceType> QPixmap renderSvgImage(const SourceType &source, const QSize &givenSize, int margin)
{
const qreal scaleFactor = QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? qGuiApp->devicePixelRatio() : 1.0;
QSvgRenderer renderer(source);
QSize scaledSize(givenSize.width() * scaleFactor, givenSize.height() * scaleFactor);
QSize renderSize(renderer.defaultSize());
renderSize.scale(size.width() - margin, size.height() - margin, Qt::KeepAspectRatio);
QRect renderBounds(QPoint(), size);
renderSize.scale(scaledSize.width() - margin, scaledSize.height() - margin, Qt::KeepAspectRatio);
QRect renderBounds(QPoint(), scaledSize);
if (renderSize.width() < renderBounds.width()) {
const auto diff = (renderBounds.width() - renderSize.width()) / 2;
renderBounds.setX(diff);
@ -129,10 +131,11 @@ template <typename SourceType> QPixmap renderSvgImage(const SourceType &source,
renderBounds.setY(diff);
renderBounds.setHeight(renderSize.height());
}
QPixmap pm(size);
QPixmap pm(scaledSize);
pm.fill(QColor(Qt::transparent));
QPainter painter(&pm);
renderer.render(&painter, renderBounds);
pm.setDevicePixelRatio(scaleFactor);
return pm;
}
} // namespace Detail

View File

@ -56,8 +56,8 @@ inline StatusIconColorSet::StatusIconColorSet(const QString &backgroundStart, co
LIB_SYNCTHING_MODEL_EXPORT QByteArray makeSyncthingIcon(
const StatusIconColorSet &colors = StatusIconColorSet{ QStringLiteral("#26B6DB"), QStringLiteral("#0882C8"), QStringLiteral("#FFFFFF") },
StatusEmblem statusEmblem = StatusEmblem::None);
LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QString &path, const QSize &size = QSize(128, 128), int margin = 0);
LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QByteArray &contents, const QSize &size = QSize(128, 128), int margin = 0);
LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QString &path, const QSize &size = QSize(32, 32), int margin = 0);
LIB_SYNCTHING_MODEL_EXPORT QPixmap renderSvgImage(const QByteArray &contents, const QSize &size = QSize(32, 32), int margin = 0);
LIB_SYNCTHING_MODEL_EXPORT QByteArray loadFontAwesomeIcon(const QString &iconName, const QColor &color, bool solid = true);
struct LIB_SYNCTHING_MODEL_EXPORT StatusIconSettings {

View File

@ -326,7 +326,7 @@ void SyncthingApplet::showAboutDialog()
"icons from <a href=\"https://fontawesome.com\">Font "
"Awesome</a> (see <a href=\"https://fontawesome.com/license\">their license</a>)</p>"),
QStringLiteral(APP_VERSION), CppUtilities::applicationInfo.dependencyVersions, QStringLiteral(APP_URL), QStringLiteral(APP_DESCRIPTION),
renderSvgImage(makeSyncthingIcon()).toImage());
renderSvgImage(makeSyncthingIcon(), QSize(128, 128)).toImage());
m_aboutDlg->setWindowTitle(tr("About") + QStringLiteral(" - " APP_NAME));
m_aboutDlg->setWindowIcon(QIcon::fromTheme(QStringLiteral("syncthingtray")));
m_aboutDlg->setAttribute(Qt::WA_DeleteOnClose);

View File

@ -240,7 +240,7 @@ void TrayWidget::showAboutDialog()
"<br>Fallback icons from KDE/Breeze project<br>Syncthing icons from <a href=\"https://syncthing.net\">Syncthing project</a><br>Using "
"icons from <a href=\"https://fontawesome.com\">Font "
"Awesome</a> (see <a href=\"https://fontawesome.com/license\">their license</a>)</p>"),
QString(), {}, QStringLiteral(APP_URL), QString(), QImage(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
QString(), {}, QStringLiteral(APP_URL), QString(), renderSvgImage(makeSyncthingIcon(), QSize(128, 128)).toImage());
s_aboutDlg->setWindowTitle(tr("About") + QStringLiteral(" - " APP_NAME));
s_aboutDlg->setWindowIcon(QIcon(QStringLiteral(":/icons/hicolor/scalable/app/syncthingtray.svg")));
}

View File

@ -564,6 +564,7 @@ QWidget *IconsOptionPage::setupWidget()
&colorMapping.setting,
colorMapping.defaultEmblem,
};
widgetsForColor.previewLabel->setMaximumSize(QSize(32, 32));
// add label for color name
gridLayout->addWidget(new QLabel(colorMapping.colorName, statusIconsGroupBox), index, 0, Qt::AlignRight | Qt::AlignVCenter);
@ -577,8 +578,7 @@ QWidget *IconsOptionPage::setupWidget()
widgetsForColor.colorButtons[1]->color(),
widgetsForColor.colorButtons[2]->color(),
},
widgetsForColor.statusEmblem),
QSize(32, 32)));
widgetsForColor.statusEmblem), widgetsForColor.previewLabel->maximumSize()));
};
for (const auto &colorButton : widgetsForColor.colorButtons) {
QObject::connect(colorButton, &ColorButton::colorChanged, updatePreview);