Skip to content

Commit

Permalink
Improve: add new area on move room if user inputs new name (#6345)
Browse files Browse the repository at this point in the history
Improve: add new area on move room if user inputs new name (#6345)
  • Loading branch information
Delwing committed Apr 13, 2023
1 parent 7fe7360 commit 66bc9e5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 27 deletions.
53 changes: 49 additions & 4 deletions src/T2DMap.cpp
Expand Up @@ -4102,10 +4102,17 @@ void T2DMap::slot_setArea()
}
set_room_area_dialog->setAttribute(Qt::WA_DeleteOnClose);
arealist_combobox = set_room_area_dialog->findChild<QComboBox*>("arealist_combobox");

if (!arealist_combobox) {
return;
}

auto label_info = set_room_area_dialog->findChild<QLabel*>("label_info");
auto font = QFont();
font.setPointSize(font.pointSize() - 1);
label_info->setFont(font);
arealist_combobox->setInsertPolicy(QComboBox::NoInsert);

QStringList sortedAreaList;
sortedAreaList = mpMap->mpRoomDB->getAreaNamesMap().values();

Expand All @@ -4115,15 +4122,46 @@ void T2DMap::slot_setArea()

std::sort( sortedAreaList.begin(), sortedAreaList.end(), sorter);


const QMap<int, QString>& areaNamesMap = mpMap->mpRoomDB->getAreaNamesMap();
for (int i = 0, total = sortedAreaList.count(); i < total; ++i) {
int areaId = areaNamesMap.key(sortedAreaList.at(i));
arealist_combobox->addItem(qsl("%1 (%2)").arg(sortedAreaList.at(i), QString::number(areaId)), QString::number(areaId));
}

connect(set_room_area_dialog, &QDialog::accepted, this, [=]() {
int newAreaId = arealist_combobox->itemData(arealist_combobox->currentIndex()).toInt();
connect(arealist_combobox, &QComboBox::currentTextChanged, this, [=](const QString newText) {
auto buttonBox = set_room_area_dialog->findChild<QDialogButtonBox*>("buttonBox");
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!newText.trimmed().isEmpty());
if (!newText.trimmed().isEmpty() && arealist_combobox->findText(newText.trimmed(), Qt::MatchExactly) == -1
&& !sortedAreaList.contains(newText.trimmed())) {
label_info->setText(tr("This will create new area: %1").arg(arealist_combobox->currentText()));
} else {
label_info->clear();
}
});

connect(set_room_area_dialog, &QDialog::accepted, [=]() {
int newAreaId;
if (arealist_combobox->findText(arealist_combobox->currentText(), Qt::MatchExactly) != -1) {
newAreaId = arealist_combobox->itemData(arealist_combobox->currentIndex()).toInt();
} else if (sortedAreaList.contains(arealist_combobox->currentText().trimmed())) {
newAreaId = mpMap->mpRoomDB->getAreaNamesMap().key(arealist_combobox->currentText());
} else {
auto newAreaName = arealist_combobox->currentText().trimmed();
newAreaId = mpMap->mpRoomDB->addArea(newAreaName);
if (!newAreaId) {
mpMap->postMessage(tr("[ ERROR ] - Unable to add \"%1\" as an area to the map.\n"
"See the \"[MAP ERROR:]\" message for the reason.",
// Intentional separator between argument
"The '[MAP ERROR:]' text should be the same as that used for the translation of \"[MAP ERROR:]%1\n\" in the 'TMAP::logerror(...)' function.").arg(
newAreaName));
return;
}
mpMap->postMessage(
tr("[ OK ] - Added \"%1\" (%2) area to map.").arg(newAreaName, QString::number(newAreaId)));
mpMap->setUnsaved(__func__);

mpMap->mpMapper->updateAreaComboBox();
}
mMultiRect = QRect(0, 0, 0, 0);
QSetIterator<int> itSelectedRoom = mMultiSelectionSet;
while (itSelectedRoom.hasNext()) {
Expand All @@ -4149,9 +4187,16 @@ void T2DMap::slot_setArea()
}
}
}
auto &targetAreaName = mpMap->mpRoomDB->getAreaNamesMap().value(newAreaId);
mpMap->mpMapper->comboBox_showArea->setCurrentText(targetAreaName);
#if (QT_VERSION) >= (QT_VERSION_CHECK(5, 15, 0))
switchArea(targetAreaName);
#else
slot_switchArea(targetAreaName);
#endif
}
}
repaint();
update();
});

set_room_area_dialog->show();
Expand Down
51 changes: 28 additions & 23 deletions src/ui/set_room_area.ui
Expand Up @@ -6,14 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>377</width>
<width>443</width>
<height>105</height>
</rect>
</property>
<property name="windowTitle">
<string>Move rooms to another area</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
Expand All @@ -22,30 +22,35 @@
</widget>
</item>
<item>
<widget class="QComboBox" name="arealist_combobox"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<widget class="QComboBox" name="arealist_combobox">
<property name="editable">
<bool>true</bool>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<layout class="QHBoxLayout" name="layout_footer">
<item>
<widget class="QLabel" name="label_info">
<property name="text">
<string>Input new area name to create one.</string>
</property>
<property name="textFormat">
<enum>Qt::AutoText</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down

0 comments on commit 66bc9e5

Please sign in to comment.