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