From f367f8a6c006c4da31fa877249042d58b3126be5 Mon Sep 17 00:00:00 2001 From: ws Date: Sat, 15 Mar 2014 12:15:49 +0100 Subject: [PATCH] fix #24877 --- libmscore/chordlist.cpp | 49 +++++++++++------------------------- libmscore/chordlist.h | 10 +++----- libmscore/harmony.cpp | 55 ++++++++++++++++++++--------------------- libmscore/style.cpp | 4 ++- libmscore/style.h | 1 + mscore/harmonyedit.cpp | 10 ++++---- 6 files changed, 54 insertions(+), 75 deletions(-) diff --git a/libmscore/chordlist.cpp b/libmscore/chordlist.cpp index 43e45f3eac9e7..1d9bd0edb3ef6 100644 --- a/libmscore/chordlist.cpp +++ b/libmscore/chordlist.cpp @@ -1428,10 +1428,11 @@ void ParsedChord::addToken(QString s, ChordTokenClass tc) // a private id is assigned for id = 0 //--------------------------------------------------------- -ChordDescription::ChordDescription(int i, ChordList* cl) +ChordDescription::ChordDescription(int i) { if (!i) - i = --(cl->privateID); +// i = --(cl->privateID); + i = -- ChordList::privateID; id = i; generated = false; renderListGenerated = false; @@ -1444,9 +1445,9 @@ ChordDescription::ChordDescription(int i, ChordList* cl) // a private id is always assigned //--------------------------------------------------------- -ChordDescription::ChordDescription(const QString& name, ChordList* cl) +ChordDescription::ChordDescription(const QString& name) { - id = --(cl->privateID); + id = -- ChordList::privateID; generated = true; names.append(name); renderListGenerated = false; @@ -1548,25 +1549,6 @@ void ChordDescription::write(Xml& xml) const int ChordList::privateID = -1000; -ChordList::ChordList() - { - } - -//--------------------------------------------------------- -// ~ChordList -//--------------------------------------------------------- - -ChordList::~ChordList() - { - if (isDetached()) { - QMapIterator i(*this); - while(i.hasNext()) { - i.next(); - delete i.value(); - } - } - } - //--------------------------------------------------------- // read //--------------------------------------------------------- @@ -1620,21 +1602,18 @@ void ChordList::read(XmlReader& e) // if no id attribute (id == 0), then assign it a private id // user chords that match these ChordDescriptions will be treated as normal recognized chords // except that the id will not be written to the score file - ChordDescription* cd = 0; - if (id) - cd = take(id); - if (!cd) - cd = new ChordDescription(id, this); + ChordDescription cd = (id && contains(id)) ? take(id) : ChordDescription(id); + // record updated id - id = cd->id; + id = cd.id; // read rest of description - cd->read(e); + cd.read(e); // restore updated id - cd->id = id; + cd.id = id; // throw away previously parsed chords - cd->parsedChords.clear(); + cd.parsedChords.clear(); // generate any missing info (including new parsed chords) - cd->complete(0,this); + cd.complete(0,this); // add to list insert(id, cd); } @@ -1674,8 +1653,8 @@ void ChordList::write(Xml& xml) const writeRenderList(xml, &renderListRoot, "renderRoot"); if (!renderListBase.isEmpty()) writeRenderList(xml, &renderListBase, "renderBase"); - foreach(ChordDescription* d, *this) - d->write(xml); + foreach(const ChordDescription& d, *this) + d.write(xml); } //--------------------------------------------------------- diff --git a/libmscore/chordlist.h b/libmscore/chordlist.h index ff28d4c915726..1a1eaff492eb3 100644 --- a/libmscore/chordlist.h +++ b/libmscore/chordlist.h @@ -188,8 +188,9 @@ struct ChordDescription { QString _quality; public: - ChordDescription(int, ChordList*); - ChordDescription(const QString&, ChordList*); + ChordDescription() {} + ChordDescription(int); + ChordDescription(const QString&); QString quality() const { return _quality; } void complete(ParsedChord* pc, const ChordList*); void read(XmlReader&); @@ -223,7 +224,7 @@ struct ChordFont { // ChordList //--------------------------------------------------------- -class ChordList : public QMap { +class ChordList : public QMap { QHash symbols; public: @@ -233,9 +234,6 @@ class ChordList : public QMap { QList chordTokenList; static int privateID; - ChordList(); - - virtual ~ChordList(); void write(Xml& xml) const; void read(XmlReader&); bool read(const QString&); diff --git a/libmscore/harmony.cpp b/libmscore/harmony.cpp index e29bf02b711f5..5fbbd513a3f73 100644 --- a/libmscore/harmony.cpp +++ b/libmscore/harmony.cpp @@ -49,10 +49,10 @@ QString Harmony::harmonyName() hc.add(_degreeList); // try to find the chord in chordList const ChordDescription* newExtension = 0; - ChordList* cl = score()->style()->chordList(); - foreach(const ChordDescription* cd, *cl) { - if (cd->chord == hc && !cd->names.isEmpty()) { - newExtension = cd; + const ChordList* cl = score()->style()->chordList(); + for (const ChordDescription& cd : *cl) { + if (cd.chord == hc && !cd.names.isEmpty()) { + newExtension = &cd; break; } } @@ -117,11 +117,11 @@ void Harmony::resolveDegreeList() // _descr->chord.print(); // try to find the chord in chordList - ChordList* cl = score()->style()->chordList(); - foreach(const ChordDescription* cd, *cl) { - if ((cd->chord == hc) && !cd->names.isEmpty()) { -qDebug("ResolveDegreeList: found in table as %s", qPrintable(cd->names.front())); - _id = cd->id; + const ChordList* cl = score()->style()->chordList(); + foreach(const ChordDescription& cd, *cl) { + if ((cd.chord == hc) && !cd.names.isEmpty()) { +qDebug("ResolveDegreeList: found in table as %s", qPrintable(cd.names.front())); + _id = cd.id; _degreeList.clear(); return; } @@ -679,14 +679,14 @@ const ChordDescription* Harmony::fromXml(const QString& kind, const QListstyle()->chordList(); - foreach(const ChordDescription* cd, *cl) { - QString k = cd->xmlKind; + const ChordList* cl = score()->style()->chordList(); + foreach(const ChordDescription& cd, *cl) { + QString k = cd.xmlKind; QString lowerCaseK = k.toLower(); // required for xmlKind Tristan - QStringList d = cd->xmlDegrees; + QStringList d = cd.xmlDegrees; if ((lowerCaseKind == lowerCaseK) && (d == degrees)) { // qDebug("harmony found in db: %s %s -> %d", qPrintable(kind), qPrintable(degrees), cd->id); - return cd; + return &cd; } } return 0; @@ -701,10 +701,10 @@ const ChordDescription* Harmony::fromXml(const QString& kind, const QListstyle()->chordList(); - foreach(const ChordDescription* cd, *cl) { - if (lowerCaseKind == cd->xmlKind) - return cd; + const ChordList* cl = score()->style()->chordList(); + foreach(const ChordDescription& cd, *cl) { + if (lowerCaseKind == cd.xmlKind) + return &cd; } return 0; } @@ -748,14 +748,14 @@ const ChordDescription* Harmony::descr(const QString& name, const ParsedChord* p const ChordList* cl = score()->style()->chordList(); const ChordDescription* match = 0; if (cl) { - foreach (const ChordDescription* cd, *cl) { - foreach (const QString& s, cd->names) { + foreach (const ChordDescription& cd, *cl) { + foreach (const QString& s, cd.names) { if (s == name) - return cd; + return &cd; else if (pc) { - foreach (const ParsedChord& sParsed, cd->parsedChords) { + foreach (const ParsedChord& sParsed, cd.parsedChords) { if (sParsed == *pc) - match = cd; + match = &cd; } } } @@ -812,13 +812,12 @@ const ChordDescription* Harmony::getDescription(const QString& name, const Parse const ChordDescription* Harmony::generateDescription() { ChordList* cl = score()->style()->chordList(); - ChordDescription* cd = new ChordDescription(_textName, cl); - cd->complete(_parsedForm, cl); + ChordDescription cd(_textName); + cd.complete(_parsedForm, cl); // remove parsed chord from description // so we will only match it literally in the future - cd->parsedChords.clear(); - cl->insert(cd->id, cd); - return cd; + cd.parsedChords.clear(); + return &*cl->insert(cd.id, cd); } //--------------------------------------------------------- diff --git a/libmscore/style.cpp b/libmscore/style.cpp index 93d64c480d473..bd4f195beaffe 100644 --- a/libmscore/style.cpp +++ b/libmscore/style.cpp @@ -1132,7 +1132,9 @@ void StyleData::save(Xml& xml, bool optimize) const const ChordDescription* StyleData::chordDescription(int id) const { - return _chordList.value(id); + if (!_chordList.contains(id)) + return 0; + return &*_chordList.find(id); } //--------------------------------------------------------- diff --git a/libmscore/style.h b/libmscore/style.h index 3339b750b45b2..698f68202e30a 100644 --- a/libmscore/style.h +++ b/libmscore/style.h @@ -367,6 +367,7 @@ class MStyle { bool isDefault(StyleIdx idx) const; const ChordDescription* chordDescription(int id) const; ChordList* chordList(); + void setChordList(ChordList*, bool custom = true); // Style gets ownership of ChordList const TextStyle& textStyle(int) const; diff --git a/mscore/harmonyedit.cpp b/mscore/harmonyedit.cpp index 897222d0ff880..90a7dbebbaeb0 100644 --- a/mscore/harmonyedit.cpp +++ b/mscore/harmonyedit.cpp @@ -133,12 +133,12 @@ void ChordStyleEditor::loadChordDescriptionFile(const QString& s) void ChordStyleEditor::setChordList(ChordList* cl) { harmonyList->clear(); - foreach (ChordDescription* d, *cl) { + foreach (const ChordDescription& d, *cl) { QTreeWidgetItem* item = new QTreeWidgetItem; - item->setData(0, Qt::UserRole, QVariant::fromValue(d)); - item->setText(0, QString("%1").arg(d->id)); - if (!d->names.isEmpty()) - item->setText(1, QString("%1").arg(d->names.front())); + item->setData(0, Qt::UserRole, QVariant::fromValue((void*)&d)); + item->setText(0, QString("%1").arg(d.id)); + if (!d.names.isEmpty()) + item->setText(1, QString("%1").arg(d.names.front())); harmonyList->addTopLevelItem(item); } delete chordList;