mapsettings.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * mapsettings.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 "mapsettings.h"
  12. #include "ui_mapsettings.h"
  13. #include "mainwindow.h"
  14. #include "../../lib/CArtHandler.h"
  15. #include "../../lib/CSkillHandler.h"
  16. #include "../../lib/entities/hero/CHeroHandler.h"
  17. #include "../../lib/spells/CSpellHandler.h"
  18. MapSettings::MapSettings(MapController & ctrl, QWidget *parent) :
  19. QDialog(parent),
  20. ui(new Ui::MapSettings),
  21. controller(ctrl)
  22. {
  23. ui->setupUi(this);
  24. setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
  25. assert(controller.map());
  26. controller.settingsDialog = this;
  27. show();
  28. for(auto const & objectPtr : LIBRARY->skillh->objects)
  29. {
  30. auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated()));
  31. item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex()));
  32. item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
  33. item->setCheckState(controller.map()->allowedAbilities.count(objectPtr->getId()) ? Qt::Checked : Qt::Unchecked);
  34. ui->listAbilities->addItem(item);
  35. }
  36. for(auto const & objectPtr : LIBRARY->spellh->objects)
  37. {
  38. auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated()));
  39. item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex()));
  40. item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
  41. item->setCheckState(controller.map()->allowedSpells.count(objectPtr->getId()) ? Qt::Checked : Qt::Unchecked);
  42. ui->listSpells->addItem(item);
  43. }
  44. for(auto const & objectPtr : LIBRARY->arth->objects)
  45. {
  46. auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated()));
  47. item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex()));
  48. item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
  49. item->setCheckState(controller.map()->allowedArtifact.count(objectPtr->getId()) ? Qt::Checked : Qt::Unchecked);
  50. ui->listArts->addItem(item);
  51. }
  52. for(auto const & objectPtr : LIBRARY->heroh->objects)
  53. {
  54. auto * item = new QListWidgetItem(QString::fromStdString(objectPtr->getNameTranslated()));
  55. item->setData(Qt::UserRole, QVariant::fromValue(objectPtr->getIndex()));
  56. item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
  57. item->setCheckState(controller.map()->allowedHeroes.count(objectPtr->getId()) ? Qt::Checked : Qt::Unchecked);
  58. ui->listHeroes->addItem(item);
  59. }
  60. ui->general->initialize(controller);
  61. ui->mods->initialize(controller);
  62. ui->victory->initialize(controller);
  63. ui->lose->initialize(controller);
  64. ui->events->initialize(controller);
  65. ui->rumors->initialize(controller);
  66. }
  67. MapSettings::~MapSettings()
  68. {
  69. controller.settingsDialog = nullptr;
  70. delete ui;
  71. }
  72. void MapSettings::on_pushButton_clicked()
  73. {
  74. auto updateMapArray = [](const QListWidget * widget, auto & arr)
  75. {
  76. arr.clear();
  77. for(int i = 0; i < widget->count(); ++i)
  78. {
  79. auto * item = widget->item(i);
  80. if (item->checkState() == Qt::Checked)
  81. arr.emplace(i);
  82. }
  83. };
  84. updateMapArray(ui->listAbilities, controller.map()->allowedAbilities);
  85. updateMapArray(ui->listSpells, controller.map()->allowedSpells);
  86. updateMapArray(ui->listArts, controller.map()->allowedArtifact);
  87. updateMapArray(ui->listHeroes, controller.map()->allowedHeroes);
  88. controller.map()->triggeredEvents.clear();
  89. ui->general->update();
  90. ui->mods->update();
  91. ui->victory->update();
  92. ui->lose->update();
  93. ui->events->update();
  94. ui->rumors->update();
  95. controller.commitChangeWithoutRedraw();
  96. close();
  97. }