armywidget.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * armywidget.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 "armywidget.h"
  12. #include "ui_armywidget.h"
  13. #include "CCreatureHandler.h"
  14. ArmyWidget::ArmyWidget(CArmedInstance & a, QWidget *parent) :
  15. QDialog(parent),
  16. army(a),
  17. ui(new Ui::ArmyWidget)
  18. {
  19. ui->setupUi(this);
  20. uiCounts[0] = ui->count0; uiSlots[0] = ui->slot0;
  21. uiCounts[1] = ui->count1; uiSlots[1] = ui->slot1;
  22. uiCounts[2] = ui->count2; uiSlots[2] = ui->slot2;
  23. uiCounts[3] = ui->count3; uiSlots[3] = ui->slot3;
  24. uiCounts[4] = ui->count4; uiSlots[4] = ui->slot4;
  25. uiCounts[5] = ui->count5; uiSlots[5] = ui->slot5;
  26. uiCounts[6] = ui->count6; uiSlots[6] = ui->slot6;
  27. for(int i = 0; i < TOTAL_SLOTS; ++i)
  28. {
  29. uiSlots[i]->addItem("");
  30. uiSlots[i]->setItemData(0, -1);
  31. for(int c = 0; c < VLC->creh->objects.size(); ++c)
  32. {
  33. auto creature = VLC->creh->objects[c];
  34. uiSlots[i]->insertItem(c + 1, creature->getNamePluralTranslated().c_str());
  35. uiSlots[i]->setItemData(c + 1, creature->getIndex());
  36. }
  37. }
  38. ui->formationTight->setChecked(true);
  39. }
  40. int ArmyWidget::searchItemIndex(int slotId, CreatureID creId) const
  41. {
  42. for(int i = 0; i < uiSlots[slotId]->count(); ++i)
  43. {
  44. if(creId.getNum() == uiSlots[slotId]->itemData(i).toInt())
  45. return i;
  46. }
  47. return 0;
  48. }
  49. void ArmyWidget::obtainData()
  50. {
  51. for(int i = 0; i < TOTAL_SLOTS; ++i)
  52. {
  53. if(army.hasStackAtSlot(SlotID(i)))
  54. {
  55. auto * creature = army.getCreature(SlotID(i));
  56. uiSlots[i]->setCurrentIndex(searchItemIndex(i, creature->getId()));
  57. uiCounts[i]->setValue(army.getStackCount(SlotID(i)));
  58. }
  59. }
  60. if(army.formation == EArmyFormation::TIGHT)
  61. ui->formationTight->setChecked(true);
  62. else
  63. ui->formationWide->setChecked(true);
  64. }
  65. bool ArmyWidget::commitChanges()
  66. {
  67. bool isArmed = false;
  68. for(int i = 0; i < TOTAL_SLOTS; ++i)
  69. {
  70. CreatureID creId(uiSlots[i]->itemData(uiSlots[i]->currentIndex()).toInt());
  71. if(creId == CreatureID::NONE)
  72. {
  73. if(army.hasStackAtSlot(SlotID(i)))
  74. army.eraseStack(SlotID(i));
  75. }
  76. else
  77. {
  78. isArmed = true;
  79. int amount = uiCounts[i]->value();
  80. if(amount)
  81. {
  82. army.setCreature(SlotID(i), creId, amount);
  83. }
  84. else
  85. {
  86. if(army.hasStackAtSlot(SlotID(i)))
  87. army.eraseStack(SlotID(i));
  88. army.putStack(SlotID(i), new CStackInstance(creId, amount, false));
  89. }
  90. }
  91. }
  92. army.setFormation(ui->formationTight->isChecked());
  93. return isArmed;
  94. }
  95. ArmyWidget::~ArmyWidget()
  96. {
  97. delete ui;
  98. }
  99. ArmyDelegate::ArmyDelegate(CArmedInstance & t): army(t), QStyledItemDelegate()
  100. {
  101. }
  102. QWidget * ArmyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  103. {
  104. return new ArmyWidget(army, parent);
  105. }
  106. void ArmyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  107. {
  108. if(auto * ed = qobject_cast<ArmyWidget *>(editor))
  109. {
  110. ed->obtainData();
  111. }
  112. else
  113. {
  114. QStyledItemDelegate::setEditorData(editor, index);
  115. }
  116. }
  117. void ArmyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  118. {
  119. if(auto * ed = qobject_cast<ArmyWidget *>(editor))
  120. {
  121. ed->commitChanges();
  122. }
  123. else
  124. {
  125. QStyledItemDelegate::setModelData(editor, model, index);
  126. }
  127. }