1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-08 22:26:51 +02:00

Merge pull request #5208 from godric3/map-editor-improve-inspector

map editor: show edited values in inspector
This commit is contained in:
Ivan Savenko
2025-01-10 15:25:29 +02:00
committed by GitHub
28 changed files with 408 additions and 48 deletions

View File

@ -84,6 +84,7 @@ set(editor_HEADERS
inspector/herospellwidget.h
inspector/PickObjectDelegate.h
inspector/portraitwidget.h
inspector/baseinspectoritemdelegate.h
resourceExtractor/ResourceConverter.h
mapeditorroles.h
)

View File

@ -11,6 +11,7 @@
#pragma once
#include <QItemDelegate>
#include "baseinspectoritemdelegate.h"
class MapController;
@ -20,7 +21,7 @@ class CGObjectInstance;
VCMI_LIB_NAMESPACE_END
class PickObjectDelegate : public QItemDelegate
class PickObjectDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:

View File

@ -115,7 +115,7 @@ ArmyWidget::~ArmyWidget()
ArmyDelegate::ArmyDelegate(CArmedInstance & t): army(t), QStyledItemDelegate()
ArmyDelegate::ArmyDelegate(CArmedInstance & t): army(t), BaseInspectorItemDelegate()
{
}
@ -141,9 +141,22 @@ void ArmyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, cons
if(auto * ed = qobject_cast<ArmyWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void ArmyDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList;
for(const auto & [_, stack] : army.stacks)
{
if(stack->count != 0 && stack->getCreature() != nullptr)
textList += QString("%1 %2").arg(stack->count).arg(QString::fromStdString(stack->getCreature()->getNamePluralTranslated()));
}
setModelTextData(model, index, textList);
}

View File

@ -11,6 +11,7 @@
#include "../StdInc.h"
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../lib/mapObjects/CArmedInstance.h"
const int TOTAL_SLOTS = 7;
@ -39,17 +40,18 @@ private:
std::array<QComboBox*, TOTAL_SLOTS> uiSlots;
};
class ArmyDelegate : public QStyledItemDelegate
class ArmyDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
ArmyDelegate(CArmedInstance &);
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CArmedInstance & army;

View File

@ -0,0 +1,30 @@
/*
* baseinspectoritemdelegate.h, 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
*
*/
#pragma once
#include <QStyledItemDelegate>
#include <QAbstractItemModel>
class BaseInspectorItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
virtual void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const {};
void setModelTextData(QAbstractItemModel * model, const QModelIndex & index, const QStringList & list) const
{
QString text = list.join("\n");
model->setItemData(index, {
{Qt::DisplayRole, QVariant{text}},
{Qt::ToolTipRole, QVariant{text}}
});
};
};

View File

