diff --git a/common/decklist.cpp b/common/decklist.cpp index 708912a29..7916759a5 100644 --- a/common/decklist.cpp +++ b/common/decklist.cpp @@ -361,15 +361,14 @@ QVector> InnerDecklistNode::sort(Qt::SortOrder order) return result; } -DeckList::DeckList() +DeckList::DeckList() : QObject(), deckHashDirty(true) { root = new InnerDecklistNode; } // TODO: https://qt-project.org/doc/qt-4.8/qobject.html#no-copy-constructor-or-assignment-operator DeckList::DeckList(const DeckList &other) - : QObject(), name(other.name), comments(other.comments), bannerCard(other.bannerCard), deckHash(other.deckHash), - lastLoadedTimestamp(other.lastLoadedTimestamp), tags(other.tags) + : QObject(), name(other.name), comments(other.comments), bannerCard(other.bannerCard), deckHash(other.deckHash), deckHashDirty(other.deckHashDirty), lastLoadedTimestamp(other.lastLoadedTimestamp), tags(other.tags) { root = new InnerDecklistNode(other.getRoot()); @@ -378,10 +377,9 @@ DeckList::DeckList(const DeckList &other) spIterator.next(); sideboardPlans.insert(spIterator.key(), new SideboardPlan(spIterator.key(), spIterator.value()->getMoveList())); } - updateDeckHash(); } -DeckList::DeckList(const QString &nativeString) +DeckList::DeckList(const QString &nativeString) : QObject() { root = new InnerDecklistNode; loadFromString_Native(nativeString); @@ -507,7 +505,8 @@ bool DeckList::loadFromXml(QXmlStreamReader *xml) } } } - updateDeckHash(); + deckHashDirty = true; + emit deckHashChanged(); if (xml->error()) { qDebug() << "Error loading deck from xml: " << xml->errorString(); return false; @@ -734,7 +733,8 @@ bool DeckList::loadFromStream_Plain(QTextStream &in) new DecklistCardNode(cardName, amount, getZoneObjFromName(zoneName), setCode, collectorNumber); } - updateDeckHash(); + deckHashDirty = true; + emit deckHashChanged(); return true; } @@ -809,6 +809,7 @@ void DeckList::cleanList() setComments(); setTags(); deckHash = QString(); + deckHashDirty = false; emit deckHashChanged(); } @@ -881,7 +882,8 @@ DecklistCardNode *DeckList::addCard(const QString &cardName, } auto *node = new DecklistCardNode(cardName, 1, zoneNode, cardSetName, cardSetCollectorNumber, cardProviderId); - updateDeckHash(); + deckHashDirty = true; + emit deckHashChanged(); return node; } @@ -907,7 +909,8 @@ bool DeckList::deleteNode(AbstractDecklistNode *node, InnerDecklistNode *rootNod } if (updateHash) { - updateDeckHash(); + deckHashDirty = true; + emit deckHashChanged(); } return true; @@ -918,7 +921,8 @@ bool DeckList::deleteNode(AbstractDecklistNode *node, InnerDecklistNode *rootNod if (inner) { if (deleteNode(node, inner)) { if (updateHash) { - updateDeckHash(); + deckHashDirty = true; + emit deckHashChanged(); } return true; @@ -929,6 +933,14 @@ bool DeckList::deleteNode(AbstractDecklistNode *node, InnerDecklistNode *rootNod return false; } +QString DeckList::getDeckHash() +{ + if (deckHashDirty) { + updateDeckHash(); + } + return deckHash; +} + void DeckList::updateDeckHash() { QStringList cardList; @@ -955,7 +967,7 @@ void DeckList::updateDeckHash() (((quint64)(unsigned char)deckHashArray[1]) << 24) + (((quint64)(unsigned char)deckHashArray[2] << 16)) + (((quint64)(unsigned char)deckHashArray[3]) << 8) + (quint64)(unsigned char)deckHashArray[4]; - deckHash = QString::number(number, 32).rightJustified(8, '0'); - emit deckHashChanged(); + deckHash = QString::number(number, 32).rightJustified(8, '0'); + deckHashDirty = false; } diff --git a/common/decklist.h b/common/decklist.h index 9c291cfba..0399b88cf 100644 --- a/common/decklist.h +++ b/common/decklist.h @@ -253,6 +253,7 @@ private: QString name, comments; QPair bannerCard; QString deckHash; + bool deckHashDirty; QString lastLoadedTimestamp; QStringList tags; QMap sideboardPlans; @@ -363,10 +364,7 @@ public: int getSideboardSize() const; - QString getDeckHash() const - { - return deckHash; - } + QString getDeckHash(); void updateDeckHash(); InnerDecklistNode *getRoot() const @@ -399,4 +397,4 @@ public: } }; -#endif \ No newline at end of file +#endif