2024-08-09 02:24:15 +01:00
|
|
|
#include "newmapconnectiondialog.h"
|
|
|
|
#include "ui_newmapconnectiondialog.h"
|
|
|
|
|
|
|
|
NewMapConnectionDialog::NewMapConnectionDialog(QWidget *parent, Map* map, const QStringList &mapNames) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::NewMapConnectionDialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
|
|
|
ui->comboBox_Direction->setEditable(false);
|
|
|
|
ui->comboBox_Direction->setMinimumContentsLength(0);
|
|
|
|
ui->comboBox_Direction->addItems(MapConnection::cardinalDirections);
|
|
|
|
|
|
|
|
ui->comboBox_Map->setMinimumContentsLength(6);
|
|
|
|
ui->comboBox_Map->addItems(mapNames);
|
2024-08-15 03:22:54 +01:00
|
|
|
ui->comboBox_Map->setInsertPolicy(QComboBox::NoInsert);
|
2024-08-09 02:24:15 +01:00
|
|
|
|
|
|
|
// Choose default direction
|
|
|
|
QMap<QString, int> directionCounts;
|
2024-08-09 06:29:28 +01:00
|
|
|
for (auto connection : map->getConnections()) {
|
2024-08-09 02:24:15 +01:00
|
|
|
directionCounts[connection->direction()]++;
|
|
|
|
}
|
|
|
|
QString defaultDirection;
|
|
|
|
int minCount = INT_MAX;
|
|
|
|
for (auto direction : MapConnection::cardinalDirections) {
|
|
|
|
if (directionCounts[direction] < minCount) {
|
|
|
|
defaultDirection = direction;
|
|
|
|
minCount = directionCounts[direction];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ui->comboBox_Direction->setTextItem(defaultDirection);
|
|
|
|
|
|
|
|
// Choose default map
|
|
|
|
QString defaultMapName;
|
|
|
|
if (mapNames.isEmpty()) {
|
|
|
|
defaultMapName = QString();
|
|
|
|
} else if (mapNames.first() == map->name && mapNames.length() > 1) {
|
|
|
|
// Prefer not to connect the map to itself
|
|
|
|
defaultMapName = mapNames.at(1);
|
|
|
|
} else {
|
|
|
|
defaultMapName = mapNames.first();
|
|
|
|
}
|
|
|
|
ui->comboBox_Map->setTextItem(defaultMapName);
|
|
|
|
}
|
|
|
|
|
|
|
|
NewMapConnectionDialog::~NewMapConnectionDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewMapConnectionDialog::accept() {
|
2024-08-15 03:22:54 +01:00
|
|
|
// Invalid map names are not allowed
|
|
|
|
if (ui->comboBox_Map->findText(ui->comboBox_Map->currentText()) < 0) {
|
|
|
|
// TODO: Display error message
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit accepted(new MapConnection(ui->comboBox_Map->currentText(), ui->comboBox_Direction->currentText()));
|
2024-08-09 02:24:15 +01:00
|
|
|
QDialog::accept();
|
|
|
|
}
|