inspector.cpp 22 KB

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