inspector.cpp 18 KB

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