/ proxyroles / regexprole.cpp
regexprole.cpp
1 #include "regexprole.h" 2 #include "qqmlsortfilterproxymodel.h" 3 #include <QDebug> 4 5 namespace qqsfpm { 6 7 /*! 8 \qmltype RegExpRole 9 \inherits ProxyRole 10 \inqmlmodule SortFilterProxyModel 11 \ingroup ProxyRoles 12 \brief A ProxyRole extracting data from a source role via a regular expression. 13 14 A RegExpRole is a \l ProxyRole that provides a role for each named capture group of its regular expression \l pattern. 15 16 In the following example, the \c date role of the source model will be extracted in 3 roles in the proxy moodel: \c year, \c month and \c day. 17 \code 18 SortFilterProxyModel { 19 sourceModel: eventModel 20 proxyRoles: RegExpRole { 21 roleName: "date" 22 pattern: "(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})" 23 } 24 } 25 \endcode 26 */ 27 28 /*! 29 \qmlproperty QString RegExpRole::roleName 30 31 This property holds the role name that the RegExpRole is using to query the source model's data to extract new roles from. 32 */ 33 QString RegExpRole::roleName() const 34 { 35 return m_roleName; 36 } 37 38 void RegExpRole::setRoleName(const QString& roleName) 39 { 40 if (m_roleName == roleName) 41 return; 42 43 m_roleName = roleName; 44 Q_EMIT roleNameChanged(); 45 } 46 47 /*! 48 \qmlproperty QString RegExpRole::pattern 49 50 This property holds the pattern of the regular expression of this RegExpRole. 51 The RegExpRole will expose a role for each of the named capture group of the pattern. 52 */ 53 QString RegExpRole::pattern() const 54 { 55 return m_regularExpression.pattern(); 56 } 57 58 void RegExpRole::setPattern(const QString& pattern) 59 { 60 if (m_regularExpression.pattern() == pattern) 61 return; 62 63 Q_EMIT namesAboutToBeChanged(); 64 m_regularExpression.setPattern(pattern); 65 invalidate(); 66 Q_EMIT patternChanged(); 67 Q_EMIT namesChanged(); 68 } 69 70 /*! 71 \qmlproperty Qt::CaseSensitivity RegExpRole::caseSensitivity 72 73 This property holds the caseSensitivity of the regular expression. 74 */ 75 Qt::CaseSensitivity RegExpRole::caseSensitivity() const 76 { 77 return m_regularExpression.patternOptions() & QRegularExpression::CaseInsensitiveOption ? 78 Qt::CaseInsensitive : Qt::CaseSensitive; 79 } 80 81 void RegExpRole::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity) 82 { 83 if (this->caseSensitivity() == caseSensitivity) 84 return; 85 86 m_regularExpression.setPatternOptions(m_regularExpression.patternOptions() ^ QRegularExpression::CaseInsensitiveOption); //toggle the option 87 Q_EMIT caseSensitivityChanged(); 88 } 89 90 QStringList RegExpRole::names() 91 { 92 QStringList nameCaptureGroups = m_regularExpression.namedCaptureGroups(); 93 nameCaptureGroups.removeAll(""); 94 return nameCaptureGroups; 95 } 96 97 QVariant RegExpRole::data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel, const QString &name) 98 { 99 QString text = proxyModel.sourceData(sourceIndex, m_roleName).toString(); 100 QRegularExpressionMatch match = m_regularExpression.match(text); 101 return match.hasMatch() ? (match.captured(name)) : QVariant{}; 102 } 103 104 }