heroartifactswidget.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "artifactwidget.h"
  12. #include "heroartifactswidget.h"
  13. #include "ui_heroartifactswidget.h"
  14. #include "inspector.h"
  15. #include "mapeditorroles.h"
  16. #include "../mapcontroller.h"
  17. #include "../../lib/constants/StringConstants.h"
  18. #include "../../lib/entities/artifact/ArtifactUtils.h"
  19. #include "../../lib/entities/artifact/CArtHandler.h"
  20. #include "../../lib/mapping/CMap.h"
  21. HeroArtifactsWidget::HeroArtifactsWidget(MapController & controller, CGHeroInstance & h, QWidget * parent) :
  22. QDialog(parent),
  23. ui(new Ui::HeroArtifactsWidget),
  24. controller(controller),
  25. hero(h),
  26. fittingSet(CArtifactFittingSet(h))
  27. {
  28. ui->setupUi(this);
  29. }
  30. HeroArtifactsWidget::~HeroArtifactsWidget()
  31. {
  32. delete ui;
  33. }
  34. void HeroArtifactsWidget::on_addButton_clicked()
  35. {
  36. ArtifactWidget artifactWidget{ fittingSet, this };
  37. connect(&artifactWidget, &ArtifactWidget::saveArtifact, this, &HeroArtifactsWidget::onSaveArtifact);
  38. artifactWidget.exec();
  39. }
  40. void HeroArtifactsWidget::on_removeButton_clicked()
  41. {
  42. auto row = ui->artifacts->currentRow();
  43. if (row == -1)
  44. {
  45. return;
  46. }
  47. auto slot = ui->artifacts->item(row, Column::SLOT)->data(MapEditorRoles::ArtifactSlotRole).toInt();
  48. fittingSet.removeArtifact(ArtifactPosition(slot));
  49. ui->artifacts->removeRow(row);
  50. }
  51. void HeroArtifactsWidget::onSaveArtifact(int32_t artifactIndex, ArtifactPosition slot)
  52. {
  53. auto artifact = controller.map()->createArtifact(LIBRARY->arth->getByIndex(artifactIndex)->getId());
  54. fittingSet.putArtifact(slot, artifact);
  55. addArtifactToTable(artifactIndex, slot);
  56. }
  57. void HeroArtifactsWidget::addArtifactToTable(int32_t artifactIndex, ArtifactPosition slot)
  58. {
  59. auto artifact = LIBRARY->arth->getByIndex(artifactIndex);
  60. auto * itemArtifact = new QTableWidgetItem;
  61. itemArtifact->setText(QString::fromStdString(artifact->getNameTranslated()));
  62. itemArtifact->setData(MapEditorRoles::ArtifactIDRole, QVariant::fromValue(artifact->getIndex()));
  63. auto * itemSlot = new QTableWidgetItem;
  64. auto slotText = ArtifactUtils::isSlotBackpack(slot) ? NArtifactPosition::backpack : NArtifactPosition::namesHero[slot.num];
  65. itemSlot->setData(MapEditorRoles::ArtifactSlotRole, QVariant::fromValue(slot.num));
  66. itemSlot->setText(QString::fromStdString(slotText));
  67. ui->artifacts->insertRow(ui->artifacts->rowCount());
  68. ui->artifacts->setItem(ui->artifacts->rowCount() - 1, Column::ARTIFACT, itemArtifact);
  69. ui->artifacts->setItem(ui->artifacts->rowCount() - 1, Column::SLOT, itemSlot);
  70. }
  71. void HeroArtifactsWidget::obtainData()
  72. {
  73. std::vector<const CArtifact *> combinedArtifactsParts;
  74. for (const auto & [artPosition, artSlotInfo] : fittingSet.artifactsWorn)
  75. {
  76. addArtifactToTable(LIBRARY->arth->getById(artSlotInfo.getArt()->getTypeId())->getIndex(), artPosition);
  77. }
  78. for (const auto & art : hero.artifactsInBackpack)
  79. {
  80. addArtifactToTable(LIBRARY->arth->getById(art.getArt()->getTypeId())->getIndex(), ArtifactPosition::BACKPACK_START);
  81. }
  82. }
  83. void HeroArtifactsWidget::commitChanges()
  84. {
  85. while(!hero.artifactsWorn.empty())
  86. {
  87. hero.removeArtifact(hero.artifactsWorn.begin()->first);
  88. }
  89. while(!hero.artifactsInBackpack.empty())
  90. {
  91. hero.removeArtifact(ArtifactPosition::BACKPACK_START + static_cast<int>(hero.artifactsInBackpack.size()) - 1);
  92. }
  93. for(const auto & [artPosition, artSlotInfo] : fittingSet.artifactsWorn)
  94. {
  95. hero.putArtifact(artPosition, artSlotInfo.getArt());
  96. }
  97. for(const auto & art : fittingSet.artifactsInBackpack)
  98. {
  99. hero.putArtifact(ArtifactPosition::BACKPACK_START + static_cast<int>(hero.artifactsInBackpack.size()), art.getArt());
  100. }
  101. }
  102. HeroArtifactsDelegate::HeroArtifactsDelegate(MapController & controller, CGHeroInstance & h)
  103. : BaseInspectorItemDelegate()
  104. , controller(controller)
  105. , hero(h)
  106. {
  107. }
  108. QWidget * HeroArtifactsDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
  109. {
  110. return new HeroArtifactsWidget(controller, hero, parent);
  111. }
  112. void HeroArtifactsDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
  113. {
  114. if (auto * ed = qobject_cast<HeroArtifactsWidget *>(editor))
  115. {
  116. ed->obtainData();
  117. }
  118. else
  119. {
  120. QStyledItemDelegate::setEditorData(editor, index);
  121. }
  122. }
  123. void HeroArtifactsDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
  124. {
  125. if (auto * ed = qobject_cast<HeroArtifactsWidget *>(editor))
  126. {
  127. ed->commitChanges();
  128. updateModelData(model, index);
  129. }
  130. else
  131. {
  132. QStyledItemDelegate::setModelData(editor, model, index);
  133. }
  134. }
  135. void HeroArtifactsDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
  136. {
  137. QStringList textList;
  138. for(const auto & [artPosition, artSlotInfo] : hero.artifactsWorn)
  139. {
  140. auto slotText = NArtifactPosition::namesHero[artPosition.num];
  141. textList += QString("%1: %2").arg(QString::fromStdString(slotText)).arg(QString::fromStdString(artSlotInfo.getArt()->getType()->getNameTranslated()));
  142. }
  143. textList += QString("%1:").arg(QString::fromStdString(NArtifactPosition::backpack));
  144. for(const auto & art : hero.artifactsInBackpack)
  145. {
  146. textList += QString::fromStdString(art.getArt()->getType()->getNameTranslated());
  147. }
  148. setModelTextData(model, index, textList);
  149. }