Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #pragma once
24 :
25 : #include <array>
26 : #include <cassert>
27 :
28 : #include "global/io/iodevice.h"
29 :
30 : #include "../types/spatium.h"
31 : #include "../types/propertyvalue.h"
32 :
33 : #include "styledef.h"
34 :
35 : namespace mu::engraving::compat {
36 : class ReadChordListHook;
37 : class ReadStyleHook;
38 : }
39 :
40 : namespace mu::engraving {
41 : class XmlReader;
42 : class XmlWriter;
43 :
44 : class MStyle
45 : {
46 : public:
47 2378 : MStyle() = default;
48 :
49 2040602 : const PropertyValue& styleV(Sid idx) const { return value(idx); }
50 324917 : Spatium styleS(Sid idx) const
51 : {
52 324917 : assert(MStyle::valueType(idx) == P_TYPE::SPATIUM);
53 324917 : return value(idx).value<Spatium>();
54 : }
55 :
56 2317822 : double styleAbsolute(Sid idx) const { assert(MStyle::valueType(idx) == P_TYPE::SPATIUM); return valueAbsolute(idx); }
57 53004 : String styleSt(Sid idx) const { assert(MStyle::valueType(idx) == P_TYPE::STRING); return value(idx).value<String>(); }
58 6271311 : bool styleB(Sid idx) const { assert(MStyle::valueType(idx) == P_TYPE::BOOL); return value(idx).toBool(); }
59 8491244 : double styleD(Sid idx) const { assert(MStyle::valueType(idx) == P_TYPE::REAL); return value(idx).toReal(); }
60 2691385 : int styleI(Sid idx) const { /* can be int or enum, so no assert */ return value(idx).toInt(); }
61 :
62 : const PropertyValue& value(Sid idx) const;
63 : double valueAbsolute(Sid idx) const;
64 :
65 : void set(Sid idx, const PropertyValue& v);
66 :
67 7217903 : double spatium() const { return styleD(Sid::spatium); }
68 137 : void setSpatium(double v) { set(Sid::spatium, v); }
69 : double defaultSpatium() const;
70 :
71 : bool isDefault(Sid idx) const;
72 : void setDefaultStyleVersion(const int defaultsVersion);
73 : int defaultStyleVersion() const;
74 :
75 : bool read(muse::io::IODevice* device, bool ign = false);
76 : bool write(muse::io::IODevice* device);
77 : void save(XmlWriter& xml, bool optimize);
78 : static bool isValid(muse::io::IODevice* device);
79 :
80 : void precomputeValues();
81 :
82 : static P_TYPE valueType(const Sid);
83 : static const char* valueName(const Sid);
84 : static Sid styleIdx(const String& name);
85 :
86 : private:
87 : friend class compat::ReadStyleHook;
88 :
89 : void read(XmlReader& e, compat::ReadChordListHook* readChordListHook, int mscVersion);
90 :
91 : bool readProperties(XmlReader&);
92 : bool readStyleValCompat(XmlReader&);
93 : bool readTextStyleValCompat(XmlReader&);
94 :
95 : void applyCompatStyleVals(int mscVersion);
96 :
97 : std::array<PropertyValue, size_t(Sid::STYLES)> m_values;
98 : std::array<double, size_t(Sid::STYLES)> m_precomputedValues;
99 : };
100 : }
|