| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /*
- * armywidget.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #include "StdInc.h"
- #include "armywidget.h"
- #include "ui_armywidget.h"
- #include "CCreatureHandler.h"
- ArmyWidget::ArmyWidget(CArmedInstance & a, QWidget *parent) :
- QDialog(parent),
- army(a),
- ui(new Ui::ArmyWidget)
- {
- ui->setupUi(this);
-
- uiCounts[0] = ui->count0; uiSlots[0] = ui->slot0;
- uiCounts[1] = ui->count1; uiSlots[1] = ui->slot1;
- uiCounts[2] = ui->count2; uiSlots[2] = ui->slot2;
- uiCounts[3] = ui->count3; uiSlots[3] = ui->slot3;
- uiCounts[4] = ui->count4; uiSlots[4] = ui->slot4;
- uiCounts[5] = ui->count5; uiSlots[5] = ui->slot5;
- uiCounts[6] = ui->count6; uiSlots[6] = ui->slot6;
-
- for(int i = 0; i < TOTAL_SLOTS; ++i)
- {
- uiCounts[i]->setText("1");
- uiSlots[i]->addItem("");
- uiSlots[i]->setItemData(0, -1);
-
- for(int c = 0; c < VLC->creh->objects.size(); ++c)
- {
- auto creature = VLC->creh->objects[c];
- uiSlots[i]->insertItem(c + 1, creature->getNamePluralTranslated().c_str());
- uiSlots[i]->setItemData(c + 1, creature->getIndex());
- }
- }
-
- ui->formationTight->setChecked(true);
- }
- int ArmyWidget::searchItemIndex(int slotId, CreatureID creId) const
- {
- for(int i = 0; i < uiSlots[slotId]->count(); ++i)
- {
- if(creId.getNum() == uiSlots[slotId]->itemData(i).toInt())
- return i;
- }
- return 0;
- }
- void ArmyWidget::obtainData()
- {
- for(int i = 0; i < TOTAL_SLOTS; ++i)
- {
- if(army.hasStackAtSlot(SlotID(i)))
- {
- auto * creature = army.getCreature(SlotID(i));
- uiSlots[i]->setCurrentIndex(searchItemIndex(i, creature->getId()));
- uiCounts[i]->setText(QString::number(army.getStackCount(SlotID(i))));
- }
- }
-
- if(army.formation == EArmyFormation::TIGHT)
- ui->formationTight->setChecked(true);
- else
- ui->formationWide->setChecked(true);
- }
- bool ArmyWidget::commitChanges()
- {
- bool isArmed = false;
- for(int i = 0; i < TOTAL_SLOTS; ++i)
- {
- CreatureID creId(uiSlots[i]->itemData(uiSlots[i]->currentIndex()).toInt());
- if(creId == -1)
- {
- if(army.hasStackAtSlot(SlotID(i)))
- army.eraseStack(SlotID(i));
- }
- else
- {
- isArmed = true;
- int amount = uiCounts[i]->text().toInt();
- if(amount)
- {
- army.setCreature(SlotID(i), creId, amount);
- }
- else
- {
- if(army.hasStackAtSlot(SlotID(i)))
- army.eraseStack(SlotID(i));
- army.putStack(SlotID(i), new CStackInstance(creId, amount, false));
- }
- }
- }
-
- army.setFormation(ui->formationTight->isChecked());
- return isArmed;
- }
- ArmyWidget::~ArmyWidget()
- {
- delete ui;
- }
- ArmyDelegate::ArmyDelegate(CArmedInstance & t): army(t), QStyledItemDelegate()
- {
- }
- QWidget * ArmyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- return new ArmyWidget(army, parent);
- }
- void ArmyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
- {
- if(auto * ed = qobject_cast<ArmyWidget *>(editor))
- {
- ed->obtainData();
- }
- else
- {
- QStyledItemDelegate::setEditorData(editor, index);
- }
- }
- void ArmyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
- {
- if(auto * ed = qobject_cast<ArmyWidget *>(editor))
- {
- auto isArmed = ed->commitChanges();
- model->setData(index, "dummy");
- if(isArmed)
- model->setData(index, "HAS ARMY");
- else
- model->setData(index, "");
- }
- else
- {
- QStyledItemDelegate::setModelData(editor, model, index);
- }
- }
|