KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
kanoopcommon.h
1/**
2 * KanoopCommonQt
3 *
4 * A Qt port of the KanoopCommon C# library
5 *
6 * Lots of useful classes to provide helper functions to Qt
7 *
8 * Stephen Punak, July 04 2019
9 */
10#ifndef KANOOPCOMMON_H
11#define KANOOPCOMMON_H
12#include <QtCore/qglobal.h>
13#include <QMap>
14#include <QString>
15
16#if defined(KANOOP_LIBRARY)
17# define KANOOP_EXPORT Q_DECL_EXPORT
18#else
19# define KANOOP_EXPORT Q_DECL_IMPORT
20#endif
21
22/**
23 * @brief Core KanoopCommonQt namespace containing shared enumerations and templates.
24 */
25namespace KANOOP {
26
27/**
28 * @brief Model roles extending Qt::UserRole for use in AbstractItemModel subclasses.
29 */
31{
32 DataRole = Qt::UserRole + 0x1000, ///< Primary data payload role
33 EntityTypeRole, ///< Role carrying an entity type integer
34 UUidRole, ///< Role carrying a QUuid
35 EntityMetadataRole, ///< Role carrying an EntityMetadata object
36 MetadataTypeRole, ///< Role carrying a metadata type integer
37 NameRole, ///< Role carrying a display name string
38
39 UserRole = DataRole + 0x8000, ///< First role available for application-specific use
40};
41
42/**
43 * @brief A QMap subclass providing bidirectional enum-to-string lookups.
44 *
45 * @tparam T Enumeration type used as the map key
46 */
47template <class T>
48class EnumToStringMap : public QMap<T, QString>
49{
50public:
51 /**
52 * @brief Look up the string name for an enum value.
53 * @param type Enum value to look up
54 * @param defaultValue Value to return if not found
55 * @return Registered string name, or defaultValue
56 */
57 QString getString(T type, const QString& defaultValue = QString()) const
58 {
59 auto it = this->find(type);
60 return it != this->end()
61 ? it.value()
62 : defaultValue;
63 }
64
65 /**
66 * @brief Look up the enum value for a string name, with case sensitivity control.
67 * @param name String to look up
68 * @param caseSensitivity Qt::CaseSensitive or Qt::CaseInsensitive
69 * @param defaultValue Value returned when no match is found
70 * @return Matching enum value, or defaultValue
71 */
72 T getType(const QString& name, Qt::CaseSensitivity caseSensitivity, const T defaultValue = T()) const
73 {
74 bool ok;
75 int enumValue = name.toInt(&ok);
76 if(ok) {
77 return (T)enumValue;
78 }
79
81 QString value = it.value();
82 if(QString::compare(value, name, caseSensitivity) == 0) {
83 return it.key();
84 }
85 }
86 return defaultValue;
87 }
88
89 /**
90 * @brief Look up the enum value for a string name using case-sensitive comparison.
91 * @param name String to look up
92 * @param defaultValue Value returned when no match is found
93 * @return Matching enum value, or defaultValue
94 */
95 T getType(const QString& name, const T defaultValue = T()) const
96 {
97 return getType(name, Qt::CaseSensitive, defaultValue);
98 }
99
100 /**
101 * @brief Return all enum values registered in this map.
102 * @return List of all registered enum values
103 */
104 QList<T> getTypes() const
105 {
106 QList<T> result = EnumToStringMap<T>::keys();
107 return result;
108 }
109
110 /**
111 * @brief Test whether the map contains a given string value.
112 * @param name String to search for
113 * @param cs Case sensitivity flag (default Qt::CaseInsensitive)
114 * @return true if name matches any registered string value
115 */
116 bool containsString(const QString& name, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const
117 {
118 typename EnumToStringMap<T>::const_iterator it = std::find_if(EnumToStringMap<T>::constBegin(), EnumToStringMap<T>::constEnd(), [name, cs](const QString& a)
119 {
120 return a.compare(name, cs) == 0;
121 });
122 if(it != EnumToStringMap<T>::constEnd()) {
123 return true;
124 }
125 return false;
126 }
127
128};
129
130} // namespace
131
132#endif // KANOOPCOMMON_H
A QMap subclass providing bidirectional enum-to-string lookups.
T getType(const QString &name, Qt::CaseSensitivity caseSensitivity, const T defaultValue=T()) const
Look up the enum value for a string name, with case sensitivity control.
QString getString(T type, const QString &defaultValue=QString()) const
Look up the string name for an enum value.
QList< T > getTypes() const
Return all enum values registered in this map.
T getType(const QString &name, const T defaultValue=T()) const
Look up the enum value for a string name using case-sensitive comparison.
bool containsString(const QString &name, Qt::CaseSensitivity cs=Qt::CaseInsensitive) const
Test whether the map contains a given string value.
Core KanoopCommonQt namespace containing shared enumerations and templates.
ModelRole
Model roles extending Qt::UserRole for use in AbstractItemModel subclasses.
@ DataRole
Primary data payload role.
@ MetadataTypeRole
Role carrying a metadata type integer.
@ NameRole
Role carrying a display name string.
@ UUidRole
Role carrying a QUuid.
@ EntityTypeRole
Role carrying an entity type integer.
@ EntityMetadataRole
Role carrying an EntityMetadata object.
@ UserRole
First role available for application-specific use.