armywidget.cpp 3.9 KB

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