inspector.cpp 23 KB

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