inspector.cpp 21 KB

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