porymap/src/ui/connectionslistitem.cpp

105 lines
3.7 KiB
C++
Raw Normal View History

2024-06-26 21:03:39 +01:00
#include "connectionslistitem.h"
#include "ui_connectionslistitem.h"
2024-08-15 03:22:54 +01:00
#include "editcommands.h"
#include "map.h"
#include <QLineEdit>
2024-06-26 21:03:39 +01:00
ConnectionsListItem::ConnectionsListItem(QWidget *parent, MapConnection * connection, const QStringList &mapNames) :
2024-06-26 21:03:39 +01:00
QFrame(parent),
2024-07-08 21:01:30 +01:00
ui(new Ui::ConnectionsListItem)
2024-06-26 21:03:39 +01:00
{
ui->setupUi(this);
const QSignalBlocker blocker1(ui->comboBox_Direction);
const QSignalBlocker blocker2(ui->comboBox_Map);
const QSignalBlocker blocker3(ui->spinBox_Offset);
ui->comboBox_Direction->setEditable(false);
ui->comboBox_Direction->setMinimumContentsLength(0);
2024-07-12 19:05:37 +01:00
ui->comboBox_Direction->addItems(MapConnection::cardinalDirections);
2024-06-26 21:03:39 +01:00
ui->comboBox_Map->setMinimumContentsLength(6);
ui->comboBox_Map->addItems(mapNames);
ui->comboBox_Map->setFocusedScrollingEnabled(false); // Scrolling could cause rapid changes to many different maps
2024-08-15 03:22:54 +01:00
ui->comboBox_Map->setInsertPolicy(QComboBox::NoInsert);
2024-07-01 18:58:58 +01:00
2024-07-01 19:10:55 +01:00
ui->spinBox_Offset->setMinimum(INT_MIN);
ui->spinBox_Offset->setMaximum(INT_MAX);
2024-07-01 18:58:58 +01:00
2024-08-15 03:22:54 +01:00
// Invalid map names are not considered a change. If editing finishes with an invalid name, restore the previous name.
connect(ui->comboBox_Map->lineEdit(), &QLineEdit::editingFinished, [this] {
const QSignalBlocker blocker(ui->comboBox_Map);
if (ui->comboBox_Map->findText(ui->comboBox_Map->currentText()) < 0)
ui->comboBox_Map->setTextItem(this->connection->targetMapName());
});
// Distinguish between move actions for the edit history
connect(ui->spinBox_Offset, &QSpinBox::editingFinished, [this] { this->actionId++; });
2024-07-08 21:01:30 +01:00
this->connection = connection;
2024-08-15 03:22:54 +01:00
this->map = connection->parentMap();
this->updateUI();
2024-06-26 21:03:39 +01:00
}
ConnectionsListItem::~ConnectionsListItem()
{
delete ui;
}
void ConnectionsListItem::updateUI() {
2024-08-15 03:22:54 +01:00
if (!this->connection)
return;
2024-06-26 21:03:39 +01:00
const QSignalBlocker blocker1(ui->comboBox_Direction);
const QSignalBlocker blocker2(ui->comboBox_Map);
const QSignalBlocker blocker3(ui->spinBox_Offset);
2024-08-04 21:08:16 +01:00
ui->comboBox_Direction->setTextItem(this->connection->direction());
ui->comboBox_Map->setTextItem(this->connection->targetMapName());
ui->spinBox_Offset->setValue(this->connection->offset());
2024-06-26 21:03:39 +01:00
}
void ConnectionsListItem::setSelected(bool selected) {
if (selected == this->isSelected)
return;
this->isSelected = selected;
2024-08-09 06:29:28 +01:00
this->setStyleSheet(selected ? ".ConnectionsListItem { border: 1px solid rgb(255, 0, 255); }"
: ".ConnectionsListItem { border-width: 1px; }");
if (selected)
emit this->selected();
}
void ConnectionsListItem::mousePressEvent(QMouseEvent *) {
this->setSelected(true);
}
2024-08-15 03:22:54 +01:00
// TODO: This happens by accident a lot. Convert to button?
2024-07-08 16:44:23 +01:00
void ConnectionsListItem::mouseDoubleClickEvent(QMouseEvent *) {
emit doubleClicked(this->connection);
2024-07-08 16:44:23 +01:00
}
2024-08-04 21:08:16 +01:00
void ConnectionsListItem::on_comboBox_Direction_currentTextChanged(const QString &direction) {
this->setSelected(true);
2024-08-15 03:22:54 +01:00
if (this->map)
this->map->editHistory.push(new MapConnectionChangeDirection(this->connection, direction));
2024-07-01 18:58:58 +01:00
}
2024-08-04 21:08:16 +01:00
void ConnectionsListItem::on_comboBox_Map_currentTextChanged(const QString &mapName) {
2024-07-08 21:01:30 +01:00
this->setSelected(true);
2024-08-15 03:22:54 +01:00
if (this->map && ui->comboBox_Map->findText(mapName) >= 0)
this->map->editHistory.push(new MapConnectionChangeMap(this->connection, mapName));
2024-07-01 18:58:58 +01:00
}
2024-08-04 21:08:16 +01:00
void ConnectionsListItem::on_spinBox_Offset_valueChanged(int offset) {
this->setSelected(true);
2024-08-15 03:22:54 +01:00
if (this->map)
this->map->editHistory.push(new MapConnectionMove(this->connection, offset, this->actionId));
2024-07-01 18:58:58 +01:00
}
2024-08-04 21:08:16 +01:00
void ConnectionsListItem::on_button_Delete_clicked() {
2024-08-15 03:22:54 +01:00
if (this->map)
this->map->editHistory.push(new MapConnectionRemove(this->map, this->connection));
2024-07-01 18:58:58 +01:00
}