armywidget.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. uiCounts[i]->setText("1");
  30. uiSlots[i]->addItem("");
  31. uiSlots[i]->setItemData(0, -1);
  32. for(int c = 0; c < VLC->creh->objects.size(); ++c)
  33. {
  34. auto creature = VLC->creh->objects[c];
  35. uiSlots[i]->insertItem(c + 1, creature->getNamePluralTranslated().c_str());
  36. uiSlots[i]->setItemData(c + 1, creature->getIndex());
  37. }
  38. }
  39. ui->formationTight->setChecked(true);
  40. }
  41. int ArmyWidget::searchItemIndex(int slotId, CreatureID creId) const
  42. {
  43. for(int i = 0; i < uiSlots[slotId]->count(); ++i)
  44. {
  45. if(creId.getNum() == uiSlots[slotId]->itemData(i).toInt())
  46. return i;
  47. }
  48. return 0;
  49. }
  50. void ArmyWidget::obtainData()
  51. {
  52. for(int i = 0; i < TOTAL_SLOTS; ++i)
  53. {
  54. if(army.hasStackAtSlot(SlotID(i)))
  55. {
  56. auto * creature = army.getCreature(SlotID(i));
  57. uiSlots[i]->setCurrentIndex(searchItemIndex(i, creature->getId()));
  58. uiCounts[i]->setText(QString::number(army.getStackCount(SlotID(i))));
  59. }
  60. }
  61. if(army.formation == EArmyFormation::TIGHT)
  62. ui->formationTight->setChecked(true);
  63. else
  64. ui->formationWide->setChecked(true);
  65. }
  66. bool ArmyWidget::commitChanges()
  67. {
  68. bool isArmed = false;
  69. for(int i = 0; i < TOTAL_SLOTS; ++i)
  70. {
  71. CreatureID creId(uiSlots[i]->itemData(uiSlots[i]->currentIndex()).toInt());
  72. if(creId == -1)
  73. {
  74. if(army.hasStackAtSlot(SlotID(i)))
  75. army.eraseStack(SlotID(i));
  76. }
  77. else
  78. {
  79. isArmed = true;
  80. int amount = uiCounts[i]->text().toInt();
  81. if(amount)
  82. {
  83. army.setCreature(SlotID(i), creId, amount);
  84. }
  85. else
  86. {
  87. if(army.hasStackAtSlot(SlotID(i)))
  88. army.eraseStack(SlotID(i));
  89. army.putStack(SlotID(i), new CStackInstance(creId, amount, false));
  90. }
  91. }
  92. }
  93. army.setFormation(ui->formationTight->isChecked());
  94. return isArmed;
  95. }
  96. ArmyWidget::~ArmyWidget()
  97. {
  98. delete ui;
  99. }
  100. ArmyDelegate::ArmyDelegate(CArmedInstance & t): army(t), QStyledItemDelegate()
  101. {
  102. }
  103. QWidget * ArmyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  104. {
  105. return new ArmyWidget(army, parent);
  106. }
  107. void ArmyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  108. {
  109. if(auto * ed = qobject_cast<ArmyWidget *>(editor))
  110. {
  111. ed->obtainData();
  112. }
  113. else
  114. {
  115. QStyledItemDelegate::setEditorData(editor, index);
  116. }
  117. }
  118. void ArmyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  119. {
  120. if(auto * ed = qobject_cast<ArmyWidget *>(editor))
  121. {
  122. auto isArmed = ed->commitChanges();
  123. model->setData(index, "dummy");
  124. if(isArmed)
  125. model->setData(index, "HAS ARMY");
  126. else
  127. model->setData(index, "");
  128. }
  129. else
  130. {
  131. QStyledItemDelegate::setModelData(editor, model, index);
  132. }
  133. }