swift
pqstring.h
Go to the documentation of this file.
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 #ifndef SWIFT_MISC_PQ_PQSTRING_H
7 #define SWIFT_MISC_PQ_PQSTRING_H
8 
9 #include <QMetaType>
10 #include <QString>
11 
12 #include "misc/metaclass.h"
13 #include "misc/swiftmiscexport.h"
14 #include "misc/valueobject.h"
15 #include "misc/variant.h"
16 
17 SWIFT_DECLARE_VALUEOBJECT_MIXINS(swift::misc::physical_quantities, CPqString)
18 
19 namespace swift::misc::physical_quantities
20 {
26  class SWIFT_MISC_EXPORT CPqString : public CValueObject<CPqString>
27  {
28  public:
30  QString convertToQString(bool i18n = false) const;
31 
34  {
37  SeparatorBestGuess
38  };
39 
42  {
43  Group,
44  Digit
45  };
46 
48  CPqString() = default;
49 
51  CPqString(const QString &value) : m_string(value) {}
52 
54  static CVariant parseToVariant(const QString &value, SeparatorMode mode = SeparatorQtDefault);
55 
57  template <class PQ>
58  static PQ parse(const QString &value, SeparatorMode mode = SeparatorQtDefault)
59  {
60  if (value.isEmpty()) { return PQ::null(); }
61  if (value.contains("null", Qt::CaseInsensitive)) { return PQ::null(); }
62  const CVariant qv = parseToVariant(value, mode);
63  if (!qv.isNull() && qv.canConvert<PQ>()) { return qv.value<PQ>(); }
64  return PQ::null();
65  }
66 
68  static double parseNumber(const QString &number, bool &success, SeparatorMode mode = SeparatorBestGuess);
69 
70  private:
71  QString m_string;
72 
74  CPqString,
75  SWIFT_METAMEMBER(string));
76  };
77 } // namespace swift::misc::physical_quantities
78 
80 
81 #endif // SWIFT_MISC_PQ_PQSTRING_H
Mix of the most commonly used mixin classes.
Definition: valueobject.h:114
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.
Represents a physical quantity by a string.
Definition: pqstring.h:27
static PQ parse(const QString &value, SeparatorMode mode=SeparatorQtDefault)
Parse into concrete type.
Definition: pqstring.h:58
SeparatorMode
Number separators / group separators.
Definition: pqstring.h:34
@ SeparatorLocale
depending on QLocale, e.g.
Definition: pqstring.h:36
@ SeparatorQtDefault
100000.00 no group separator
Definition: pqstring.h:35
CPqString()=default
Default constructor.
CPqString(const QString &value)
Constructor, for values such as 10km/h.
Definition: pqstring.h:51
SeparatorIndex
Group and digit separator.
Definition: pqstring.h:42
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition: metaclass.h:73
#define SWIFT_METACLASS(CLASS,...)
Macro to define a nested metaclass that describes the attributes of its enclosing class.
Definition: metaclass.h:53
#define SWIFT_MISC_EXPORT
Export a class or function from the library.
#define SWIFT_DECLARE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template declaration of mixins for a CValueObject subclass to be placed near the top of the ...
Definition: valueobject.h:65