Allow custom map connection direction input

This commit is contained in:
GriffinR 2025-04-14 11:43:54 -04:00
parent c54d875d3c
commit e19932b90c
3 changed files with 51 additions and 42 deletions

View File

@ -36,19 +36,18 @@ private:
protected:
virtual void mousePressEvent(QMouseEvent*) override;
virtual void focusInEvent(QFocusEvent*) override;
virtual void keyPressEvent(QKeyEvent*) override;
virtual bool eventFilter(QObject*, QEvent *event) override;
signals:
void selected();
void openMapClicked(MapConnection*);
private slots:
void on_comboBox_Direction_currentTextChanged(QString direction);
void on_comboBox_Map_currentTextChanged(QString mapName);
void on_spinBox_Offset_valueChanged(int offset);
void on_button_Delete_clicked();
void on_button_OpenMap_clicked();
private:
void commitDirection();
void commitMap(const QString &mapName);
void commitMove(int offset);
void commitRemove();
};
#endif // CONNECTIONSLISTITEM_H

View File

@ -7,44 +7,58 @@
ConnectionsListItem::ConnectionsListItem(QWidget *parent, MapConnection * connection, const QStringList &mapNames) :
QFrame(parent),
ui(new Ui::ConnectionsListItem)
ui(new Ui::ConnectionsListItem),
connection(connection),
map(connection->parentMap())
{
ui->setupUi(this);
setFocusPolicy(Qt::StrongFocus);
const QSignalBlocker blocker1(ui->comboBox_Direction);
const QSignalBlocker blocker2(ui->comboBox_Map);
const QSignalBlocker blocker3(ui->spinBox_Offset);
ui->comboBox_Direction->setEditable(false);
// Direction
const QSignalBlocker b_Direction(ui->comboBox_Direction);
ui->comboBox_Direction->setMinimumContentsLength(0);
ui->comboBox_Direction->addItems(MapConnection::cardinalDirections);
ui->comboBox_Direction->installEventFilter(this);
// We don't use QComboBox::currentTextChanged here to avoid unnecessary commits while typing.
connect(ui->comboBox_Direction, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ConnectionsListItem::commitDirection);
connect(ui->comboBox_Direction->lineEdit(), &QLineEdit::editingFinished, this, &ConnectionsListItem::commitDirection);
// Map
const QSignalBlocker b_Map(ui->comboBox_Map);
ui->comboBox_Map->setMinimumContentsLength(6);
ui->comboBox_Map->addItems(mapNames);
ui->comboBox_Map->setFocusedScrollingEnabled(false); // Scrolling could cause rapid changes to many different maps
ui->comboBox_Map->setInsertPolicy(QComboBox::NoInsert);
ui->comboBox_Map->installEventFilter(this);
ui->spinBox_Offset->setMinimum(INT_MIN);
ui->spinBox_Offset->setMaximum(INT_MAX);
// The map combo box only commits the change if it's a valid map name, so unlike Direction we can use QComboBox::currentTextChanged.
connect(ui->comboBox_Map, &QComboBox::currentTextChanged, this, &ConnectionsListItem::commitMap);
// 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)
const QSignalBlocker b(ui->comboBox_Map);
if (this->connection && 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++; });
// Offset
const QSignalBlocker b_Offset(ui->spinBox_Offset);
ui->spinBox_Offset->setMinimum(INT_MIN);
ui->spinBox_Offset->setMaximum(INT_MAX);
ui->spinBox_Offset->installEventFilter(this);
connect(ui->spinBox_Offset, &QSpinBox::editingFinished, [this] { this->actionId++; }); // Distinguish between move actions for the edit history
connect(ui->spinBox_Offset, &QSpinBox::valueChanged, this, &ConnectionsListItem::commitMove);
// If the connection changes externally we want to update to reflect the change.
connect(connection, &MapConnection::offsetChanged, this, &ConnectionsListItem::updateUI);
connect(connection, &MapConnection::directionChanged, this, &ConnectionsListItem::updateUI);
connect(connection, &MapConnection::targetMapNameChanged, this, &ConnectionsListItem::updateUI);
this->connection = connection;
this->map = connection->parentMap();
connect(ui->button_Delete, &QToolButton::clicked, this, &ConnectionsListItem::commitRemove);
connect(ui->button_OpenMap, &QToolButton::clicked, [this] { emit openMapClicked(this->connection); });
this->updateUI();
}
@ -66,13 +80,19 @@ void ConnectionsListItem::updateUI() {
ui->spinBox_Offset->setValue(this->connection->offset());
}
bool ConnectionsListItem::eventFilter(QObject*, QEvent *event) {
if (event->type() == QEvent::FocusIn)
this->setSelected(true);
return false;
}
void ConnectionsListItem::setSelected(bool selected) {
if (selected == this->isSelected)
return;
this->isSelected = selected;
this->setStyleSheet(selected ? ".ConnectionsListItem { border: 1px solid rgb(255, 0, 255); }"
: ".ConnectionsListItem { border-width: 1px; }");
this->setStyleSheet(selected ? QStringLiteral(".ConnectionsListItem { border: 1px solid rgb(255, 0, 255); }")
: QStringLiteral(".ConnectionsListItem { border-width: 1px; }"));
if (selected)
emit this->selected();
}
@ -81,41 +101,32 @@ void ConnectionsListItem::mousePressEvent(QMouseEvent *) {
this->setSelected(true);
}
void ConnectionsListItem::on_comboBox_Direction_currentTextChanged(QString direction) {
this->setSelected(true);
if (this->map)
void ConnectionsListItem::commitDirection() {
const QString direction = ui->comboBox_Direction->currentText();
if (this->map && this->connection && this->connection->direction() != direction) {
this->map->commit(new MapConnectionChangeDirection(this->connection, direction));
}
}
void ConnectionsListItem::on_comboBox_Map_currentTextChanged(QString mapName) {
this->setSelected(true);
void ConnectionsListItem::commitMap(const QString &mapName) {
if (this->map && ui->comboBox_Map->findText(mapName) >= 0)
this->map->commit(new MapConnectionChangeMap(this->connection, mapName));
}
void ConnectionsListItem::on_spinBox_Offset_valueChanged(int offset) {
this->setSelected(true);
void ConnectionsListItem::commitMove(int offset) {
if (this->map)
this->map->commit(new MapConnectionMove(this->connection, offset, this->actionId));
}
void ConnectionsListItem::on_button_Delete_clicked() {
void ConnectionsListItem::commitRemove() {
if (this->map)
this->map->commit(new MapConnectionRemove(this->map, this->connection));
}
void ConnectionsListItem::on_button_OpenMap_clicked() {
emit openMapClicked(this->connection);
}
void ConnectionsListItem::focusInEvent(QFocusEvent* event) {
this->setSelected(true);
QFrame::focusInEvent(event);
}
void ConnectionsListItem::keyPressEvent(QKeyEvent* event) {
if (event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace) {
on_button_Delete_clicked();
commitRemove();
event->accept();
} else {
QFrame::keyPressEvent(event);
}

View File

@ -8,7 +8,6 @@ NewMapConnectionDialog::NewMapConnectionDialog(QWidget *parent, Map* map, const
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
ui->comboBox_Direction->setEditable(false);
ui->comboBox_Direction->addItems(MapConnection::cardinalDirections);
ui->comboBox_Map->addItems(mapNames);