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) {
50 const QString state(source.at(1));
51 int finalState = KIconLoader::DefaultState;
52
53 if (state == QLatin1String("active")) {
54 finalState = KIconLoader::ActiveState;
55 } else if (state == QLatin1String("disabled")) {
56 finalState = KIconLoader::DisabledState;
57 } else if (state == QLatin1String("last")) {
58 finalState = KIconLoader::LastState;
59 }
60
61 // apply the effect for state
62 if (finalState == KIconLoader::ActiveState) {
64 }
65
66 if (finalState == KIconLoader::DisabledState) {
68 }
69 }
70
71 if (!pixmap.isNull() && size) {
72 *size = pixmap.size();
73 }
74
75 return pixmap;
76 }
77};
78
79#endif
static void toDisabled(QImage &image)
Applies a disabled effect.
static void toActive(QImage &image)
Applies an effect for an icon that is in an 'active' state.
@ 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.
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 Nov 22 2024 12:05:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.