code review

This commit is contained in:
Laserlicht
2025-07-17 21:49:41 +02:00
parent 8ae7b96bba
commit e00668251d
4 changed files with 17 additions and 44 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ void ObjectSelector::getBannedObjectCategories()
for (int row = 0; row < ui->tableWidgetBannedObjectCategories->rowCount() - 1; ++row)
{
auto val = static_cast<ObjectConfig::EObjectCategory>(static_cast<QComboBox *>(ui->tableWidgetBannedObjectCategories->cellWidget(row, 0))->currentData().toInt());
if(std::find(obj.bannedObjectCategories.begin(), obj.bannedObjectCategories.end(), val) == obj.bannedObjectCategories.end())
if(vstd::contains(obj.bannedObjectCategories, val))
obj.bannedObjectCategories.push_back(val);
}
}
@@ -10,7 +10,6 @@
#pragma once
#include <QWidget>
#include "../StdInc.h"
#include "../../lib/rmg/CRmgTemplate.h"
Q_DECLARE_METATYPE(CompoundMapObjectID);
+16 -41
View File
@@ -13,40 +13,6 @@
#include "townhintselector.h"
#include "ui_townhintselector.h"
std::string joinVector(const std::vector<int>& vec, char delimiter) {
std::string result;
for (std::size_t i = 0; i < vec.size(); ++i) {
result += std::to_string(vec[i]);
if (i != vec.size() - 1) {
result += delimiter;
}
}
return result;
}
std::vector<int> splitStringToVector(const std::string& input, char delimiter) {
std::vector<int> result;
std::string temp;
for (std::size_t i = 0; i < input.size(); ++i) {
if (input[i] == delimiter) {
if (!temp.empty()) {
result.push_back(std::atoi(temp.c_str()));
temp.clear();
}
} else {
temp += input[i];
}
}
// Don't forget the last value
if (!temp.empty()) {
result.push_back(std::atoi(temp.c_str()));
}
return result;
}
enum modes { UNKNOWN, LIKE_ZONE, NOT_LIKE_ZONE, RELATED_TO_ZONE_TERRAIN };
TownHintSelector::TownHintSelector(std::vector<rmg::ZoneOptions::CTownHints> & townHints) :
@@ -80,11 +46,15 @@ TownHintSelector::TownHintSelector(std::vector<rmg::ZoneOptions::CTownHints> & t
ui->tableWidgetTownHints->setCellWidget(row, 0, combo);
std::vector<std::string> values(zones.size());
std::transform(zones.begin(), zones.end(), values.begin(), [](const int val) { return std::to_string(val); });
std::string valuesText = boost::algorithm::join(values, ",");
QLineEdit *lineEdit = new QLineEdit;
QRegularExpression regex("[0-9,]*");
QRegularExpression regex("^\\d+(,\\d+)*$");
QRegularExpressionValidator *validator = new QRegularExpressionValidator(regex, lineEdit);
lineEdit->setValidator(validator);
lineEdit->setText(QString::fromStdString(joinVector(zones, ',')));
lineEdit->setText(QString::fromStdString(valuesText));
ui->tableWidgetTownHints->setCellWidget(row, 1, lineEdit);
auto deleteButton = new QPushButton(tr("Delete"));
@@ -150,23 +120,28 @@ void TownHintSelector::showTownHintSelector(std::vector<rmg::ZoneOptions::CTownH
void TownHintSelector::on_buttonBoxResult_accepted()
{
townHints.clear();
for (int row = 0; row < ui->tableWidgetTownHints->rowCount() - 1; ++row)
for (int row = 0; row < ui->tableWidgetTownHints->rowCount() - 1; ++row) // iterate over all rows except the add button row
{
auto mode = static_cast<modes>(static_cast<QComboBox *>(ui->tableWidgetTownHints->cellWidget(row, 0))->currentData().toInt());
auto text = static_cast<QLineEdit *>(ui->tableWidgetTownHints->cellWidget(row, 1))->text().toStdString();
auto values = splitStringToVector(text, ',');
std::vector<std::string> values;
boost::split(values, text, boost::is_any_of(","));
if (!values.empty() && values.back().empty()) // remove "no number" after last comma; other cases are covered by regex
values.pop_back();
std::vector<int> numValues(values.size());
std::transform(values.begin(), values.end(), numValues.begin(), [](const std::string& str) { return std::stoi(str); });
rmg::ZoneOptions::CTownHints hint;
switch (mode)
{
case LIKE_ZONE:
hint.likeZone = values.at(0);
hint.likeZone = numValues.at(0);
break;
case NOT_LIKE_ZONE:
hint.notLikeZone = values;
hint.notLikeZone = numValues;
break;
case RELATED_TO_ZONE_TERRAIN:
hint.relatedToZoneTerrain = values.at(0);
hint.relatedToZoneTerrain = numValues.at(0);
break;
}
townHints.push_back(hint);
@@ -10,7 +10,6 @@
#pragma once
#include <QWidget>
#include "../StdInc.h"
#include "../../lib/rmg/CRmgTemplate.h"
namespace Ui {