herospellwidget.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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) : hero(h), BaseInspectorItemDelegate()
  95. {
  96. }
  97. QWidget * HeroSpellDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
  98. {
  99. return new HeroSpellWidget(hero, parent);
  100. }
  101. void HeroSpellDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
  102. {
  103. if (auto * ed = qobject_cast<HeroSpellWidget *>(editor))
  104. {
  105. ed->obtainData();
  106. }
  107. else
  108. {
  109. QStyledItemDelegate::setEditorData(editor, index);
  110. }
  111. }
  112. void HeroSpellDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
  113. {
  114. if (auto * ed = qobject_cast<HeroSpellWidget *>(editor))
  115. {
  116. ed->commitChanges();
  117. updateModelData(model, index);
  118. }
  119. else
  120. {
  121. QStyledItemDelegate::setModelData(editor, model, index);
  122. }
  123. }
  124. void HeroSpellDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
  125. {
  126. QMap<int, QVariant> data;
  127. if(hero.spellbookContainsSpell(SpellID::PRESET))
  128. {
  129. QStringList textList;
  130. textList += QObject::tr("Custom Spells:");
  131. for(auto const & spellID : hero.getSpellsInSpellbook())
  132. {
  133. if(spellID != SpellID::PRESET)
  134. textList += QString::fromStdString(spellID.toSpell()->getNameTranslated());
  135. }
  136. QString text = textList.join("\n");
  137. data[Qt::DisplayRole] = QVariant(text);
  138. data[Qt::ToolTipRole] = QVariant(text);
  139. }
  140. else
  141. {
  142. data[Qt::DisplayRole] = QObject::tr("Default Spells");
  143. data[Qt::ToolTipRole] = QObject::tr("Default Spells");
  144. }
  145. model->setItemData(index, data);
  146. }