Skip to content

Commit

Permalink
Parse dynamics when finished editing
Browse files Browse the repository at this point in the history
  • Loading branch information
CMakeScore committed Nov 10, 2024
1 parent 2359b83 commit a5b3ef8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/engraving/dom/dynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,18 +424,31 @@ void Dynamic::manageBarlineCollisions()

void Dynamic::setDynamicType(const String& tag)
{
const DynamicType dt = parseDynamicText(tag);

if (dt == DynamicType::OTHER) {
LOGD("setDynamicType: other <%s>", muPrintable(tag));
}

setDynamicType(dt);
}

DynamicType Dynamic::parseDynamicText(const String& tag) {
std::string utf8Tag = tag.toStdString();
size_t n = DYN_LIST.size();
for (size_t i = 0; i < n; ++i) {
if (TConv::toXml(DynamicType(i)).ascii() == utf8Tag || DYN_LIST[i].text == utf8Tag) {
setDynamicType(DynamicType(i));
setXmlText(String::fromUtf8(DYN_LIST[i].text));
return;
std::regex dynamicRegex(R"((?:<sym>.*?</sym>)+|(?:\b)[fmnprsz]+(?:\b(?=[^>]|$)))");
for (std::sregex_iterator it(utf8Tag.begin(), utf8Tag.end(), dynamicRegex), end; it != end; ++it) {
std::smatch match = *it;
std::string matchStr = match.str();
size_t n = DYN_LIST.size();
for (size_t i = 0; i < n; ++i) {
if (TConv::toXml(DynamicType(i)).ascii() == matchStr || DYN_LIST[i].text == matchStr) {
utf8Tag.replace(match.position(0), match.length(0), DYN_LIST[i].text);
setXmlText(String::fromStdString(utf8Tag));
return DynamicType(i);
}
}
}
LOGD("setDynamicType: other <%s>", muPrintable(tag));
setDynamicType(DynamicType::OTHER);
setXmlText(tag);
return DynamicType::OTHER;
}

String Dynamic::dynamicText(DynamicType t)
Expand Down
2 changes: 2 additions & 0 deletions src/engraving/dom/dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Dynamic final : public TextBase
void setDynamicType(DynamicType val) { m_dynamicType = val; }
void setDynamicType(const String&);

DynamicType parseDynamicText(const String&);

DynamicType dynamicType() const { return m_dynamicType; }
int subtype() const override { return static_cast<int>(m_dynamicType); }
TranslatableString subtypeUserName() const override;
Expand Down
7 changes: 7 additions & 0 deletions src/engraving/dom/textedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "mscoreview.h"
#include "navigate.h"
#include "score.h"
#include "dynamic.h"
#include "lyrics.h"

#include "log.h"
Expand Down Expand Up @@ -142,6 +143,12 @@ void TextBase::endEdit(EditData& ed)

ted->cursor()->endEdit();

if (isDynamic()) {
Dynamic* d = toDynamic(this);
const DynamicType dt = d->parseDynamicText(xmlText());
undoChangeProperty(Pid::DYNAMIC_TYPE, dt);
}

UndoStack* undo = score()->undoStack();
IF_ASSERT_FAILED(undo) {
return;
Expand Down

0 comments on commit a5b3ef8

Please sign in to comment.