| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "propertynode.h" |
| 5 | |
| 6 | #include "aggregate.h" |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | /*! |
| 11 | \class PropertyNode |
| 12 | |
| 13 | This class describes one instance of using the Q_PROPERTY macro. |
| 14 | */ |
| 15 | |
| 16 | /*! |
| 17 | The constructor sets the \a parent and the \a name, but |
| 18 | everything else is left to default values. |
| 19 | */ |
| 20 | PropertyNode::PropertyNode(Aggregate *parent, const QString &name) : Node(NodeType::Property, parent, name) |
| 21 | { |
| 22 | // nothing |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /*! |
| 27 | Returns a string representing an access function \a role. |
| 28 | */ |
| 29 | QString PropertyNode::roleName(FunctionRole role) |
| 30 | { |
| 31 | switch (role) { |
| 32 | case FunctionRole::Getter: |
| 33 | return "getter"; |
| 34 | case FunctionRole::Setter: |
| 35 | return "setter"; |
| 36 | case FunctionRole::Resetter: |
| 37 | return "resetter"; |
| 38 | case FunctionRole::Notifier: |
| 39 | return "notifier"; |
| 40 | case FunctionRole::Bindable: |
| 41 | return "bindable"; |
| 42 | default: |
| 43 | break; |
| 44 | } |
| 45 | return QString(); |
| 46 | } |
| 47 | |
| 48 | /*! |
| 49 | Sets this property's \e {overridden from} property to |
| 50 | \a baseProperty, which indicates that this property |
| 51 | overrides \a baseProperty. To begin with, all the values |
| 52 | in this property are set to the corresponding values in |
| 53 | \a baseProperty. |
| 54 | |
| 55 | We probably should ensure that the constant and final |
| 56 | attributes are not being overridden improperly. |
| 57 | */ |
| 58 | void PropertyNode::setOverriddenFrom(const PropertyNode *baseProperty) |
| 59 | { |
| 60 | for (qsizetype i{0}; i < (qsizetype)FunctionRole::NumFunctionRoles; ++i) { |
| 61 | if (m_functions[i].isEmpty()) |
| 62 | m_functions[i] = baseProperty->m_functions[i]; |
| 63 | } |
| 64 | if (m_stored == FlagValueDefault) |
| 65 | m_stored = baseProperty->m_stored; |
| 66 | if (m_writable == FlagValueDefault) |
| 67 | m_writable = baseProperty->m_writable; |
| 68 | if (m_user == FlagValueDefault) |
| 69 | m_user = baseProperty->m_user; |
| 70 | m_overrides = baseProperty; |
| 71 | } |
| 72 | |
| 73 | /*! |
| 74 | Returns a string containing the data type qualified with "const" either |
| 75 | prepended to the data type or appended to it, or without the const |
| 76 | qualification, depending circumstances in the PropertyNode internal state. |
| 77 | */ |
| 78 | QString PropertyNode::qualifiedDataType() const |
| 79 | { |
| 80 | if (m_propertyType != PropertyType::StandardProperty || m_type.startsWith(s: QLatin1String("const "))) |
| 81 | return m_type; |
| 82 | |
| 83 | if (setters().isEmpty() && resetters().isEmpty()) { |
| 84 | if (m_type.contains(c: QLatin1Char('*')) || m_type.contains(c: QLatin1Char('&'))) { |
| 85 | // 'QWidget *' becomes 'QWidget *' const |
| 86 | return m_type + " const"; |
| 87 | } else { |
| 88 | /* |
| 89 | 'int' becomes 'const int' ('int const' is |
| 90 | correct C++, but looks wrong) |
| 91 | */ |
| 92 | return "const "+ m_type; |
| 93 | } |
| 94 | } else { |
| 95 | return m_type; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /*! |
| 100 | Returns true if this property has an access function named \a name. |
| 101 | */ |
| 102 | bool PropertyNode::hasAccessFunction(const QString &name) const |
| 103 | { |
| 104 | for (const auto &getter : getters()) { |
| 105 | if (getter->name() == name) |
| 106 | return true; |
| 107 | } |
| 108 | for (const auto &setter : setters()) { |
| 109 | if (setter->name() == name) |
| 110 | return true; |
| 111 | } |
| 112 | for (const auto &resetter : resetters()) { |
| 113 | if (resetter->name() == name) |
| 114 | return true; |
| 115 | } |
| 116 | for (const auto ¬ifier : notifiers()) { |
| 117 | if (notifier->name() == name) |
| 118 | return true; |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | /*! |
| 124 | Returns the role of \a functionNode for this property. |
| 125 | */ |
| 126 | PropertyNode::FunctionRole PropertyNode::role(const FunctionNode *functionNode) const |
| 127 | { |
| 128 | for (qsizetype i{0}; i < (qsizetype)FunctionRole::NumFunctionRoles; i++) { |
| 129 | if (m_functions[i].contains(t: const_cast<FunctionNode *>(functionNode))) |
| 130 | return (FunctionRole)i; |
| 131 | } |
| 132 | return FunctionRole::Notifier; // TODO: Figure out a better way to handle 'not found'. |
| 133 | } |
| 134 | |
| 135 | QT_END_NAMESPACE |
| 136 |