@ -110,7 +110,7 @@ void HeroArtifactsWidget::commitChanges()
}
}
HeroArtifactsDelegate::HeroArtifactsDelegate(CGHeroInstance & h) : QStyledItemDelegate(), hero(h)
HeroArtifactsDelegate::HeroArtifactsDelegate(CGHeroInstance & h) : BaseInspectorItemDelegate(), hero(h)
{
}
@ -136,9 +136,26 @@ void HeroArtifactsDelegate::setModelData(QWidget * editor, QAbstractItemModel *
if (auto * ed = qobject_cast<HeroArtifactsWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void HeroArtifactsDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList;
for(const auto & [artPosition, artSlotInfo] : hero.artifactsWorn)
{
auto slotText = NArtifactPosition::namesHero[artPosition.num];
textList += QString("%1: %2").arg(QString::fromStdString(slotText)).arg(QString::fromStdString(artSlotInfo.artifact->getType()->getNameTranslated()));
}
textList += QString("%1:").arg(QString::fromStdString(NArtifactPosition::backpack));
for(const auto & art : hero.artifactsInBackpack)
{
textList += QString::fromStdString(art.artifact->getType()->getNameTranslated());
}
setModelTextData(model, index, textList);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
namespace Ui {
@ -48,17 +49,18 @@ private:
};
class HeroArtifactsDelegate : public QStyledItemDelegate
class HeroArtifactsDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
HeroArtifactsDelegate(CGHeroInstance &);
QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
void setEditorData(QWidget * editor, const QModelIndex & index) const override;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CGHeroInstance & hero;

View File

@ -11,6 +11,7 @@
#include "heroskillswidget.h"
#include "ui_heroskillswidget.h"
#include "../../lib/constants/StringConstants.h"
#include "../../lib/entities/hero/CHeroHandler.h"
#include "../../lib/CSkillHandler.h"
#include "inspector.h"
@ -115,7 +116,7 @@ void HeroSkillsWidget::commitChanges()
}
}
HeroSkillsDelegate::HeroSkillsDelegate(CGHeroInstance & h): hero(h), QStyledItemDelegate()
HeroSkillsDelegate::HeroSkillsDelegate(CGHeroInstance & h): hero(h), BaseInspectorItemDelegate()
{
}
@ -141,9 +142,34 @@ void HeroSkillsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model
if(auto * ed = qobject_cast<HeroSkillsWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void HeroSkillsDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList;
textList += QString("%1: %2").arg(QString::fromStdString(VLC->generaltexth->primarySkillNames[PrimarySkill::ATTACK])).arg(hero.getBasePrimarySkillValue(PrimarySkill::ATTACK));
textList += QString("%1: %2").arg(QString::fromStdString(VLC->generaltexth->primarySkillNames[PrimarySkill::DEFENSE])).arg(hero.getBasePrimarySkillValue(PrimarySkill::DEFENSE));
textList += QString("%1: %2").arg(QString::fromStdString(VLC->generaltexth->primarySkillNames[PrimarySkill::SPELL_POWER])).arg(hero.getBasePrimarySkillValue(PrimarySkill::SPELL_POWER));
textList += QString("%1: %2").arg(QString::fromStdString(VLC->generaltexth->primarySkillNames[PrimarySkill::KNOWLEDGE])).arg(hero.getBasePrimarySkillValue(PrimarySkill::KNOWLEDGE));
;
auto heroSecondarySkills = hero.secSkills;
if(heroSecondarySkills.size() == 1 && heroSecondarySkills[0].first == SecondarySkill::NONE)
{
textList += QObject::tr("Default secondary skills:");
heroSecondarySkills = hero.getHeroType()->secSkillsInit;
}
else
{
textList += QObject::tr("Secondary skills:");
}
for(auto & [skill, skillLevel] : heroSecondarySkills)
textList += QString("%1 %2").arg(QString::fromStdString(VLC->skillh->getById(skill)->getNameTranslated())).arg(LevelIdentifiers[skillLevel - 1].first);
setModelTextData(model, index, textList);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
namespace Ui {
@ -42,17 +43,18 @@ private:
std::set<int> occupiedSkills;
};
class HeroSkillsDelegate : public QStyledItemDelegate
class HeroSkillsDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
HeroSkillsDelegate(CGHeroInstance &);
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CGHeroInstance & hero;

View File

@ -101,7 +101,7 @@ void HeroSpellWidget::on_customizeSpells_toggled(bool checked)
initSpellLists();
}
HeroSpellDelegate::HeroSpellDelegate(CGHeroInstance & h) : hero(h), QStyledItemDelegate()
HeroSpellDelegate::HeroSpellDelegate(CGHeroInstance & h) : hero(h), BaseInspectorItemDelegate()
{
}
@ -127,9 +127,29 @@ void HeroSpellDelegate::setModelData(QWidget * editor, QAbstractItemModel * mode
if (auto * ed = qobject_cast<HeroSpellWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void HeroSpellDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList;
if(hero.spellbookContainsSpell(SpellID::PRESET))
{
textList += QObject::tr("Custom Spells:");
for(auto const & spellID : hero.getSpellsInSpellbook())
{
if(spellID != SpellID::PRESET)
textList += QString::fromStdString(spellID.toSpell()->getNameTranslated());
}
}
else
{
textList += QObject::tr("Default Spells");
}
setModelTextData(model, index, textList);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
namespace Ui {
@ -39,17 +40,18 @@ private:
void initSpellLists();
};
class HeroSpellDelegate : public QStyledItemDelegate
class HeroSpellDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
HeroSpellDelegate(CGHeroInstance &);
QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex& index) const override;
void setEditorData(QWidget * editor, const QModelIndex & index) const override;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CGHeroInstance & hero;

View File

@ -20,6 +20,7 @@
#include "../lib/mapObjects/ObjectTemplate.h"
#include "../lib/mapping/CMap.h"
#include "../lib/constants/StringConstants.h"
#include "../lib/CPlayerState.h"
#include "../lib/texts/CGeneralTextHandler.h"
#include "townbuildingswidget.h"
@ -249,7 +250,7 @@ void Inspector::updateProperties(CGDwelling * o)
if (o->ID == Obj::RANDOM_DWELLING || o->ID == Obj::RANDOM_DWELLING_LVL)
{
auto * delegate = new PickObjectDelegate(controller, PickObjectDelegate::typedFilter<CGTownInstance>);
addProperty(QObject::tr("Same as town"), PropertyEditorPlaceholder(), delegate, false);
addProperty(QObject::tr("Same as town"), o->randomizationInfo, delegate, false);
}
}
@ -855,6 +856,7 @@ QTableWidgetItem * Inspector::addProperty(const QString & value)
{
auto * item = new QTableWidgetItem(value);
item->setFlags(Qt::NoItemFlags);
item->setToolTip(value);
return item;
}
@ -910,6 +912,17 @@ QTableWidgetItem * Inspector::addProperty(CGCreature::Character value)
return item;
}
QTableWidgetItem * Inspector::addProperty(const std::optional<CGDwellingRandomizationInfo> & value)
{
QString text = QObject::tr("Select town");
if(value.has_value() && !value->instanceId.empty())
text = QString::fromStdString(value->instanceId);
auto * item = new QTableWidgetItem(text);
item->setFlags(Qt::NoItemFlags);
return item;
}
//========================================================================
Inspector::Inspector(MapController & c, CGObjectInstance * o, QTableWidget * t): obj(o), table(t), controller(c)

