| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquicknativeiconloader_p.h" |
| 5 | |
| 6 | #include <QtCore/qobject.h> |
| 7 | #include <QtCore/qmetaobject.h> |
| 8 | #include <QtQml/qqml.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | QQuickNativeIconLoader::QQuickNativeIconLoader(int slot, QObject *parent) |
| 13 | : m_parent(parent), |
| 14 | m_slot(slot), |
| 15 | m_enabled(false) |
| 16 | { |
| 17 | Q_ASSERT(slot != -1 && parent); |
| 18 | } |
| 19 | |
| 20 | bool QQuickNativeIconLoader::isEnabled() const |
| 21 | { |
| 22 | return m_enabled; |
| 23 | } |
| 24 | |
| 25 | void QQuickNativeIconLoader::setEnabled(bool enabled) |
| 26 | { |
| 27 | m_enabled = enabled; |
| 28 | if (m_enabled) |
| 29 | loadIcon(); |
| 30 | } |
| 31 | |
| 32 | QIcon QQuickNativeIconLoader::toQIcon() const |
| 33 | { |
| 34 | const QIcon fallback = QPixmap::fromImage(image: image()); |
| 35 | return QIcon::fromTheme(name: m_icon.name(), fallback); |
| 36 | } |
| 37 | |
| 38 | QQuickIcon QQuickNativeIconLoader::icon() const |
| 39 | { |
| 40 | return m_icon; |
| 41 | } |
| 42 | |
| 43 | void QQuickNativeIconLoader::setIcon(const QQuickIcon &icon) |
| 44 | { |
| 45 | m_icon = icon; |
| 46 | if (m_enabled) |
| 47 | loadIcon(); |
| 48 | } |
| 49 | |
| 50 | void QQuickNativeIconLoader::loadIcon() |
| 51 | { |
| 52 | if (m_icon.source().isEmpty()) { |
| 53 | clear(m_parent); |
| 54 | } else { |
| 55 | load(qmlEngine(m_parent), m_icon.source()); |
| 56 | if (m_slot != -1 && isLoading()) { |
| 57 | connectFinished(m_parent, m_slot); |
| 58 | m_slot = -1; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (!isLoading()) |
| 63 | m_parent->metaObject()->method(index: m_slot).invoke(obj: m_parent); |
| 64 | } |
| 65 | |
| 66 | QT_END_NAMESPACE |
| 67 |
