swift
namevariantpair.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "misc/namevariantpair.h"
5 
7 
9 
10 namespace swift::misc
11 {
12  CNameVariantPair::CNameVariantPair(const QString &name, const CVariant &variant, const CIcon &icon)
13  : m_name(name), m_variant(variant), m_icon(icon)
14  {}
15 
17 
18  bool CNameVariantPair::hasIcon() const { return this->m_icon.isSet(); }
19 
20  QString CNameVariantPair::convertToQString(bool i18n) const
21  {
22  QString s(this->m_name);
23  s.append(" ").append(this->m_variant.toQString(i18n));
24  return s;
25  }
26 
28  {
29  if (index.isMyself()) { return QVariant::fromValue(*this); }
30  ColumnIndex i = index.frontCasted<ColumnIndex>();
31  switch (i)
32  {
33  case IndexName: return QVariant(this->m_name);
34  case IndexVariant: return this->m_variant;
35  default: return CValueObject::propertyByIndex(index);
36  }
37  }
38 
39  void CNameVariantPair::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
40  {
41  if (index.isMyself())
42  {
43  (*this) = variant.value<CNameVariantPair>();
44  return;
45  }
46  ColumnIndex i = index.frontCasted<ColumnIndex>();
47 
48  // special case, handle icon and allow to set it
49  // doing this in the switch gives gcc warning as IndexIcon is no member of ColumnIndex
50  if (static_cast<int>(i) == static_cast<int>(IndexIcon))
51  {
52  if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::Int)
53  {
54  CIcons::IconIndex iconIndex = variant.value<CIcons::IconIndex>();
55  this->m_icon = CIcon::iconByIndex(iconIndex);
56  }
57  else { this->m_icon = variant.value<swift::misc::CIcon>(); }
58  return;
59  }
60 
61  // properties
62  switch (i)
63  {
64  case IndexName: this->setName(variant.value<QString>()); break;
65  case IndexVariant: this->m_variant = variant; break;
66  default: CValueObject::setPropertyByIndex(index, variant); break;
67  }
68  }
69 } // namespace swift::misc
Value object for icons. An icon is stored in the global icon repository and identified by its index....
Definition: icon.h:39
CIcons::IconIndex getIndex() const
Index.
Definition: icon.cpp:24
bool isSet() const
Icon set?
Definition: icon.cpp:32
static const CIcon & iconByIndex(CIcons::IconIndex index)
Icon for given index.
Definition: icon.cpp:54
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition: icons.h:32
Value / variant pair.
QString convertToQString(bool i18n=false) const
Cast as QString.
bool hasIcon() const
Has icon.
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
void setName(const QString &name)
Set name.
CNameVariantPair()=default
Default constructor.
swift::misc::CIcons::IconIndex toIcon() const
As icon, not implemented by all classes.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
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.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
ColumnIndex
Base class enums.
Definition: mixinindex.h:44
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
QString toQString(bool i18n=false) const
Cast as QString.
Definition: mixinstring.h:76
Free functions in swift::misc.
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67