swift
role.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/network/role.h"
5 
6 #include <QJsonValue>
7 #include <QStringBuilder>
8 #include <QtGlobal>
9 
10 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CRole)
11 
12 namespace swift::misc::network
13 {
14  CRole::CRole(const QString &name, const QString &description) : m_name(name), m_description(description) {}
15 
17  {
18  Q_UNUSED(i18n);
19  return u"Role: " % m_name % u" description: " % m_description % this->getDbKeyAsStringInParentheses(" ");
20  }
21 
23  {
24  if (index.isMyself()) { return QVariant::fromValue(*this); }
25  if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
26  {
27  return IDatastoreObjectWithIntegerKey::propertyByIndex(index);
28  }
29  const ColumnIndex i = index.frontCasted<ColumnIndex>();
30  switch (i)
31  {
32  case IndexName: return QVariant::fromValue(m_name);
33  case IndexDescription: return QVariant::fromValue(m_description);
34  default: break;
35  }
36  return CValueObject::propertyByIndex(index);
37  }
38 
40  {
41  if (index.isMyself())
42  {
43  (*this) = variant.value<CRole>();
44  return;
45  }
46  if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
47  {
48  IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant);
49  return;
50  }
51  const ColumnIndex i = index.frontCasted<ColumnIndex>();
52  switch (i)
53  {
54  case IndexName: this->setName(variant.value<QString>()); break;
55  case IndexDescription: this->setDescription(variant.value<QString>()); break;
56  default: break;
57  }
58  CValueObject::setPropertyByIndex(index, variant);
59  }
60 
62  {
63  CRole role;
64  role.setName(json.value("name").toString());
65  role.setDescription(json.value("description").toString());
66  role.setDbKey(json.value("idrole").toInt(-1));
67  return role;
68  }
69 } // namespace swift::misc::network
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
void setDbKey(int key)
Set the DB key.
Definition: datastore.h:96
QString getDbKeyAsStringInParentheses(const QString &prefix={}) const
Db key in parentheses, e.g. "(3)".
Definition: datastore.cpp:36
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:158
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:165
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition: role.cpp:16
static CRole fromDatabaseJson(const QJsonObject &json)
Role from DB JSON.
Definition: role.cpp:61
CRole()=default
Constructor.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
Definition: role.cpp:22
void setName(const QString &name)
Name.
Definition: role.h:46
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: role.cpp:39
void setDescription(const QString &description)
Description.
Definition: role.h:52
QJsonValue value(QLatin1StringView key) const const
int toInt(int defaultValue) const const
QString toString() const const
QVariant fromValue(T &&value)
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67