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  : CSequence<CNameVariantPair>(other)
14  {}
15 
17  {
18  return this->contains(&CNameVariantPair::getName, name);
19  }
20 
22  {
23  if (this->isEmpty()) { return {}; }
25  if (sorted) { codes.sort(); }
26  return codes;
27  }
28 
30  {
31  if (name.isEmpty()) { return {}; }
32  return this->findBy(&CNameVariantPair::getName, name).frontOrDefault();
33  }
34 
36  {
37  if (name.isEmpty()) { return {}; }
38  return getValue(name).getVariant();
39  }
40 
42  {
43  if (name.isEmpty()) { return {}; }
44  const CVariant cs(getValue(name).getVariant());
45  if (cs.isNull() || !cs.canConvert<QString>()) { return {}; }
46  return cs.value<QString>();
47  }
48 
49  bool CNameVariantPairList::addOrReplaceValue(const QString &name, const CVariant &value, const CIcon &icon)
50  {
51  if (name.isEmpty()) { return false; }
52  int i = getIndexForName(name);
53  if (i < 0)
54  {
55  this->push_back(CNameVariantPair(name, value, icon));
56  return false;
57  }
58  else
59  {
60  (*this)[i] = CNameVariantPair(name, value, icon);
61  return true;
62  }
63  }
64 
66  {
67  for (int i = 0; i < this->size(); i++)
68  {
69  if ((*this)[i].getName() == name) { return i; }
70  }
71  return -1;
72  }
73 } // 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.
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.
CNameVariantPairList()=default
Default constructor.
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:166
bool isNull() const
True if this variant is null.
Definition: variant.h:246
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.
bool isEmpty() const const
void sort(Qt::CaseSensitivity cs)
#define SWIFT_DEFINE_SEQUENCE_MIXINS(Namespace, T, List)
Explicit template definition of mixins for a CSequence subclass.
Definition: sequence.h:63