herospellwidget.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.removeAllSpells();
  90. }
  91. ui->filter->clear();
  92. ui->filter->setEnabled(checked);
  93. ui->tabWidget->setEnabled(checked);
  94. initSpellLists();
  95. }
  96. void HeroSpellWidget::on_filter_textChanged(const QString & keyword)
  97. {
  98. if (keyword.toStdString().find_first_not_of(' ') == std::string::npos)
  99. {
  100. const auto exists = QString::fromStdString(".*");
  101. showItemIfMatches(exists);
  102. }
  103. else
  104. {
  105. const auto doesNotContainKeyword = QString::fromStdString("^((?!") + keyword + QString::fromStdString(").)*$");
  106. hideItemIfMatches(doesNotContainKeyword);
  107. const auto containsKeyword = QString::fromStdString(".*") + keyword + QString::fromStdString(".*");
  108. showItemIfMatches(containsKeyword);
  109. }
  110. hideEmptySpellLists();
  111. }
  112. void HeroSpellWidget::showItemIfMatches(const QString & match)
  113. {
  114. toggleHiddenForItemIfMatches(match, false);
  115. }
  116. void HeroSpellWidget::hideItemIfMatches(const QString & match)
  117. {
  118. toggleHiddenForItemIfMatches(match, true);
  119. }
  120. void HeroSpellWidget::toggleHiddenForItemIfMatches(const QString & match, bool hidden)
  121. {
  122. QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 };
  123. for (const QListWidget * list : spellLists)
  124. {
  125. const auto items = list->findItems(match, Qt::MatchRegularExpression);
  126. for (QListWidgetItem * item : items)
  127. {
  128. item->setHidden(hidden);
  129. }
  130. }
  131. }
  132. void HeroSpellWidget::hideEmptySpellLists()
  133. {
  134. QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 };
  135. auto toggleSpellListVisibility = [&](const QListWidget * list, bool visible)
  136. {
  137. auto * parent = list->parentWidget();
  138. int index = ui->tabWidget->indexOf(parent);
  139. ui->tabWidget->setTabVisible(index, visible);
  140. };
  141. for (const QListWidget * list : spellLists)
  142. {
  143. const auto allItems = list->findItems("*", Qt::MatchWildcard);
  144. bool isListEmpty = true;
  145. for (QListWidgetItem * item : allItems)
  146. {
  147. if (!item->isHidden())
  148. {
  149. isListEmpty = false;
  150. break;
  151. }
  152. }
  153. toggleSpellListVisibility(list, !isListEmpty);
  154. }
  155. }
  156. HeroSpellDelegate::HeroSpellDelegate(CGHeroInstance & h)
  157. : BaseInspectorItemDelegate()
  158. , hero(h)
  159. {
  160. }
  161. QWidget * HeroSpellDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
  162. {
  163. return new HeroSpellWidget(hero, parent);
  164. }
  165. void HeroSpellDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
  166. {
  167. if (auto * ed = qobject_cast<HeroSpellWidget *>(editor))
  168. {
  169. ed->obtainData();
  170. }
  171. else
  172. {
  173. QStyledItemDelegate::setEditorData(editor, index);
  174. }
  175. }
  176. void HeroSpellDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
  177. {
  178. if (auto * ed = qobject_cast<HeroSpellWidget *>(editor))
  179. {
  180. ed->commitChanges();
  181. updateModelData(model, index);
  182. }
  183. else
  184. {
  185. QStyledItemDelegate::setModelData(editor, model, index);
  186. }
  187. }
  188. void HeroSpellDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
  189. {
  190. QStringList textList;
  191. if(hero.spellbookContainsSpell(SpellID::PRESET))
  192. {
  193. textList += QObject::tr("Custom Spells:");
  194. for(auto const & spellID : hero.getSpellsInSpellbook())
  195. {
  196. if(spellID != SpellID::PRESET)
  197. textList += QString::fromStdString(spellID.toSpell()->getNameTranslated());
  198. }
  199. }
  200. else
  201. {
  202. textList += QObject::tr("Default Spells");
  203. }
  204. setModelTextData(model, index, textList);
  205. }