forked from MrBesen/lolautoaccept
138 lines
3.2 KiB
C++
138 lines
3.2 KiB
C++
#include "stagesettings.h"
|
|
#include "ui_stagesettings.h"
|
|
|
|
#include <QLocale>
|
|
|
|
#include <Log.h>
|
|
|
|
#include "championsearch.h"
|
|
#include "champrow.h"
|
|
|
|
StageSettings::StageSettings(QWidget *parent) : QWidget(parent), ui(new Ui::StageSettings) {
|
|
ui->setupUi(this);
|
|
}
|
|
|
|
StageSettings::~StageSettings() {
|
|
delete ui;
|
|
}
|
|
|
|
QString StageSettings::getName() const {
|
|
return ui->groupBox->title();
|
|
}
|
|
|
|
void StageSettings::setName(const QString& n) {
|
|
ui->groupBox->setTitle(n);
|
|
ui->checkBox->setText(tr("Enable %1").arg(n));
|
|
}
|
|
|
|
bool StageSettings::getState() const {
|
|
return ui->checkBox->checkState() == Qt::CheckState::Checked;
|
|
}
|
|
|
|
void StageSettings::setState(bool b) {
|
|
ui->checkBox->setCheckState(b ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
|
|
emit toggled(b);
|
|
updateEnabled();
|
|
}
|
|
|
|
StageSettings::SelectedChamp::SelectedChamp(std::string name, uint32_t id) : name(name), id(id) {}
|
|
|
|
std::vector<StageSettings::SelectedChamp> StageSettings::getChampions() const {
|
|
std::vector<SelectedChamp> out;
|
|
out.reserve(ui->championList->count());
|
|
for(int32_t i = 0; i < ui->championList->count(); ++i) {
|
|
ChampRow* cr = (ChampRow*) ui->championList->item(i);
|
|
out.emplace_back(cr->getChamp().toStdString(), cr->getChampID());
|
|
}
|
|
return out;
|
|
}
|
|
|
|
void StageSettings::setChampions(const std::vector<std::string>& champs) {
|
|
clear();
|
|
|
|
// add new champs
|
|
for(const std::string& champ : champs) {
|
|
resolveAndAddChamp(champ, champ == champs.back());
|
|
}
|
|
}
|
|
|
|
void StageSettings::setDataDragon(DataDragon* dd_) {
|
|
dd = dd_;
|
|
}
|
|
|
|
void StageSettings::addChamp(const DataDragon::ChampData& cd, QPixmap icon) {
|
|
auto champr = new ChampRow();
|
|
ui->championList->addItem(champr);
|
|
|
|
champr->setChamp(cd, icon);
|
|
}
|
|
|
|
void StageSettings::loadConfig(Config::StageConfig& c) {
|
|
setChampions(c.champs);
|
|
setState(c.enabled);
|
|
}
|
|
|
|
void StageSettings::toggledinternal(int state) {
|
|
emit toggled(state == Qt::CheckState::Checked);
|
|
updateEnabled();
|
|
}
|
|
|
|
void StageSettings::addChamp() {
|
|
Log::trace << "add champ";
|
|
|
|
// popup with champion search
|
|
ChampionSearch cs(dd, this);
|
|
int res = cs.exec();
|
|
Log::info << "championsearch result: " << res;
|
|
if(res == QDialog::Accepted) {
|
|
auto cr = cs.getSearchResult();
|
|
if(cr) {
|
|
ui->championList->addItem(cr);
|
|
emit championsChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
void StageSettings::removeChamp() {
|
|
Log::trace << "remove champ";
|
|
int row = ui->championList->currentRow(); // what if no row is selected?
|
|
delete ui->championList->takeItem(row);
|
|
|
|
emit championsChanged();
|
|
}
|
|
|
|
void StageSettings::moved() {
|
|
Log::trace << "moved";
|
|
emit championsChanged();
|
|
}
|
|
|
|
void StageSettings::resolveAndAddChamp(const std::string& name, bool emitchange) {
|
|
if(dd) {
|
|
int count = 0;
|
|
auto cd = dd->getBestMatchingChamp(name, &count);
|
|
|
|
dd->getImageAsnyc(cd.id, [this, name, cd, emitchange](QPixmap img) {
|
|
addChamp(cd, img);
|
|
if(emitchange) {
|
|
emit championsChanged();
|
|
}
|
|
});
|
|
} else {
|
|
Log::error << __PRETTY_FUNCTION__ << " Datadragon not set!";
|
|
}
|
|
}
|
|
|
|
void StageSettings::clear() {
|
|
while(ui->championList->count()) {
|
|
delete ui->championList->takeItem(0);
|
|
}
|
|
}
|
|
|
|
void StageSettings::updateEnabled() {
|
|
bool active = ui->checkBox->isChecked();
|
|
|
|
ui->championList->setEnabled(active);
|
|
ui->addChampion->setEnabled(active);
|
|
ui->removeChampion->setEnabled(active);
|
|
}
|