1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

reduce code duplication

This commit is contained in:
godric3 2024-07-26 20:42:16 +02:00
parent 3d3f388fb8
commit 67ab43526f
8 changed files with 113 additions and 123 deletions

View File

@ -68,6 +68,56 @@ std::string defaultBuildingIdConversion(BuildingID bId)
}
}
QStandardItem * getBuildingParentFromTreeModel(const CBuilding * building, QStandardItemModel & model)
{
QStandardItem * parent = nullptr;
std::vector<QModelIndex> stack(1);
while (!parent && !stack.empty())
{
auto pindex = stack.back();
stack.pop_back();
auto rowCount = model.rowCount(pindex);
for (int i = 0; i < rowCount; ++i)
{
QModelIndex index = model.index(i, 0, pindex);
if (building->upgrade.getNum() == model.itemFromIndex(index)->data(Qt::UserRole).toInt())
{
parent = model.itemFromIndex(index);
break;
}
if (model.hasChildren(index))
stack.push_back(index);
}
}
return parent;
}
std::set<QVariant> getBuildingVariantsFromModel(QStandardItemModel & model, int modelColumn, Qt::CheckState checkState)
{
std::set<QVariant> result;
std::vector<QModelIndex> stack(1);
while (!stack.empty())
{
auto pindex = stack.back();
stack.pop_back();
auto rowCount = model.rowCount(pindex);
for (int i = 0; i < rowCount; ++i)
{
QModelIndex index = model.index(i, modelColumn, pindex);
if (auto * item = model.itemFromIndex(index))
if (item->checkState() == checkState)
result.emplace(item->data(Qt::UserRole));
index = model.index(i, 0, pindex);
if (model.hasChildren(index))
stack.push_back(index);
}
}
return result;
}
TownBuildingsWidget::TownBuildingsWidget(CGTownInstance & t, QWidget *parent) :
town(t),
QDialog(parent),
@ -96,7 +146,7 @@ QStandardItem * TownBuildingsWidget::addBuilding(const CTown & ctown, int bId, s
return nullptr;
}
QString name = tr(building->getNameTranslated().c_str());
QString name = QString::fromStdString(building->getNameTranslated());
if(name.isEmpty())
name = QString::fromStdString(defaultBuildingIdConversion(buildingId));
@ -122,25 +172,7 @@ QStandardItem * TownBuildingsWidget::addBuilding(const CTown & ctown, int bId, s
}
else
{
QStandardItem * parent = nullptr;
std::vector<QModelIndex> stack;
stack.push_back(QModelIndex());
while(!parent && !stack.empty())
{
auto pindex = stack.back();
stack.pop_back();
for(int i = 0; i < model.rowCount(pindex); ++i)
{
QModelIndex index = model.index(i, 0, pindex);
if(building->upgrade.getNum() == model.itemFromIndex(index)->data(Qt::UserRole).toInt())
{
parent = model.itemFromIndex(index);
break;
}
if(model.hasChildren(index))
stack.push_back(index);
}
}
QStandardItem * parent = getBuildingParentFromTreeModel(building, model);
if(!parent)
parent = addBuilding(ctown, building->upgrade.getNum(), remaining);
@ -172,25 +204,12 @@ void TownBuildingsWidget::addBuildings(const CTown & ctown)
std::set<BuildingID> TownBuildingsWidget::getBuildingsFromModel(int modelColumn, Qt::CheckState checkState)
{
auto buildingVariants = getBuildingVariantsFromModel(model, modelColumn, checkState);
std::set<BuildingID> result;
std::vector<QModelIndex> stack;
stack.push_back(QModelIndex());
while(!stack.empty())
for (auto buildingId : buildingVariants)
{
auto pindex = stack.back();
stack.pop_back();
for(int i = 0; i < model.rowCount(pindex); ++i)
{
QModelIndex index = model.index(i, modelColumn, pindex);
if(auto * item = model.itemFromIndex(index))
if(item->checkState() == checkState)
result.emplace(item->data(Qt::UserRole).toInt());
index = model.index(i, 0, pindex); //children are linked to first column of the model
if(model.hasChildren(index))
stack.push_back(index);
}
result.insert(buildingId.toInt());
}
return result;
}
@ -242,13 +261,13 @@ void TownBuildingsWidget::setRowColumnCheckState(QStandardItem * item, Column co
void TownBuildingsWidget::setAllRowsColumnCheckState(Column column, Qt::CheckState checkState)
{
std::vector<QModelIndex> stack;
stack.push_back(QModelIndex());
std::vector<QModelIndex> stack(1);
while (!stack.empty())
{
auto parentIndex = stack.back();
stack.pop_back();
for (int i = 0; i < model.rowCount(parentIndex); ++i)
auto rowCount = model.rowCount(parentIndex);
for (int i = 0; i < rowCount; ++i)
{
QModelIndex index = model.index(i, column, parentIndex);
if (auto* item = model.itemFromIndex(index))

View File

@ -19,6 +19,10 @@ class TownBuildingsWidget;
std::string defaultBuildingIdConversion(BuildingID bId);
QStandardItem * getBuildingParentFromTreeModel(const CBuilding * building, QStandardItemModel & model);
std::set<QVariant> getBuildingVariantsFromModel(QStandardItemModel & model, int modelColumn, Qt::CheckState checkState);
class TownBuildingsWidget : public QDialog
{
Q_OBJECT

View File

@ -19,13 +19,13 @@ TownEventDialog::TownEventDialog(CGTownInstance & t, QListWidgetItem * item, QWi
QDialog(parent),
ui(new Ui::TownEventDialog),
town(t),
item(item)
townEventListItem(item)
{
ui->setupUi(this);
ui->buildingsTree->setModel(&buildingsModel);
params = item->data(Qt::UserRole).toMap();
params = townEventListItem->data(Qt::UserRole).toMap();
ui->eventFirstOccurrence->setMinimum(1);
ui->eventFirstOccurrence->setMaximum(999);
ui->eventRepeatAfter->setMaximum(999);
@ -40,8 +40,6 @@ TownEventDialog::TownEventDialog(CGTownInstance & t, QListWidgetItem * item, QWi
initResources();
initBuildings();
initCreatures();
show();
}
TownEventDialog::~TownEventDialog()
@ -108,7 +106,7 @@ QStandardItem * TownEventDialog::addBuilding(const CTown& ctown, BuildingID buil
return nullptr;
}
QString name = tr(building->getNameTranslated().c_str());
QString name = QString::fromStdString(building->getNameTranslated());
if (name.isEmpty())
name = QString::fromStdString(defaultBuildingIdConversion(buildingId));
@ -129,25 +127,7 @@ QStandardItem * TownEventDialog::addBuilding(const CTown& ctown, BuildingID buil
}
else
{
QStandardItem * parent = nullptr;
std::vector<QModelIndex> stack;
stack.push_back(QModelIndex());
while (!parent && !stack.empty())
{
auto pindex = stack.back();
stack.pop_back();
for (int i = 0; i < buildingsModel.rowCount(pindex); ++i)
{
QModelIndex index = buildingsModel.index(i, 0, pindex);
if (building->upgrade.getNum() == buildingsModel.itemFromIndex(index)->data(Qt::UserRole).toInt())
{
parent = buildingsModel.itemFromIndex(index);
break;
}
if (buildingsModel.hasChildren(index))
stack.push_back(index);
}
}
QStandardItem * parent = getBuildingParentFromTreeModel(building, buildingsModel);
if (!parent)
parent = addBuilding(ctown, building->upgrade.getNum(), remaining);
@ -215,9 +195,9 @@ void TownEventDialog::on_TownEventDialog_finished(int result)
descriptor["buildings"] = buildingsToVariant();
descriptor["creatures"] = creaturesToVariant();
item->setData(Qt::UserRole, descriptor);
townEventListItem->setData(Qt::UserRole, descriptor);
auto itemText = tr("Day %1 - %2").arg(ui->eventFirstOccurrence->value(), 3).arg(ui->eventNameText->text());
item->setText(itemText);
townEventListItem->setText(itemText);
}
QVariant TownEventDialog::playersToVariant()
@ -234,7 +214,7 @@ QVariant TownEventDialog::playersToVariant()
QVariantMap TownEventDialog::resourcesToVariant()
{
auto res = item->data(Qt::UserRole).toMap().value("resources").toMap();
auto res = townEventListItem->data(Qt::UserRole).toMap().value("resources").toMap();
for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
{
auto * itemType = ui->resourcesTable->item(i, 0);
@ -247,24 +227,8 @@ QVariantMap TownEventDialog::resourcesToVariant()
QVariantList TownEventDialog::buildingsToVariant()
{
QVariantList buildingsList;
std::vector<QModelIndex> stack;
stack.push_back(QModelIndex());
while (!stack.empty())
{
auto pindex = stack.back();
stack.pop_back();
for (int i = 0; i < buildingsModel.rowCount(pindex); ++i)
{
QModelIndex index = buildingsModel.index(i, 1, pindex);
if (auto * item = buildingsModel.itemFromIndex(index))
if (item->checkState() == Qt::Checked)
buildingsList.push_back(item->data(Qt::UserRole));
index = buildingsModel.index(i, 0, pindex);
if (buildingsModel.hasChildren(index))
stack.push_back(index);
}
}
auto buildings = getBuildingVariantsFromModel(buildingsModel, 1, Qt::Checked);
QVariantList buildingsList(buildings.begin(), buildings.end());
return buildingsList;
}

View File

@ -47,7 +47,7 @@ private:
Ui::TownEventDialog * ui;
CGTownInstance & town;
QListWidgetItem * item;
QListWidgetItem * townEventListItem;
QMap<QString, QVariant> params;
mutable QStandardItemModel buildingsModel;
};
QStandardItemModel buildingsModel;
};

View File

@ -132,18 +132,15 @@ void TownEventsWidget::on_timedEventAdd_clicked()
void TownEventsWidget::on_timedEventRemove_clicked()
{
if (auto* item = ui->eventsList->currentItem())
ui->eventsList->takeItem(ui->eventsList->row(item));
delete ui->eventsList->takeItem(ui->eventsList->currentRow());
}
void TownEventsWidget::on_eventsList_itemActivated(QListWidgetItem* item)
{
new TownEventDialog(town, item, parentWidget());
TownEventDialog dlg{ town, item, parentWidget() };
dlg.exec();
}
void TownEventsWidget::onItemChanged(QStandardItem * item)
{
}
TownEventsDelegate::TownEventsDelegate(CGTownInstance & town, MapController & c) : town(town), controller(c), QStyledItemDelegate()
{

View File

@ -30,7 +30,6 @@ public:
void obtainData();
void commitChanges(MapController & controller);
private slots:
void onItemChanged(QStandardItem * item);
void on_timedEventAdd_clicked();
void on_timedEventRemove_clicked();
void on_eventsList_itemActivated(QListWidgetItem * item);
@ -56,4 +55,4 @@ public:
private:
CGTownInstance & town;
MapController & controller;
};
};

View File

@ -20,8 +20,21 @@ TownSpellsWidget::TownSpellsWidget(CGTownInstance & town, QWidget * parent) :
town(town)
{
ui->setupUi(this);
BuildingID 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 < GameConstants::SPELL_LEVELS; i++)
possibleSpellLists[0] = ui->possibleSpellList1;
possibleSpellLists[1] = ui->possibleSpellList2;
possibleSpellLists[2] = ui->possibleSpellList3;
possibleSpellLists[3] = ui->possibleSpellList4;
possibleSpellLists[4] = ui->possibleSpellList5;
requiredSpellLists[0] = ui->requiredSpellList1;
requiredSpellLists[1] = ui->requiredSpellList2;
requiredSpellLists[2] = ui->requiredSpellList3;
requiredSpellLists[3] = ui->requiredSpellList4;
requiredSpellLists[4] = ui->requiredSpellList5;
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++)
{
ui->tabWidget->setTabEnabled(i, vstd::contains(town.getTown()->buildings, mageGuilds[i]));
}
@ -56,8 +69,6 @@ void TownSpellsWidget::resetSpells()
void TownSpellsWidget::initSpellLists()
{
QListWidget * possibleSpellLists[] = { ui->possibleSpellList1, ui->possibleSpellList2, ui->possibleSpellList3, ui->possibleSpellList4, ui->possibleSpellList5 };
QListWidget * requiredSpellLists[] = { ui->requiredSpellList1, ui->requiredSpellList2, ui->requiredSpellList3, ui->requiredSpellList4, ui->requiredSpellList5 };
auto spells = VLC->spellh->getDefaultAllowed();
for (int i = 0; i < GameConstants::SPELL_LEVELS; i++)
{
@ -93,33 +104,26 @@ void TownSpellsWidget::commitChanges()
resetSpells();
return;
}
auto updateTownSpellList = [](auto uiSpellLists, auto & townSpellList) {
for (QListWidget * spellList : uiSpellLists)
{
for (int i = 0; i < spellList->count(); ++i)
{
QListWidgetItem * item = spellList->item(i);
if (item->checkState() == Qt::Checked)
{
townSpellList.push_back(item->data(Qt::UserRole).toInt());
}
}
}
};
town.possibleSpells.clear();
town.obligatorySpells.clear();
town.possibleSpells.push_back(SpellID::PRESET);
QListWidget * possibleSpellLists[] = { ui->possibleSpellList1, ui->possibleSpellList2, ui->possibleSpellList3, ui->possibleSpellList4, ui->possibleSpellList5 };
QListWidget * requiredSpellLists[] = { ui->requiredSpellList1, ui->requiredSpellList2, ui->requiredSpellList3, ui->requiredSpellList4, ui->requiredSpellList5 };
for (auto spellList : possibleSpellLists)
{
for (int i = 0; i < spellList->count(); i++)
{
auto * item = spellList->item(i);
if (item->checkState() == Qt::Checked)
{
town.possibleSpells.push_back(item->data(Qt::UserRole).toInt());
}
}
}
for (auto spellList : requiredSpellLists)
{
for (int i = 0; i < spellList->count(); i++)
{
auto * item = spellList->item(i);
if (item->checkState() == Qt::Checked)
{
town.obligatorySpells.push_back(item->data(Qt::UserRole).toInt());
}
}
}
updateTownSpellList(possibleSpellLists, town.possibleSpells);
updateTownSpellList(requiredSpellLists, town.obligatorySpells);
}
void TownSpellsWidget::on_customizeSpells_toggled(bool checked)

View File

@ -36,6 +36,9 @@ private:
CGTownInstance & town;
std::array<QListWidget *, 5> possibleSpellLists;
std::array<QListWidget *, 5> requiredSpellLists;
void resetSpells();
void initSpellLists();
};