2024-07-12 21:18:43 +02:00
|
|
|
/*
|
|
|
|
* townspellswidget.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "townspellswidget.h"
|
|
|
|
#include "ui_townspellswidget.h"
|
|
|
|
#include "inspector.h"
|
2024-07-28 16:07:52 +02:00
|
|
|
#include "mapeditorroles.h"
|
2024-07-12 21:18:43 +02:00
|
|
|
#include "../../lib/constants/StringConstants.h"
|
|
|
|
#include "../../lib/spells/CSpellHandler.h"
|
|
|
|
|
|
|
|
TownSpellsWidget::TownSpellsWidget(CGTownInstance & town, QWidget * parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::TownSpellsWidget),
|
|
|
|
town(town)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2024-07-26 20:42:16 +02:00
|
|
|
|
2024-08-01 22:28:23 +02:00
|
|
|
possibleSpellLists = { ui->possibleSpellList1, ui->possibleSpellList2, ui->possibleSpellList3, ui->possibleSpellList4, ui->possibleSpellList5 };
|
|
|
|
requiredSpellLists = { ui->requiredSpellList1, ui->requiredSpellList2, ui->requiredSpellList3, ui->requiredSpellList4, ui->requiredSpellList5 };
|
2024-07-26 20:42:16 +02:00
|
|
|
|
|
|
|
std::array<BuildingID, 5> mageGuilds = {BuildingID::MAGES_GUILD_1, BuildingID::MAGES_GUILD_2, BuildingID::MAGES_GUILD_3, BuildingID::MAGES_GUILD_4, BuildingID::MAGES_GUILD_5};
|
|
|
|
for (int i = 0; i < mageGuilds.size(); i++)
|
2024-07-12 21:18:43 +02:00
|
|
|
{
|
|
|
|
ui->tabWidget->setTabEnabled(i, vstd::contains(town.getTown()->buildings, mageGuilds[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TownSpellsWidget::~TownSpellsWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TownSpellsWidget::obtainData()
|
|
|
|
{
|
|
|
|
initSpellLists();
|
|
|
|
if (vstd::contains(town.possibleSpells, SpellID::PRESET)) {
|
|
|
|
ui->customizeSpells->setChecked(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->customizeSpells->setChecked(false);
|
|
|
|
ui->tabWidget->setEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TownSpellsWidget::resetSpells()
|
|
|
|
{
|
|
|
|
town.possibleSpells.clear();
|
|
|
|
town.obligatorySpells.clear();
|
2024-07-16 21:16:26 +02:00
|
|
|
for (auto spellID : VLC->spellh->getDefaultAllowed())
|
|
|
|
town.possibleSpells.push_back(spellID);
|
2024-07-12 21:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TownSpellsWidget::initSpellLists()
|
|
|
|
{
|
2024-07-16 21:16:26 +02:00
|
|
|
auto spells = VLC->spellh->getDefaultAllowed();
|
2024-07-12 21:18:43 +02:00
|
|
|
for (int i = 0; i < GameConstants::SPELL_LEVELS; i++)
|
|
|
|
{
|
2024-07-16 21:16:26 +02:00
|
|
|
std::vector<SpellID> spellsByLevel;
|
|
|
|
auto getSpellsByLevel = [i](auto spellID) {
|
|
|
|
return spellID.toEntity(VLC)->getLevel() == i + 1;
|
2024-07-12 21:18:43 +02:00
|
|
|
};
|
|
|
|
vstd::copy_if(spells, std::back_inserter(spellsByLevel), getSpellsByLevel);
|
|
|
|
possibleSpellLists[i]->clear();
|
|
|
|
requiredSpellLists[i]->clear();
|
2024-07-16 21:16:26 +02:00
|
|
|
for (auto spellID : spellsByLevel)
|
2024-07-12 21:18:43 +02:00
|
|
|
{
|
2024-07-16 21:16:26 +02:00
|
|
|
auto spell = spellID.toEntity(VLC);
|
2024-07-12 21:18:43 +02:00
|
|
|
auto * possibleItem = new QListWidgetItem(QString::fromStdString(spell->getNameTranslated()));
|
2024-07-28 16:07:52 +02:00
|
|
|
possibleItem->setData(MapEditorRoles::SpellIDRole, QVariant::fromValue(spell->getIndex()));
|
2024-07-12 21:18:43 +02:00
|
|
|
possibleItem->setFlags(possibleItem->flags() | Qt::ItemIsUserCheckable);
|
|
|
|
possibleItem->setCheckState(vstd::contains(town.possibleSpells, spell->getId()) ? Qt::Checked : Qt::Unchecked);
|
|
|
|
possibleSpellLists[i]->addItem(possibleItem);
|
|
|
|
|
|
|
|
auto * requiredItem = new QListWidgetItem(QString::fromStdString(spell->getNameTranslated()));
|
2024-07-28 16:07:52 +02:00
|
|
|
requiredItem->setData(MapEditorRoles::SpellIDRole, QVariant::fromValue(spell->getIndex()));
|
2024-07-12 21:18:43 +02:00
|
|
|
requiredItem->setFlags(requiredItem->flags() | Qt::ItemIsUserCheckable);
|
|
|
|
requiredItem->setCheckState(vstd::contains(town.obligatorySpells, spell->getId()) ? Qt::Checked : Qt::Unchecked);
|
|
|
|
requiredSpellLists[i]->addItem(requiredItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TownSpellsWidget::commitChanges()
|
|
|
|
{
|
|
|
|
if (!ui->tabWidget->isEnabled())
|
|
|
|
{
|
|
|
|
resetSpells();
|
|
|
|
return;
|
|
|
|
}
|
2024-07-26 20:42:16 +02:00
|
|
|
|
|
|
|
auto updateTownSpellList = [](auto uiSpellLists, auto & townSpellList) {
|
2024-08-01 23:15:24 +02:00
|
|
|
for (const QListWidget * spellList : uiSpellLists)
|
2024-07-12 21:18:43 +02:00
|
|
|
{
|
2024-07-26 20:42:16 +02:00
|
|
|
for (int i = 0; i < spellList->count(); ++i)
|
2024-07-12 21:18:43 +02:00
|
|
|
{
|
2024-08-01 23:15:24 +02:00
|
|
|
const auto * item = spellList->item(i);
|
2024-07-26 20:42:16 +02:00
|
|
|
if (item->checkState() == Qt::Checked)
|
|
|
|
{
|
2024-07-28 16:07:52 +02:00
|
|
|
townSpellList.push_back(item->data(MapEditorRoles::SpellIDRole).toInt());
|
2024-07-26 20:42:16 +02:00
|
|
|
}
|
2024-07-12 21:18:43 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-26 20:42:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
town.possibleSpells.clear();
|
|
|
|
town.obligatorySpells.clear();
|
|
|
|
town.possibleSpells.push_back(SpellID::PRESET);
|
|
|
|
updateTownSpellList(possibleSpellLists, town.possibleSpells);
|
|
|
|
updateTownSpellList(requiredSpellLists, town.obligatorySpells);
|
2024-07-12 21:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TownSpellsWidget::on_customizeSpells_toggled(bool checked)
|
|
|
|
{
|
|
|
|
if (checked)
|
|
|
|
{
|
|
|
|
town.possibleSpells.push_back(SpellID::PRESET);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resetSpells();
|
|
|
|
}
|
|
|
|
ui->tabWidget->setEnabled(checked);
|
|
|
|
initSpellLists();
|
|
|
|
}
|
|
|
|
|
2024-08-01 23:15:24 +02:00
|
|
|
TownSpellsDelegate::TownSpellsDelegate(CGTownInstance & town) : QStyledItemDelegate(), town(town)
|
2024-07-12 21:18:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget * TownSpellsDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
|
|
|
|
{
|
|
|
|
return new TownSpellsWidget(town, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TownSpellsDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
|
|
|
|
{
|
|
|
|
if (auto * ed = qobject_cast<TownSpellsWidget *>(editor))
|
|
|
|
{
|
|
|
|
ed->obtainData();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QStyledItemDelegate::setEditorData(editor, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TownSpellsDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
|
|
|
|
{
|
|
|
|
if (auto * ed = qobject_cast<TownSpellsWidget *>(editor))
|
|
|
|
{
|
|
|
|
ed->commitChanges();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QStyledItemDelegate::setModelData(editor, model, index);
|
|
|
|
}
|
2024-08-01 22:28:23 +02:00
|
|
|
}
|