View File

@ -13,6 +13,7 @@
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QStyledItemDelegate>
#include "baseinspectoritemdelegate.h"
#include "../lib/int3.h"
#include "../lib/GameConstants.h"
#include "../lib/mapObjects/CGCreature.h"
@ -101,6 +102,7 @@ protected:
QTableWidgetItem * addProperty(bool value);
QTableWidgetItem * addProperty(CGObjectInstance * value);
QTableWidgetItem * addProperty(CGCreature::Character value);
QTableWidgetItem * addProperty(const std::optional<CGDwellingRandomizationInfo> & value);
QTableWidgetItem * addProperty(PropertyEditorPlaceholder value);
//===============END OF DECLARATION=======================================
@ -117,7 +119,7 @@ public:
protected:
template<class T>
void addProperty(const QString & key, const T & value, QAbstractItemDelegate * delegate, bool restricted)
void addProperty(const QString & key, const T & value, BaseInspectorItemDelegate * delegate, bool restricted)
{
auto * itemValue = addProperty(value);
if(!restricted)
@ -144,6 +146,11 @@ protected:
++row;
}
itemKey->setFlags(restricted ? Qt::NoItemFlags : Qt::ItemIsEnabled);
if(delegate != nullptr)
{
QModelIndex index = table->model()->index(itemValue->row(), itemValue->column());
delegate->updateModelData(table->model(), index);
}
}
template<class T>
@ -161,11 +168,11 @@ protected:
};
class InspectorDelegate : public QStyledItemDelegate
class InspectorDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;

View File

@ -9,6 +9,7 @@
*/
#pragma once
#include "../StdInc.h"
#include "baseinspectoritemdelegate.h"
#include <QDialog>
namespace Ui {
@ -31,11 +32,11 @@ private:
};
class MessageDelegate : public QStyledItemDelegate
class MessageDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;

View File

