armywidget.cpp 4.0 KB

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