/ proxyroles / joinrole.cpp
joinrole.cpp
 1  #include "joinrole.h"
 2  #include "qqmlsortfilterproxymodel.h"
 3  
 4  namespace qqsfpm {
 5  
 6  /*!
 7      \qmltype JoinRole
 8      \inherits SingleRole
 9      \inqmlmodule SortFilterProxyModel
10      \ingroup ProxyRoles
11      \brief a role made from concatenating other roles.
12  
13      A JoinRole is a simple \l ProxyRole that concatenates other roles.
14  
15      In the following example, the \c fullName role is computed by the concatenation of the \c firstName role and the \c lastName role separated by a space :
16      \code
17      SortFilterProxyModel {
18         sourceModel: contactModel
19         proxyRoles: JoinRole {
20             name: "fullName"
21             roleNames: ["firstName", "lastName"]
22        }
23      }
24      \endcode
25  
26  */
27  
28  /*!
29      \qmlproperty list<string> JoinRole::roleNames
30  
31      This property holds the role names that are joined by this role.
32  */
33  QStringList JoinRole::roleNames() const
34  {
35      return m_roleNames;
36  }
37  
38  void JoinRole::setRoleNames(const QStringList& roleNames)
39  {
40      if (m_roleNames == roleNames)
41          return;
42  
43      m_roleNames = roleNames;
44      Q_EMIT roleNamesChanged();
45      invalidate();
46  }
47  
48  /*!
49      \qmlproperty string JoinRole::separator
50  
51      This property holds the separator that is used to join the roles specified in \l roleNames.
52  
53      By default, it's a space.
54  */
55  QString JoinRole::separator() const
56  {
57      return m_separator;
58  }
59  
60  void JoinRole::setSeparator(const QString& separator)
61  {
62      if (m_separator == separator)
63          return;
64  
65      m_separator = separator;
66      Q_EMIT separatorChanged();
67      invalidate();
68  }
69  
70  QVariant JoinRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel& proxyModel)
71  {
72      QString result;
73  
74      for (const QString& roleName : qAsConst(m_roleNames))
75          result += proxyModel.sourceData(sourceIndex, roleName).toString() + m_separator;
76  
77      if (!m_roleNames.isEmpty())
78          result.chop(m_separator.length());
79  
80      return result;
81  }
82  
83  }