herospellwidget.cpp 3.8 KB

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