Sfoglia il codice sorgente

use custom roles instead of Qt::UserRole

godric3 1 anno fa
parent
commit
2b9461ed6e

+ 1 - 0
mapeditor/CMakeLists.txt

@@ -85,6 +85,7 @@ set(editor_HEADERS
 		inspector/PickObjectDelegate.h
 		inspector/portraitwidget.h
 		resourceExtractor/ResourceConverter.h
+		mapeditorroles.h
 )
 
 set(editor_FORMS

+ 6 - 5
mapeditor/inspector/townbuildingswidget.cpp

@@ -10,6 +10,7 @@
 #include "StdInc.h"
 #include "townbuildingswidget.h"
 #include "ui_townbuildingswidget.h"
+#include "mapeditorroles.h"
 #include "../lib/entities/building/CBuilding.h"
 #include "../lib/entities/faction/CTownHandler.h"
 #include "../lib/texts/CGeneralTextHandler.h"
@@ -80,7 +81,7 @@ QStandardItem * getBuildingParentFromTreeModel(const CBuilding * building, QStan
 		for (int i = 0; i < rowCount; ++i)
 		{
 			QModelIndex index = model.index(i, 0, pindex);
-			if (building->upgrade.getNum() == model.itemFromIndex(index)->data(Qt::UserRole).toInt())
+			if (building->upgrade.getNum() == model.itemFromIndex(index)->data(MapEditorRoles::BuildingIDRole).toInt())
 			{
 				parent = model.itemFromIndex(index);
 				break;
@@ -106,7 +107,7 @@ std::set<QVariant> getBuildingVariantsFromModel(QStandardItemModel & model, int
 			QModelIndex index = model.index(i, modelColumn, pindex);
 			if (auto * item = model.itemFromIndex(index))
 				if (item->checkState() == checkState)
-					result.emplace(item->data(Qt::UserRole));
+					result.emplace(item->data(MapEditorRoles::BuildingIDRole));
 			index = model.index(i, 0, pindex);
 			if (model.hasChildren(index))
 				stack.push_back(index);
@@ -154,17 +155,17 @@ QStandardItem * TownBuildingsWidget::addBuilding(const CTown & ctown, int bId, s
 	QList<QStandardItem *> checks;
 	
 	checks << new QStandardItem(name);
-	checks.back()->setData(bId, Qt::UserRole);
+	checks.back()->setData(bId, MapEditorRoles::BuildingIDRole);
 	
 	checks << new QStandardItem;
 	checks.back()->setCheckable(true);
 	checks.back()->setCheckState(town.forbiddenBuildings.count(buildingId) ? Qt::Unchecked : Qt::Checked);
-	checks.back()->setData(bId, Qt::UserRole);
+	checks.back()->setData(bId, MapEditorRoles::BuildingIDRole);
 	
 	checks << new QStandardItem;
 	checks.back()->setCheckable(true);
 	checks.back()->setCheckState(town.builtBuildings.count(buildingId) ? Qt::Checked : Qt::Unchecked);
-	checks.back()->setData(bId, Qt::UserRole);
+	checks.back()->setData(bId, MapEditorRoles::BuildingIDRole);
 	
 	if(building->getBase() == buildingId)
 	{

+ 9 - 6
mapeditor/inspector/towneventdialog.cpp

@@ -12,6 +12,9 @@
 #include "townbuildingswidget.h"
 #include "towneventdialog.h"
 #include "ui_towneventdialog.h"
+#include "mapeditorroles.h"
+#include "../../lib/entities/building/CBuilding.h"
+#include "../../lib/entities/faction/CTownHandler.h"
 #include "../../lib/constants/NumericConstants.h"
 #include "../../lib/constants/StringConstants.h"
 
@@ -28,7 +31,7 @@ TownEventDialog::TownEventDialog(CGTownInstance & t, QListWidgetItem * item, QWi
 
 	ui->buildingsTree->setModel(&buildingsModel);
 
-	params = townEventListItem->data(Qt::UserRole).toMap();
+	params = townEventListItem->data(MapEditorRoles::TownEventRole).toMap();
 	ui->eventFirstOccurrence->setMinimum(FIRST_DAY_FOR_EVENT);
 	ui->eventFirstOccurrence->setMaximum(LAST_DAY_FOR_EVENT);
 	ui->eventRepeatAfter->setMaximum(MAXIMUM_REPEAT_AFTER);
@@ -56,7 +59,7 @@ void TownEventDialog::initPlayers()
 	{
 		bool isAffected = (1 << i) & params.value("players").toInt();
 		auto * item = new QListWidgetItem(QString::fromStdString(GameConstants::PLAYER_COLOR_NAMES[i]));
-		item->setData(Qt::UserRole, QVariant::fromValue(i));
+		item->setData(MapEditorRoles::PlayerIDRole, QVariant::fromValue(i));
 		item->setCheckState(isAffected ? Qt::Checked : Qt::Unchecked);
 		ui->playersAffected->addItem(item);
 	}
@@ -121,12 +124,12 @@ QStandardItem * TownEventDialog::addBuilding(const CTown& ctown, BuildingID buil
 	QList<QStandardItem *> checks;
 
 	checks << new QStandardItem(name);
-	checks.back()->setData(bId, Qt::UserRole);
+	checks.back()->setData(bId, MapEditorRoles::BuildingIDRole);
 
 	checks << new QStandardItem;
 	checks.back()->setCheckable(true);
 	checks.back()->setCheckState(params["buildings"].toList().contains(bId) ? Qt::Checked : Qt::Unchecked);
-	checks.back()->setData(bId, Qt::UserRole);
+	checks.back()->setData(bId, MapEditorRoles::BuildingIDRole);
 
 	if (building->getBase() == buildingId)
 	{
@@ -203,7 +206,7 @@ void TownEventDialog::on_TownEventDialog_finished(int result)
 	descriptor["buildings"] = buildingsToVariant();
 	descriptor["creatures"] = creaturesToVariant();
 
-	townEventListItem->setData(Qt::UserRole, descriptor);
+	townEventListItem->setData(MapEditorRoles::TownEventRole, descriptor);
 	auto itemText = tr("Day %1 - %2").arg(ui->eventFirstOccurrence->value(), 3).arg(ui->eventNameText->text());
 	townEventListItem->setText(itemText);
 }
@@ -222,7 +225,7 @@ QVariant TownEventDialog::playersToVariant()
 
 QVariantMap TownEventDialog::resourcesToVariant()
 {
-	auto res = townEventListItem->data(Qt::UserRole).toMap().value("resources").toMap();
+	auto res = params.value("resources").toMap();
 	for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
 	{
 		auto * itemType = ui->resourcesTable->item(i, 0);

+ 4 - 3
mapeditor/inspector/towneventswidget.cpp

@@ -12,6 +12,7 @@
 #include "towneventswidget.h"
 #include "ui_towneventswidget.h"
 #include "towneventdialog.h"
+#include "mapeditorroles.h"
 #include "mapsettings/eventsettings.h"
 #include "../../lib/constants/NumericConstants.h"
 #include "../../lib/constants/StringConstants.h"
@@ -105,7 +106,7 @@ void TownEventsWidget::obtainData()
 		auto itemText = tr("Day %1 - %2").arg(event.firstOccurrence+1, 3).arg(eventName);
 
 		auto * item = new QListWidgetItem(itemText);
-		item->setData(Qt::UserRole, toVariant(event));
+		item->setData(MapEditorRoles::TownEventRole, toVariant(event));
 		ui->eventsList->addItem(item);
 	}
 }
@@ -116,7 +117,7 @@ void TownEventsWidget::commitChanges(MapController& controller)
 	for (int i = 0; i < ui->eventsList->count(); ++i)
 	{
 		const auto * item = ui->eventsList->item(i);
-		town.events.push_back(eventFromVariant(*controller.map(), town, item->data(Qt::UserRole)));
+		town.events.push_back(eventFromVariant(*controller.map(), town, item->data(MapEditorRoles::TownEventRole)));
 	}
 }
 
@@ -125,7 +126,7 @@ void TownEventsWidget::on_timedEventAdd_clicked()
 	CCastleEvent event;
 	event.name = tr("New event").toStdString();
 	auto* item = new QListWidgetItem(QString::fromStdString(event.name));
-	item->setData(Qt::UserRole, toVariant(event));
+	item->setData(MapEditorRoles::TownEventRole, toVariant(event));
 	ui->eventsList->addItem(item);
 	on_eventsList_itemActivated(item);
 }

+ 4 - 3
mapeditor/inspector/townspellswidget.cpp

@@ -11,6 +11,7 @@
 #include "townspellswidget.h"
 #include "ui_townspellswidget.h"
 #include "inspector.h"
+#include "mapeditorroles.h"
 #include "../../lib/constants/StringConstants.h"
 #include "../../lib/spells/CSpellHandler.h"
 
@@ -83,13 +84,13 @@ void TownSpellsWidget::initSpellLists()
 		{
 			auto spell = spellID.toEntity(VLC);
 			auto * possibleItem = new QListWidgetItem(QString::fromStdString(spell->getNameTranslated()));
-			possibleItem->setData(Qt::UserRole, QVariant::fromValue(spell->getIndex()));
+			possibleItem->setData(MapEditorRoles::SpellIDRole, QVariant::fromValue(spell->getIndex()));
 			possibleItem->setFlags(possibleItem->flags() | Qt::ItemIsUserCheckable);
 			possibleItem->setCheckState(vstd::contains(town.possibleSpells, spell->getId()) ? Qt::Checked : Qt::Unchecked);
 			possibleSpellLists[i]->addItem(possibleItem);
 
 			auto * requiredItem = new QListWidgetItem(QString::fromStdString(spell->getNameTranslated()));
-			requiredItem->setData(Qt::UserRole, QVariant::fromValue(spell->getIndex()));
+			requiredItem->setData(MapEditorRoles::SpellIDRole, QVariant::fromValue(spell->getIndex()));
 			requiredItem->setFlags(requiredItem->flags() | Qt::ItemIsUserCheckable);
 			requiredItem->setCheckState(vstd::contains(town.obligatorySpells, spell->getId()) ? Qt::Checked : Qt::Unchecked);
 			requiredSpellLists[i]->addItem(requiredItem);
@@ -113,7 +114,7 @@ void TownSpellsWidget::commitChanges()
 				QListWidgetItem * item = spellList->item(i);
 				if (item->checkState() == Qt::Checked)
 				{
-					townSpellList.push_back(item->data(Qt::UserRole).toInt());
+					townSpellList.push_back(item->data(MapEditorRoles::SpellIDRole).toInt());
 				}
 			}
 		}

+ 20 - 0
mapeditor/mapeditorroles.h

@@ -0,0 +1,20 @@
+/*
+ * mapeditorroles.h, part of VCMI engine
+ *
+ * Authors: listed in file AUTHORS in main folder
+ *
+ * License: GNU General Public License v2.0 or later
+ * Full text of license available in license.txt file, in main folder
+ *
+ */
+#pragma once
+
+#include "StdInc.h"
+
+enum MapEditorRoles
+{
+    TownEventRole = Qt::UserRole +1,
+    PlayerIDRole,
+    BuildingIDRole,
+	SpellIDRole
+};