lolautoaccept/src/stagesettings.cpp

142 lines
3.3 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);
auto champsmodel = ui->championList->model();
QObject::connect(champsmodel, &QAbstractItemModel::rowsMoved, this, &StageSettings::moved);
}
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(QString 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(), cr->getChampID());
}
return out;
}
void StageSettings::setChampions(const std::vector<QString>& champs) {
clear();
// add new champs
for(const QString& 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();
qInfo() << "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 QString& 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 {
qCritical() << __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);
}