inspector.cpp 24 KB

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