File indexing completed on 2024-04-28 03:56:15

0001 /*
0002  *   SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef KROLENAMES_H
0008 #define KROLENAMES_H
0009 
0010 #include <QObject>
0011 #include <qqml.h>
0012 
0013 #include <memory>
0014 
0015 class QAbstractItemModel;
0016 class KRoleNamesPrivate;
0017 
0018 /**
0019  * @class KRoleNames
0020  *
0021  * @brief A mapper between roles and role names of an attachee model.
0022  *
0023  * KRoleNames exposes runtime-invokable methods to map from roles to role names
0024  * and vice-versa. It can be used to retrieve data from a model in an imperative
0025  * fashion when enum with roles is not available at runtime (i.e. not exported
0026  * via Q_ENUM macro) but role names are known; or just to maintain consistency
0027  * with view delegates (which use role names as properties).
0028  *
0029  * @since 6.0
0030  */
0031 class KRoleNames : public QObject
0032 {
0033     Q_OBJECT
0034 public:
0035     explicit KRoleNames(QObject *parent = nullptr);
0036     ~KRoleNames() override;
0037 
0038     /**
0039      * Maps role number to role name.
0040      *
0041      * Returns an empty string if role is not found in attachee model's
0042      * roleNames() hash map.
0043      *
0044      * @since 6.0
0045      */
0046     Q_INVOKABLE QByteArray roleName(int role) const;
0047 
0048     /**
0049      * Maps role name to role number.
0050      *
0051      * Returns -1 if role name is not found in attachee model's
0052      * roleNames() hash map.
0053      *
0054      * @since 6.0
0055      */
0056     Q_INVOKABLE int role(const QByteArray &roleName) const;
0057 
0058     static KRoleNames *qmlAttachedProperties(QObject *object);
0059 
0060 private:
0061     std::unique_ptr<KRoleNamesPrivate> const d;
0062 };
0063 
0064 QML_DECLARE_TYPEINFO(KRoleNames, QML_HAS_ATTACHED_PROPERTIES)
0065 
0066 #endif