swift
Loading...
Searching...
No Matches
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
5
7
9
10namespace 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
16 CIcons::IconIndex CNameVariantPair::toIcon() const { return m_icon.getIndex(); }
17
18 bool CNameVariantPair::hasIcon() const { return this->m_icon.isSet(); }
19
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())
30 {
31 return QVariant::fromValue(*this);
32 }
33 const auto i = index.frontCasted<ColumnIndex>();
34 switch (i)
35 {
36 case IndexName: return { this->m_name };
37 case IndexVariant: return this->m_variant; // NOLINT(modernize-return-braced-init-list)
38 default: return CValueObject::propertyByIndex(index);
39 }
40 }
41
43 {
44 if (index.isMyself())
45 {
46 (*this) = variant.value<CNameVariantPair>();
47 return;
48 }
49 const auto i = index.frontCasted<ColumnIndex>();
50
51 // special case, handle icon and allow to set it
52 // doing this in the switch gives gcc warning as IndexIcon is no member of ColumnIndex
53 if (static_cast<int>(i) == static_cast<int>(IndexIcon))
54 {
55 if (static_cast<QMetaType::Type>(variant.typeId()) == QMetaType::Int)
56 {
57 const auto iconIndex = variant.value<CIcons::IconIndex>();
58 this->m_icon = CIcon::iconByIndex(iconIndex);
59 }
60 else
61 {
62 this->m_icon = variant.value<swift::misc::CIcon>();
63 }
64 return;
65 }
66
67 // properties
68 switch (i)
69 {
70 case IndexName: this->setName(variant.value<QString>()); break;
71 case IndexVariant: this->m_variant = variant; break;
72 default: CValueObject::setPropertyByIndex(index, variant); break;
73 }
74 }
75} // 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
static const CIcon & iconByIndex(CIcons::IconIndex index)
Icon for given index.
Definition icon.cpp:63
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition icons.h:32
ColumnIndex
Properties by index.
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:69
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition mixinindex.h:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
Free functions in swift::misc.
QString & append(QChar ch)
QVariant fromValue(T &&value)
int typeId() const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition valueobject.h:67