KIconThemes 5.109.0
kquickiconprovider.h
1/*
2 SPDX-FileCopyrightText: 2011 Artur Duque de Souza <asouza@kde.org>
3 SPDX-FileCopyrightText: 2022 Alexander Lohnau <alexander.lohnau@gmx.de>
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#ifndef KQUICK_ICON_PROVIDER_H
8#define KQUICK_ICON_PROVIDER_H
9
10#include <KIconEffect>
11#include <KIconLoader>
12#include <QIcon>
13#include <QPixmap>
14#include <QQuickImageProvider>
15#include <QSize>
16
27class KQuickIconProvider : public QQuickImageProvider
28{
29public:
31 : QQuickImageProvider(QQuickImageProvider::Pixmap)
32 {
33 }
34
35 QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override
36 {
37 // We need to handle QIcon::state
38 const QStringList source = id.split(QLatin1Char('/'));
39
40 QPixmap pixmap;
41 if (requestedSize.isValid()) {
42 pixmap = QIcon::fromTheme(source.at(0)).pixmap(requestedSize);
43 } else if (size->isValid()) {
44 pixmap = QIcon::fromTheme(source.at(0)).pixmap(*size);
45 } else {
46 pixmap = QIcon::fromTheme(source.at(0)).pixmap(KIconLoader::global()->currentSize(KIconLoader::Desktop));
47 }
48
49 if (source.size() == 2) {
51 const QString state(source.at(1));
52 int finalState = KIconLoader::DefaultState;
53
54 if (state == QLatin1String("active")) {
55 finalState = KIconLoader::ActiveState;
56 } else if (state == QLatin1String("disabled")) {
57 finalState = KIconLoader::DisabledState;
58 } else if (state == QLatin1String("last")) {
59 finalState = KIconLoader::LastState;
60 }
61
62 // apply the effect for state
63 pixmap = effect->apply(pixmap, KIconLoader::Desktop, finalState);
64 }
65
66 if (!pixmap.isNull() && size) {
67 *size = pixmap.size();
68 }
69
70 return pixmap;
71 }
72};
73
74#endif
Applies effects to icons.
Definition kiconeffect.h:40
QImage apply(const QImage &src, int group, int state) const
Applies an effect to an image.
@ Desktop
Desktop icons.
Definition kiconloader.h:129
static KIconLoader * global()
Returns the global icon loader initialized with the application name.
@ ActiveState
Icon is active.
Definition kiconloader.h:177
@ DisabledState
Icon is disabled.
Definition kiconloader.h:178
@ LastState
Last state (last constant)
Definition kiconloader.h:180
@ DefaultState
The default state.
Definition kiconloader.h:176
KIconEffect * iconEffect() const
Returns a pointer to the KIconEffect object used by the icon loader.
Class which exposes the KIcon* functioality to QML.
Definition kquickiconprovider.h:28