Skip to content

Commit

Permalink
tmp 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Eism committed Feb 19, 2025
1 parent 2f2df0d commit ec40924
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 94 deletions.
30 changes: 5 additions & 25 deletions src/engraving/dom/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,6 @@ void FBox::add(EngravingItem* e)
FretDiagram* fd = toFretDiagram(e);
fd->setFlag(ElementFlag::MOVABLE, false);
m_fretDiagrams.push_back(fd);
} else if (e->isHarmony()) {
Harmony* harmony = toHarmony(e);
harmony->setFlag(ElementFlag::MOVABLE, false);
harmony->setIsPlayable(false);
m_harmonies.push_back(harmony);
} else {
LOGD("FBox::add: element not allowed");
return;
Expand Down Expand Up @@ -856,15 +851,10 @@ PropertyValue FBox::propertyDefault(Pid propertyId) const

void FBox::init()
{
for (Harmony* harmony : m_harmonies) {
harmony->deleteLater();
}

for (FretDiagram* fretDiagram : m_fretDiagrams) {
fretDiagram->deleteLater();
}

m_harmonies.clear();
m_fretDiagrams.clear();

for (mu::engraving::Segment* segment = score()->firstSegment(mu::engraving::SegmentType::ChordRest); segment;
Expand All @@ -874,28 +864,18 @@ void FBox::init()
continue;
}

Harmony* harmony = nullptr;
FretDiagram* fretDiagram = nullptr;

if (item->isHarmony()) {
harmony = toHarmony(item)->clone();
// todo
continue;
} else if (item->isFretDiagram()) {
fretDiagram = toFretDiagram(item)->clone();
harmony = fretDiagram->harmony()->clone();
}

if (!harmony || !harmony->play()) {
continue;
fretDiagram->harmony()->setAutoplace(false);
fretDiagram->setAutoplace(false);
}

add(harmony);

if (!fretDiagram) {
// todo: generate and call add
m_fretDiagrams.push_back(nullptr);
} else {
add(fretDiagram);
}
add(fretDiagram);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/engraving/dom/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ class FBox : public VBox
bool setProperty(Pid propertyId, const PropertyValue& val) override;
PropertyValue propertyDefault(Pid propertyId) const override;

const std::vector<Harmony*> harmonies() const { return m_harmonies; }
const std::vector<FretDiagram*> fretDiagrams() const { return m_fretDiagrams; }

void init();
Expand All @@ -229,7 +228,6 @@ class FBox : public VBox
private:
void resolveContentRect();

std::vector<Harmony*> m_harmonies;
std::vector<FretDiagram*> m_fretDiagrams;

double m_textScale = 0.0;
Expand Down
12 changes: 11 additions & 1 deletion src/engraving/dom/harmony.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,8 @@ void Harmony::render()
m_fontList.clear();
for (const ChordFont& cf : chordList->fonts) {
Font ff(font());
ff.setPointSizeF(ff.pointSizeF() * cf.mag);
double mag = m_userMag.value_or(cf.mag);
ff.setPointSizeF(ff.pointSizeF() * mag);
if (!(cf.family.isEmpty() || cf.family == "default")) {
ff.setFamily(cf.family, Font::Type::Harmony);
}
Expand Down Expand Up @@ -1891,4 +1892,13 @@ Sid Harmony::getPropertyStyle(Pid pid) const
}
return TextBase::getPropertyStyle(pid);
}

double Harmony::mag() const
{
if (m_userMag.has_value()) {
return m_userMag.value();
}

return EngravingItem::mag();
}
}
5 changes: 5 additions & 0 deletions src/engraving/dom/harmony.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class Harmony final : public TextBase
bool setProperty(Pid propertyId, const PropertyValue& v) override;
PropertyValue propertyDefault(Pid id) const override;

double mag() const override;
void setUserMag(double m) { m_userMag = m; }

//! HACK Temporary hack
bool isDrawEditMode() const { return m_isDrawEditMode; }
void setIsDrawEditMode(bool val) { m_isDrawEditMode = val; }
Expand Down Expand Up @@ -246,6 +249,8 @@ class Harmony final : public TextBase
NoteCaseType m_baseRenderCase = NoteCaseType::AUTO; // case to render

bool m_isDrawEditMode = false;

