From 621a47de9cc311bdb5d56c9bf3df980210af6010 Mon Sep 17 00:00:00 2001 From: Mawu3n4 Date: Wed, 6 Aug 2014 17:54:47 +0200 Subject: [PATCH 1/6] add: find token cards by prefix --- cockatrice/src/carddatabase.cpp | 95 ++++++++++++++++----------------- cockatrice/src/carddatabase.h | 8 ++- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index b3d4bca9..c1a4ab22 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -13,7 +13,6 @@ #include #include #include -#include const int CardDatabase::versionNeeded = 3; @@ -125,38 +124,29 @@ void PictureLoader::processLoadQueue() } PictureToLoad ptl = loadQueue.takeFirst(); mutex.unlock(); - - //The list of paths to the folders in which to search for images - QList picsPaths = QList() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName() + ".full" - << _picsPath + "/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full" - << _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full"; + QString correctedName = ptl.getCard()->getCorrectedName(); + QString picsPath = _picsPath; + QString setName = ptl.getSetName(); QImage image; - QImageReader imgReader; - imgReader.setDecideFormatFromContent(true); - bool found = false; - - //Iterates through the list of paths, searching for images with the desired name with any QImageReader-supported extension - for (int i = 0; i < picsPaths.length() && !found; i ++) { - imgReader.setFileName(picsPaths.at(i)); - if (imgReader.read(&image)) { - emit imageLoaded(ptl.getCard(), image); - found = true; - } + if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("CUSTOM").arg(correctedName))) { + if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg(setName).arg(correctedName))) + //if (!image.load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(setName).arg(correctedName).arg(1))) + if (!image.load(QString("%1/%2/%3/%4.full.jpg").arg(picsPath).arg("downloadedPics").arg(setName).arg(correctedName))) { + if (picDownload) { + cardsToDownload.append(ptl); + if (!downloadRunning) + startNextPicDownload(); + } else { + if (ptl.nextSet()) + loadQueue.prepend(ptl); + else + emit imageLoaded(ptl.getCard(), QImage()); + } + } } - if (!found) { - if (picDownload) { - cardsToDownload.append(ptl); - if (!downloadRunning) - startNextPicDownload(); - } else { - if (ptl.nextSet()) - loadQueue.prepend(ptl); - else - emit imageLoaded(ptl.getCard(), QImage()); - } - } + emit imageLoaded(ptl.getCard(), image); } } @@ -218,27 +208,25 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply) qDebug() << "Download failed:" << reply->errorString(); } - const QByteArray &picData = reply->peek(reply->size()); //peek is used to keep the data in the buffer for use by QImageReader + const QByteArray &picData = reply->readAll(); QImage testImage; - - QImageReader imgReader; - imgReader.setDecideFormatFromContent(true); - imgReader.setDevice(reply); - QString extension = "." + imgReader.format(); //the format is determined prior to reading the QImageReader data into a QImage object, as that wipes the QImageReader buffer - if (extension == ".jpeg") - extension = ".jpg"; - - if (imgReader.read(&testImage)) { - if (!QDir().mkpath(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())) { - qDebug() << picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + " could not be created."; - return; + if (testImage.loadFromData(picData)) { + if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) { + QDir dir(picsPath); + if (!dir.exists()) + return; + dir.mkdir("downloadedPics"); + } + if (!QDir(QString(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())).exists()) { + QDir dir(QString(picsPath + "/downloadedPics")); + dir.mkdir(cardBeingDownloaded.getSetName()); } QString suffix; if (!cardBeingDownloaded.getStripped()) suffix = ".full"; - QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension); + QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + ".jpg"); if (!newPic.open(QIODevice::WriteOnly)) return; newPic.write(picData); @@ -450,7 +438,7 @@ void CardInfo::updatePixmapCache() qDebug() << "Updating pixmap cache for" << name; clearPixmapCache(); loadPixmap(); - + emit pixmapUpdated(); } @@ -555,7 +543,7 @@ CardDatabase::~CardDatabase() { clear(); delete noCard; - + pictureLoader->deleteLater(); pictureLoaderThread->wait(); delete pictureLoaderThread; @@ -569,7 +557,7 @@ void CardDatabase::clear() delete setIt.value(); } sets.clear(); - + QHashIterator i(cards); while (i.hasNext()) { i.next(); @@ -717,11 +705,22 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) } } +CardInfo *CardNameMap::findByPrefix(const std::string &prefix) { + for (CardNameMap::iterator it = this->begin(); it != this->end(); ++it) { + auto std::mismatch(prefix.begin(), prefix.end(), it.key().toStdString().begin()) + if (auto.first == prefix.end()) + return it.value(); + } + return NULL; +} + CardInfo *CardDatabase::getCardFromMap(CardNameMap &cardMap, const QString &cardName, bool createIfNotFound) { + CardInfo *foundCard; + if (cardName.isEmpty()) return noCard; - else if (cardMap.contains(cardName)) - return cardMap.value(cardName); + else if ((foundCard = cardMap.findByPrefix(cardName.toStdString()))) + return foundCard; else if (createIfNotFound) { CardInfo *newCard = new CardInfo(this, cardName, true); newCard->addToSet(getSet("TK")); diff --git a/cockatrice/src/carddatabase.h b/cockatrice/src/carddatabase.h index bb151e7d..dc961d96 100644 --- a/cockatrice/src/carddatabase.h +++ b/cockatrice/src/carddatabase.h @@ -182,7 +182,13 @@ signals: enum LoadStatus { Ok, VersionTooOld, Invalid, NotLoaded, FileError, NoCards }; -typedef QHash CardNameMap; + +class CardNameMap: public QHash +{ + public: + CardInfo *findByPrefix(const std::string &prefix); +}; + typedef QHash SetNameMap; class CardDatabase : public QObject { From 7d190ddaf7d6dbc3e572910493a9a8921a67bae0 Mon Sep 17 00:00:00 2001 From: Mawu3n4 Date: Wed, 6 Aug 2014 18:01:47 +0200 Subject: [PATCH 2/6] fix: pushed old version --- cockatrice/src/carddatabase.cpp | 74 +++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index c1a4ab22..80c48030 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -13,6 +13,7 @@ #include #include #include +#include const int CardDatabase::versionNeeded = 3; @@ -124,29 +125,38 @@ void PictureLoader::processLoadQueue() } PictureToLoad ptl = loadQueue.takeFirst(); mutex.unlock(); - QString correctedName = ptl.getCard()->getCorrectedName(); - QString picsPath = _picsPath; - QString setName = ptl.getSetName(); + + //The list of paths to the folders in which to search for images + QList picsPaths = QList() << _picsPath + "/CUSTOM/" + ptl.getCard()->getCorrectedName() + ".full" + << _picsPath + "/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full" + << _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full"; QImage image; - if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg("CUSTOM").arg(correctedName))) { - if (!image.load(QString("%1/%2/%3.full.jpg").arg(picsPath).arg(setName).arg(correctedName))) - //if (!image.load(QString("%1/%2/%3%4.full.jpg").arg(picsPath).arg(setName).arg(correctedName).arg(1))) - if (!image.load(QString("%1/%2/%3/%4.full.jpg").arg(picsPath).arg("downloadedPics").arg(setName).arg(correctedName))) { - if (picDownload) { - cardsToDownload.append(ptl); - if (!downloadRunning) - startNextPicDownload(); - } else { - if (ptl.nextSet()) - loadQueue.prepend(ptl); - else - emit imageLoaded(ptl.getCard(), QImage()); - } - } + QImageReader imgReader; + imgReader.setDecideFormatFromContent(true); + bool found = false; + + //Iterates through the list of paths, searching for images with the desired name with any QImageReader-supported extension + for (int i = 0; i < picsPaths.length() && !found; i ++) { + imgReader.setFileName(picsPaths.at(i)); + if (imgReader.read(&image)) { + emit imageLoaded(ptl.getCard(), image); + found = true; + } } - emit imageLoaded(ptl.getCard(), image); + if (!found) { + if (picDownload) { + cardsToDownload.append(ptl); + if (!downloadRunning) + startNextPicDownload(); + } else { + if (ptl.nextSet()) + loadQueue.prepend(ptl); + else + emit imageLoaded(ptl.getCard(), QImage()); + } + } } } @@ -208,25 +218,27 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply) qDebug() << "Download failed:" << reply->errorString(); } - const QByteArray &picData = reply->readAll(); + const QByteArray &picData = reply->peek(reply->size()); //peek is used to keep the data in the buffer for use by QImageReader QImage testImage; - if (testImage.loadFromData(picData)) { - if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) { - QDir dir(picsPath); - if (!dir.exists()) - return; - dir.mkdir("downloadedPics"); - } - if (!QDir(QString(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())).exists()) { - QDir dir(QString(picsPath + "/downloadedPics")); - dir.mkdir(cardBeingDownloaded.getSetName()); + + QImageReader imgReader; + imgReader.setDecideFormatFromContent(true); + imgReader.setDevice(reply); + QString extension = "." + imgReader.format(); //the format is determined prior to reading the QImageReader data into a QImage object, as that wipes the QImageReader buffer + if (extension == ".jpeg") + extension = ".jpg"; + + if (imgReader.read(&testImage)) { + if (!QDir().mkpath(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())) { + qDebug() << picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + " could not be created."; + return; } QString suffix; if (!cardBeingDownloaded.getStripped()) suffix = ".full"; - QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + ".jpg"); + QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension); if (!newPic.open(QIODevice::WriteOnly)) return; newPic.write(picData); From e8abd6eb63a10e491c4c170ff54af83f340b6455 Mon Sep 17 00:00:00 2001 From: Dibe Zackaria Date: Wed, 6 Aug 2014 20:13:59 +0200 Subject: [PATCH 3/6] fix: typo --- cockatrice/src/carddatabase.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 80c48030..eef6b624 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -719,8 +719,8 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) CardInfo *CardNameMap::findByPrefix(const std::string &prefix) { for (CardNameMap::iterator it = this->begin(); it != this->end(); ++it) { - auto std::mismatch(prefix.begin(), prefix.end(), it.key().toStdString().begin()) - if (auto.first == prefix.end()) + auto check = std::mismatch(prefix.begin(), prefix.end(), it.key().toStdString().begin()) + if (check.first == prefix.end()) return it.value(); } return NULL; From 9092f2298b87c11f818affcc9df5e60391308d4d Mon Sep 17 00:00:00 2001 From: Dibe Zackaria Date: Wed, 6 Aug 2014 20:27:04 +0200 Subject: [PATCH 4/6] fix: remove auto --- cockatrice/src/carddatabase.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index eef6b624..71f0c623 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -719,8 +719,8 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) CardInfo *CardNameMap::findByPrefix(const std::string &prefix) { for (CardNameMap::iterator it = this->begin(); it != this->end(); ++it) { - auto check = std::mismatch(prefix.begin(), prefix.end(), it.key().toStdString().begin()) - if (check.first == prefix.end()) + if (std::mismatch(prefix.begin(), prefix.end(), + it.key().toStdString().begin()).first == prefix.end()) return it.value(); } return NULL; From 10ab53c4accc187475d902acfb41ffbe68276e2a Mon Sep 17 00:00:00 2001 From: Mawu3n4 Date: Thu, 7 Aug 2014 17:43:14 +0200 Subject: [PATCH 5/6] fix: find by prefix if card not found only --- cockatrice/src/carddatabase.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 71f0c623..89f2e638 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -719,7 +719,7 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) CardInfo *CardNameMap::findByPrefix(const std::string &prefix) { for (CardNameMap::iterator it = this->begin(); it != this->end(); ++it) { - if (std::mismatch(prefix.begin(), prefix.end(), + if (std::mismatch(prefix.begin(), prefix.end(), it.key().toStdString().begin()).first == prefix.end()) return it.value(); } @@ -731,6 +731,8 @@ CardInfo *CardDatabase::getCardFromMap(CardNameMap &cardMap, const QString &card if (cardName.isEmpty()) return noCard; + else if (cardMap.contains(cardName)) + return cardMap.value(cardName); else if ((foundCard = cardMap.findByPrefix(cardName.toStdString()))) return foundCard; else if (createIfNotFound) { From d55f6fdec10afec1c33968727d066158cc25876c Mon Sep 17 00:00:00 2001 From: Mawu3n4 Date: Fri, 8 Aug 2014 14:24:29 +0200 Subject: [PATCH 6/6] fix: show card only found once by prefix --- cockatrice/src/carddatabase.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cockatrice/src/carddatabase.cpp b/cockatrice/src/carddatabase.cpp index 89f2e638..dc87c1cd 100644 --- a/cockatrice/src/carddatabase.cpp +++ b/cockatrice/src/carddatabase.cpp @@ -718,12 +718,18 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml) } CardInfo *CardNameMap::findByPrefix(const std::string &prefix) { + int count = 0; + CardInfo *found; + for (CardNameMap::iterator it = this->begin(); it != this->end(); ++it) { if (std::mismatch(prefix.begin(), prefix.end(), - it.key().toStdString().begin()).first == prefix.end()) - return it.value(); + it.key().toStdString().begin()).first == prefix.end()) { + count++; + found = it.value(); + } } - return NULL; + + return (count == 1 ? found : NULL); } CardInfo *CardDatabase::getCardFromMap(CardNameMap &cardMap, const QString &cardName, bool createIfNotFound) {