diff --git a/include/runepagelist.h b/include/runepagelist.h index 2a84188..2fcf16d 100644 --- a/include/runepagelist.h +++ b/include/runepagelist.h @@ -6,6 +6,7 @@ #include "clientapi.h" #include "config.h" +#include "datadragon.h" #include "runeaspekt.h" #include "runestyle.h" @@ -13,7 +14,6 @@ namespace Ui { class RunePageList; } -class DataDragon; class DropEvent; class ClientAPI; @@ -56,6 +56,7 @@ private slots: private: void clearItems(); void addRunepageItem(QString name, int id, const ::RunePage& rp, bool isCurrent = false); + const DataDragon::ChampData& findChamp(const QString& name); QString getRuneDescription(const ::RunePage& runepage); QString getRuneText(uint32_t id); diff --git a/src/runepagelist.cpp b/src/runepagelist.cpp index 67b29d8..0f94f28 100644 --- a/src/runepagelist.cpp +++ b/src/runepagelist.cpp @@ -8,7 +8,6 @@ #include #include "clipboardpopup.h" -#include "datadragon.h" #include "runeeditor.h" RunePageList::RunePageList(QWidget* parent) : QListWidget(parent), ui(new Ui::RunePageList) { @@ -201,12 +200,10 @@ void RunePageList::addRunepageItem(QString name, int id, const ::RunePage& rp, b item->setData(RoleId, (uint) id); item->setData(RolePointer, (qulonglong) new ::RunePage(rp)); - if(dd) { - const DataDragon::ChampData& champData = dd->getBestMatchingChamp(name); - if(champData.key != -1) { - QPixmap iamge = dd->getImage(champData.id); - item->setIcon(iamge); - } + const DataDragon::ChampData& champData = findChamp(name); + if(champData.key != -1) { + QPixmap iamge = dd->getImage(champData.id); + item->setIcon(iamge); } QString tooltipStr; @@ -223,6 +220,48 @@ void RunePageList::addRunepageItem(QString name, int id, const ::RunePage& rp, b addItem(item); } +const DataDragon::ChampData& RunePageList::findChamp(const QString& name) { + if(!dd) { + return DataDragon::EMPTYCHAMP; + } + + // try direct + int count = 0; + const DataDragon::ChampData& directChampData = dd->getBestMatchingChamp(name, &count); + if(directChampData.key != -1) { + return directChampData; + } + + // not specific + if(count > 1) { + return DataDragon::EMPTYCHAMP; + } + + // try for substrings + static const QRegularExpression splittingRegex("\\W+"); + QStringList list = name.split(splittingRegex, QString::SplitBehavior::SkipEmptyParts); + QSet matchedIds; + const DataDragon::ChampData* lastMatched = nullptr; + for(const QString& entry : list) { + count = 0; + const DataDragon::ChampData& splitChampData = dd->getBestMatchingChamp(entry, &count); + if(count == 1) { + matchedIds.insert(splitChampData.key); + lastMatched = &splitChampData; + } else if(count > 1) { + // not specific + return DataDragon::EMPTYCHAMP; + } + } + + if(lastMatched && matchedIds.size() == 1) { + return *lastMatched; + } + + // not specific or not found + return DataDragon::EMPTYCHAMP; +} + QString RunePageList::getRuneDescription(const ::RunePage& runepage) { QString outStr; outStr.reserve(100);