inspector.cpp 21 KB

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