herospellwidget.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * herosspellwidget.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 "herospellwidget.h"
  12. #include "ui_herospellwidget.h"
  13. #include "inspector.h"
  14. #include "../../lib/GameLibrary.h"
  15. #include "../../lib/constants/StringConstants.h"
  16. #include "../../lib/spells/CSpellHandler.h"
  17. HeroSpellWidget::HeroSpellWidget(CGHeroInstance & h, QWidget * parent) :
  18. QDialog(parent),
  19. ui(new Ui::HeroSpellWidget),
  20. hero(h)
  21. {
  22. ui->setupUi(this);
  23. }
  24. HeroSpellWidget::~HeroSpellWidget()
  25. {
  26. delete ui;
  27. }
  28. void HeroSpellWidget::obtainData()
  29. {
  30. initSpellLists();
  31. if (hero.spellbookContainsSpell(SpellID::PRESET)) {
  32. ui->customizeSpells->setChecked(true);
  33. }
  34. else
  35. {
  36. ui->customizeSpells->setChecked(false);
  37. ui->tabWidget->setEnabled(false);
  38. }
  39. }
  40. void HeroSpellWidget::initSpellLists()
  41. {
  42. QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 };
  43. for (int i = 0; i < GameConstants::SPELL_LEVELS; i++)
  44. {
  45. std::vector<const CSpell*> spellsByLevel;
  46. for (auto const & spellID : LIBRARY->spellh->getDefaultAllowed())
  47. {
  48. if (spellID.toSpell()->getLevel() == i + 1)
  49. spellsByLevel.push_back(spellID.toSpell());
  50. }
  51. spellLists[i]->clear();
  52. for (auto spell : spellsByLevel)
  53. {
  54. auto* item = new QListWidgetItem(QString::fromStdString(spell->getNameTranslated()));
  55. item->setData(Qt::UserRole, QVariant::fromValue(spell->getIndex()));
  56. item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
  57. item->setCheckState(hero.spellbookContainsSpell(spell->getId()) ? Qt::Checked : Qt::Unchecked);
  58. spellLists[i]->addItem(item);
  59. }
  60. }
  61. }
  62. void HeroSpellWidget::commitChanges()
  63. {
  64. QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 };
  65. for (auto spellList : spellLists)
  66. {
  67. for (int i = 0; i < spellList->count(); i++)
  68. {
  69. auto * item = spellList->item(i);
  70. if (item->checkState() == Qt::Checked)
  71. {
  72. hero.addSpellToSpellbook(item->data(Qt::UserRole).toInt());
  73. }
  74. else
  75. {
  76. hero.removeSpellFromSpellbook(item->data(Qt::UserRole).toInt());
  77. }
  78. }
  79. }
  80. }
  81. void HeroSpellWidget::on_customizeSpells_toggled(bool checked)
  82. {
  83. if (checked)
  84. {
  85. hero.addSpellToSpellbook(SpellID::PRESET);
  86. }
  87. else
  88. {
  89. hero.removeSpellFromSpellbook(SpellID::PRESET);
  90. hero.removeSpellbook();
  91. }
  92. ui->tabWidget->setEnabled(checked);
  93. initSpellLists();
  94. }
  95. HeroSpellDelegate::HeroSpellDelegate(CGHeroInstance & h)
  96. : BaseInspectorItemDelegate()
  97. , hero(h)
  98. {
  99. }
  100. QWidget * HeroSpellDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
  101. {
  102. return new HeroSpellWidget(hero, parent);
  103. }
  104. void HeroSpellDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
  105. {
  106. if (auto * ed = qobject_cast<HeroSpellWidget *>(editor))
  107. {
  108. ed->obtainData();
  109. }
  110. else
  111. {
  112. QStyledItemDelegate::setEditorData(editor, index);
  113. }
  114. }
  115. void HeroSpellDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
  116. {
  117. if (auto * ed = qobject_cast<HeroSpellWidget *>(editor))
  118. {
  119. ed->commitChanges();
  120. updateModelData(model, index);
  121. }
  122. else
  123. {
  124. QStyledItemDelegate::setModelData(editor, model, index);
  125. }
  126. }
  127. void HeroSpellDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
  128. {
  129. QStringList textList;
  130. if(hero.spellbookContainsSpell(SpellID::PRESET))
  131. {
  132. textList += QObject::tr("Custom Spells:");
  133. for(auto const & spellID : hero.getSpellsInSpellbook())
  134. {
  135. if(spellID != SpellID::PRESET)
  136. textList += QString::fromStdString(spellID.toSpell()->getNameTranslated());
  137. }
  138. }
  139. else
  140. {
  141. textList += QObject::tr("Default Spells");
  142. }
  143. setModelTextData(model, index, textList);
  144. }