towneventswidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * towneventswidget.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 "towneventswidget.h"
  12. #include "ui_towneventswidget.h"
  13. #include "townevent.h"
  14. #include "mapsettings/eventsettings.h"
  15. #include "../../lib/constants/NumericConstants.h"
  16. #include "../../lib/constants/StringConstants.h"
  17. TownEventsWidget::TownEventsWidget(CGTownInstance & town, QWidget * parent) :
  18. QDialog(parent),
  19. ui(new Ui::TownEventsWidget),
  20. town(town)
  21. {
  22. ui->setupUi(this);
  23. }
  24. TownEventsWidget::~TownEventsWidget()
  25. {
  26. delete ui;
  27. }
  28. QVariant toVariant(const std::set<BuildingID> & buildings)
  29. {
  30. QVariantList result;
  31. for (auto b : buildings)
  32. result.push_back(QVariant::fromValue(b.num));
  33. return result;
  34. }
  35. QVariant toVariant(const std::vector<si32> & creatures)
  36. {
  37. QVariantList result;
  38. for (auto c : creatures)
  39. result.push_back(QVariant::fromValue(c));
  40. return result;
  41. }
  42. std::set<BuildingID> buildingsFromVariant(const QVariant& v)
  43. {
  44. std::set<BuildingID> result;
  45. for (auto r : v.toList()) {
  46. result.insert(BuildingID(r.toInt()));
  47. }
  48. return result;
  49. }
  50. std::vector<si32> creaturesFromVariant(const QVariant& v)
  51. {
  52. std::vector<si32> result;
  53. for (auto r : v.toList()) {
  54. result.push_back(r.toInt());
  55. }
  56. return result;
  57. }
  58. QVariant toVariant(const CCastleEvent& event)
  59. {
  60. QVariantMap result;
  61. result["name"] = QString::fromStdString(event.name);
  62. result["message"] = QString::fromStdString(event.message.toString());
  63. result["players"] = QVariant::fromValue(event.players);
  64. result["humanAffected"] = QVariant::fromValue(event.humanAffected);
  65. result["computerAffected"] = QVariant::fromValue(event.computerAffected);
  66. result["firstOccurrence"] = QVariant::fromValue(event.firstOccurrence);
  67. result["nextOccurrence"] = QVariant::fromValue(event.nextOccurrence);
  68. result["resources"] = toVariant(event.resources);
  69. result["buildings"] = toVariant(event.buildings);
  70. result["creatures"] = toVariant(event.creatures);
  71. return QVariant(result);
  72. }
  73. CCastleEvent eventFromVariant(CMapHeader& map, CGTownInstance& town, const QVariant& variant)
  74. {
  75. CCastleEvent result;
  76. auto v = variant.toMap();
  77. result.name = v.value("name").toString().toStdString();
  78. result.message.appendTextID(mapRegisterLocalizedString("map", map, TextIdentifier("town", town.instanceName, "event", result.name, "message"), v.value("message").toString().toStdString()));
  79. result.players = v.value("players").toInt();
  80. result.humanAffected = v.value("humanAffected").toInt();
  81. result.computerAffected = v.value("computerAffected").toInt();
  82. result.firstOccurrence = v.value("firstOccurrence").toInt();
  83. result.nextOccurrence = v.value("nextOccurrence").toInt();
  84. result.resources = resourcesFromVariant(v.value("resources"));
  85. result.buildings = buildingsFromVariant(v.value("buildings"));
  86. result.creatures = creaturesFromVariant(v.value("creatures"));
  87. return result;
  88. }
  89. void TownEventsWidget::obtainData()
  90. {
  91. for (const auto & event : town.events)
  92. {
  93. auto eventName = QString::fromStdString(event.name);
  94. auto itemText = tr("Day %1 - %2").arg(event.firstOccurrence+1, 3).arg(eventName);
  95. auto * item = new QListWidgetItem(itemText);
  96. item->setData(Qt::UserRole, toVariant(event));
  97. ui->eventsList->addItem(item);
  98. }
  99. }
  100. void TownEventsWidget::commitChanges(MapController& controller)
  101. {
  102. town.events.clear();
  103. for (int i = 0; i < ui->eventsList->count(); ++i)
  104. {
  105. const auto * item = ui->eventsList->item(i);
  106. town.events.push_back(eventFromVariant(*controller.map(), town, item->data(Qt::UserRole)));
  107. }
  108. }
  109. void TownEventsWidget::on_timedEventAdd_clicked()
  110. {
  111. CCastleEvent event;
  112. event.name = tr("New event").toStdString();
  113. auto* item = new QListWidgetItem(QString::fromStdString(event.name));
  114. item->setData(Qt::UserRole, toVariant(event));
  115. ui->eventsList->addItem(item);
  116. on_eventsList_itemActivated(item);
  117. }
  118. void TownEventsWidget::on_timedEventRemove_clicked()
  119. {
  120. if (auto* item = ui->eventsList->currentItem())
  121. ui->eventsList->takeItem(ui->eventsList->row(item));
  122. }
  123. void TownEventsWidget::on_eventsList_itemActivated(QListWidgetItem* item)
  124. {
  125. new TownEvent(town, item, parentWidget());
  126. }
  127. void TownEventsWidget::onItemChanged(QStandardItem * item)
  128. {
  129. }
  130. TownEventsDelegate::TownEventsDelegate(CGTownInstance & town, MapController & c) : town(town), controller(c), QStyledItemDelegate()
  131. {
  132. }
  133. QWidget* TownEventsDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
  134. {
  135. return new TownEventsWidget(town, parent);;
  136. }
  137. void TownEventsDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
  138. {
  139. if (auto * ed = qobject_cast<TownEventsWidget *>(editor))
  140. {
  141. ed->obtainData();
  142. }
  143. else
  144. {
  145. QStyledItemDelegate::setEditorData(editor, index);
  146. }
  147. }
  148. void TownEventsDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
  149. {
  150. if (auto * ed = qobject_cast<TownEventsWidget *>(editor))
  151. {
  152. ed->commitChanges(controller);
  153. }
  154. else
  155. {
  156. QStyledItemDelegate::setModelData(editor, model, index);
  157. }
  158. }