|
28 | 28 | #include "dom/measure.h" |
29 | 29 | #include "dom/score.h" |
30 | 30 | #include "dom/segment.h" |
| 31 | +#include "dom/spanner.h" |
| 32 | +#include "dom/spannermap.h" |
31 | 33 | #include "dom/text.h" |
32 | 34 | #include "editing/editsystemlocks.h" |
33 | 35 | #include "types/typesconv.h" |
|
39 | 41 |
|
40 | 42 | using namespace mu::engraving::apiv1; |
41 | 43 |
|
| 44 | +Score::SpannerListContext* Score::acquireSpannerContext(bool hasFilter, mu::engraving::ElementType filterType) |
| 45 | +{ |
| 46 | + for (const auto& ctx : m_spannerListContexts) { |
| 47 | + if (ctx->hasFilter == hasFilter && ctx->filterType == filterType) { |
| 48 | + return ctx.get(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + auto context = std::make_unique<SpannerListContext>(); |
| 53 | + context->owner = this; |
| 54 | + context->hasFilter = hasFilter; |
| 55 | + context->filterType = filterType; |
| 56 | + m_spannerListContexts.push_back(std::move(context)); |
| 57 | + return m_spannerListContexts.back().get(); |
| 58 | +} |
| 59 | + |
| 60 | +qsizetype Score::spannerListCount(QQmlListProperty<Spanner>* list) |
| 61 | +{ |
| 62 | + auto* ctx = static_cast<SpannerListContext*>(list->data); |
| 63 | + if (!ctx || !ctx->owner) { |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + mu::engraving::Score* nativeScore = ctx->owner->score(); |
| 68 | + if (!nativeScore) { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + const auto& spanMap = nativeScore->spannerMap().map(); |
| 73 | + if (!ctx->hasFilter) { |
| 74 | + return static_cast<qsizetype>(spanMap.size()); |
| 75 | + } |
| 76 | + |
| 77 | + qsizetype count = 0; |
| 78 | + for (const auto& [tick, spanner] : spanMap) { |
| 79 | + UNUSED(tick); |
| 80 | + if (spanner && spanner->type() == ctx->filterType) { |
| 81 | + ++count; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return count; |
| 86 | +} |
| 87 | + |
| 88 | +Spanner* Score::spannerListAt(QQmlListProperty<Spanner>* list, qsizetype index) |
| 89 | +{ |
| 90 | + auto* ctx = static_cast<SpannerListContext*>(list->data); |
| 91 | + if (!ctx || !ctx->owner || index < 0) { |
| 92 | + return nullptr; |
| 93 | + } |
| 94 | + |
| 95 | + mu::engraving::Score* nativeScore = ctx->owner->score(); |
| 96 | + if (!nativeScore) { |
| 97 | + return nullptr; |
| 98 | + } |
| 99 | + |
| 100 | + const auto& spanMap = nativeScore->spannerMap().map(); |
| 101 | + qsizetype current = 0; |
| 102 | + for (const auto& [tick, spanner] : spanMap) { |
| 103 | + UNUSED(tick); |
| 104 | + if (!spanner) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + if (ctx->hasFilter && spanner->type() != ctx->filterType) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + if (current == index) { |
| 113 | + return qobject_cast<Spanner*>(wrap(spanner, Ownership::SCORE)); |
| 114 | + } |
| 115 | + |
| 116 | + ++current; |
| 117 | + } |
| 118 | + |
| 119 | + return nullptr; |
| 120 | +} |
| 121 | + |
42 | 122 | Cursor* Score::newCursor() |
43 | 123 | { |
44 | 124 | return new Cursor(score()); |
@@ -262,6 +342,37 @@ QQmlListProperty<System> Score::systems() |
262 | 342 | return wrapContainerProperty<System>(this, score()->systems()); |
263 | 343 | } |
264 | 344 |
|
| 345 | +//--------------------------------------------------------- |
| 346 | +// Score::spanners |
| 347 | +//--------------------------------------------------------- |
| 348 | + |
| 349 | +QQmlListProperty<Spanner> Score::spanners() |
| 350 | +{ |
| 351 | + auto* context = acquireSpannerContext(false, mu::engraving::ElementType::INVALID); |
| 352 | + return QQmlListProperty<Spanner>(this, context, &Score::spannerListCount, &Score::spannerListAt); |
| 353 | +} |
| 354 | + |
| 355 | +//--------------------------------------------------------- |
| 356 | +// Score::spannersOfType |
| 357 | +//--------------------------------------------------------- |
| 358 | + |
| 359 | +QQmlListProperty<Spanner> Score::spannersOfType(int elementType) |
| 360 | +{ |
| 361 | + using mu::engraving::ElementType; |
| 362 | + |
| 363 | + if (elementType == int(ElementType::INVALID)) { |
| 364 | + return spanners(); |
| 365 | + } |
| 366 | + |
| 367 | + if (elementType <= int(ElementType::INVALID) |
| 368 | + || elementType >= int(ElementType::ROOT_ITEM)) { |
| 369 | + return QQmlListProperty<Spanner>(); |
| 370 | + } |
| 371 | + |
| 372 | + auto* context = acquireSpannerContext(true, static_cast<ElementType>(elementType)); |
| 373 | + return QQmlListProperty<Spanner>(this, context, &Score::spannerListCount, &Score::spannerListAt); |
| 374 | +} |
| 375 | + |
265 | 376 | //--------------------------------------------------------- |
266 | 377 | // Score::startCmd |
267 | 378 | //--------------------------------------------------------- |
|
0 commit comments