@ -102,7 +102,7 @@ void PortraitWidget::on_buttonPrev_clicked()
drawPortrait();
}
PortraitDelegate::PortraitDelegate(CGHeroInstance & h): hero(h), QStyledItemDelegate()
PortraitDelegate::PortraitDelegate(CGHeroInstance & h): hero(h), BaseInspectorItemDelegate()
{
}
@ -128,9 +128,21 @@ void PortraitDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
if(auto * ed = qobject_cast<PortraitWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void PortraitDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QMap<int, QVariant> data;
if(hero.customPortraitSource == HeroTypeID::NONE)
data[Qt::DisplayRole] = QObject::tr("Default");
else
data[Qt::DisplayRole] = QString::fromStdString(hero.customPortraitSource.toHeroType()->getNameTranslated());
model->setItemData(index, data);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
namespace Ui {
@ -47,17 +48,18 @@ private:
int portraitIndex;
};
class PortraitDelegate : public QStyledItemDelegate
class PortraitDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
PortraitDelegate(CGHeroInstance &);
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CGHeroInstance & hero;

View File

@ -391,7 +391,7 @@ void QuestWidget::on_lCreatureRemove_clicked()
ui->lCreatures->removeRow(i);
}
QuestDelegate::QuestDelegate(MapController & c, CQuest & t): controller(c), quest(t), QStyledItemDelegate()
QuestDelegate::QuestDelegate(MapController & c, CQuest & t): controller(c), quest(t), BaseInspectorItemDelegate()
{
}
@ -417,6 +417,7 @@ void QuestDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, c
if(auto *ed = qobject_cast<QuestWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
@ -439,3 +440,81 @@ bool QuestDelegate::eventFilter(QObject * object, QEvent * event)
}
return QStyledItemDelegate::eventFilter(object, event);
}
void QuestDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList(QObject::tr("Quest:"));
textList += QObject::tr("Day of Week: %1").arg(quest.mission.dayOfWeek);
textList += QObject::tr("Days Passed: %1").arg(quest.mission.daysPassed);
textList += QObject::tr("Hero Level: %1").arg(quest.mission.heroLevel);
textList += QObject::tr("Hero Experience: %1").arg(quest.mission.heroExperience);
textList += QObject::tr("Mana Points: %1").arg(quest.mission.manaPoints);
textList += QObject::tr("Mana Percentage: %1").arg(quest.mission.manaPercentage);
textList += QObject::tr("Primary Skills: %1/%2/%3/%4").arg(quest.mission.primary[0]).arg(quest.mission.primary[1]).arg(quest.mission.primary[2]).arg(quest.mission.primary[3]);
QStringList resourcesList;
for(GameResID resource = GameResID::WOOD; resource < GameResID::COUNT ; resource++)
{
if(resource == GameResID::MITHRIL)
continue;
if(quest.mission.resources[resource] == 0)
continue;
MetaString str;
str.appendName(resource);
resourcesList += QString("%1: %2").arg(QString::fromStdString(str.toString())).arg(quest.mission.resources[resource]);
}
textList += QObject::tr("Resources: %1").arg(resourcesList.join(", "));
QStringList artifactsList;
for(auto artifact : quest.mission.artifacts)
{
artifactsList += QString::fromStdString(VLC->artifacts()->getById(artifact)->getNameTranslated());
}
textList += QObject::tr("Artifacts: %1").arg(artifactsList.join(", "));
QStringList spellsList;
for(auto spell : quest.mission.spells)
{
spellsList += QString::fromStdString(VLC->spells()->getById(spell)->getNameTranslated());
}
textList += QObject::tr("Spells: %1").arg(spellsList.join(", "));
QStringList secondarySkillsList;
for(auto & [skill, skillLevel] : quest.mission.secondary)
{
secondarySkillsList += QString("%1 (%2)").arg(QString::fromStdString(VLC->skills()->getById(skill)->getNameTranslated())).arg(skillLevel);
}
textList += QObject::tr("Secondary Skills: %1").arg(secondarySkillsList.join(", "));
QStringList creaturesList;
for(auto & creature : quest.mission.creatures)
{
creaturesList += QString("%1 %2").arg(creature.count).arg(QString::fromStdString(creature.getType()->getNamePluralTranslated()));
}
textList += QObject::tr("Creatures: %1").arg(creaturesList.join(", "));
QStringList heroesList;
for(auto & hero : quest.mission.heroes)
{
heroesList += QString::fromStdString(VLC->heroTypes()->getById(hero)->getNameTranslated());
}
textList += QObject::tr("Heroes: %1").arg(heroesList.join(", "));
QStringList heroClassesList;
for(auto & heroClass : quest.mission.heroClasses)
{
heroClassesList += QString::fromStdString(VLC->heroClasses()->getById(heroClass)->getNameTranslated());
}
textList += QObject::tr("Hero Classes: %1").arg(heroClassesList.join(", "));
QStringList playersList;
for(auto & player : quest.mission.players)
{
MetaString str;
str.appendName(player);
playersList += QString::fromStdString(str.toString());
}
textList += QObject::tr("Players: %1").arg(playersList.join(", "));
setModelTextData(model, index, textList);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include "../StdInc.h"
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../lib/mapObjects/CQuest.h"
namespace Ui {
@ -46,17 +47,18 @@ private:
Ui::QuestWidget *ui;
};
class QuestDelegate : public QStyledItemDelegate
class QuestDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
QuestDelegate(MapController &, CQuest &);
QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
void setEditorData(QWidget * editor, const QModelIndex & index) const override;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
protected:
bool eventFilter(QObject * object, QEvent * event) override;

