inspector.cpp 21 KB

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