swift
namevariantpairlist.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 
6 #include "misc/range.h"
7 
8 SWIFT_DEFINE_SEQUENCE_MIXINS(swift::misc, CNameVariantPair, CNameVariantPairList)
9 
10 namespace swift::misc
11 {
13 
15  : CSequence<CNameVariantPair>(other)
16  {}
17 
18  bool CNameVariantPairList::containsName(const QString &name) const
19  {
20  return this->contains(&CNameVariantPair::getName, name);
21  }
22 
23  QStringList CNameVariantPairList::getNames(bool sorted) const
24  {
25  if (this->isEmpty()) { return QStringList(); }
27  if (sorted) { codes.sort(); }
28  return codes;
29  }
30 
32  {
33  if (name.isEmpty()) { return CNameVariantPair(); }
34  return this->findBy(&CNameVariantPair::getName, name).frontOrDefault();
35  }
36 
38  {
39  if (name.isEmpty()) { return CVariant(); }
40  return getValue(name).getVariant();
41  }
42 
43  QString CNameVariantPairList::getValueAsString(const QString &name) const
44  {
45  if (name.isEmpty()) { return QString(); }
46  const CVariant cs(getValue(name).getVariant());
47  if (cs.isNull() || !cs.canConvert<QString>()) { return QString(); }
48  return cs.value<QString>();
49  }
50 
51  bool CNameVariantPairList::addOrReplaceValue(const QString &name, const CVariant &value, const CIcon &icon)
52  {
53  if (name.isEmpty()) { return false; }
54  int i = getIndexForName(name);
55  if (i < 0)
56  {
57  this->push_back(CNameVariantPair(name, value, icon));
58  return false;
59  }
60  else
61  {
62  (*this)[i] = CNameVariantPair(name, value, icon);
63  return true;
64  }
65  }
66 
67  int CNameVariantPairList::getIndexForName(const QString &name) const
68  {
69  for (int i = 0; i < this->size(); i++)
70  {
71  if ((*this)[i].getName() == name) { return i; }
72  }
73  return -1;
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
Value / variant pair.
CVariant getVariant() const
Get variant.
const QString & getName() const
Get name.
QStringList getNames(bool sorted=true) const
Get all names.
CNameVariantPairList()
Default constructor.
CNameVariantPair getValue(const QString &name) const
Get value.
int getIndexForName(const QString &name) const
Get name's index, -1 if not found.
CVariant getVariantValue(const QString &name) const
Get pair's variant value or default if not existing.
QString getValueAsString(const QString &name) const
Get pair's variant value as string (empty if not existing)
bool addOrReplaceValue(const QString &name, const CVariant &value, const CIcon &icon=CIcon())
Add value, if name already exists replace (true) If one is sure(!) the name does not exists,...
bool containsName(const QString &name) const
Contains name.
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition: range.h:109
auto transform(F function) const
Return a new container generated by applying some transformation function to all elements of this one...
Definition: range.h:403
size_type size() const
Returns number of elements in the sequence.
Definition: sequence.h:273
const_reference frontOrDefault() const
Access the first element, or a default-initialized value if the sequence is empty.
Definition: sequence.h:239
CSequence findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
Definition: sequence.h:398
void push_back(const CNameVariantPair &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
bool isEmpty() const
Synonym for empty.
Definition: sequence.h:285
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
T value() const
Return the value converted to the type T.
Definition: variant.h:169
bool isNull() const
True if this variant is null.
Definition: variant.h:249
bool canConvert(int typeId) const
True if this variant can be converted to the type with the given metatype ID.
auto MemberTransform(T memberFunc)
Returns a function object that returns the value returned by one of it's argument member functions.
Definition: predicates.h:54
Free functions in swift::misc.
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63