armywidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 const & 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. uiSlots[i]->completer()->setCompletionMode(QCompleter::PopupCompletion);
  38. uiSlots[i]->completer()->setFilterMode(Qt::MatchContains);
  39. }
  40. ui->formationTight->setChecked(true);
  41. }
  42. int ArmyWidget::searchItemIndex(int slotId, CreatureID creId) const
  43. {
  44. for(int i = 0; i < uiSlots[slotId]->count(); ++i)
  45. {
  46. if(creId.getNum() == uiSlots[slotId]->itemData(i).toInt())
  47. return i;
  48. }
  49. return 0;
  50. }
  51. void ArmyWidget::obtainData()
  52. {
  53. for(int i = 0; i < TOTAL_SLOTS; ++i)
  54. {
  55. if(army.hasStackAtSlot(SlotID(i)))
  56. {
  57. auto * creature = army.getCreature(SlotID(i));
  58. uiSlots[i]->setCurrentIndex(searchItemIndex(i, creature->getId()));
  59. uiCounts[i]->setValue(army.getStackCount(SlotID(i)));
  60. }
  61. }
  62. if(army.formation == EArmyFormation::TIGHT)
  63. ui->formationTight->setChecked(true);
  64. else
  65. ui->formationWide->setChecked(true);
  66. }
  67. bool ArmyWidget::commitChanges()
  68. {
  69. bool isArmed = false;
  70. for(int i = 0; i < TOTAL_SLOTS; ++i)
  71. {
  72. CreatureID creId(uiSlots[i]->itemData(uiSlots[i]->currentIndex()).toInt());
  73. if(creId == CreatureID::NONE)
  74. {
  75. if(army.hasStackAtSlot(SlotID(i)))
  76. army.eraseStack(SlotID(i));
  77. }
  78. else
  79. {
  80. isArmed = true;
  81. int amount = uiCounts[i]->value();
  82. if(amount)
  83. {
  84. army.setCreature(SlotID(i), creId, amount);
  85. }
  86. else
  87. {
  88. if(army.hasStackAtSlot(SlotID(i)))
  89. army.eraseStack(SlotID(i));
  90. army.putStack(SlotID(i), new CStackInstance(creId, amount, false));
  91. }
  92. }
  93. }
  94. army.setFormation(ui->formationTight->isChecked() ? EArmyFormation::TIGHT : EArmyFormation::LOOSE );
  95. return isArmed;
  96. }
  97. ArmyWidget::~ArmyWidget()
  98. {
  99. delete ui;
  100. }
  101. ArmyDelegate::ArmyDelegate(CArmedInstance & t)
  102. : BaseInspectorItemDelegate()
  103. , army(t)
  104. {
  105. }
  106. QWidget * ArmyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  107. {
  108. return new ArmyWidget(army, parent);
  109. }
  110. void ArmyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  111. {
  112. if(auto * ed = qobject_cast<ArmyWidget *>(editor))
  113. {
  114. ed->obtainData();
  115. }
  116. else
  117. {
  118. QStyledItemDelegate::setEditorData(editor, index);
  119. }
  120. }
  121. void ArmyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  122. {
  123. if(auto * ed = qobject_cast<ArmyWidget *>(editor))
  124. {
  125. ed->commitChanges();
  126. updateModelData(model, index);
  127. }
  128. else
  129. {
  130. QStyledItemDelegate::setModelData(editor, model, index);
  131. }
  132. }
  133. void ArmyDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
  134. {
  135. QStringList textList;
  136. for(const auto & [_, stack] : army.stacks)
  137. {
  138. if(stack->count != 0 && stack->getCreature() != nullptr)
  139. textList += QString("%1 %2").arg(stack->count).arg(QString::fromStdString(stack->getCreature()->getNamePluralTranslated()));
  140. }
  141. setModelTextData(model, index, textList);
  142. }