| 1 | // Copyright (C) 2019 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
| 3 |  | 
| 4 | #include "qqmltablemodelcolumn_p.h" | 
| 5 |  | 
| 6 | #include <QtQml/qqmlinfo.h> | 
| 7 |  | 
| 8 | QT_BEGIN_NAMESPACE | 
| 9 |  | 
| 10 | /*! | 
| 11 |     \qmltype TableModelColumn | 
| 12 | //!  \nativetype QQmlTableModelColumn | 
| 13 |     \inqmlmodule Qt.labs.qmlmodels | 
| 14 |     \brief Represents a column in a model. | 
| 15 |     \since 5.14 | 
| 16 |  | 
| 17 |     \section1 Supported Roles | 
| 18 |  | 
| 19 |     TableModelColumn supports all of \l {Qt::ItemDataRole}{Qt's roles}, | 
| 20 |     with the exception of \c Qt::InitialSortOrderRole. | 
| 21 |     Roles can be accessed by as listed below, e.g. | 
| 22 |     \code | 
| 23 |     text: display | 
| 24 |  | 
| 25 |     required property string display | 
| 26 |     \endcode | 
| 27 |  | 
| 28 |     \table | 
| 29 |     \row \li Qt::DisplayRole \li display | 
| 30 |     \row \li Qt::DecorationRole \li decoration | 
| 31 |     \row \li Qt::EditRole \li edit | 
| 32 |     \row \li Qt::ToolTipRole \li toolTip | 
| 33 |     \row \li Qt::StatusTipRole \li statusTip | 
| 34 |     \row \li Qt::WhatsThisRole \li whatsThis | 
| 35 |     \row \li Qt::FontRole \li font | 
| 36 |     \row \li Qt::TextAlignmentRole \li textAlignment | 
| 37 |     \row \li Qt::BackgroundRole \li background | 
| 38 |     \row \li Qt::ForegroundRole \li foreground | 
| 39 |     \row \li Qt::CheckStateRole \li checkState | 
| 40 |     \row \li Qt::AccessibleTextRole \li accessibleText | 
| 41 |     \row \li Qt::AccessibleDescriptionRole \li accessibleDescription | 
| 42 |     \row \li Qt::SizeHintRole \li sizeHintRoleNam | 
| 43 |     \endtable | 
| 44 |  | 
| 45 |     \sa TableModel, TableView | 
| 46 | */ | 
| 47 |  | 
| 48 | static constexpr QLatin1StringView displayRoleName("display" ); | 
| 49 | static constexpr QLatin1StringView decorationRoleName("decoration" ); | 
| 50 | static constexpr QLatin1StringView editRoleName("edit" ); | 
| 51 | static constexpr QLatin1StringView toolTipRoleName("toolTip" ); | 
| 52 | static constexpr QLatin1StringView statusTipRoleName("statusTip" ); | 
| 53 | static constexpr QLatin1StringView whatsThisRoleName("whatsThis" ); | 
| 54 |  | 
| 55 | static constexpr QLatin1StringView fontRoleName("font" ); | 
| 56 | static constexpr QLatin1StringView textAlignmentRoleName("textAlignment" ); | 
| 57 | static constexpr QLatin1StringView backgroundRoleName("background" ); | 
| 58 | static constexpr QLatin1StringView foregroundRoleName("foreground" ); | 
| 59 | static constexpr QLatin1StringView checkStateRoleName("checkState" ); | 
| 60 |  | 
| 61 | static constexpr QLatin1StringView accessibleTextRoleName("accessibleText" ); | 
| 62 | static constexpr QLatin1StringView accessibleDescriptionRoleName("accessibleDescription" ); | 
| 63 |  | 
| 64 | static constexpr QLatin1StringView sizeHintRoleName("sizeHint" ); | 
| 65 |  | 
| 66 |  | 
| 67 | QQmlTableModelColumn::QQmlTableModelColumn(QObject *parent) | 
| 68 |     : QObject(parent) | 
| 69 | { | 
| 70 | } | 
| 71 |  | 
| 72 | QQmlTableModelColumn::~QQmlTableModelColumn() | 
| 73 | { | 
| 74 | } | 
| 75 |  | 
| 76 | #define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, setterGetterName, setterSetterName, setterSignal, roleName) \ | 
| 77 | QJSValue QQmlTableModelColumn::getterGetterName() const \ | 
| 78 | { \ | 
| 79 |     return mGetters.value(roleName); \ | 
| 80 | } \ | 
| 81 | \ | 
| 82 | void QQmlTableModelColumn::getterSetterName(const QJSValue &stringOrFunction) \ | 
| 83 | { \ | 
| 84 |     if (!stringOrFunction.isString() && !stringOrFunction.isCallable()) { \ | 
| 85 |         qmlWarning(this).quote() << "getter for " << roleName << " must be a function"; \ | 
| 86 |         return; \ | 
| 87 |     } \ | 
| 88 |     if (stringOrFunction.strictlyEquals(decoration())) \ | 
| 89 |         return; \ | 
| 90 | \ | 
| 91 |     mGetters[roleName] = stringOrFunction; \ | 
| 92 |     emit decorationChanged(); \ | 
| 93 | } \ | 
| 94 | \ | 
| 95 | QJSValue QQmlTableModelColumn::setterGetterName() const \ | 
| 96 | { \ | 
| 97 |     return mSetters.value(roleName); \ | 
| 98 | } \ | 
| 99 | \ | 
| 100 | void QQmlTableModelColumn::setterSetterName(const QJSValue &function) \ | 
| 101 | { \ | 
| 102 |     if (!function.isCallable()) { \ | 
| 103 |         qmlWarning(this).quote() << "setter for " << roleName << " must be a function"; \ | 
| 104 |         return; \ | 
| 105 |     } \ | 
| 106 | \ | 
| 107 |     if (function.strictlyEquals(getSetDisplay())) \ | 
| 108 |         return; \ | 
| 109 | \ | 
| 110 |     mSetters[roleName] = function; \ | 
| 111 |     emit setDisplayChanged(); \ | 
| 112 | } | 
| 113 |  | 
| 114 | DEFINE_ROLE_PROPERTIES(display, setDisplay, displayChanged, | 
| 115 |     getSetDisplay, setSetDisplay, setDisplayChanged, displayRoleName) | 
| 116 | DEFINE_ROLE_PROPERTIES(decoration, setDecoration, decorationChanged, | 
| 117 |     getSetDecoration, setSetDecoration, setDecorationChanged, decorationRoleName) | 
| 118 | DEFINE_ROLE_PROPERTIES(edit, setEdit, editChanged, | 
| 119 |     getSetEdit, setSetEdit, setEditChanged, editRoleName) | 
| 120 | DEFINE_ROLE_PROPERTIES(toolTip, setToolTip, toolTipChanged, | 
| 121 |     getSetToolTip, setSetToolTip, setToolTipChanged, toolTipRoleName) | 
| 122 | DEFINE_ROLE_PROPERTIES(statusTip, setStatusTip, statusTipChanged, | 
| 123 |     getSetStatusTip, setSetStatusTip, setStatusTipChanged, statusTipRoleName) | 
| 124 | DEFINE_ROLE_PROPERTIES(whatsThis, setWhatsThis, whatsThisChanged, | 
| 125 |     getSetWhatsThis, setSetWhatsThis, setWhatsThisChanged, whatsThisRoleName) | 
| 126 |  | 
| 127 | DEFINE_ROLE_PROPERTIES(font, setFont, fontChanged, | 
| 128 |     getSetFont, setSetFont, setFontChanged, fontRoleName) | 
| 129 | DEFINE_ROLE_PROPERTIES(textAlignment, setTextAlignment, textAlignmentChanged, | 
| 130 |     getSetTextAlignment, setSetTextAlignment, setTextAlignmentChanged, textAlignmentRoleName) | 
| 131 | DEFINE_ROLE_PROPERTIES(background, setBackground, backgroundChanged, | 
| 132 |     getSetBackground, setSetBackground, setBackgroundChanged, backgroundRoleName) | 
| 133 | DEFINE_ROLE_PROPERTIES(foreground, setForeground, foregroundChanged, | 
| 134 |     getSetForeground, setSetForeground, setForegroundChanged, foregroundRoleName) | 
| 135 | DEFINE_ROLE_PROPERTIES(checkState, setCheckState, checkStateChanged, | 
| 136 |     getSetCheckState, setSetCheckState, setCheckStateChanged, checkStateRoleName) | 
| 137 |  | 
| 138 | DEFINE_ROLE_PROPERTIES(accessibleText, setAccessibleText, accessibleTextChanged, | 
| 139 |     getSetAccessibleText, setSetAccessibleText, setAccessibleTextChanged, accessibleTextRoleName) | 
| 140 | DEFINE_ROLE_PROPERTIES(accessibleDescription, setAccessibleDescription, accessibleDescriptionChanged, | 
| 141 |     getSetAccessibleDescription, setSetAccessibleDescription, setAccessibleDescriptionChanged, accessibleDescriptionRoleName) | 
| 142 |  | 
| 143 | DEFINE_ROLE_PROPERTIES(sizeHint, setSizeHint, sizeHintChanged, | 
| 144 |     getSetSizeHint, setSetSizeHint, setSizeHintChanged, sizeHintRoleName) | 
| 145 |  | 
| 146 | QJSValue QQmlTableModelColumn::getterAtRole(const QString &roleName) | 
| 147 | { | 
| 148 |     auto it = mGetters.find(key: roleName); | 
| 149 |     if (it == mGetters.end()) | 
| 150 |         return QJSValue(); | 
| 151 |     return *it; | 
| 152 | } | 
| 153 |  | 
| 154 | QJSValue QQmlTableModelColumn::setterAtRole(const QString &roleName) | 
| 155 | { | 
| 156 |     auto it = mSetters.find(key: roleName); | 
| 157 |     if (it == mSetters.end()) | 
| 158 |         return QJSValue(); | 
| 159 |     return *it; | 
| 160 | } | 
| 161 |  | 
| 162 | const QHash<QString, QJSValue> QQmlTableModelColumn::getters() const | 
| 163 | { | 
| 164 |     return mGetters; | 
| 165 | } | 
| 166 |  | 
| 167 | const QHash<int, QString> QQmlTableModelColumn::supportedRoleNames() | 
| 168 | { | 
| 169 |     static const QHash<int, QString> names { | 
| 170 |         {Qt::DisplayRole, displayRoleName}, | 
| 171 |         {Qt::DecorationRole, decorationRoleName}, | 
| 172 |         {Qt::EditRole, editRoleName}, | 
| 173 |         {Qt::ToolTipRole, toolTipRoleName}, | 
| 174 |         {Qt::StatusTipRole, statusTipRoleName}, | 
| 175 |         {Qt::WhatsThisRole, whatsThisRoleName}, | 
| 176 |         {Qt::FontRole, fontRoleName}, | 
| 177 |         {Qt::TextAlignmentRole, textAlignmentRoleName}, | 
| 178 |         {Qt::BackgroundRole, backgroundRoleName}, | 
| 179 |         {Qt::ForegroundRole, foregroundRoleName}, | 
| 180 |         {Qt::CheckStateRole, checkStateRoleName}, | 
| 181 |         {Qt::AccessibleTextRole, accessibleTextRoleName}, | 
| 182 |         {Qt::AccessibleDescriptionRole, accessibleDescriptionRoleName}, | 
| 183 |         {Qt::SizeHintRole, sizeHintRoleName} | 
| 184 |     }; | 
| 185 |     return names; | 
| 186 | } | 
| 187 |  | 
| 188 | QT_END_NAMESPACE | 
| 189 |  | 
| 190 | #include "moc_qqmltablemodelcolumn_p.cpp" | 
| 191 |  |