Skip to content

Commit

Permalink
[musicxml] Replaced QMap to std::map
Browse files Browse the repository at this point in the history
Backport of musescore#21205, part 2
  • Loading branch information
igorkorsukov authored and Jojo-Schmitz committed Sep 5, 2024
1 parent 56b39ae commit 01150a8
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 408 deletions.
75 changes: 40 additions & 35 deletions importexport/musicxml/exportxml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

#include "config.h"

#include "global/containers.h"

#include "libmscore/accidental.h"
#include "libmscore/arpeggio.h"
#include "libmscore/articulation.h"
Expand Down Expand Up @@ -130,7 +132,7 @@ namespace Ms {
// typedefs
//---------------------------------------------------------

typedef QMap<int, const FiguredBass*> FigBassMap;
typedef std::map<int, const FiguredBass*> FigBassMap;

//---------------------------------------------------------
// attributes -- prints <attributes> tag when necessary
Expand Down Expand Up @@ -313,7 +315,7 @@ struct MeasurePrintContext final
//---------------------------------------------------------

typedef QHash<const ChordRest* const, const Trill*> TrillHash;
typedef QMap<const Instrument*, int> MxmlInstrumentMap;
typedef std::map<const Instrument*, int> MxmlInstrumentMap;

class ExportMusicXml {
Score* _score;
Expand Down Expand Up @@ -2306,12 +2308,12 @@ void ExportMusicXml::keysig(const KeySig* ks, ClefType ct, int staff, bool visib
// are in insertion order -> sorting required

// first put the KeySyms in a map
QMap<qreal, KeySym> map;
std::map<qreal, KeySym> map;
for (const KeySym& ksym : keysyms) {
map.insert(ksym.spos.x(), ksym);
map.insert({ ksym.spos.x(), ksym});
}
// then write them (automatically sorted on key)
for (const KeySym& ksym : map) {
for (const KeySym& ksym : mu::values(map)) {
int line = static_cast<int>(round(2 * ksym.spos.y()));
int step = (po - line) % 7;
_xml.tag("key-step", QString(QChar(table2[step])));
Expand Down Expand Up @@ -3891,7 +3893,7 @@ void ExportMusicXml::chord(Chord* chord, int staff, const std::vector<Lyrics*>*
{
Part* part = chord->score()->staff(chord->track() / VOICES)->part();
int partNr = _score->parts().indexOf(part);
int instNr = instrMap.value(part->instrument(_tick), -1);
int instNr = mu::value(instrMap, part->instrument(_tick), -1);
/*
qDebug("chord() %p parent %p isgrace %d #gracenotes %d graceidx %d",
chord, chord->parent(), chord->isGrace(), chord->graceNotes().size(), chord->graceIndex());
Expand Down Expand Up @@ -5971,10 +5973,10 @@ static void figuredBass(XmlWriter& xml, int strack, int etrack, int track, const
if (extend) {
//qDebug("figuredbass() extend to %d + %d = %d",
// cr->tick(), fb->ticks(), cr->tick() + fb->ticks());
fbMap.insert(strack, fb);
fbMap.insert({ strack, fb });
}
else
fbMap.remove(strack);
mu::remove(fbMap, strack);
const Fraction crEndTick = cr->tick() + cr->actualTicks();
const Fraction fbEndTick = fb->segment()->tick() + fb->ticks();
const bool writeDuration = fb->ticks() < cr->actualTicks();
Expand All @@ -5995,8 +5997,8 @@ static void figuredBass(XmlWriter& xml, int strack, int etrack, int track, const
}
}
// check for extend pending
if (fbMap.contains(strack)) {
const FiguredBass* fb = fbMap.value(strack);
if (mu::contains(fbMap, strack)) {
const FiguredBass* fb = fbMap.at(strack);
Fraction crEndTick = cr->tick() + cr->actualTicks();
Fraction fbEndTick = fb->segment()->tick() + fb->ticks();
bool writeDuration = fb->ticks() < cr->actualTicks();
Expand All @@ -6006,7 +6008,7 @@ static void figuredBass(XmlWriter& xml, int strack, int etrack, int track, const
}
if (fbEndTick <= crEndTick) {
//qDebug("figuredbass() at tick %d extend done", cr->tick() + cr->actualTicks());
fbMap.remove(strack);
mu::remove(fbMap, strack);
}
}
}
Expand Down Expand Up @@ -6140,7 +6142,7 @@ void ExportMusicXml::keysigTimesig(const Measure* m, const Part* p)
//qDebug("keysigTimesig m %p strack %d etrack %d", m, strack, etrack);

// search all staves for non-generated key signatures
QMap<int, KeySig*> keysigs; // map staff to key signature
std::map<int, KeySig*> keysigs; // map staff to key signature
for (Segment* seg = m->first(); seg; seg = seg->next()) {
if (seg->tick() > m->tick())
break;
Expand All @@ -6160,31 +6162,31 @@ void ExportMusicXml::keysigTimesig(const Measure* m, const Part* p)
//ClefType ct = rest->staff()->clef(rest->tick());

// write the key signatues
if (!keysigs.isEmpty()) {
if (!keysigs.empty()) {
// determine if all staves have a keysig and all keysigs are identical
// in that case a single <key> is written, without number=... attribute
int nstaves = p->nstaves();
bool singleKey = true;
// check if all staves have a keysig
for (int i = 0; i < nstaves; i++)
if (!keysigs.contains(i))
if (!mu::contains(keysigs, i))
singleKey = false;
// check if all keysigs are identical
if (singleKey)
for (int i = 1; i < nstaves; i++)
if (!(keysigs.value(i)->key() == keysigs.value(0)->key()))
if (!(keysigs.at(i)->key() == keysigs.at(0)->key()))
singleKey = false;

// write the keysigs
//qDebug(" singleKey %d", singleKey);
if (singleKey) {
// keysig applies to all staves
keysig(keysigs.value(0), p->staff(0)->clef(m->tick()), 0, keysigs.value(0)->visible());
keysig(keysigs.at(0), p->staff(0)->clef(m->tick()), 0, keysigs.at(0)->visible());
}
else {
// staff-specific keysigs
for (int st : keysigs.keys())
keysig(keysigs.value(st), p->staff(st)->clef(m->tick()), st + 1, keysigs.value(st)->visible());
for (int st : mu::keys(keysigs))
keysig(keysigs.at(st), p->staff(st)->clef(m->tick()), st + 1, keysigs.at(st)->visible());
}
}
else {
Expand All @@ -6200,7 +6202,7 @@ void ExportMusicXml::keysigTimesig(const Measure* m, const Part* p)
}

// search all staves for non-generated time signatures
QMap<int, TimeSig*> timesigs; // map staff to time signature
std::map<int, TimeSig*> timesigs; // map staff to time signature
for (Segment* seg = m->first(); seg; seg = seg->next()) {
if (seg->tick() > m->tick())
break;
Expand All @@ -6226,7 +6228,7 @@ void ExportMusicXml::keysigTimesig(const Measure* m, const Part* p)
bool singleTime = true;
// check if all staves have a keysig
for (size_t i = 0; i < nstaves; i++) {
if (!timesigs.contains(static_cast<int>(i)))
if (!mu::contains(timesigs, static_cast<int>(i)))
singleTime = false;
}
// check if all timesigs are identical
Expand All @@ -6245,7 +6247,7 @@ void ExportMusicXml::keysigTimesig(const Measure* m, const Part* p)
}
else {
// staff-specific timesig
for (int st : timesigs.keys())
for (int st : mu::keys(timesigs))
timesig(timesigs[st], st + 1);
}
}
Expand Down Expand Up @@ -6368,16 +6370,16 @@ static void initInstrMap(MxmlInstrumentMap& im, const InstrumentList* il, const
im.clear();
for (auto i = il->begin(); i != il->end(); ++i) {
const Instrument* pinstr = i->second;
if (!im.contains(pinstr))
im.insert(pinstr, im.size());
if (!mu::contains(im, pinstr))
im.insert({ pinstr, static_cast<int>(im.size()) });
}
}

//---------------------------------------------------------
// initReverseInstrMap
//---------------------------------------------------------

typedef QMap<int, const Instrument*> MxmlReverseInstrumentMap;
typedef std::map<int, const Instrument*> MxmlReverseInstrumentMap;

/**
Initialize the number t Instrument* map for a Part
Expand All @@ -6387,9 +6389,9 @@ typedef QMap<int, const Instrument*> MxmlReverseInstrumentMap;
static void initReverseInstrMap(MxmlReverseInstrumentMap& rim, const MxmlInstrumentMap& im)
{
rim.clear();
for (const Instrument* i : im.keys()) {
int instNr = im.value(i);
rim.insert(instNr, i);
for (const Instrument* i : mu::keys(im)) {
int instNr = im.at(i);
rim.insert({ instNr, i });
}
}

Expand Down Expand Up @@ -6839,19 +6841,22 @@ static void partList(XmlWriter& xml, Score* score, MxmlInstrumentMap& instrMap)
else {
MxmlReverseInstrumentMap rim;
initReverseInstrMap(rim, instrMap);
for (int instNr : rim.keys()) {
scoreInstrument(xml, idx + 1, instNr + 1, MScoreTextToMXML::toPlainText(rim.value(instNr)->trackName()), rim.value(instNr));
for (int instNr : mu::keys(rim)) {
const Instrument* instr = rim.at(instNr);
scoreInstrument(xml, idx + 1, instNr + 1,
MScoreTextToMXML::toPlainText(instr->trackName()),
instr);
}
for (auto ii = rim.constBegin(); ii != rim.constEnd(); ii++) {
int instNr = ii.key();
for (auto ii = rim.cbegin(); ii != rim.cend(); ii++) {
int instNr = ii->first;
int midiPort = part->midiPort() + 1;
if (ii.value()->channel().size() > 0)
midiPort = score->masterScore()->midiMapping(ii.value()->channel(0)->channel())->port() + 1;
if (ii->second->channel().size() > 0)
midiPort = score->masterScore()->midiMapping(ii->second->channel(0)->channel())->port() + 1;
if (midiPort >= 1 && midiPort <= 16)
xml.tag(QString("midi-device %1 port=\"%2\"").arg(instrId(idx+1, instNr + 1)).arg(midiPort), "");
else
xml.tag(QString("midi-device %1").arg(instrId(idx+1, instNr + 1)), "");
midiInstrument(xml, idx + 1, instNr + 1, rim.value(instNr), score);
midiInstrument(xml, idx + 1, instNr + 1, rim.at(instNr), score);
}
}

Expand Down Expand Up @@ -7391,7 +7396,7 @@ void ExportMusicXml::writeMeasure(const Measure* const m,
if (staves > 1)
_xml.tag("staves", staves);
if (instrMap.size() > 1)
_xml.tag("instruments", instrMap.size());
_xml.tag("instruments", static_cast<int>(instrMap.size()));
}

// make sure clefs at end of measure get exported at start of next measure
Expand Down
Loading

0 comments on commit 01150a8

Please sign in to comment.