Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions src/private/dquickdciiconimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

#include <DIconTheme>
#include <DGuiApplicationHelper>
#include <DPlatformTheme>

Check warning on line 9 in src/private/dquickdciiconimage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DPlatformTheme> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QUrlQuery>

Check warning on line 11 in src/private/dquickdciiconimage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QUrlQuery> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QQuickWindow>

Check warning on line 12 in src/private/dquickdciiconimage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QQuickWindow> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QPoint>

Check warning on line 13 in src/private/dquickdciiconimage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPoint> not found. Please note: Cppcheck does not need standard library headers to get proper results.

DQUICK_BEGIN_NAMESPACE
DGUI_USE_NAMESPACE
Expand Down Expand Up @@ -189,8 +191,43 @@

void DQuickDciIconImagePrivate::layout()
{
auto dd = QQuickItemPrivate::get(imageItem);
dd->anchors()->setCenterIn(imageItem->parentItem());
Q_Q(DQuickDciIconImage);
if (!q->isComponentComplete())
return;

qreal targetX = (q->width() - imageItem->width()) / 2.0;
qreal targetY = (q->height() - imageItem->height()) / 2.0;

if (!q->parentItem() || !q->window()) {
imageItem->setPosition(QPointF(targetX, targetY));
return;
}

QPointF scenePos = q->mapToScene(QPointF(targetX, targetY));

qreal dpr = q->window()->effectiveDevicePixelRatio();

qreal physicalX = qRound(scenePos.x() * dpr);
qreal physicalY = qRound(scenePos.y() * dpr);

QPointF localPos = q->mapFromScene(QPointF(physicalX / dpr, physicalY / dpr));

imageItem->setPosition(localPos);
}

void DQuickDciIconImagePrivate::scheduleLayout()
{
Q_Q(DQuickDciIconImage);

if (!layoutTimer) {
layoutTimer = new QTimer(q);
layoutTimer->setSingleShot(true);
layoutTimer->setInterval(50);
QObject::connect(layoutTimer, &QTimer::timeout, q, [this]() {
layout();
});
}
layoutTimer->start();
}

void DQuickDciIconImagePrivate::updateImageSourceUrl()
Expand All @@ -211,6 +248,9 @@
connect(d->imageItem, &QQuickImage::implicitWidthChanged, this, [this, d]() { setImplicitWidth(d->imageItem->implicitWidth()); });
connect(d->imageItem, &QQuickImage::implicitHeightChanged, this, [this, d]() { setImplicitHeight(d->imageItem->implicitHeight()); });
connect(this, &DQuickDciIconImage::smoothChanged, d->imageItem, &QQuickImage::setSmooth);
connect(d->imageItem, &QQuickItem::widthChanged, this, [d]() { d->scheduleLayout(); });
connect(d->imageItem, &QQuickItem::heightChanged, this, [d]() { d->scheduleLayout(); });
connect(this, &QQuickItem::scaleChanged, this, [this]() {setSmooth(!qFuzzyCompare(scale(), 1.0)); });
}

DQuickDciIconImage::~DQuickDciIconImage()
Expand Down Expand Up @@ -406,17 +446,39 @@
D_D(DQuickDciIconImage);
QQmlEngine::setContextForObject(d->imageItem, QQmlEngine::contextForObject(this));
QQuickItem::classBegin();
d->imageItem->setSmooth(smooth());
}

void DQuickDciIconImage::componentComplete()
{
D_D(DQuickDciIconImage);
d->imageItem->componentComplete();
QQuickItem::componentComplete();
setSmooth(!qFuzzyCompare(scale(), 1.0));
d->layout();
}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void DQuickDciIconImage::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickItem::geometryChanged(newGeometry, oldGeometry);
d_func()->scheduleLayout();
}
#else
void DQuickDciIconImage::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickItem::geometryChange(newGeometry, oldGeometry);
d_func()->scheduleLayout();
}
#endif

void DQuickDciIconImage::itemChange(ItemChange change, const ItemChangeData &value)
{
QQuickItem::itemChange(change, value);
if (change == ItemParentHasChanged || change == ItemDevicePixelRatioHasChanged || change == ItemSceneChange) {
d_func()->scheduleLayout();
}
}

class DQuickIconAttachedPrivate : public DCORE_NAMESPACE::DObjectPrivate
{
D_DECLARE_PUBLIC(DQuickIconAttached)
Expand Down
6 changes: 6 additions & 0 deletions src/private/dquickdciiconimage_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ class DQuickDciIconImage : public QQuickItem, DCORE_NAMESPACE::DObject
protected:
void classBegin() override;
void componentComplete() override;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
#else
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
#endif
void itemChange(ItemChange change, const ItemChangeData &value) override;
};

class DQuickIconAttachedPrivate;
Expand Down
5 changes: 4 additions & 1 deletion src/private/dquickdciiconimage_p_p.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

Expand All @@ -10,9 +10,10 @@
#include "dquickdciiconimage_p.h"
#include "dquickiconimage_p_p.h"

#include <dobject_p.h>

Check warning on line 13 in src/private/dquickdciiconimage_p_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dobject_p.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DDciIconPalette>

Check warning on line 14 in src/private/dquickdciiconimage_p_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DDciIconPalette> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DDciIconPlayer>

Check warning on line 15 in src/private/dquickdciiconimage_p_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DDciIconPlayer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QTimer>

Check warning on line 16 in src/private/dquickdciiconimage_p_p.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.

DQUICK_BEGIN_NAMESPACE
class DQuickDciIconImageItemPrivate;
Expand Down Expand Up @@ -41,11 +42,13 @@
public:
DQuickDciIconImagePrivate(DQuickDciIconImage *qq);
void layout();
void scheduleLayout();
void updateImageSourceUrl();
void play(DQMLGlobalObject::ControlState mode);

DDciIconPalette palette;
DQuickIconImage *imageItem;
QTimer *layoutTimer = nullptr;
DQMLGlobalObject::ControlState mode = DQMLGlobalObject::NormalState;
DGuiApplicationHelper::ColorType theme = DGuiApplicationHelper::ColorType::LightType;
bool fallbackToQIcon = true;
Expand Down
Loading