KIconThemes

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
17/**
18 * Class which exposes the KIcon* functioality to QML.
19 * For dependency reasons, this is a header-only class.
20 *
21 * This needs to be registered in the engine using the following code:
22 * @code
23 * engine->addImageProvider(QStringLiteral("icon"), new KQuickIconProvider);
24 * @endcode
25 * @since 5.98
26 */
28{
29public:
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.
@ ActiveState
Icon is active.
@ DisabledState
Icon is disabled.
@ LastState
Last state (last constant)
@ DefaultState
The default state.
static KIconLoader * global()
Returns the global icon loader initialized with the application name.
KIconEffect * iconEffect() const
Returns a pointer to the KIconEffect object used by the icon loader.
Class which exposes the KIcon* functioality to QML.
QPixmap pixmap(QWindow *window, const QSize &size, Mode mode, State state) const const
QIcon fromTheme(const QString &name)
const_reference at(qsizetype i) const const
qsizetype size() const const
bool isNull() const const
QSize size() const const
QQuickImageProvider(ImageType type, Flags flags)
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:43:58 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.