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 * @class KRoleNames
20 *
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 * Maps role number to role name.
44 *
45 * Returns an empty string if role is not found in attachee model's
46 * roleNames() hash map.
47 *
48 * @since 6.0
49 */
50 Q_INVOKABLE QByteArray roleName(int role) const;
51
52 /**
53 * Maps role name to role number.
54 *
55 * Returns -1 if role name is not found in attachee model's
56 * roleNames() hash map.
57 *
58 * @since 6.0
59 */
60 Q_INVOKABLE int role(const QByteArray &roleName) const;
61
62 static KRoleNames *qmlAttachedProperties(QObject *object);
63
64private:
65 std::unique_ptr<KRoleNamesPrivate> const d;
66};
67
68QML_DECLARE_TYPEINFO(KRoleNames, QML_HAS_ATTACHED_PROPERTIES)
69
70#endif
71

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