View File

@ -742,6 +742,7 @@ void RewardsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, c
if(auto * ed = qobject_cast<RewardsWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
@ -757,3 +758,66 @@ QWidget * RewardsDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
{
return new RewardsWidget(map, object, parent);
}
void RewardsDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList(QObject::tr("Rewards:"));
for (const auto & vinfo : object.configuration.info)
{
textList += QObject::tr("Reward Message: %1").arg(QString::fromStdString(vinfo.message.toString()));
textList += QObject::tr("Hero Level: %1").arg(vinfo.reward.heroLevel);
textList += QObject::tr("Hero Experience: %1").arg(vinfo.reward.heroExperience);
textList += QObject::tr("Mana Diff: %1").arg(vinfo.reward.manaDiff);
textList += QObject::tr("Mana Percentage: %1").arg(vinfo.reward.manaPercentage);
textList += QObject::tr("Move Points: %1").arg(vinfo.reward.movePoints);
textList += QObject::tr("Move Percentage: %1").arg(vinfo.reward.movePercentage);
textList += QObject::tr("Primary Skills: %1/%2/%3/%4").arg(vinfo.reward.primary[0]).arg(vinfo.reward.primary[1]).arg(vinfo.reward.primary[2]).arg(vinfo.reward.primary[3]);
QStringList resourcesList;
for(GameResID resource = GameResID::WOOD; resource < GameResID::COUNT ; resource++)
{
if(resource == GameResID::MITHRIL)
continue; // translated as "Abandoned"?
if(vinfo.reward.resources[resource] == 0)
continue;
MetaString str;
str.appendName(resource);
resourcesList += QString("%1: %2").arg(QString::fromStdString(str.toString())).arg(vinfo.reward.resources[resource]);
}
textList += QObject::tr("Resources: %1").arg(resourcesList.join(", "));
QStringList artifactsList;
for (auto artifact : vinfo.reward.artifacts)
{
artifactsList += QString::fromStdString(VLC->artifacts()->getById(artifact)->getNameTranslated());
}
textList += QObject::tr("Artifacts: %1").arg(artifactsList.join(", "));
QStringList spellsList;
for (auto spell : vinfo.reward.spells)
{
spellsList += QString::fromStdString(VLC->spells()->getById(spell)->getNameTranslated());
}
textList += QObject::tr("Spells: %1").arg(spellsList.join(", "));
QStringList secondarySkillsList;
for(auto & [skill, skillLevel] : vinfo.reward.secondary)
{
secondarySkillsList += QString("%1 (%2)").arg(QString::fromStdString(VLC->skills()->getById(skill)->getNameTranslated())).arg(skillLevel);
}
textList += QObject::tr("Secondary Skills: %1").arg(secondarySkillsList.join(", "));
QStringList creaturesList;
for (auto & creature : vinfo.reward.creatures)
{
creaturesList += QString("%1 %2").arg(creature.count).arg(QString::fromStdString(creature.getType()->getNamePluralTranslated()));
}
textList += QObject::tr("Creatures: %1").arg(creaturesList.join(", "));
if (vinfo.reward.spellCast.first != SpellID::NONE)
{
textList += QObject::tr("Spell Cast: %1 (%2)").arg(QString::fromStdString(VLC->spells()->getById(vinfo.reward.spellCast.first)->getNameTranslated())).arg(vinfo.reward.spellCast.second);
}
QStringList bonusesList;
for (auto & bonus : vinfo.reward.bonuses)
{
bonusesList += QString("%1 %2 (%3)").arg(QString::fromStdString(vstd::findKey(bonusDurationMap, bonus.duration))).arg(QString::fromStdString(vstd::findKey(bonusNameMap, bonus.type))).arg(bonus.val);
}
textList += QObject::tr("Bonuses: %1").arg(bonusesList.join(", "));
}
setModelTextData(model, index, textList);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include "../StdInc.h"
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../lib/mapObjects/CRewardableObject.h"
namespace Ui {
@ -67,18 +68,19 @@ private:
CMap & map;
};
class RewardsDelegate : public QStyledItemDelegate
class RewardsDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
RewardsDelegate(CMap &, CRewardableObject &);
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CRewardableObject & object;

View File

@ -318,7 +318,7 @@ void TownBuildingsWidget::onItemChanged(const QStandardItem * item) {
connect(&model, &QStandardItemModel::itemChanged, this, &TownBuildingsWidget::onItemChanged);
}
TownBuildingsDelegate::TownBuildingsDelegate(CGTownInstance & t): town(t), QStyledItemDelegate()
TownBuildingsDelegate::TownBuildingsDelegate(CGTownInstance & t): town(t), BaseInspectorItemDelegate()
{
}
@ -332,11 +332,6 @@ void TownBuildingsDelegate::setEditorData(QWidget *editor, const QModelIndex &in
if(auto * ed = qobject_cast<TownBuildingsWidget *>(editor))
{
auto * ctown = town.getTown();
if(!ctown)
ctown = VLC->townh->randomTown;
if(!ctown)
throw std::runtime_error("No Town defined for type selected");
ed->addBuildings(*ctown);
}
else
@ -353,6 +348,7 @@ void TownBuildingsDelegate::setModelData(QWidget *editor, QAbstractItemModel *mo
town.removeAllBuildings();
for(const auto & building : ed->getBuiltBuildings())
town.addBuilding(building);
updateModelData(model, index);
}
else
{
@ -360,4 +356,26 @@ void TownBuildingsDelegate::setModelData(QWidget *editor, QAbstractItemModel *mo
}
}
void TownBuildingsDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList(QObject::tr("Built buildings:"));
auto * ctown = town.getTown();
for(const auto & buildingID : town.getBuildings())
{
if(buildingID == BuildingID::DEFAULT)
continue;
auto name = QString::fromStdString(ctown->buildings.at(buildingID)->getNameTranslated());
if(name.isEmpty())
name = QString::fromStdString(defaultBuildingIdConversion(buildingID));
textList += name;
}
textList += QObject::tr("Forbidden buildings:");
for(const auto & buildingID : town.forbiddenBuildings)
{
auto name = QString::fromStdString(ctown->buildings.at(buildingID)->getNameTranslated());
if(name.isEmpty())
name = QString::fromStdString(defaultBuildingIdConversion(buildingID));
textList += name;
}
setModelTextData(model, index, textList);
}

View File

@ -11,6 +11,7 @@
#include "../StdInc.h"
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../lib/mapObjects/CGTownInstance.h"
namespace Ui {
@ -66,17 +67,18 @@ private:
mutable QStandardItemModel model;
};
class TownBuildingsDelegate : public QStyledItemDelegate
class TownBuildingsDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
TownBuildingsDelegate(CGTownInstance &);
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CGTownInstance & town;

View File

@ -105,10 +105,6 @@ void TownEventDialog::initResources()
void TownEventDialog::initBuildings()
{
auto * ctown = town.getTown();
if (!ctown)
ctown = VLC->townh->randomTown;
if (!ctown)
throw std::runtime_error("No Town defined for type selected");
auto allBuildings = ctown->getAllBuildings();
while (!allBuildings.empty())
{

View File

@ -143,7 +143,7 @@ void TownEventsWidget::on_eventsList_itemActivated(QListWidgetItem* item)
}
TownEventsDelegate::TownEventsDelegate(CGTownInstance & town, MapController & c) : QStyledItemDelegate(), town(town), controller(c)
TownEventsDelegate::TownEventsDelegate(CGTownInstance & town, MapController & c) : BaseInspectorItemDelegate(), town(town), controller(c)
{
}
@ -169,9 +169,21 @@ void TownEventsDelegate::setModelData(QWidget * editor, QAbstractItemModel * mod
if (auto * ed = qobject_cast<TownEventsWidget *>(editor))
{
ed->commitChanges(controller);
updateModelData(model, index);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void TownEventsDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList eventList(QObject::tr("Town Events:"));
for (const auto & event : town.events)
{
auto eventName = QString::fromStdString(event.name);
eventList += tr("Day %1 - %2").arg(event.firstOccurrence + 1).arg(eventName);
}
setModelTextData(model, index, eventList);
}

View File

@ -11,6 +11,7 @@
#include "../StdInc.h"
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../lib/mapping/CMapDefines.h"
#include "../lib/mapObjects/CGTownInstance.h"
#include "../mapcontroller.h"
@ -40,17 +41,18 @@ private:
CGTownInstance & town;
};
class TownEventsDelegate : public QStyledItemDelegate
class TownEventsDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
TownEventsDelegate(CGTownInstance &, MapController &);
QWidget* createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
void setEditorData(QWidget * editor, const QModelIndex & index) const override;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CGTownInstance & town;

View File

@ -132,7 +132,7 @@ void TownSpellsWidget::on_customizeSpells_toggled(bool checked)
initSpellLists();
}
TownSpellsDelegate::TownSpellsDelegate(CGTownInstance & town) : QStyledItemDelegate(), town(town)
TownSpellsDelegate::TownSpellsDelegate(CGTownInstance & town) : BaseInspectorItemDelegate(), town(town)
{
}
@ -158,9 +158,41 @@ void TownSpellsDelegate::setModelData(QWidget * editor, QAbstractItemModel * mod
if (auto * ed = qobject_cast<TownSpellsWidget *>(editor))
{
ed->commitChanges();
updateModelData(model, index);
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void TownSpellsDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
{
QStringList textList;
QStringList requiredSpellsList;
QStringList possibleSpellsList;
if(vstd::contains(town.possibleSpells, SpellID::PRESET)) {
textList += QObject::tr("Custom Spells:");
textList += QObject::tr("Required:");
for(auto spellID : town.obligatorySpells)
{
auto spell = spellID.toEntity(VLC);
requiredSpellsList += QString::fromStdString(spell->getNameTranslated());
}
textList += requiredSpellsList.join(",");
textList += QObject::tr("Possible:");
for(auto spellID : town.possibleSpells)
{
if(spellID == SpellID::PRESET)
continue;
auto spell = spellID.toEntity(VLC);
possibleSpellsList += QString::fromStdString(spell->getNameTranslated());
}
textList += possibleSpellsList.join(",");
}
else
{
textList += QObject::tr("Default Spells");
}
setModelTextData(model, index, textList);
}

View File

@ -10,6 +10,7 @@
#pragma once
#include <QDialog>
#include "baseinspectoritemdelegate.h"
#include "../../lib/mapObjects/CGTownInstance.h"
namespace Ui {
@ -43,17 +44,18 @@ private:
void initSpellLists();
};
class TownSpellsDelegate : public QStyledItemDelegate
class TownSpellsDelegate : public BaseInspectorItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
using BaseInspectorItemDelegate::BaseInspectorItemDelegate;
TownSpellsDelegate(CGTownInstance&);
QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex& index) const override;
void setEditorData(QWidget * editor, const QModelIndex & index) const override;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const override;
void updateModelData(QAbstractItemModel * model, const QModelIndex & index) const override;
private:
CGTownInstance& town;

View File

@ -80,8 +80,6 @@ PlayerParams::PlayerParams(MapController & ctrl, int playerId, QWidget *parent)
if(auto town = dynamic_cast<CGTownInstance*>(controller.map()->objects[i].get()))
{
auto * ctown = town->getTown();
if(!ctown)
ctown = VLC->townh->randomTown;
if(ctown && town->getOwner().getNum() == playerColor)
{