1/*
2 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#ifndef KROLENAMES_H
8#define KROLENAMES_H
9
10#include <QObject>
11#include <qqml.h>
12
13#include <memory>
14
15class QAbstractItemModel;
16class KRoleNamesPrivate;
17
18/*!
19 * \qmltype KRoleNames
20 * \inqmlmodule org.kde.kitemmodels
21 * \brief A mapper between roles and role names of an attachee model.
22 *
23 * KRoleNames exposes runtime-invokable methods to map from roles to role names
24 * and vice-versa. It can be used to retrieve data from a model in an imperative
25 * fashion when enum with roles is not available at runtime (i.e. not exported
26 * via Q_ENUM macro) but role names are known; or just to maintain consistency
27 * with view delegates (which use role names as properties).
28 *
29 * \since 6.0
30 */
31class KRoleNames : public QObject
32{
33 Q_OBJECT
34 QML_ELEMENT
35 QML_UNCREATABLE("KRoleNames can only be used as an attached property")
36 QML_ATTACHED(KRoleNames)
37 QML_ADDED_IN_MINOR_VERSION(1)
38public:
39 explicit KRoleNames(QObject *parent = nullptr);
40 ~KRoleNames() override;
41
42 /*!
43 * \qmlattachedmethod KRoleNames::roleName(int role)
44 *
45 * Maps role number to role name.
46 *
47 * Returns an empty string if role is not found in attachee model's
48 * roleNames() hash map.
49 *
50 * \since 6.0
51 */
52 Q_INVOKABLE QByteArray roleName(int role) const;
53
54 /*!
55 * \qmlattachedmethod int KRoleNames::role(var roleName)
56 *
57 * Maps role name to role number.
58 *
59 * Returns -1 if role name is not found in attachee model's
60 * roleNames() hash map.
61 *
62 * \since 6.0
63 */
64 Q_INVOKABLE int role(const QByteArray &roleName) const;
65
66 static KRoleNames *qmlAttachedProperties(QObject *object);
67
68private:
69 std::unique_ptr<KRoleNamesPrivate> const d;
70};
71
72QML_DECLARE_TYPEINFO(KRoleNames, QML_HAS_ATTACHED_PROPERTIES)
73
74#endif
75

source code of kitemmodels/src/qml/krolenames.h