1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/*
* SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
* SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
* SPDX-FileCopyrightText: 2024 Martchus <martchus@gmx.net>
*
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "./quickicon.h"
#include "./scenegraph/managedtexturenode.h"
#include <QBitmap>
#include <QGuiApplication>
#include <QIcon>
#include <QPainter>
#include <QQuickImageProvider>
#include <QQuickWindow>
#include <QSGSimpleTextureNode>
#include <QSGTexture>
#include <QScreen>
#include <cstdlib>
namespace QtGui {
Q_GLOBAL_STATIC(ImageTexturesCache, s_iconImageCache)
Icon::Icon(QQuickItem *parent)
: QQuickItem(parent)
, m_active(false)
, m_selected(false)
{
setFlag(ItemHasContents, true);
// Using 32 because Icon used to redefine implicitWidth and implicitHeight and hardcode them to 32
setImplicitSize(32, 32);
connect(this, &QQuickItem::smoothChanged, this, &QQuickItem::polish);
connect(this, &QQuickItem::enabledChanged, this, &QQuickItem::polish);
}
Icon::~Icon()
{
}
void Icon::componentComplete()
{
QQuickItem::componentComplete();
QQmlEngine *engine = qmlEngine(this);
Q_ASSERT(engine);
updatePaintedGeometry();
}
void Icon::setSource(const QVariant &icon)
{
if (m_source == icon) {
return;
}
m_source = icon;
polish();
Q_EMIT sourceChanged();
}
QVariant Icon::source() const
{
return m_source;
}
void Icon::setActive(const bool active)
{
if (active == m_active) {
return;
}
m_active = active;
polish();
Q_EMIT activeChanged();
}
bool Icon::active() const
{
return m_active;
}
void Icon::setSelected(const bool selected)
{
if (selected == m_selected) {
return;
}
m_selected = selected;
polish();
Q_EMIT selectedChanged();
}
bool Icon::selected() const
{
return m_selected;
}
QSGNode *Icon::createSubtree(qreal initialOpacity)
{
auto opacityNode = new QSGOpacityNode{};
opacityNode->setFlag(QSGNode::OwnedByParent, true);
opacityNode->setOpacity(initialOpacity);
auto *mNode = new ManagedTextureNode;
mNode->setTexture(s_iconImageCache->loadTexture(window(), m_icon, QQuickWindow::TextureCanUseAtlas));
opacityNode->appendChildNode(mNode);
return opacityNode;
}
void Icon::updateSubtree(QSGNode *node, qreal opacity)
{
auto opacityNode = static_cast<QSGOpacityNode *>(node);
opacityNode->setOpacity(opacity);
auto textureNode = static_cast<ManagedTextureNode *>(opacityNode->firstChild());
textureNode->setFiltering(smooth() ? QSGTexture::Linear : QSGTexture::Nearest);
}
QSGNode *Icon::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData * /*data*/)
{
if (m_source.isNull() || qFuzzyIsNull(width()) || qFuzzyIsNull(height())) {
delete node;
return nullptr;
}
if (!node) {
node = new QSGNode{};
}
if (node->childCount() == 0) {
node->appendChildNode(createSubtree(1.0));
m_textureChanged = true;
}
if (node->childCount() > 1) {
auto toRemove = node->firstChild();
node->removeChildNode(toRemove);
delete toRemove;
}
updateSubtree(node->firstChild(), 1.0);
if (m_textureChanged) {
auto mNode = static_cast<ManagedTextureNode *>(node->lastChild()->firstChild());
mNode->setTexture(s_iconImageCache->loadTexture(window(), m_icon, QQuickWindow::TextureCanUseAtlas));
m_textureChanged = false;
m_sizeChanged = true;
}
if (m_sizeChanged) {
const QSizeF iconPixSize(m_icon.width() / m_devicePixelRatio, m_icon.height() / m_devicePixelRatio);
const QSizeF itemPixSize = QSizeF((size() * m_devicePixelRatio).toSize()) / m_devicePixelRatio;
QRectF nodeRect(QPoint(0, 0), itemPixSize);
if (itemPixSize.width() != 0 && itemPixSize.height() != 0) {
if (iconPixSize != itemPixSize) {
// At this point, the image will already be scaled, but we need to output it in
// the correct aspect ratio, painted centered in the viewport. So:
QRectF destination(QPointF(0, 0), QSizeF(m_icon.size()).scaled(m_paintedSize, Qt::KeepAspectRatio));
destination.moveCenter(nodeRect.center());
destination.moveTopLeft(QPointF(destination.topLeft().toPoint() * m_devicePixelRatio) / m_devicePixelRatio);
nodeRect = destination;
}
}
// Adjust the final node on the pixel grid
const auto globalPixelPos = mapToScene(nodeRect.topLeft()) * m_devicePixelRatio;
const auto posAdjust = QPointF(globalPixelPos.x() - std::round(globalPixelPos.x()), globalPixelPos.y() - std::round(globalPixelPos.y()));
nodeRect.moveTopLeft(nodeRect.topLeft() - posAdjust);
for (int i = 0; i < node->childCount(); ++i) {
auto mNode = static_cast<ManagedTextureNode *>(node->childAtIndex(i)->firstChild());
mNode->setRect(nodeRect);
}
m_sizeChanged = false;
}
return node;
}
void Icon::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickItem::geometryChange(newGeometry, oldGeometry);
if (newGeometry.size() != oldGeometry.size()) {
m_sizeChanged = true;
updatePaintedGeometry();
polish();
}
}
void Icon::updatePolish()
{
QQuickItem::updatePolish();
if (const auto *const w = window()) {
m_devicePixelRatio = w->effectiveDevicePixelRatio();
}
if (m_source.isNull()) {
updatePaintedGeometry();
update();
return;
}
if (const auto itemSize = QSizeF(width(), height()); !itemSize.isNull()) {
const auto size = itemSize.toSize();
switch (m_source.userType()) {
case QMetaType::QPixmap:
m_icon = m_source.value<QPixmap>().toImage();
break;
case QMetaType::QImage:
m_icon = m_source.value<QImage>();
break;
case QMetaType::QBitmap:
m_icon = m_source.value<QBitmap>().toImage();
break;
case QMetaType::QIcon:
m_icon = iconPixmap(m_source.value<QIcon>());
break;
case QMetaType::QColor:
m_icon = QImage(size, QImage::Format_Alpha8);
m_icon.fill(m_source.value<QColor>());
break;
default:
break;
}
if (m_icon.isNull()) {
m_icon = QImage(size, QImage::Format_RGB32);
m_icon.fill(Qt::red);
}
}
m_textureChanged = true;
updatePaintedGeometry();
update();
}
QIcon::Mode Icon::iconMode() const
{
if (!isEnabled()) {
return QIcon::Disabled;
} else if (m_selected) {
return QIcon::Selected;
} else if (m_active) {
return QIcon::Active;
}
return QIcon::Normal;
}
qreal Icon::paintedWidth() const
{
return std::round(m_paintedSize.width());
}
qreal Icon::paintedHeight() const
{
return std::round(m_paintedSize.height());
}
QSize Icon::iconSizeHint() const
{
const auto s = static_cast<int>(std::min(width(), height()));
return QSize(s, s);
}
QImage Icon::iconPixmap(const QIcon &icon) const
{
const QSize actualSize = icon.actualSize(iconSizeHint());
return icon.pixmap(actualSize, m_devicePixelRatio, iconMode(), QIcon::On).toImage();
}
void Icon::updatePaintedGeometry()
{
auto newSize = QSizeF();
if (!m_icon.width() || !m_icon.height()) {
newSize = {0, 0};
} else {
const qreal roundedWidth = std::round(32 * m_devicePixelRatio) / m_devicePixelRatio;
if (const auto roundedSize = QSizeF(roundedWidth, roundedWidth); size() == roundedSize) {
m_paintedSize = roundedSize;
m_textureChanged = true;
update();
Q_EMIT paintedAreaChanged();
return;
}
const QSizeF iconPixSize(m_icon.width() / m_devicePixelRatio, m_icon.height() / m_devicePixelRatio);
const qreal w = widthValid() ? width() : iconPixSize.width();
const qreal widthScale = w / iconPixSize.width();
const qreal h = heightValid() ? height() : iconPixSize.height();
const qreal heightScale = h / iconPixSize.height();
if (widthScale <= heightScale) {
newSize = QSizeF(w, widthScale * iconPixSize.height());
} else if (heightScale < widthScale) {
newSize = QSizeF(heightScale * iconPixSize.width(), h);
}
}
if (newSize != m_paintedSize) {
m_paintedSize = newSize;
m_textureChanged = true;
update();
Q_EMIT paintedAreaChanged();
}
}
void Icon::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value)
{
if (change == QQuickItem::ItemDevicePixelRatioHasChanged) {
if (const auto *const w = window()) {
m_devicePixelRatio = w->effectiveDevicePixelRatio();
}
polish();
} else if (change == QQuickItem::ItemSceneChange && value.window) {
m_devicePixelRatio = value.window->effectiveDevicePixelRatio();
}
QQuickItem::itemChange(change, value);
}
}
#include "moc_quickicon.cpp"
|