std::optional<double> m_userMag;
};
} // namespace mu::engraving
#endif
4 changes: 0 additions & 4 deletions src/engraving/dom/scoretree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,6 @@ EngravingObjectList FBox::scanChildren() const
{
EngravingObjectList children;

for (Harmony* harmony : m_harmonies) {
children.push_back(harmony);
}

for (FretDiagram* fretDiagram : m_fretDiagrams) {
children.push_back(fretDiagram);
}
Expand Down
108 changes: 46 additions & 62 deletions src/engraving/rendering/score/tlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,43 +1453,39 @@ void TLayout::layoutFBox(const FBox* item, FBox::LayoutData* ldata, const Layout

ldata->setPos(PointF());

const std::vector<Harmony*> harmonies = item->harmonies();
const std::vector<FretDiagram*> fretDiagrams = item->fretDiagrams();
if (harmonies.empty() || fretDiagrams.empty()) {
if (fretDiagrams.empty()) {
return;
}

const int totalHarmonies = harmonies.size();
const int totalDiagrams = fretDiagrams.size();
double maxFretDiagramHeight = 0.0;
double maxFretDiagramWidth = 0.0;
double maxHarmonyHeight = 0.0;

for (int i = 0; i < totalHarmonies; ++i) {
Harmony* harmony = harmonies[i];
for (int i = 0; i < totalDiagrams; ++i) {
FretDiagram *fretDiagram = fretDiagrams[i];
if (!fretDiagram) {
continue;
}

layoutItem(harmony, const_cast<LayoutContext&>(ctx));
maxHarmonyHeight = std::max(maxHarmonyHeight, harmony->ldata()->harmonyHeight.value());
fretDiagram->setUserMag(item->diagramScale());

if (FretDiagram* fretDiagram = fretDiagrams[i]) {
fretDiagram->setUserMag(item->diagramScale());
Harmony *harmony = fretDiagram->harmony();
harmony->setUserMag(item->textScale());
harmony->render();

layoutItem(fretDiagram, const_cast<LayoutContext&>(ctx));
layoutItem(fretDiagram, const_cast<LayoutContext &>(ctx));

double height = 0.0;
double width = 0.0;
auto shapes = fretDiagram->shape().elements();
for (ShapeElement& shape : shapes) {
height += shape.height();
width += shape.width();
}
double height = fretDiagram->ldata()->bbox().height() + fretDiagram->harmony()->ldata()->harmonyHeight +
ctx.conf().styleMM(Sid::harmonyFretDist);
double width = fretDiagram->ldata()->bbox().width();

maxFretDiagramHeight = std::max(maxFretDiagramHeight, height);
maxFretDiagramWidth = std::max(maxFretDiagramWidth, width);
}
maxFretDiagramHeight = std::max(maxFretDiagramHeight, height);
maxFretDiagramWidth = std::max(maxFretDiagramWidth, width);
}

double cellWidth = maxFretDiagramWidth;
double cellHeight = maxFretDiagramHeight + 4 + maxHarmonyHeight;
double cellHeight = maxFretDiagramHeight;

ldata->cellWidth = cellWidth;
ldata->cellHeight = cellHeight;
Expand All @@ -1500,8 +1496,8 @@ void TLayout::layoutFBox(const FBox* item, FBox::LayoutData* ldata, const Layout
const double rowGap = item->rowGap().val() * spatium;
const double columnGap = item->columnGap().val() * spatium;

const int rows = std::ceil(double(totalHarmonies) / double(chordsPerRow));
const int columns = std::min(totalHarmonies, chordsPerRow);
const int rows = std::ceil(double(totalDiagrams) / double(chordsPerRow));
const int columns = std::min(totalDiagrams, chordsPerRow);

static constexpr double MARGINS = 8.0;
const double totalTableHeight = rows * cellHeight + (rows - 1) * rowGap + MARGINS;
Expand Down Expand Up @@ -1531,35 +1527,26 @@ void TLayout::layoutFBox(const FBox* item, FBox::LayoutData* ldata, const Layout
: alignH == AlignH::RIGHT ? item->width() - totalTableWidth : 0.0;
const double startY = !muse::RealIsNull(topMargin) ? topMargin : -bottomMargin;

for (int i = 0; i < totalHarmonies; ++i) {
for (int i = 0; i < totalDiagrams; ++i) {
int row = i / chordsPerRow;
int col = i % chordsPerRow;

int itemsInRow = std::min(chordsPerRow, totalHarmonies - row * chordsPerRow);
int itemsInRow = std::min(chordsPerRow, totalDiagrams - row * chordsPerRow);
double rowOffsetX = alignH == AlignH::HCENTER
? (totalTableWidth - (itemsInRow * cellWidth + (itemsInRow - 1) * columnGap)) / 2
: alignH == AlignH::RIGHT
? totalTableWidth - (itemsInRow * cellWidth + (itemsInRow - 1) * columnGap) - rightMargin + spatium
: leftMargin + spatium;

Harmony* harmony = harmonies[i];
FretDiagram* fretDiagram = fretDiagrams[i];

const double harmonyHeight = harmony->ldata()->harmonyHeight;
const double fretDiagramWidth = fretDiagram ? maxFretDiagramWidth : 0.0;
const double harmonyWidth = harmony->ldata()->bbox().width();

double x = startX + rowOffsetX + col * (cellWidth + columnGap);
double y = startY + row * (cellHeight + rowGap) + harmonyHeight;

double harmonyX = x + (fretDiagramWidth - harmonyWidth) / 2;
double harmonyY = y;

harmony->mutldata()->setPos(PointF(harmonyX, harmonyY));
double y = startY + row * (cellHeight + rowGap);

if (fretDiagram) {
double fretDiagramX = x;
double fretDiagramY = harmonyY + harmonyHeight + 4;
double fretDiagramY = y + fretDiagram->harmony()->ldata()->harmonyHeight + ctx.conf().styleMM(Sid::harmonyFretDist);
LOGD() << "============= harmony height: " << fretDiagram->harmony()->ldata()->harmonyHeight;
fretDiagram->mutldata()->setPos(PointF(fretDiagramX, fretDiagramY));
}
}
Expand Down Expand Up @@ -2742,41 +2729,38 @@ void TLayout::layoutFretDiagram(const FretDiagram* item, FretDiagram::LayoutData

ldata->setShape(shape);

if (!item->explicitParent()->isSegment()) {
ldata->setPos(PointF());
return;
}

// We need to get the width of the notehead/rest in order to position the fret diagram correctly
Segment* pSeg = item->segment();
double noteheadWidth = 0;
if (pSeg->isChordRestType()) {
staff_idx_t idx = item->staff()->idx();
for (EngravingItem* e = pSeg->firstElementOfSegment(idx); e; e = pSeg->nextElementOfSegment(e, idx)) {
if (e->isRest()) {
const Rest* r = toRest(e);
LD_CONDITION(r->ldata()->sym.has_value());
noteheadWidth = item->symWidth(r->ldata()->sym());
break;
} else if (e->isNote()) {
Note* n = toNote(e);
noteheadWidth = n->headWidth();
break;
if (item->explicitParent()->isSegment()) {
// We need to get the width of the notehead/rest in order to position the fret diagram correctly
Segment* pSeg = item->segment();
double noteheadWidth = 0;
if (pSeg->isChordRestType()) {
staff_idx_t idx = item->staff()->idx();
for (EngravingItem* e = pSeg->firstElementOfSegment(idx); e; e = pSeg->nextElementOfSegment(e, idx)) {
if (e->isRest()) {
const Rest* r = toRest(e);
LD_CONDITION(r->ldata()->sym.has_value());
noteheadWidth = item->symWidth(r->ldata()->sym());
break;
} else if (e->isNote()) {
Note* n = toNote(e);
noteheadWidth = n->headWidth();
break;
}
}
}
}

ldata->setPos((noteheadWidth - item->mainWidth()) / 2, -(ldata->shape().bottom() + item->styleP(Sid::fretY)));
ldata->setPos((noteheadWidth - item->mainWidth()) / 2, -(ldata->shape().bottom() + item->styleP(Sid::fretY)));
}

if (item->autoplace()) {
const Segment* s = toSegment(item->explicitParent());
const Measure* m = s->measure();
LD_CONDITION(ldata->isSetPos());
LD_CONDITION(m->ldata()->isSetPos());
LD_CONDITION(s->ldata()->isSetPos());
}

Autoplace::autoplaceSegmentElement(item, ldata);
Autoplace::autoplaceSegmentElement(item, ldata);
}

Harmony* harmony = item->harmony();
if (harmony) {
Expand Down

0 comments on commit ec40924

Please sign in to comment.