inspector.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * inspector.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 "inspector.h"
  12. #include "../lib/ArtifactUtils.h"
  13. #include "../lib/CArtHandler.h"
  14. #include "../lib/spells/CSpellHandler.h"
  15. #include "../lib/CHeroHandler.h"
  16. #include "../lib/CRandomGenerator.h"
  17. #include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
  18. #include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
  19. #include "../lib/mapObjects/ObjectTemplate.h"
  20. #include "../lib/mapping/CMap.h"
  21. #include "../lib/constants/StringConstants.h"
  22. #include "townbulidingswidget.h"
  23. #include "armywidget.h"
  24. #include "messagewidget.h"
  25. #include "rewardswidget.h"
  26. #include "questwidget.h"
  27. #include "heroskillswidget.h"
  28. static QList<std::pair<QString, QVariant>> MissionIdentifiers
  29. {
  30. {QObject::tr("None"), QVariant::fromValue(int(CQuest::Emission::MISSION_NONE))},
  31. {QObject::tr("Reach level"), QVariant::fromValue(int(CQuest::Emission::MISSION_LEVEL))},
  32. {QObject::tr("Stats"), QVariant::fromValue(int(CQuest::Emission::MISSION_PRIMARY_STAT))},
  33. {QObject::tr("Kill hero"), QVariant::fromValue(int(CQuest::Emission::MISSION_KILL_HERO))},
  34. {QObject::tr("Kill monster"), QVariant::fromValue(int(CQuest::Emission::MISSION_KILL_CREATURE))},
  35. {QObject::tr("Artifact"), QVariant::fromValue(int(CQuest::Emission::MISSION_ART))},
  36. {QObject::tr("Army"), QVariant::fromValue(int(CQuest::Emission::MISSION_ARMY))},
  37. {QObject::tr("Resources"), QVariant::fromValue(int(CQuest::Emission::MISSION_RESOURCES))},
  38. {QObject::tr("Hero"), QVariant::fromValue(int(CQuest::Emission::MISSION_HERO))},
  39. {QObject::tr("Player"), QVariant::fromValue(int(CQuest::Emission::MISSION_PLAYER))},
  40. };
  41. static QList<std::pair<QString, QVariant>> CharacterIdentifiers
  42. {
  43. {QObject::tr("Compliant"), QVariant::fromValue(int(CGCreature::Character::COMPLIANT))},
  44. {QObject::tr("Friendly"), QVariant::fromValue(int(CGCreature::Character::FRIENDLY))},
  45. {QObject::tr("Agressive"), QVariant::fromValue(int(CGCreature::Character::AGRESSIVE))},
  46. {QObject::tr("Hostile"), QVariant::fromValue(int(CGCreature::Character::HOSTILE))},
  47. {QObject::tr("Savage"), QVariant::fromValue(int(CGCreature::Character::SAVAGE))},
  48. };
  49. //===============IMPLEMENT OBJECT INITIALIZATION FUNCTIONS================
  50. Initializer::Initializer(CGObjectInstance * o, const PlayerColor & pl) : defaultPlayer(pl)
  51. {
  52. logGlobal->info("New object instance initialized");
  53. ///IMPORTANT! initialize order should be from base objects to derived objects
  54. INIT_OBJ_TYPE(CGResource);
  55. INIT_OBJ_TYPE(CGArtifact);
  56. INIT_OBJ_TYPE(CArmedInstance);
  57. INIT_OBJ_TYPE(CGShipyard);
  58. INIT_OBJ_TYPE(CGGarrison);
  59. INIT_OBJ_TYPE(CGMine);
  60. INIT_OBJ_TYPE(CGDwelling);
  61. INIT_OBJ_TYPE(CGTownInstance);
  62. INIT_OBJ_TYPE(CGCreature);
  63. INIT_OBJ_TYPE(CGHeroInstance);
  64. INIT_OBJ_TYPE(CGSignBottle);
  65. INIT_OBJ_TYPE(CGLighthouse);
  66. //INIT_OBJ_TYPE(CRewardableObject);
  67. //INIT_OBJ_TYPE(CGPandoraBox);
  68. //INIT_OBJ_TYPE(CGEvent);
  69. //INIT_OBJ_TYPE(CGSeerHut);
  70. }
  71. void Initializer::initialize(CArmedInstance * o)
  72. {
  73. if(!o) return;
  74. }
  75. void Initializer::initialize(CGSignBottle * o)
  76. {
  77. if(!o) return;
  78. }
  79. void Initializer::initialize(CGCreature * o)
  80. {
  81. if(!o) return;
  82. o->character = CGCreature::Character::HOSTILE;
  83. if(!o->hasStackAtSlot(SlotID(0)))
  84. o->putStack(SlotID(0), new CStackInstance(CreatureID(o->subID), 0, false));
  85. }
  86. void Initializer::initialize(CGDwelling * o)
  87. {
  88. if(!o) return;
  89. o->tempOwner = defaultPlayer;
  90. switch(o->ID)
  91. {
  92. case Obj::RANDOM_DWELLING:
  93. case Obj::RANDOM_DWELLING_LVL:
  94. case Obj::RANDOM_DWELLING_FACTION:
  95. o->initRandomObjectInfo();
  96. }
  97. }
  98. void Initializer::initialize(CGGarrison * o)
  99. {
  100. if(!o) return;
  101. o->tempOwner = defaultPlayer;
  102. o->removableUnits = true;
  103. }
  104. void Initializer::initialize(CGShipyard * o)
  105. {
  106. if(!o) return;
  107. o->tempOwner = defaultPlayer;
  108. }
  109. void Initializer::initialize(CGLighthouse * o)
  110. {
  111. if(!o) return;
  112. o->tempOwner = defaultPlayer;
  113. }
  114. void Initializer::initialize(CGHeroInstance * o)
  115. {
  116. if(!o)
  117. return;
  118. o->tempOwner = defaultPlayer;
  119. if(o->ID == Obj::PRISON)
  120. {
  121. o->subID = 0;
  122. o->tempOwner = PlayerColor::NEUTRAL;
  123. }
  124. if(o->ID == Obj::HERO)
  125. {
  126. for(auto t : VLC->heroh->objects)
  127. {
  128. if(t->heroClass->getId() == HeroClassID(o->subID))
  129. {
  130. o->type = t;
  131. break;
  132. }
  133. }
  134. }
  135. if(o->type)
  136. {
  137. // o->type = VLC->heroh->objects.at(o->subID);
  138. o->gender = o->type->gender;
  139. o->portrait = o->type->imageIndex;
  140. o->randomizeArmy(o->type->heroClass->faction);
  141. }
  142. }
  143. void Initializer::initialize(CGTownInstance * o)
  144. {
  145. if(!o) return;
  146. const std::vector<std::string> castleLevels{"village", "fort", "citadel", "castle", "capitol"};
  147. int lvl = vstd::find_pos(castleLevels, o->appearance->stringID);
  148. o->builtBuildings.insert(BuildingID::DEFAULT);
  149. if(lvl > -1) o->builtBuildings.insert(BuildingID::TAVERN);
  150. if(lvl > 0) o->builtBuildings.insert(BuildingID::FORT);
  151. if(lvl > 1) o->builtBuildings.insert(BuildingID::CITADEL);
  152. if(lvl > 2) o->builtBuildings.insert(BuildingID::CASTLE);
  153. if(lvl > 3) o->builtBuildings.insert(BuildingID::CAPITOL);
  154. for(auto spell : VLC->spellh->objects) //add all regular spells to town
  155. {
  156. if(!spell->isSpecial() && !spell->isCreatureAbility())
  157. o->possibleSpells.push_back(spell->id);
  158. }
  159. }
  160. void Initializer::initialize(CGArtifact * o)
  161. {
  162. if(!o) return;
  163. if(o->ID == Obj::SPELL_SCROLL)
  164. {
  165. std::vector<SpellID> out;
  166. for(auto spell : VLC->spellh->objects) //spellh size appears to be greater (?)
  167. {
  168. if(VLC->spellh->getDefaultAllowed().at(spell->id))
  169. {
  170. out.push_back(spell->id);
  171. }
  172. }
  173. auto a = ArtifactUtils::createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
  174. o->storedArtifact = a;
  175. }
  176. }
  177. void Initializer::initialize(CGMine * o)
  178. {
  179. if(!o) return;
  180. o->tempOwner = defaultPlayer;
  181. if(o->isAbandoned())
  182. {
  183. for(auto r = GameResID(0); r < GameResID::COUNT; ++r)
  184. o->abandonedMineResources.insert(r);
  185. }
  186. else
  187. {
  188. o->producedResource = GameResID(o->subID);
  189. o->producedQuantity = o->defaultResProduction();
  190. }
  191. }
  192. void Initializer::initialize(CGResource * o)
  193. {
  194. if(!o) return;
  195. o->amount = CGResource::RANDOM_AMOUNT;
  196. }
  197. //===============IMPLEMENT PROPERTIES SETUP===============================
  198. void Inspector::updateProperties(CArmedInstance * o)
  199. {
  200. if(!o) return;
  201. auto * delegate = new ArmyDelegate(*o);
  202. addProperty("Army", PropertyEditorPlaceholder(), delegate, false);
  203. }
  204. void Inspector::updateProperties(CGDwelling * o)
  205. {
  206. if(!o) return;
  207. addProperty("Owner", o->tempOwner, false);
  208. }
  209. void Inspector::updateProperties(CGLighthouse * o)
  210. {
  211. if(!o) return;
  212. addProperty("Owner", o->tempOwner, false);
  213. }
  214. void Inspector::updateProperties(CGGarrison * o)
  215. {
  216. if(!o) return;
  217. addProperty("Owner", o->tempOwner, false);
  218. addProperty("Removable units", o->removableUnits, false);
  219. }
  220. void Inspector::updateProperties(CGShipyard * o)
  221. {
  222. if(!o) return;
  223. addProperty("Owner", o->tempOwner, false);
  224. }
  225. void Inspector::updateProperties(CGHeroInstance * o)
  226. {
  227. if(!o) return;
  228. addProperty("Owner", o->tempOwner, o->ID == Obj::PRISON); //field is not editable for prison
  229. addProperty<int>("Experience", o->exp, false);
  230. addProperty("Hero class", o->type ? o->type->heroClass->getNameTranslated() : "", true);
  231. { //Gender
  232. auto * delegate = new InspectorDelegate;
  233. delegate->options = {{"MALE", QVariant::fromValue(int(EHeroGender::MALE))}, {"FEMALE", QVariant::fromValue(int(EHeroGender::FEMALE))}};
  234. addProperty<std::string>("Gender", (o->gender == EHeroGender::FEMALE ? "FEMALE" : "MALE"), delegate , false);
  235. }
  236. addProperty("Name", o->getNameTranslated(), false);
  237. addProperty("Biography", o->getBiographyTranslated(), new MessageDelegate, false);
  238. addProperty("Portrait", o->portrait, false);
  239. auto * delegate = new HeroSkillsDelegate(*o);
  240. addProperty("Skills", PropertyEditorPlaceholder(), delegate, false);
  241. if(o->type)
  242. { //Hero type
  243. auto * delegate = new InspectorDelegate;
  244. for(int i = 0; i < VLC->heroh->objects.size(); ++i)
  245. {
  246. if(map->allowedHeroes.at(i))
  247. {
  248. if(o->ID == Obj::PRISON || (o->type && VLC->heroh->objects[i]->heroClass->getIndex() == o->type->heroClass->getIndex()))
  249. {
  250. delegate->options.push_back({QObject::tr(VLC->heroh->objects[i]->getNameTranslated().c_str()), QVariant::fromValue(VLC->heroh->objects[i]->getId().getNum())});
  251. }
  252. }
  253. }
  254. addProperty("Hero type", o->type->getNameTranslated(), delegate, false);
  255. }
  256. }
  257. void Inspector::updateProperties(CGTownInstance * o)
  258. {
  259. if(!o) return;
  260. addProperty("Town name", o->getNameTranslated(), false);
  261. auto * delegate = new TownBuildingsDelegate(*o);
  262. addProperty("Buildings", PropertyEditorPlaceholder(), delegate, false);
  263. }
  264. void Inspector::updateProperties(CGArtifact * o)
  265. {
  266. if(!o) return;
  267. addProperty("Message", o->message, false);
  268. CArtifactInstance * instance = o->storedArtifact;
  269. if(instance)
  270. {
  271. SpellID spellId = instance->getScrollSpellID();
  272. if(spellId != SpellID::NONE)
  273. {
  274. auto * delegate = new InspectorDelegate;
  275. for(auto spell : VLC->spellh->objects)
  276. {
  277. if(map->allowedSpells.at(spell->id))
  278. delegate->options.push_back({QObject::tr(spell->getNameTranslated().c_str()), QVariant::fromValue(int(spell->getId()))});
  279. }
  280. addProperty("Spell", VLC->spellh->getById(spellId)->getNameTranslated(), delegate, false);
  281. }
  282. }
  283. }
  284. void Inspector::updateProperties(CGMine * o)
  285. {
  286. if(!o) return;
  287. addProperty("Owner", o->tempOwner, false);
  288. addProperty("Resource", o->producedResource);
  289. addProperty("Productivity", o->producedQuantity, false);
  290. }
  291. void Inspector::updateProperties(CGResource * o)
  292. {
  293. if(!o) return;
  294. addProperty("Amount", o->amount, false);
  295. addProperty("Message", o->message, false);
  296. }
  297. void Inspector::updateProperties(CGSignBottle * o)
  298. {
  299. if(!o) return;
  300. addProperty("Message", o->message, new MessageDelegate, false);
  301. }
  302. void Inspector::updateProperties(CGCreature * o)
  303. {
  304. if(!o) return;
  305. addProperty("Message", o->message, false);
  306. { //Character
  307. auto * delegate = new InspectorDelegate;
  308. delegate->options = CharacterIdentifiers;
  309. addProperty<CGCreature::Character>("Character", (CGCreature::Character)o->character, delegate, false);
  310. }
  311. addProperty("Never flees", o->neverFlees, false);
  312. addProperty("Not growing", o->notGrowingTeam, false);
  313. addProperty("Artifact reward", o->gainedArtifact); //TODO: implement in setProperty
  314. addProperty("Army", PropertyEditorPlaceholder(), true);
  315. addProperty("Amount", o->stacks[SlotID(0)]->count, false);
  316. //addProperty("Resources reward", o->resources); //TODO: implement in setProperty
  317. }
  318. void Inspector::updateProperties(CRewardableObject * o)
  319. {
  320. if(!o) return;
  321. auto * delegate = new RewardsDelegate(*map, *o);
  322. addProperty("Reward", PropertyEditorPlaceholder(), delegate, false);
  323. }
  324. void Inspector::updateProperties(CGPandoraBox * o)
  325. {
  326. if(!o) return;
  327. addProperty("Message", o->message, new MessageDelegate, false);
  328. }
  329. void Inspector::updateProperties(CGEvent * o)
  330. {
  331. if(!o) return;
  332. addProperty("Remove after", o->removeAfterVisit, false);
  333. addProperty("Human trigger", o->humanActivate, false);
  334. addProperty("Cpu trigger", o->computerActivate, false);
  335. //ui8 availableFor; //players whom this event is available for
  336. }
  337. void Inspector::updateProperties(CGSeerHut * o)
  338. {
  339. if(!o) return;
  340. { //Mission type
  341. auto * delegate = new InspectorDelegate;
  342. delegate->options = MissionIdentifiers;
  343. addProperty<CQuest::Emission>("Mission type", o->quest->missionType, delegate, false);
  344. }
  345. addProperty("First visit text", o->quest->firstVisitText, new MessageDelegate, false);
  346. addProperty("Next visit text", o->quest->nextVisitText, new MessageDelegate, false);
  347. addProperty("Completed text", o->quest->completedText, new MessageDelegate, false);
  348. { //Quest
  349. auto * delegate = new QuestDelegate(*map, *o);
  350. addProperty("Quest", PropertyEditorPlaceholder(), delegate, false);
  351. }
  352. }
  353. void Inspector::updateProperties()
  354. {
  355. if(!obj)
  356. return;
  357. table->setRowCount(0); //cleanup table
  358. addProperty("Indentifier", obj);
  359. addProperty("ID", obj->ID.getNum());
  360. addProperty("SubID", obj->subID);
  361. addProperty("InstanceName", obj->instanceName);
  362. addProperty("TypeName", obj->typeName);
  363. addProperty("SubTypeName", obj->subTypeName);
  364. if(!dynamic_cast<CGHeroInstance*>(obj))
  365. {
  366. auto factory = VLC->objtypeh->getHandlerFor(obj->ID, obj->subID);
  367. addProperty("IsStatic", factory->isStaticObject());
  368. }
  369. auto * delegate = new InspectorDelegate();
  370. delegate->options.push_back({QObject::tr("neutral"), QVariant::fromValue(PlayerColor::NEUTRAL.getNum())});
  371. for(int p = 0; p < map->players.size(); ++p)
  372. if(map->players[p].canAnyonePlay())
  373. delegate->options.push_back({QString::fromStdString(GameConstants::PLAYER_COLOR_NAMES[p]), QVariant::fromValue(PlayerColor(p).getNum())});
  374. addProperty("Owner", obj->tempOwner, delegate, true);
  375. UPDATE_OBJ_PROPERTIES(CArmedInstance);
  376. UPDATE_OBJ_PROPERTIES(CGResource);
  377. UPDATE_OBJ_PROPERTIES(CGArtifact);
  378. UPDATE_OBJ_PROPERTIES(CGMine);
  379. UPDATE_OBJ_PROPERTIES(CGGarrison);
  380. UPDATE_OBJ_PROPERTIES(CGShipyard);
  381. UPDATE_OBJ_PROPERTIES(CGDwelling);
  382. UPDATE_OBJ_PROPERTIES(CGTownInstance);
  383. UPDATE_OBJ_PROPERTIES(CGCreature);
  384. UPDATE_OBJ_PROPERTIES(CGHeroInstance);
  385. UPDATE_OBJ_PROPERTIES(CGSignBottle);
  386. UPDATE_OBJ_PROPERTIES(CGLighthouse);
  387. UPDATE_OBJ_PROPERTIES(CRewardableObject);
  388. UPDATE_OBJ_PROPERTIES(CGPandoraBox);
  389. UPDATE_OBJ_PROPERTIES(CGEvent);
  390. UPDATE_OBJ_PROPERTIES(CGSeerHut);
  391. table->show();
  392. }
  393. //===============IMPLEMENT PROPERTY UPDATE================================
  394. void Inspector::setProperty(const QString & key, const QTableWidgetItem * item)
  395. {
  396. if(!item->data(Qt::UserRole).isNull())
  397. {
  398. setProperty(key, item->data(Qt::UserRole));
  399. return;
  400. }
  401. if(item->flags() & Qt::ItemIsUserCheckable)
  402. {
  403. setProperty(key, QVariant::fromValue(item->checkState() == Qt::Checked));
  404. return;
  405. }
  406. setProperty(key, item->text());
  407. }
  408. void Inspector::setProperty(const QString & key, const QVariant & value)
  409. {
  410. if(!obj)
  411. return;
  412. if(key == "Owner")
  413. obj->tempOwner = PlayerColor(value.toInt());
  414. SET_PROPERTIES(CArmedInstance);
  415. SET_PROPERTIES(CGTownInstance);
  416. SET_PROPERTIES(CGArtifact);
  417. SET_PROPERTIES(CGMine);
  418. SET_PROPERTIES(CGResource);
  419. SET_PROPERTIES(CGDwelling);
  420. SET_PROPERTIES(CGGarrison);
  421. SET_PROPERTIES(CGCreature);
  422. SET_PROPERTIES(CGHeroInstance);
  423. SET_PROPERTIES(CGShipyard);
  424. SET_PROPERTIES(CGSignBottle);
  425. SET_PROPERTIES(CGLighthouse);
  426. SET_PROPERTIES(CRewardableObject);
  427. SET_PROPERTIES(CGPandoraBox);
  428. SET_PROPERTIES(CGEvent);
  429. SET_PROPERTIES(CGSeerHut);
  430. }
  431. void Inspector::setProperty(CArmedInstance * o, const QString & key, const QVariant & value)
  432. {
  433. if(!o) return;
  434. }
  435. void Inspector::setProperty(CGLighthouse * o, const QString & key, const QVariant & value)
  436. {
  437. if(!o) return;
  438. }
  439. void Inspector::setProperty(CRewardableObject * o, const QString & key, const QVariant & value)
  440. {
  441. if(!o) return;
  442. }
  443. void Inspector::setProperty(CGPandoraBox * o, const QString & key, const QVariant & value)
  444. {
  445. if(!o) return;
  446. if(key == "Message")
  447. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString()));
  448. }
  449. void Inspector::setProperty(CGEvent * o, const QString & key, const QVariant & value)
  450. {
  451. if(!o) return;
  452. if(key == "Remove after")
  453. o->removeAfterVisit = value.toBool();
  454. if(key == "Human trigger")
  455. o->humanActivate = value.toBool();
  456. if(key == "Cpu trigger")
  457. o->computerActivate = value.toBool();
  458. }
  459. void Inspector::setProperty(CGTownInstance * o, const QString & key, const QVariant & value)
  460. {
  461. if(!o) return;
  462. if(key == "Town name")
  463. o->setNameTextId(mapRegisterLocalizedString("map", *map, TextIdentifier("town", o->instanceName, "name"), value.toString().toStdString()));
  464. }
  465. void Inspector::setProperty(CGSignBottle * o, const QString & key, const QVariant & value)
  466. {
  467. if(!o) return;
  468. if(key == "Message")
  469. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("sign", o->instanceName, "message"), value.toString().toStdString()));
  470. }
  471. void Inspector::setProperty(CGMine * o, const QString & key, const QVariant & value)
  472. {
  473. if(!o) return;
  474. if(key == "Productivity")
  475. o->producedQuantity = value.toString().toInt();
  476. }
  477. void Inspector::setProperty(CGArtifact * o, const QString & key, const QVariant & value)
  478. {
  479. if(!o) return;
  480. if(key == "Message")
  481. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString()));
  482. if(o->storedArtifact && key == "Spell")
  483. {
  484. o->storedArtifact = ArtifactUtils::createScroll(SpellID(value.toInt()));
  485. }
  486. }
  487. void Inspector::setProperty(CGDwelling * o, const QString & key, const QVariant & value)
  488. {
  489. if(!o) return;
  490. }
  491. void Inspector::setProperty(CGGarrison * o, const QString & key, const QVariant & value)
  492. {
  493. if(!o) return;
  494. if(key == "Removable units")
  495. o->removableUnits = value.toBool();
  496. }
  497. void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVariant & value)
  498. {
  499. if(!o) return;
  500. if(key == "Gender")
  501. o->gender = EHeroGender(value.toInt());
  502. if(key == "Name")
  503. o->nameCustomTextId = mapRegisterLocalizedString("map", *map, TextIdentifier("hero", o->instanceName, "name"), value.toString().toStdString());
  504. if(key == "Biography")
  505. o->biographyCustomTextId = mapRegisterLocalizedString("map", *map, TextIdentifier("hero", o->instanceName, "biography"), value.toString().toStdString());
  506. if(key == "Experience")
  507. o->exp = value.toString().toInt();
  508. if(key == "Hero type")
  509. {
  510. for(auto t : VLC->heroh->objects)
  511. {
  512. if(t->getNameTranslated() == value.toString().toStdString())
  513. o->type = t.get();
  514. }
  515. o->gender = o->type->gender;
  516. o->portrait = o->type->imageIndex;
  517. o->randomizeArmy(o->type->heroClass->faction);
  518. updateProperties(); //updating other properties after change
  519. }
  520. }
  521. void Inspector::setProperty(CGShipyard * o, const QString & key, const QVariant & value)
  522. {
  523. if(!o) return;
  524. }
  525. void Inspector::setProperty(CGResource * o, const QString & key, const QVariant & value)
  526. {
  527. if(!o) return;
  528. if(key == "Amount")
  529. o->amount = value.toString().toInt();
  530. }
  531. void Inspector::setProperty(CGCreature * o, const QString & key, const QVariant & value)
  532. {
  533. if(!o) return;
  534. if(key == "Message")
  535. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("monster", o->instanceName, "message"), value.toString().toStdString()));
  536. if(key == "Character")
  537. o->character = CGCreature::Character(value.toInt());
  538. if(key == "Never flees")
  539. o->neverFlees = value.toBool();
  540. if(key == "Not growing")
  541. o->notGrowingTeam = value.toBool();
  542. if(key == "Amount")
  543. o->stacks[SlotID(0)]->count = value.toString().toInt();
  544. }
  545. void Inspector::setProperty(CGSeerHut * o, const QString & key, const QVariant & value)
  546. {
  547. if(!o) return;
  548. if(key == "Mission type")
  549. o->quest->missionType = CQuest::Emission(value.toInt());
  550. if(key == "First visit text")
  551. o->quest->firstVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("quest", o->instanceName, "firstVisit"), value.toString().toStdString()));
  552. if(key == "Next visit text")
  553. o->quest->nextVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("quest", o->instanceName, "nextVisit"), value.toString().toStdString()));
  554. if(key == "Completed text")
  555. o->quest->completedText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("quest", o->instanceName, "completed"), value.toString().toStdString()));
  556. }
  557. //===============IMPLEMENT PROPERTY VALUE TYPE============================
  558. QTableWidgetItem * Inspector::addProperty(CGObjectInstance * value)
  559. {
  560. auto * item = new QTableWidgetItem(QString::number(data_cast<CGObjectInstance>(value)));
  561. item->setFlags(Qt::NoItemFlags);
  562. return item;
  563. }
  564. QTableWidgetItem * Inspector::addProperty(Inspector::PropertyEditorPlaceholder value)
  565. {
  566. auto item = new QTableWidgetItem("...");
  567. item->setFlags(Qt::NoItemFlags);
  568. return item;
  569. }
  570. QTableWidgetItem * Inspector::addProperty(unsigned int value)
  571. {
  572. auto * item = new QTableWidgetItem(QString::number(value));
  573. item->setFlags(Qt::NoItemFlags);
  574. //item->setData(Qt::UserRole, QVariant::fromValue(value));
  575. return item;
  576. }
  577. QTableWidgetItem * Inspector::addProperty(int value)
  578. {
  579. auto * item = new QTableWidgetItem(QString::number(value));
  580. item->setFlags(Qt::NoItemFlags);
  581. //item->setData(Qt::UserRole, QVariant::fromValue(value));
  582. return item;
  583. }
  584. QTableWidgetItem * Inspector::addProperty(bool value)
  585. {
  586. auto item = new QTableWidgetItem;
  587. item->setFlags(Qt::ItemIsUserCheckable);
  588. item->setCheckState(value ? Qt::Checked : Qt::Unchecked);
  589. return item;
  590. }
  591. QTableWidgetItem * Inspector::addProperty(const std::string & value)
  592. {
  593. return addProperty(QString::fromStdString(value));
  594. }
  595. QTableWidgetItem * Inspector::addProperty(const TextIdentifier & value)
  596. {
  597. return addProperty(VLC->generaltexth->translate(value.get()));
  598. }
  599. QTableWidgetItem * Inspector::addProperty(const MetaString & value)
  600. {
  601. return addProperty(value.toString());
  602. }
  603. QTableWidgetItem * Inspector::addProperty(const QString & value)
  604. {
  605. auto * item = new QTableWidgetItem(value);
  606. item->setFlags(Qt::NoItemFlags);
  607. return item;
  608. }
  609. QTableWidgetItem * Inspector::addProperty(const int3 & value)
  610. {
  611. auto * item = new QTableWidgetItem(QString("(%1, %2, %3)").arg(value.x, value.y, value.z));
  612. item->setFlags(Qt::NoItemFlags);
  613. return item;
  614. }
  615. QTableWidgetItem * Inspector::addProperty(const PlayerColor & value)
  616. {
  617. auto str = QObject::tr("UNFLAGGABLE");
  618. if(value == PlayerColor::NEUTRAL)
  619. str = QObject::tr("neutral");
  620. if(value.isValidPlayer())
  621. str = QString::fromStdString(GameConstants::PLAYER_COLOR_NAMES[value]);
  622. auto * item = new QTableWidgetItem(str);
  623. item->setFlags(Qt::NoItemFlags);
  624. item->setData(Qt::UserRole, QVariant::fromValue(value.getNum()));
  625. return item;
  626. }
  627. QTableWidgetItem * Inspector::addProperty(const GameResID & value)
  628. {
  629. auto * item = new QTableWidgetItem(QString::fromStdString(GameConstants::RESOURCE_NAMES[value.toEnum()]));
  630. item->setFlags(Qt::NoItemFlags);
  631. item->setData(Qt::UserRole, QVariant::fromValue(value.getNum()));
  632. return item;
  633. }
  634. QTableWidgetItem * Inspector::addProperty(CGCreature::Character value)
  635. {
  636. auto * item = new QTableWidgetItem;
  637. item->setFlags(Qt::NoItemFlags);
  638. item->setData(Qt::UserRole, QVariant::fromValue(int(value)));
  639. for(auto & i : CharacterIdentifiers)
  640. {
  641. if(i.second.toInt() == value)
  642. {
  643. item->setText(i.first);
  644. break;
  645. }
  646. }
  647. return item;
  648. }
  649. QTableWidgetItem * Inspector::addProperty(CQuest::Emission value)
  650. {
  651. auto * item = new QTableWidgetItem;
  652. item->setFlags(Qt::NoItemFlags);
  653. item->setData(Qt::UserRole, QVariant::fromValue(int(value)));
  654. for(auto & i : MissionIdentifiers)
  655. {
  656. if(i.second.toInt() == value)
  657. {
  658. item->setText(i.first);
  659. break;
  660. }
  661. }
  662. return item;
  663. }
  664. //========================================================================
  665. Inspector::Inspector(CMap * m, CGObjectInstance * o, QTableWidget * t): obj(o), table(t), map(m)
  666. {
  667. }
  668. /*
  669. * Delegates
  670. */
  671. QWidget * InspectorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  672. {
  673. return new QComboBox(parent);
  674. }
  675. void InspectorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  676. {
  677. if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
  678. {
  679. for(auto & i : options)
  680. {
  681. ed->addItem(i.first);
  682. ed->setItemData(ed->count() - 1, i.second);
  683. }
  684. }
  685. else
  686. {
  687. QStyledItemDelegate::setEditorData(editor, index);
  688. }
  689. }
  690. void InspectorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  691. {
  692. if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
  693. {
  694. if(!options.isEmpty())
  695. {
  696. QMap<int, QVariant> data;
  697. data[Qt::DisplayRole] = options[ed->currentIndex()].first;
  698. data[Qt::UserRole] = options[ed->currentIndex()].second;
  699. model->setItemData(index, data);
  700. }
  701. }
  702. else
  703. {
  704. QStyledItemDelegate::setModelData(editor, model, index);
  705. }
  706. }