From d8d196b79de8c32534f2abe78b8c27b542e2b4d5 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 26 Aug 2024 23:53:08 -0400 Subject: [PATCH] Connect wild mon chart to editor --- include/editor.h | 7 ++- include/mainwindow.h | 1 - include/ui/wildmonchart.h | 11 ++-- src/editor.cpp | 36 +++++++++---- src/mainwindow.cpp | 23 +++------ src/ui/encountertabledelegates.cpp | 5 +- src/ui/encountertablemodel.cpp | 82 +++++++++++++++++------------- src/ui/montabwidget.cpp | 11 ++-- src/ui/wildmonchart.cpp | 60 ++++++++++++++-------- 9 files changed, 139 insertions(+), 97 deletions(-) diff --git a/include/editor.h b/include/editor.h index bb211cba..31b5c11b 100644 --- a/include/editor.h +++ b/include/editor.h @@ -26,6 +26,7 @@ #include "movablerect.h" #include "cursortilerect.h" #include "mapruler.h" +#include "encountertablemodel.h" class DraggablePixmapItem; class MetatilesPixmapItem; @@ -84,7 +85,7 @@ public: void removeSelectedConnection(); void addNewWildMonGroup(QWidget *window); void deleteWildMonGroup(); - QTableView* getCurrentWildMonTable(); + EncounterTableModel* getCurrentWildMonTable(); void updateDiveMap(QString mapName); void updateEmergeMap(QString mapName); void setSelectedConnection(MapConnection *connection); @@ -224,7 +225,9 @@ private slots: signals: void objectsChanged(); void openConnectedMap(MapConnection*); - void wildMonDataChanged(); + void wildMonTableOpened(EncounterTableModel*); + void wildMonTableClosed(); + void wildMonTableEdited(); void warpEventDoubleClicked(QString, int, Event::Group); void currentMetatilesSelectionChanged(); void mapRulerStatusChanged(const QString &); diff --git a/include/mainwindow.h b/include/mainwindow.h index b0b2c3a6..c2726efc 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -183,7 +183,6 @@ private slots: void onOpenConnectedMap(MapConnection*); void onMapNeedsRedrawing(); void onTilesetsSaved(QString, QString); - void onWildMonDataChanged(); void openNewMapPopupWindow(); void onNewMapCreated(); void onMapCacheCleared(); diff --git a/include/ui/wildmonchart.h b/include/ui/wildmonchart.h index 5f9a0c4d..07ea7b7b 100644 --- a/include/ui/wildmonchart.h +++ b/include/ui/wildmonchart.h @@ -21,7 +21,8 @@ public: public slots: void setTable(const EncounterTableModel *table); - void createCharts(); + void clearTable(); + void refresh(); private: Ui::WildMonChart *ui; @@ -56,15 +57,17 @@ private: void clearTableData(); void readTable(); - void createSpeciesDistributionChart(); - void createLevelDistributionChart(); + QChart* createSpeciesDistributionChart(); + QChart* createLevelDistributionChart(); QBarSet* createLevelDistributionBarSet(const QString &, const QString &, bool); + void refreshSpeciesDistributionChart(); + void refreshLevelDistributionChart(); void saveSpeciesColors(const QList &); void applySpeciesColors(const QList &); QChart::ChartTheme currentTheme() const; void updateTheme(); - void stopChartAnimation(); + void stopChartAnimation(QChart*); void showHelpDialog(); }; diff --git a/src/editor.cpp b/src/editor.cpp index 6e2ff76f..d2cbbf50 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -7,7 +7,6 @@ #include "mapsceneeventfilter.h" #include "metatile.h" #include "montabwidget.h" -#include "encountertablemodel.h" #include "editcommands.h" #include "config.h" #include "scripting.h" @@ -43,6 +42,11 @@ Editor::Editor(Ui::MainWindow* ui) selectNewEvents = false; } }); + + // Send signals used for updating the wild pokemon summary chart + connect(ui->stackedWidget_WildMons, &QStackedWidget::currentChanged, [this] { + emit wildMonTableOpened(getCurrentWildMonTable()); + }); } Editor::~Editor() @@ -79,9 +83,7 @@ void Editor::saveUiFields() { } void Editor::setProject(Project * project) { - if (this->project) { - closeProject(); - } + closeProject(); this->project = project; MapConnection::project = project; } @@ -194,6 +196,7 @@ void Editor::setEditingConnections() { void Editor::clearWildMonTables() { QStackedWidget *stack = ui->stackedWidget_WildMons; + const QSignalBlocker blocker(stack); // delete widgets from previous map data if they exist while (stack->count()) { @@ -203,6 +206,7 @@ void Editor::clearWildMonTables() { } ui->comboBox_EncounterGroupLabel->clear(); + emit wildMonTableClosed(); } void Editor::displayWildMonTables() { @@ -243,8 +247,12 @@ void Editor::displayWildMonTables() { } tabIndex++; } + connect(tabWidget, &MonTabWidget::currentChanged, [this] { + emit wildMonTableOpened(getCurrentWildMonTable()); + }); } stack->setCurrentIndex(0); + emit wildMonTableOpened(getCurrentWildMonTable()); } void Editor::addNewWildMonGroup(QWidget *window) { @@ -359,7 +367,7 @@ void Editor::addNewWildMonGroup(QWidget *window) { tabIndex++; } saveEncounterTabData(); - emit wildMonDataChanged(); + emit wildMonTableEdited(); } } @@ -397,7 +405,8 @@ void Editor::deleteWildMonGroup() { project->encounterGroupLabels.remove(i); displayWildMonTables(); - emit wildMonDataChanged(); + saveEncounterTabData(); + emit wildMonTableEdited(); } } @@ -650,7 +659,8 @@ void Editor::configureEncounterJSON(QWidget *window) { // Re-draw the tab accordingly. displayWildMonTables(); - emit wildMonDataChanged(); + saveEncounterTabData(); + emit wildMonTableEdited(); } } @@ -684,10 +694,14 @@ void Editor::saveEncounterTabData() { } } -QTableView* Editor::getCurrentWildMonTable() { - QStackedWidget *stack = ui->stackedWidget_WildMons; - auto tabWidget = static_cast(stack->widget(stack->currentIndex())); - return tabWidget->tableAt(tabWidget->currentIndex()); +EncounterTableModel* Editor::getCurrentWildMonTable() { + auto tabWidget = static_cast(ui->stackedWidget_WildMons->currentWidget()); + if (!tabWidget) return nullptr; + + auto tableView = tabWidget->tableAt(tabWidget->currentIndex()); + if (!tableView) return nullptr; + + return static_cast(tableView->model()); } void Editor::updateEncounterFields(EncounterFields newFields) { diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index aa1d0122..c4e23b81 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -239,10 +239,8 @@ void MainWindow::initExtraSignals() { connect(ui->tabWidget_EventType, &QTabWidget::currentChanged, this, &MainWindow::eventTabChanged); // Change pages on wild encounter groups - QStackedWidget *stack = ui->stackedWidget_WildMons; - QComboBox *labelCombo = ui->comboBox_EncounterGroupLabel; - connect(labelCombo, QOverload::of(&QComboBox::currentIndexChanged), [=](int index){ - stack->setCurrentIndex(index); + connect(ui->comboBox_EncounterGroupLabel, QOverload::of(&QComboBox::currentIndexChanged), [this](int index){ + ui->stackedWidget_WildMons->setCurrentIndex(index); }); // Convert the layout of the map tools' frame into an adjustable FlowLayout @@ -309,7 +307,7 @@ void MainWindow::initEditor() { connect(this->editor, &Editor::openConnectedMap, this, &MainWindow::onOpenConnectedMap); connect(this->editor, &Editor::warpEventDoubleClicked, this, &MainWindow::openWarpMap); connect(this->editor, &Editor::currentMetatilesSelectionChanged, this, &MainWindow::currentMetatilesSelectionChanged); - connect(this->editor, &Editor::wildMonDataChanged, this, &MainWindow::onWildMonDataChanged); + connect(this->editor, &Editor::wildMonTableEdited, [this] { this->markMapEdited(); }); connect(this->editor, &Editor::mapRulerStatusChanged, this, &MainWindow::onMapRulerStatusChanged); connect(this->editor, &Editor::tilesetUpdated, this, &Scripting::cb_TilesetUpdated); connect(ui->toolButton_Open_Scripts, &QToolButton::pressed, this->editor, &Editor::openMapScripts); @@ -2560,11 +2558,6 @@ void MainWindow::onTilesetsSaved(QString primaryTilesetLabel, QString secondaryT redrawMapScene(); } -void MainWindow::onWildMonDataChanged() { - editor->saveEncounterTabData(); - markMapEdited(); -} - void MainWindow::onMapRulerStatusChanged(const QString &status) { if (status.isEmpty()) { label_MapRulerStatus->hide(); @@ -2659,12 +2652,10 @@ void MainWindow::on_pushButton_DeleteWildMonGroup_clicked() { void MainWindow::on_pushButton_SummaryChart_clicked() { if (!this->wildMonChart) { - // TODO: Move to editor, connect to signals for when the table data changes - QTableView *table = this->editor->getCurrentWildMonTable(); - EncounterTableModel *data = table ? static_cast(table->model()) : nullptr; - this->wildMonChart = new WildMonChart(this, data); - } else { - this->wildMonChart->createCharts(); + this->wildMonChart = new WildMonChart(this, this->editor->getCurrentWildMonTable()); + connect(this->editor, &Editor::wildMonTableOpened, this->wildMonChart, &WildMonChart::setTable); + connect(this->editor, &Editor::wildMonTableClosed, this->wildMonChart, &WildMonChart::clearTable); + connect(this->editor, &Editor::wildMonTableEdited, this->wildMonChart, &WildMonChart::refresh); } openSubWindow(this->wildMonChart); } diff --git a/src/ui/encountertabledelegates.cpp b/src/ui/encountertabledelegates.cpp index 7824087e..e2901d16 100644 --- a/src/ui/encountertabledelegates.cpp +++ b/src/ui/encountertabledelegates.cpp @@ -79,8 +79,11 @@ QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewIt editor->setMaximum(this->project->miscConstants.value("max_level_define").toInt()); } else if (col == EncounterTableModel::ColumnType::EncounterRate) { + // The games multiply the encounter rate value provided here by 16, so the input limit is the max/16. + // TODO: Read MAX_ENCOUNTER_RATE? + static const int maxEncounterRate = 2880; editor->setMinimum(0); - editor->setMaximum(180); + editor->setMaximum(maxEncounterRate / 16); } return editor; diff --git a/src/ui/encountertablemodel.cpp b/src/ui/encountertablemodel.cpp index 00199735..f7e73996 100644 --- a/src/ui/encountertablemodel.cpp +++ b/src/ui/encountertablemodel.cpp @@ -142,45 +142,57 @@ QVariant EncounterTableModel::headerData(int section, Qt::Orientation orientatio } bool EncounterTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { - if (role == Qt::EditRole) { - if (!checkIndex(index)) - return false; + if (role != Qt::EditRole) + return false; + if (!checkIndex(index)) + return false; - int row = index.row(); - int col = index.column(); + int row = index.row(); + int col = index.column(); - switch (col) { - case ColumnType::Species: - this->monInfo.wildPokemon[row].species = value.toString(); - break; - - case ColumnType::MinLevel: { - int minLevel = value.toInt(); - this->monInfo.wildPokemon[row].minLevel = minLevel; - if (minLevel > this->monInfo.wildPokemon[row].maxLevel) - this->monInfo.wildPokemon[row].maxLevel = minLevel; - break; + switch (col) { + case ColumnType::Species: { + QString species = value.toString(); + if (this->monInfo.wildPokemon[row].species != species) { + this->monInfo.wildPokemon[row].species = species; + emit edited(); } - - case ColumnType::MaxLevel: { - int maxLevel = value.toInt(); - this->monInfo.wildPokemon[row].maxLevel = maxLevel; - if (maxLevel < this->monInfo.wildPokemon[row].minLevel) - this->monInfo.wildPokemon[row].minLevel = maxLevel; - break; - } - - case ColumnType::EncounterRate: - this->monInfo.encounterRate = value.toInt(); - break; - - default: - return false; - } - emit edited(); - return true; + break; } - return false; + + case ColumnType::MinLevel: { + int minLevel = value.toInt(); + if (this->monInfo.wildPokemon[row].minLevel != minLevel) { + this->monInfo.wildPokemon[row].minLevel = minLevel; + this->monInfo.wildPokemon[row].maxLevel = qMax(minLevel, this->monInfo.wildPokemon[row].maxLevel); + emit edited(); + } + break; + } + + case ColumnType::MaxLevel: { + int maxLevel = value.toInt(); + if (this->monInfo.wildPokemon[row].maxLevel != maxLevel) { + this->monInfo.wildPokemon[row].maxLevel = maxLevel; + this->monInfo.wildPokemon[row].minLevel = qMin(maxLevel, this->monInfo.wildPokemon[row].minLevel); + emit edited(); + } + break; + } + + case ColumnType::EncounterRate: { + int encounterRate = value.toInt(); + if (this->monInfo.encounterRate != encounterRate) { + this->monInfo.encounterRate = encounterRate; + emit edited(); + } + break; + } + + default: + return false; + } + return true; } Qt::ItemFlags EncounterTableModel::flags(const QModelIndex &index) const { diff --git a/src/ui/montabwidget.cpp b/src/ui/montabwidget.cpp index 6845a02b..74c52dc5 100644 --- a/src/ui/montabwidget.cpp +++ b/src/ui/montabwidget.cpp @@ -68,7 +68,7 @@ void MonTabWidget::paste(int index) { WildMonInfo newInfo = getDefaultMonInfo(this->editor->project->wildMonFields.at(index)); combineEncounters(newInfo, encounterClipboard); populateTab(index, newInfo); - emit editor->wildMonDataChanged(); + emit editor->wildMonTableEdited(); } void MonTabWidget::actionCopyTab(int index) { @@ -88,21 +88,19 @@ void MonTabWidget::actionCopyTab(int index) { } void MonTabWidget::actionAddDeleteTab(int index) { + clearTableAt(index); if (activeTabs[index]) { // delete tab - clearTableAt(index); deactivateTab(index); editor->saveEncounterTabData(); - emit editor->wildMonDataChanged(); } else { // add tab - clearTableAt(index); populateTab(index, getDefaultMonInfo(editor->project->wildMonFields.at(index))); editor->saveEncounterTabData(); setCurrentIndex(index); - emit editor->wildMonDataChanged(); } + emit editor->wildMonTableEdited(); } void MonTabWidget::clearTableAt(int tabIndex) { @@ -130,6 +128,7 @@ void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo) { EncounterTableModel *model = new EncounterTableModel(monInfo, editor->project->wildMonFields, tabIndex, this); connect(model, &EncounterTableModel::edited, editor, &Editor::saveEncounterTabData); + connect(model, &EncounterTableModel::edited, editor, &Editor::wildMonTableEdited); speciesTable->setModel(model); speciesTable->setItemDelegateForColumn(EncounterTableModel::ColumnType::Species, new SpeciesComboDelegate(editor->project, this)); @@ -158,6 +157,8 @@ void MonTabWidget::populateTab(int tabIndex, WildMonInfo monInfo) { } QTableView *MonTabWidget::tableAt(int tabIndex) { + if (tabIndex < 0) + return nullptr; return static_cast(this->widget(tabIndex)); } diff --git a/src/ui/wildmonchart.cpp b/src/ui/wildmonchart.cpp index 257959d9..dfdda38d 100644 --- a/src/ui/wildmonchart.cpp +++ b/src/ui/wildmonchart.cpp @@ -30,9 +30,9 @@ WildMonChart::WildMonChart(QWidget *parent, const EncounterTableModel *table) : connect(ui->button_Help, &QAbstractButton::clicked, this, &WildMonChart::showHelpDialog); // Changing these settings changes which level distribution chart is shown - connect(ui->groupBox_Species, &QGroupBox::clicked, this, &WildMonChart::createLevelDistributionChart); - connect(ui->comboBox_Species, &QComboBox::currentTextChanged, this, &WildMonChart::createLevelDistributionChart); - connect(ui->comboBox_Group, &QComboBox::currentTextChanged, this, &WildMonChart::createLevelDistributionChart); + connect(ui->groupBox_Species, &QGroupBox::clicked, this, &WildMonChart::refreshLevelDistributionChart); + connect(ui->comboBox_Species, &QComboBox::currentTextChanged, this, &WildMonChart::refreshLevelDistributionChart); + connect(ui->comboBox_Group, &QComboBox::currentTextChanged, this, &WildMonChart::refreshLevelDistributionChart); // Set up Theme combo box for (auto i : themes) @@ -59,9 +59,14 @@ WildMonChart::~WildMonChart() { }; void WildMonChart::setTable(const EncounterTableModel *table) { + if (this->table == table) + return; this->table = table; - readTable(); - createCharts(); + refresh(); +} + +void WildMonChart::clearTable() { + setTable(nullptr); } void WildMonChart::clearTableData() { @@ -78,6 +83,7 @@ void WildMonChart::clearTableData() { const QSignalBlocker blocker2(ui->comboBox_Group); ui->comboBox_Species->clear(); ui->comboBox_Group->clear(); + ui->comboBox_Group->setEnabled(false); } // Extract all the data from the table that we need for the charts @@ -151,15 +157,28 @@ void WildMonChart::readTable() { ui->comboBox_Group->setEnabled(usesGroupLabels()); } -void WildMonChart::createCharts() { - createSpeciesDistributionChart(); - createLevelDistributionChart(); - +void WildMonChart::refresh() { + readTable(); + refreshSpeciesDistributionChart(); + refreshLevelDistributionChart(); + // Turn off the animation once it's played, otherwise it replays any time the window changes size. // TODO: Store timer, disable if closing or creating new chart //QTimer::singleShot(chart->animationDuration() + 500, this, &WildMonChart::stopChartAnimation); } +void WildMonChart::refreshSpeciesDistributionChart() { + if (ui->chartView_SpeciesDistribution->chart()) + ui->chartView_SpeciesDistribution->chart()->deleteLater(); + ui->chartView_SpeciesDistribution->setChart(createSpeciesDistributionChart()); +} + +void WildMonChart::refreshLevelDistributionChart() { + if (ui->chartView_LevelDistribution->chart()) + ui->chartView_LevelDistribution->chart()->deleteLater(); + ui->chartView_LevelDistribution->setChart(createLevelDistributionChart()); +} + QStringList WildMonChart::getSpeciesNamesAlphabetical() const { return this->speciesToGroupedData.keys(); } @@ -190,7 +209,7 @@ bool WildMonChart::usesGroupLabels() const { return this->groupNames.length() > 1; } -void WildMonChart::createSpeciesDistributionChart() { +QChart* WildMonChart::createSpeciesDistributionChart() { QList barSets; for (const auto species : getSpeciesNamesAlphabetical()) { // Add encounter chance data @@ -249,9 +268,7 @@ void WildMonChart::createSpeciesDistributionChart() { series->attachAxis(axisY); } - if (ui->chartView_SpeciesDistribution->chart()) - ui->chartView_SpeciesDistribution->chart()->deleteLater(); - ui->chartView_SpeciesDistribution->setChart(chart); + return chart; } QBarSet* WildMonChart::createLevelDistributionBarSet(const QString &species, const QString &groupName, bool individual) { @@ -281,14 +298,14 @@ QBarSet* WildMonChart::createLevelDistributionBarSet(const QString &species, con const QSignalBlocker blocker2(ui->comboBox_Species); ui->groupBox_Species->setChecked(true); ui->comboBox_Species->setCurrentText(species); - createLevelDistributionChart(); + refreshLevelDistributionChart(); }); } return set; } -void WildMonChart::createLevelDistributionChart() { +QChart* WildMonChart::createLevelDistributionChart() { const QString groupName = ui->comboBox_Group->currentText(); LevelRange levelRange; @@ -344,9 +361,7 @@ void WildMonChart::createLevelDistributionChart() { }; axisY->setMax(roundUp(qCeil(axisY->max()), 5)); - if (ui->chartView_LevelDistribution->chart()) - ui->chartView_LevelDistribution->chart()->deleteLater(); - ui->chartView_LevelDistribution->setChart(chart); + return chart; } QChart::ChartTheme WildMonChart::currentTheme() const { @@ -365,7 +380,6 @@ void WildMonChart::updateTheme() { QChart *chart = ui->chartView_SpeciesDistribution->chart(); if (!chart) return; - this->speciesToColor.clear(); chart->setTheme(theme); saveSpeciesColors(static_cast(chart->series().at(0))->barSets()); @@ -377,6 +391,7 @@ void WildMonChart::updateTheme() { } void WildMonChart::saveSpeciesColors(const QList &barSets) { + this->speciesToColor.clear(); for (auto set : barSets) this->speciesToColor.insert(set->label(), set->color()); } @@ -386,9 +401,10 @@ void WildMonChart::applySpeciesColors(const QList &barSets) { set->setColor(this->speciesToColor.value(set->label())); } -void WildMonChart::stopChartAnimation() { - if (ui->chartView_SpeciesDistribution->chart()) - ui->chartView_SpeciesDistribution->chart()->setAnimationOptions(QChart::NoAnimation); +void WildMonChart::stopChartAnimation(QChart *chart) { + if (!chart) + return; + chart->setAnimationOptions(QChart::NoAnimation); } void WildMonChart::showHelpDialog() {