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 : #include "style.h"
24 :
25 : #include <QQmlEngine>
26 : #include <QMetaEnum>
27 :
28 : #include "engraving/style/defaultstyle.h"
29 : #include "engraving/dom/score.h"
30 :
31 : #include "log.h"
32 :
33 : using namespace mu::engraving::apiv1;
34 :
35 0 : MStyle* mu::engraving::apiv1::styleWrap(mu::engraving::MStyle* style, mu::engraving::Score* score)
36 : {
37 0 : MStyle* st = new MStyle(style, score);
38 : // All wrapper objects should belong to JavaScript code.
39 0 : QQmlEngine::setObjectOwnership(st, QQmlEngine::JavaScriptOwnership);
40 0 : return st;
41 : }
42 :
43 0 : mu::engraving::Sid MStyle::keyToSid(const QString& key)
44 : {
45 0 : static const QMetaEnum sidEnum = QMetaEnum::fromType<Sid>();
46 :
47 0 : QByteArray ba = key.toLatin1();
48 : bool ok;
49 0 : int val = sidEnum.keyToValue(ba.constData(), &ok);
50 :
51 0 : if (ok) {
52 0 : return static_cast<Sid>(val);
53 : } else {
54 0 : LOGW("Invalid style key: %s", qPrintable(key));
55 0 : return Sid::NOSTYLE;
56 : }
57 0 : }
58 :
59 0 : QVariant MStyle::value(const QString& key) const
60 : {
61 0 : const Sid sid = keyToSid(key);
62 :
63 0 : if (sid == Sid::NOSTYLE) {
64 0 : return QVariant();
65 : }
66 :
67 0 : const PropertyValue val = m_style->value(sid);
68 0 : return val.toQVariant();
69 0 : }
70 :
71 0 : void MStyle::setValue(const QString& key, QVariant value)
72 : {
73 0 : const Sid sid = keyToSid(key);
74 :
75 0 : if (sid == Sid::NOSTYLE) {
76 0 : return;
77 : }
78 :
79 0 : if (m_score) {
80 : // Style belongs to actual score: change style value in undoable way
81 0 : m_score->undoChangeStyleVal(sid, PropertyValue::fromQVariant(value, mu::engraving::MStyle::valueType(sid)));
82 : } else {
83 : // Style is not bound to a score: change the value directly
84 0 : m_style->set(sid, PropertyValue::fromQVariant(value, mu::engraving::MStyle::valueType(sid)));
85 : }
86 : }
87 :
88 0 : void MStyle::resetValue(const QString& key)
89 : {
90 0 : const Sid sid = keyToSid(key);
91 :
92 0 : if (sid == Sid::NOSTYLE) {
93 0 : return;
94 : }
95 :
96 0 : const mu::engraving::MStyle& defStyle = DefaultStyle::defaultStyle();
97 0 : if (m_score) {
98 : // Style belongs to actual score: change style value in undoable way
99 0 : m_score->undoChangeStyleVal(sid, defStyle.value(sid));
100 : } else {
101 : // Style is not bound to a score: change the value directly
102 0 : m_style->set(sid, defStyle.value(sid));
103 : }
104 : }
|