heroartifactswidget.cpp 4.3 KB

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