KItemModels

krolenames.h
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
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{
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 */
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
A mapper between roles and role names of an attachee model.
Definition krolenames.h:32
Q_INVOKABLE int role(const QByteArray &roleName) const
Maps role name to role number.
Q_INVOKABLE QByteArray roleName(int role) const
Maps role number to role name.
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:07:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.