Loading...
Searching...
No Matches
ValueConversion.hpp
1#pragma once
2#include <State/Value.hpp>
3
4#include <QChar>
5#include <QString>
6#include <QStringList>
7#include <QVariant>
8
9namespace State
10{
11struct AddressAccessor;
12namespace convert
13{
14
15template <typename To>
16To value(const ossia::value& val)
17{
18 static_assert(sizeof(To) == -1, "Type not supported.");
19}
20
21template <>
22SCORE_LIB_STATE_EXPORT QVariant value(const ossia::value& val);
23template <>
24SCORE_LIB_STATE_EXPORT ossia::value value(const ossia::value& val);
25template <>
26SCORE_LIB_STATE_EXPORT State::AddressAccessor value(const ossia::value& val);
27template <>
28SCORE_LIB_STATE_EXPORT int value(const ossia::value& val);
29template <>
30SCORE_LIB_STATE_EXPORT float value(const ossia::value& val);
31template <>
32SCORE_LIB_STATE_EXPORT bool value(const ossia::value& val);
33template <>
34SCORE_LIB_STATE_EXPORT double value(const ossia::value& val);
35template <>
36SCORE_LIB_STATE_EXPORT QString value(const ossia::value& val);
37template <>
38SCORE_LIB_STATE_EXPORT QChar value(const ossia::value& val);
39template <>
40SCORE_LIB_STATE_EXPORT std::string value(const ossia::value& val);
41template <>
42SCORE_LIB_STATE_EXPORT char value(const ossia::value& val);
43template <>
44SCORE_LIB_STATE_EXPORT vec2f value(const ossia::value& val);
45template <>
46SCORE_LIB_STATE_EXPORT vec3f value(const ossia::value& val);
47template <>
48SCORE_LIB_STATE_EXPORT vec4f value(const ossia::value& val);
49template <>
50SCORE_LIB_STATE_EXPORT list_t value(const ossia::value& val);
51template <>
52SCORE_LIB_STATE_EXPORT ossia::value_map_type value(const ossia::value& val);
53
54SCORE_LIB_STATE_EXPORT bool convert(const ossia::value& orig, ossia::value& toConvert);
55
56// Whether a string is text at all: valid UTF-8 with no control character
57// beyond tab, CR and LF. ossia's STRING is a std::string, so a device may put
58// a PNG in one, and decoding that as text destroys it.
59SCORE_LIB_STATE_EXPORT bool isBinary(const QByteArray& bytes) noexcept;
60
61// What a cell shows for bytes that are not text: the first few in hex, and the
62// count.
63SCORE_LIB_STATE_EXPORT QString binarySummary(const QByteArray& bytes);
64
65// The head of a one-line summary and its "[+N lines]" marker, so a delegate
66// can draw the marker in its own pen. `marker` is empty when the text fits.
68{
69 QString head;
70 QString marker;
71};
72SCORE_LIB_STATE_EXPORT SingleLine splitSingleLine(const QString& text);
73
74// Text collapsed for a one-line table cell: the first line, then a count of
75// what did not fit.
76SCORE_LIB_STATE_EXPORT QString toSingleLine(const QString& text);
77
78// Whether text() has anything toSingleLine would have to fold away.
79SCORE_LIB_STATE_EXPORT bool isMultiLine(const QString& text) noexcept;
80
81// The map form. The grammar has no rule for it, so a brace-delimited list of
82// `key: value` is scanned by hand. parseValue calls this for input that starts
83// with a brace; a map nested inside a list is still out of reach, since the
84// list rule is the grammar own.
85SCORE_LIB_STATE_EXPORT std::optional<ossia::value> parseMap(const QString& text);
86
87// What a one-line cell shows for a string value, and the tooltip that goes
88// with it: a summary for bytes that are not text, the first line and a marker
89// for text that does not fit, the text itself otherwise. The tooltip is empty
90// when the cell already shows the whole value.
91//
92// Every value column needs both, and decoding bytes that are not text -- to
93// show them, or worse to put a whole blob in a tooltip -- is the thing they
94// each have to avoid.
95SCORE_LIB_STATE_EXPORT QString stringCellText(const QByteArray& bytes);
96SCORE_LIB_STATE_EXPORT QString stringCellToolTip(const QByteArray& bytes);
97
98// The body of a quoted string literal: backslash, double quote and the
99// whitespace a single line cannot carry become escapes, so that what
100// toPrettyString writes is what parseValue reads back.
101SCORE_LIB_STATE_EXPORT QString escapeStringLiteral(const QString& s);
102
103// Adornishments to allow to differentiate between different value types, e.g.
104// 'a', ['a', 12], or "str" for a string.
105SCORE_LIB_STATE_EXPORT QString toPrettyString(const ossia::value& val);
106
107// Decimal notation, so that e.g. a byte count reads 137438953472 instead of
108// 1.37439e+11. Falls back to the exponent form for magnitudes where decimal
109// notation would be unreadable.
110SCORE_LIB_STATE_EXPORT QString toPrettyString(float f);
111
112// As the user reads it: a string without its quotes. Composite values keep the
113// adorned form, having no plain one.
114SCORE_LIB_STATE_EXPORT QString toDisplayString(const ossia::value& val);
115
116// We require the type to crrectly read back (e.g. int / float / char)
117// and as an optimization, since we may need it multiple times,
118// we chose to leave the caller save it however he wants. Hence the specific
119// API.
120SCORE_LIB_STATE_EXPORT QString
121textualType(const ossia::value& val); // For JSONValue serialization
122SCORE_LIB_STATE_EXPORT ossia::value fromQVariant(const QVariant& val);
123SCORE_LIB_STATE_EXPORT QString
124prettyType(const ossia::value& val); // For display to the user, translated
125SCORE_LIB_STATE_EXPORT
126QString prettyType(ossia::val_type); // For display to the user, translated
127SCORE_LIB_STATE_EXPORT const std::array<const QString, 11>&
128ValuePrettyTypesArray(); // For display to the user, translated
129SCORE_LIB_STATE_EXPORT const QStringList&
130ValuePrettyTypesList(); // For display to the user, translated
131SCORE_LIB_STATE_EXPORT const std::array<std::pair<QString, ossia::val_type>, 10>&
132ValuePrettyTypesMap();
133}
134}
Utilities for OSSIA data structures.
Definition DeviceInterface.hpp:35
Definition Address.hpp:136
Definition ValueConversion.hpp:68