swift
Loading...
Searching...
No Matches
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"
14#include "misc/valueobject.h"
15#include "misc/variant.h"
16
17SWIFT_DECLARE_VALUEOBJECT_MIXINS(swift::misc::physical_quantities, CPqString)
18
19namespace 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
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())
61 {
62 return PQ::null();
63 }
64 if (value.contains("null", Qt::CaseInsensitive))
65 {
66 return PQ::null();
67 }
68 const CVariant qv = parseToVariant(value, mode);
69 if (!qv.isNull() && qv.canConvert<PQ>())
70 {
71 return qv.value<PQ>();
72 }
73 return PQ::null();
74 }
75
77 static double parseNumber(const QString &number, bool &success, SeparatorMode mode = SeparatorBestGuess);
78
79 private:
80 QString m_string;
81
84 SWIFT_METAMEMBER(string));
85 };
86} // namespace swift::misc::physical_quantities
87
89
90#endif // SWIFT_MISC_PQ_PQSTRING_H
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
T value() const
Return the value converted to the type T.
Definition variant.h:154
bool isNull() const
True if this variant is null.
Definition variant.h:227
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 CVariant parseToVariant(const QString &value, SeparatorMode mode=SeparatorQtDefault)
Parse a string value like "100m", "10.3Mhz".
Definition pqstring.cpp:28
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
QString convertToQString(bool i18n=false) const
Cast as QString.
Definition pqstring.cpp:26
SeparatorIndex
Group and digit separator.
Definition pqstring.h:42
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition metaclass.h:72
#define SWIFT_METACLASS(CLASS,...)
Macro to define a nested metaclass that describes the attributes of its enclosing class.
Definition metaclass.h:52
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
CaseInsensitive
#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