heroartifactswidget.cpp 5.7 KB

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