-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6065 from dmitrio95/plugin-api-style-settings
Plugin API: add basic access to score style settings
- Loading branch information
1 parent
a4a9ba1
commit 0c29dd9
Showing
9 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//============================================================================= | ||
// MuseScore | ||
// Music Composition & Notation | ||
// | ||
// Copyright (C) 2020 MuseScore BVBA and others | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 2. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program; if not, write to the Free Software | ||
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
//============================================================================= | ||
|
||
#include "style.h" | ||
|
||
#include "libmscore/score.h" | ||
|
||
namespace Ms { | ||
namespace PluginAPI { | ||
|
||
//--------------------------------------------------------- | ||
// wrap | ||
//--------------------------------------------------------- | ||
|
||
MStyle* wrap(Ms::MStyle* style, Ms::Score* score) | ||
{ | ||
MStyle* st = new MStyle(style, score); | ||
// All wrapper objects should belong to JavaScript code. | ||
QQmlEngine::setObjectOwnership(st, QQmlEngine::JavaScriptOwnership); | ||
return st; | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// MStyle::keyToSid | ||
//--------------------------------------------------------- | ||
|
||
Sid MStyle::keyToSid(const QString& key) { | ||
static QMetaEnum sidEnum = QMetaEnum::fromType<Sid>(); | ||
|
||
bool ok; | ||
int val = sidEnum.keyToValue(key.toLatin1().constData(), &ok); | ||
|
||
if (ok) { | ||
return static_cast<Sid>(val); | ||
} | ||
else { | ||
qWarning("Invalid style key: %s", qPrintable(key)); | ||
return Sid::NOSTYLE; | ||
} | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// MStyle::value | ||
/// Returns a value of style setting named \p key. | ||
/// Key should be one of \ref Sid values. Type of the | ||
/// returned value depends on type of the corresponding | ||
/// style setting. | ||
//--------------------------------------------------------- | ||
|
||
QVariant MStyle::value(const QString& key) const { | ||
const Sid sid = keyToSid(key); | ||
|
||
if (sid == Sid::NOSTYLE) | ||
return QVariant(); | ||
|
||
const QVariant val = _style->value(sid); | ||
|
||
if (!strcmp(Ms::MStyle::valueType(sid), "Ms::Spatium")) | ||
return val.value<Ms::Spatium>().val(); | ||
|
||
return val; | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// MStyle::setValue | ||
/// Sets the value of style setting named \p key to \p value. | ||
/// Key should be one of \ref Sid values. | ||
//--------------------------------------------------------- | ||
|
||
void MStyle::setValue(const QString& key, QVariant value) { | ||
const Sid sid = keyToSid(key); | ||
|
||
if (sid == Sid::NOSTYLE) | ||
return; | ||
|
||
if (!strcmp(Ms::MStyle::valueType(sid), "Ms::Spatium")) | ||
value = QVariant::fromValue(Ms::Spatium(value.toReal())); | ||
|
||
if (_score) { | ||
// Style belongs to actual score: change style value in undoable way | ||
switch (sid) { | ||
case Sid::spatium: { | ||
const qreal oldSpatium = _score->spatium(); | ||
const qreal newSpatium = value.toReal(); | ||
|
||
if (newSpatium > 0.0 && oldSpatium != newSpatium) { | ||
_score->undoChangeStyleVal(Sid::spatium, newSpatium); | ||
_score->spatiumChanged(oldSpatium, newSpatium); | ||
} | ||
} | ||
break; | ||
default: | ||
_score->undoChangeStyleVal(sid, value); | ||
break; | ||
} | ||
} | ||
else { | ||
// Style is not bound to a score: change the value directly | ||
_style->set(sid, value); | ||
} | ||
} | ||
|
||
} // namespace PluginAPI | ||
} // namespace Ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//============================================================================= | ||
// MuseScore | ||
// Music Composition & Notation | ||
// | ||
// Copyright (C) 2020 MuseScore BVBA and others | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 2. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program; if not, write to the Free Software | ||
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
//============================================================================= | ||
|
||
#ifndef __PLUGIN_API_STYLE_H__ | ||
#define __PLUGIN_API_STYLE_H__ | ||
|
||
#include "libmscore/style.h" | ||
|
||
namespace Ms { | ||
|
||
class Score; | ||
|
||
namespace PluginAPI { | ||
|
||
//--------------------------------------------------------- | ||
// MStyle | ||
/// Provides an access to score style settings. | ||
/// Style settings for a score can be obtained by | ||
/// querying the \ref Score.style property. | ||
/// | ||
/// Usage example: | ||
/// \code | ||
/// var style = curScore.style; | ||
/// var genClef = style.value("genClef"); // retrieves style setting on clefs generation, true by default | ||
/// style.setValue("genClef", false); // disables generating clefs for this score | ||
/// \endcode | ||
/// \since MuseScore 3.5 | ||
/// \see \ref Sid | ||
//--------------------------------------------------------- | ||
|
||
class MStyle : public QObject { | ||
Q_OBJECT | ||
|
||
Ms::MStyle* _style; | ||
Ms::Score* _score; | ||
|
||
static Sid keyToSid(const QString& key); | ||
|
||
public: | ||
/// \cond MS_INTERNAL | ||
MStyle(Ms::MStyle* style, Ms::Score* score) | ||
: QObject(), _style(style), _score(score) {} | ||
/// \endcond | ||
|
||
Q_INVOKABLE QVariant value(const QString& key) const; | ||
Q_INVOKABLE void setValue(const QString& key, QVariant value); | ||
}; | ||
|
||
extern MStyle* wrap(Ms::MStyle*, Ms::Score*); | ||
|
||
} // namespace PluginAPI | ||
} // namespace Ms | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters