heroskillswidget.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * heroskillswidget.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 "heroskillswidget.h"
  12. #include "ui_heroskillswidget.h"
  13. #include "../../lib/constants/StringConstants.h"
  14. #include "../../lib/CSkillHandler.h"
  15. #include "inspector.h"
  16. static QList<std::pair<QString, QVariant>> LevelIdentifiers
  17. {
  18. {QObject::tr("Beginner"), QVariant::fromValue(1)},
  19. {QObject::tr("Advanced"), QVariant::fromValue(2)},
  20. {QObject::tr("Expert"), QVariant::fromValue(3)},
  21. };
  22. HeroSkillsWidget::HeroSkillsWidget(CGHeroInstance & h, QWidget *parent) :
  23. QDialog(parent),
  24. ui(new Ui::HeroSkillsWidget),
  25. hero(h)
  26. {
  27. ui->setupUi(this);
  28. ui->labelAttack->setText(QString::fromStdString(NPrimarySkill::names[0]));
  29. ui->labelDefence->setText(QString::fromStdString(NPrimarySkill::names[1]));
  30. ui->labelPower->setText(QString::fromStdString(NPrimarySkill::names[2]));
  31. ui->labelKnowledge->setText(QString::fromStdString(NPrimarySkill::names[3]));
  32. auto * delegate = new InspectorDelegate;
  33. for(auto const & s : VLC->skillh->objects)
  34. delegate->options.push_back({QString::fromStdString(s->getNameTranslated()), QVariant::fromValue(s->getId().getNum())});
  35. ui->skills->setItemDelegateForColumn(0, delegate);
  36. delegate = new InspectorDelegate;
  37. delegate->options = LevelIdentifiers;
  38. ui->skills->setItemDelegateForColumn(1, delegate);
  39. }
  40. HeroSkillsWidget::~HeroSkillsWidget()
  41. {
  42. delete ui;
  43. }
  44. void HeroSkillsWidget::on_addButton_clicked()
  45. {
  46. ui->skills->setRowCount(ui->skills->rowCount() + 1);
  47. }
  48. void HeroSkillsWidget::on_removeButton_clicked()
  49. {
  50. ui->skills->removeRow(ui->skills->currentRow());
  51. }
  52. void HeroSkillsWidget::on_checkBox_toggled(bool checked)
  53. {
  54. ui->skills->setEnabled(checked);
  55. ui->addButton->setEnabled(checked);
  56. ui->removeButton->setEnabled(checked);
  57. }
  58. void HeroSkillsWidget::obtainData()
  59. {
  60. ui->attack->setValue(hero.getBasePrimarySkillValue(PrimarySkill::ATTACK));
  61. ui->defence->setValue(hero.getBasePrimarySkillValue(PrimarySkill::DEFENSE));
  62. ui->power->setValue(hero.getBasePrimarySkillValue(PrimarySkill::SPELL_POWER));
  63. ui->knowledge->setValue(hero.getBasePrimarySkillValue(PrimarySkill::KNOWLEDGE));
  64. if(!hero.secSkills.empty() && hero.secSkills.front().first.getNum() == -1)
  65. return;
  66. ui->checkBox->setChecked(true);
  67. ui->skills->setRowCount(hero.secSkills.size());
  68. int i = 0;
  69. for(auto & s : hero.secSkills)
  70. {
  71. auto * itemSkill = new QTableWidgetItem;
  72. itemSkill->setText(QString::fromStdString(VLC->skillh->getById(s.first)->getNameTranslated()));
  73. itemSkill->setData(Qt::UserRole, QVariant::fromValue(s.first.getNum()));
  74. ui->skills->setItem(i, 0, itemSkill);
  75. auto * itemLevel = new QTableWidgetItem;
  76. itemLevel->setText(LevelIdentifiers[s.second - 1].first);
  77. itemLevel->setData(Qt::UserRole, LevelIdentifiers[s.second - 1].second);
  78. ui->skills->setItem(i++, 1, itemLevel);
  79. }
  80. }
  81. void HeroSkillsWidget::commitChanges()
  82. {
  83. hero.pushPrimSkill(PrimarySkill::ATTACK, ui->attack->value());
  84. hero.pushPrimSkill(PrimarySkill::DEFENSE, ui->defence->value());
  85. hero.pushPrimSkill(PrimarySkill::SPELL_POWER, ui->power->value());
  86. hero.pushPrimSkill(PrimarySkill::KNOWLEDGE, ui->knowledge->value());
  87. hero.secSkills.clear();
  88. if(!ui->checkBox->isChecked())
  89. {
  90. hero.secSkills.push_back(std::make_pair(SecondarySkill(-1), -1));
  91. return;
  92. }
  93. for(int i = 0; i < ui->skills->rowCount(); ++i)
  94. {
  95. if(ui->skills->item(i, 0) && ui->skills->item(i, 1))
  96. hero.secSkills.push_back(std::make_pair(SecondarySkill(ui->skills->item(i, 0)->data(Qt::UserRole).toInt()), ui->skills->item(i, 1)->data(Qt::UserRole).toInt()));
  97. }
  98. }
  99. HeroSkillsDelegate::HeroSkillsDelegate(CGHeroInstance & h): hero(h), QStyledItemDelegate()
  100. {
  101. }
  102. QWidget * HeroSkillsDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  103. {
  104. return new HeroSkillsWidget(hero, parent);
  105. }
  106. void HeroSkillsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  107. {
  108. if(auto * ed = qobject_cast<HeroSkillsWidget *>(editor))
  109. {
  110. ed->obtainData();
  111. }
  112. else
  113. {
  114. QStyledItemDelegate::setEditorData(editor, index);
  115. }
  116. }
  117. void HeroSkillsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  118. {
  119. if(auto * ed = qobject_cast<HeroSkillsWidget *>(editor))
  120. {
  121. ed->commitChanges();
  122. }
  123. else
  124. {
  125. QStyledItemDelegate::setModelData(editor, model, index);
  126. }
  127. }