townspellswidget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * townspellswidget.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "townspellswidget.h"
  12. #include "ui_townspellswidget.h"
  13. #include "inspector.h"
  14. #include "mapeditorroles.h"
  15. #include "../../lib/constants/StringConstants.h"
  16. #include "../../lib/spells/CSpellHandler.h"
  17. TownSpellsWidget::TownSpellsWidget(CGTownInstance & town, QWidget * parent) :
  18. QDialog(parent),
  19. ui(new Ui::TownSpellsWidget),
  20. town(town)
  21. {
  22. ui->setupUi(this);
  23. possibleSpellLists = { ui->possibleSpellList1, ui->possibleSpellList2, ui->possibleSpellList3, ui->possibleSpellList4, ui->possibleSpellList5 };
  24. requiredSpellLists = { ui->requiredSpellList1, ui->requiredSpellList2, ui->requiredSpellList3, ui->requiredSpellList4, ui->requiredSpellList5 };
  25. 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};
  26. for (int i = 0; i < mageGuilds.size(); i++)
  27. {
  28. ui->tabWidget->setTabEnabled(i, vstd::contains(town.getTown()->buildings, mageGuilds[i]));
  29. }
  30. }
  31. TownSpellsWidget::~TownSpellsWidget()
  32. {
  33. delete ui;
  34. }
  35. void TownSpellsWidget::obtainData()
  36. {
  37. initSpellLists();
  38. if (vstd::contains(town.possibleSpells, SpellID::PRESET)) {
  39. ui->customizeSpells->setChecked(true);
  40. }
  41. else
  42. {
  43. ui->customizeSpells->setChecked(false);
  44. ui->tabWidget->setEnabled(false);
  45. }
  46. }
  47. void TownSpellsWidget::resetSpells()
  48. {
  49. town.possibleSpells.clear();
  50. town.obligatorySpells.clear();
  51. for (auto spellID : VLC->spellh->getDefaultAllowed())
  52. town.possibleSpells.push_back(spellID);
  53. }
  54. void TownSpellsWidget::initSpellLists()
  55. {
  56. auto spells = VLC->spellh->getDefaultAllowed();
  57. for (int i = 0; i < GameConstants::SPELL_LEVELS; i++)
  58. {
  59. std::vector<SpellID> spellsByLevel;
  60. auto getSpellsByLevel = [i](auto spellID) {
  61. return spellID.toEntity(VLC)->getLevel() == i + 1;
  62. };
  63. vstd::copy_if(spells, std::back_inserter(spellsByLevel), getSpellsByLevel);
  64. possibleSpellLists[i]->clear();
  65. requiredSpellLists[i]->clear();
  66. for (auto spellID : spellsByLevel)
  67. {
  68. auto spell = spellID.toEntity(VLC);
  69. auto * possibleItem = new QListWidgetItem(QString::fromStdString(spell->getNameTranslated()));
  70. possibleItem->setData(MapEditorRoles::SpellIDRole, QVariant::fromValue(spell->getIndex()));
  71. possibleItem->setFlags(possibleItem->flags() | Qt::ItemIsUserCheckable);
  72. possibleItem->setCheckState(vstd::contains(town.possibleSpells, spell->getId()) ? Qt::Checked : Qt::Unchecked);
  73. possibleSpellLists[i]->addItem(possibleItem);
  74. auto * requiredItem = new QListWidgetItem(QString::fromStdString(spell->getNameTranslated()));
  75. requiredItem->setData(MapEditorRoles::SpellIDRole, QVariant::fromValue(spell->getIndex()));
  76. requiredItem->setFlags(requiredItem->flags() | Qt::ItemIsUserCheckable);
  77. requiredItem->setCheckState(vstd::contains(town.obligatorySpells, spell->getId()) ? Qt::Checked : Qt::Unchecked);
  78. requiredSpellLists[i]->addItem(requiredItem);
  79. }
  80. }
  81. }
  82. void TownSpellsWidget::commitChanges()
  83. {
  84. if (!ui->tabWidget->isEnabled())
  85. {
  86. resetSpells();
  87. return;
  88. }
  89. auto updateTownSpellList = [](auto uiSpellLists, auto & townSpellList) {
  90. for (const QListWidget * spellList : uiSpellLists)
  91. {
  92. for (int i = 0; i < spellList->count(); ++i)
  93. {
  94. const auto * item = spellList->item(i);
  95. if (item->checkState() == Qt::Checked)
  96. {
  97. townSpellList.push_back(item->data(MapEditorRoles::SpellIDRole).toInt());
  98. }
  99. }
  100. }
  101. };
  102. town.possibleSpells.clear();
  103. town.obligatorySpells.clear();
  104. town.possibleSpells.push_back(SpellID::PRESET);
  105. updateTownSpellList(possibleSpellLists, town.possibleSpells);
  106. updateTownSpellList(requiredSpellLists, town.obligatorySpells);
  107. }
  108. void TownSpellsWidget::on_customizeSpells_toggled(bool checked)
  109. {
  110. if (checked)
  111. {
  112. town.possibleSpells.push_back(SpellID::PRESET);
  113. }
  114. else
  115. {
  116. resetSpells();
  117. }
  118. ui->tabWidget->setEnabled(checked);
  119. initSpellLists();
  120. }
  121. TownSpellsDelegate::TownSpellsDelegate(CGTownInstance & town) : QStyledItemDelegate(), town(town)
  122. {
  123. }
  124. QWidget * TownSpellsDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
  125. {
  126. return new TownSpellsWidget(town, parent);
  127. }
  128. void TownSpellsDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
  129. {
  130. if (auto * ed = qobject_cast<TownSpellsWidget *>(editor))
  131. {
  132. ed->obtainData();
  133. }
  134. else
  135. {
  136. QStyledItemDelegate::setEditorData(editor, index);
  137. }
  138. }
  139. void TownSpellsDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
  140. {
  141. if (auto * ed = qobject_cast<TownSpellsWidget *>(editor))
  142. {
  143. ed->commitChanges();
  144. }
  145. else
  146. {
  147. QStyledItemDelegate::setModelData(editor, model, index);
  148. }
  149. }