lolautoaccept/src/stagesettings.cpp

146 lines
3.6 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);
setMinimumSize(ui->gridLayout->minimumSize());
}
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) {
auto champr = new ChampRow();
ui->championList->addItem(champr);
champr->setChamp(champ, 0, QPixmap("/home/yannis/.cache/lolautoaccept/square/Orianna.png")); // TODO
}
}
void StageSettings::setDataDragon(DataDragon* dd_) {
dd = dd_;
}
void StageSettings::addChamp(const std::string& champname, uint32_t id, QPixmap icon) {
auto champr = new ChampRow();
ui->championList->addItem(champr);
champr->setChamp(champname, id, icon); // TODO
}
void StageSettings::toggledinternal(int state) {
emit toggled(state == Qt::CheckState::Checked);
updateEnabled();
}
void StageSettings::addChamp() {
Log::trace << "add champ";
// TODO: 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);
// TODO: emit value changed something
}
} else {
// not accepted
}
}
void StageSettings::removeChamp() {
Log::trace << "remove champ";
int row = ui->championList->currentRow(); // what if no row is selected?
delete ui->championList->takeItem(row);
}
void StageSettings::moveUp() {
Log::trace << "move up";
if(ui->championList->selectedItems().isEmpty()) return;
auto selection = ui->championList->selectedItems().at(0);
ui->championList->removeItemWidget(selection);
ui->championList->insertItem(0, selection);
}
void StageSettings::moveDown() {
Log::trace << "move down";
}
void StageSettings::resolveAndAddChamp(const std::string& name) {
if(dd) {
int count = 0;
auto cd = dd->getBestMatchingChamp(name, &count);
dd->getImageAsnyc(cd.id, [this, name, cd](QPixmap img) {
addChamp(name, cd.key, img);
emit championChanged(0, QString::fromStdString(cd.name));
});
} 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->moveup->setEnabled(active);
ui->movedown->setEnabled(active);
ui->removeChampion->setEnabled(active);
}