File indexing completed on 2024-09-08 12:18:06
0001 /* 0002 SPDX-FileCopyrightText: 2011 Artur Duque de Souza <asouza@kde.org> 0003 SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de> 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #ifndef KQUICK_ICON_PROVIDER_H 0008 #define KQUICK_ICON_PROVIDER_H 0009 0010 #include <KIconEffect> 0011 #include <KIconLoader> 0012 #include <QIcon> 0013 #include <QPixmap> 0014 #include <QQuickImageProvider> 0015 #include <QSize> 0016 0017 /** 0018 * Class which exposes the KIcon* functioality to QML. 0019 * For dependency reasons, this is a header-only class. 0020 * 0021 * This needs to be registered in the engine using the following code: 0022 * @code 0023 * engine->addImageProvider(QStringLiteral("icon"), new KQuickIconProvider); 0024 * @endcode 0025 * @since 5.98 0026 */ 0027 class KQuickIconProvider : public QQuickImageProvider 0028 { 0029 public: 0030 KQuickIconProvider() 0031 : QQuickImageProvider(QQuickImageProvider::Pixmap) 0032 { 0033 } 0034 0035 QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override 0036 { 0037 // We need to handle QIcon::state 0038 const QStringList source = id.split(QLatin1Char('/')); 0039 0040 QPixmap pixmap; 0041 if (requestedSize.isValid()) { 0042 pixmap = QIcon::fromTheme(source.at(0)).pixmap(requestedSize); 0043 } else if (size->isValid()) { 0044 pixmap = QIcon::fromTheme(source.at(0)).pixmap(*size); 0045 } else { 0046 pixmap = QIcon::fromTheme(source.at(0)).pixmap(KIconLoader::global()->currentSize(KIconLoader::Desktop)); 0047 } 0048 0049 if (source.size() == 2) { 0050 KIconEffect *effect = KIconLoader::global()->iconEffect(); 0051 const QString state(source.at(1)); 0052 int finalState = KIconLoader::DefaultState; 0053 0054 if (state == QLatin1String("active")) { 0055 finalState = KIconLoader::ActiveState; 0056 } else if (state == QLatin1String("disabled")) { 0057 finalState = KIconLoader::DisabledState; 0058 } else if (state == QLatin1String("last")) { 0059 finalState = KIconLoader::LastState; 0060 } 0061 0062 // apply the effect for state 0063 pixmap = effect->apply(pixmap, KIconLoader::Desktop, finalState); 0064 } 0065 0066 if (!pixmap.isNull() && size) { 0067 *size = pixmap.size(); 0068 } 0069 0070 return pixmap; 0071 } 0072 }; 0073 0074 #endif