Skip to content

Commit

Permalink
Redesign IV Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Admiral-Fish committed Oct 1, 2020
1 parent ec1f044 commit 567e95b
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 119 deletions.
103 changes: 65 additions & 38 deletions Forms/Tools/IVCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QInputDialog>
#include <QMessageBox>
#include <QSettings>
#include <QSpinBox>

IVCalculator::IVCalculator(QWidget *parent) : QWidget(parent), ui(new Ui::IVCalculator), connected(false)
{
Expand Down Expand Up @@ -66,6 +67,10 @@ void IVCalculator::setupModels()
ui->comboBoxPokemon->setInsertPolicy(QComboBox::NoInsert);
ui->comboBoxPokemon->completer()->setCompletionMode(QCompleter::PopupCompletion);

addRow();

connect(ui->pushButtonAddRow, &QPushButton::clicked, this, &IVCalculator::addRow);
connect(ui->pushButtonDeleteRow, &QPushButton::clicked, this, &IVCalculator::deleteRow);
connect(ui->pushButtonFindIVs, &QPushButton::clicked, this, &IVCalculator::findIVs);
connect(ui->comboBoxPokemon, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &IVCalculator::pokemonIndexChanged);
connect(ui->comboBoxAltForm, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &IVCalculator::altformIndexChanged);
Expand Down Expand Up @@ -127,56 +132,78 @@ void IVCalculator::displayIVs(QLabel *label, const QVector<u8> &ivs)
label->setText(result);
}

void IVCalculator::findIVs()
void IVCalculator::addRow()
{
QVector<QVector<u16>> stats;
QVector<u8> levels;
QSpinBox *level = new QSpinBox(ui->scrollAreaWidgetContents);
level->setRange(1, 100);

QSpinBox *hp = new QSpinBox(ui->scrollAreaWidgetContents);
hp->setRange(1, 651);

QSpinBox *atk = new QSpinBox(ui->scrollAreaWidgetContents);
atk->setRange(1, 437);

QSpinBox *def = new QSpinBox(ui->scrollAreaWidgetContents);
def->setRange(1, 545);

QSpinBox *spa = new QSpinBox(ui->scrollAreaWidgetContents);
spa->setRange(1, 420);

QSpinBox *spd = new QSpinBox(ui->scrollAreaWidgetContents);
spd->setRange(1, 545);

QSpinBox *spe = new QSpinBox(ui->scrollAreaWidgetContents);
spe->setRange(1, 435);

QStringList entries = ui->textEdit->toPlainText().split("\n");
entries.removeAll(QString());
rows++;
ui->gridLayoutEntry->addWidget(level, rows, 0);
ui->gridLayoutEntry->addWidget(hp, rows, 1);
ui->gridLayoutEntry->addWidget(atk, rows, 2);
ui->gridLayoutEntry->addWidget(def, rows, 3);
ui->gridLayoutEntry->addWidget(spa, rows, 4);
ui->gridLayoutEntry->addWidget(spd, rows, 5);
ui->gridLayoutEntry->addWidget(spe, rows, 6);
}

bool flag = !entries.isEmpty();
void IVCalculator::deleteRow()
{
if (rows == 1)
{
return;
}

for (const QString &entry : entries)
for (int column = 0; column < 7; column++)
{
QStringList values = entry.split(" ");
values.removeAll(QString());
QLayoutItem *item = ui->gridLayoutEntry->itemAtPosition(rows, column);
ui->gridLayoutEntry->removeItem(item);

if (values.size() != 7)
{
flag = false;
break;
}
delete item->widget();
delete item;
}

levels.append(static_cast<u8>(values.at(0).toUInt(&flag)));
if (!flag)
{
break;
}
rows--;
}

void IVCalculator::findIVs()
{
QVector<QVector<u16>> stats;
QVector<u8> levels;

for (int row = 1; row < rows; row++)
{
QLayoutItem *item = ui->gridLayoutEntry->itemAtPosition(row, 0);
QSpinBox *widget = reinterpret_cast<QSpinBox *>(item->widget());

levels.append(widget->value());

QVector<u16> stat;
for (u8 i = 1; i < 7; i++)
for (int column = 1; column < 7; column++)
{
stat.append(static_cast<u16>(values.at(i).toUInt(&flag)));
if (!flag)
{
break;
}
item = ui->gridLayoutEntry->itemAtPosition(row, column);
widget = reinterpret_cast<QSpinBox *>(item->widget());
stat.append(widget->value());
}
stats.append(stat);

if (!flag)
{
break;
}
}

if (!flag)
{
QMessageBox error;
error.setText(tr("Invalid input"));
error.exec();
return;
}

u8 nature = static_cast<u8>(ui->comboBoxNature->currentIndex());
Expand Down
3 changes: 3 additions & 0 deletions Forms/Tools/IVCalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ class IVCalculator : public QWidget

private:
Ui::IVCalculator *ui;
int rows = 0;

void setupModels();
void displayIVs(QLabel *label, const QVector<u8> &ivs);
bool connected;

private slots:
void addRow();
void deleteRow();
void findIVs();
void pokemonIndexChanged(int index);
void altformIndexChanged(int index);
Expand Down
Loading

0 comments on commit 567e95b

Please sign in to comment.