inspector.cpp 22 KB

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