From a4fdb0de6410e3cff659bdfb1b8598be29a6013c Mon Sep 17 00:00:00 2001 From: garak Date: Wed, 8 Feb 2023 17:08:42 -0500 Subject: [PATCH] fix new map popup window to allow layout selection --- forms/newmappopup.ui | 154 +++++++++++++++++++++++++++++++-------- include/ui/newmappopup.h | 3 + src/mainwindow.cpp | 101 +++++++++++++------------ src/ui/maplistmodels.cpp | 1 + src/ui/newmappopup.cpp | 61 +++++++++++++--- 5 files changed, 234 insertions(+), 86 deletions(-) diff --git a/forms/newmappopup.ui b/forms/newmappopup.ui index b102b1cc..3a83073f 100644 --- a/forms/newmappopup.ui +++ b/forms/newmappopup.ui @@ -7,7 +7,7 @@ 0 0 410 - 621 + 687 @@ -73,14 +73,14 @@ - + Map Width - + <html><head/><body><p>Width (in blocks) of the new map.</p></body></html> @@ -90,14 +90,14 @@ - + Map Height - + <html><head/><body><p>Height (in blocks) of the new map.</p></body></html> @@ -107,14 +107,14 @@ - + Border Width - + <html><head/><body><p>Width (in blocks) of the new map's border.</p></body></html> @@ -124,14 +124,14 @@ - + Border Height - + <html><head/><body><p>Height (in blocks) of the new map's border.</p></body></html> @@ -141,14 +141,14 @@ - + Primary Tileset - + <html><head/><body><p>The primary tileset for the new map.</p></body></html> @@ -158,14 +158,14 @@ - + Secondary Tileset - + <html><head/><body><p>The secondary tileset for the new map.</p></body></html> @@ -175,14 +175,14 @@ - + Type - + <html><head/><body><p>The map type is a general attribute, which is used for many different things. For example. it determines whether biking or running is allowed.</p></body></html> @@ -192,14 +192,14 @@ - + Location - + <html><head/><body><p>The section of the region map which the map is grouped under. This also determines the name of the map that is displayed when the player enters it.</p></body></html> @@ -209,14 +209,14 @@ - + Song - + <html><head/><body><p>The default background music for this map.</p></body></html> @@ -226,14 +226,14 @@ - + Can Fly To - + <html><head/><body><p>Whether to add a heal location to the new map.</p></body></html> @@ -243,14 +243,14 @@ - + Show Location Name - + <html><head/><body><p>Whether or not to display the location name when the player enters the map.</p></body></html> @@ -260,14 +260,14 @@ - + Allow Running - + <html><head/><body><p>Allows the player to use Running Shoes</p></body></html> @@ -277,14 +277,14 @@ - + Allow Biking - + <html><head/><body><p>Allows the player to use a Bike</p></body></html> @@ -294,14 +294,14 @@ - + Allow Dig & Escape Rope - + <html><head/><body><p>Allows the player to use Dig or Escape Rope</p></body></html> @@ -311,14 +311,14 @@ - + Floor Number - + <html><head/><body><p>Floor number to be used for maps with elevators.</p></body></html> @@ -328,6 +328,96 @@ + + + + false + + + + + + + Layout + + + + + + + + + Use Existing Layout + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + @@ -396,7 +486,7 @@ 0 0 410 - 21 + 22 diff --git a/include/ui/newmappopup.h b/include/ui/newmappopup.h index e668e863..3d24715d 100644 --- a/include/ui/newmappopup.h +++ b/include/ui/newmappopup.h @@ -23,6 +23,7 @@ public: bool importedMap; QString layoutId; void init(); + void initUi(); void init(MapSortOrder type, QVariant data); void init(Layout *); static void setDefaultSettings(Project *project); @@ -60,6 +61,8 @@ private: static struct Settings settings; private slots: + void on_checkBox_UseExistingLayout_stateChanged(int state); + void on_comboBox_Layout_currentTextChanged(const QString &text); void on_pushButton_NewMap_Accept_clicked(); void on_lineEdit_NewMap_Name_textChanged(const QString &); }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b62aa339..47ce878b 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1153,68 +1153,79 @@ void MainWindow::sortMapList() { } void MainWindow::onOpenMapListContextMenu(const QPoint &point) { - /// !TODO - // QModelIndex index = mapListProxyModel->mapToSource(ui->mapList->indexAt(point)); - // if (!index.isValid()) { - // return; - // } + QStandardItemModel *model; + int dataRole; + FilterChildrenProxyModel *proxy; + QTreeView *list; + void (MainWindow::*addFunction)(QAction *); + QString actionText; - switch (ui->mapListContainer->currentIndex()) { - // - case MapListTab::Groups: + switch (this->mapSortOrder) { + case MapSortOrder::SortByGroup: + model = this->mapGroupModel; + dataRole = MapListRoles::GroupRole; + proxy = this->groupListProxyModel; + list = this->ui->mapList; + addFunction = &MainWindow::onAddNewMapToGroupClick; + actionText = "Add New Map to Group"; break; - case MapListTab::Areas: + case MapSortOrder::SortByArea: + model = this->mapAreaModel; + dataRole = Qt::UserRole; + proxy = this->areaListProxyModel; + list = this->ui->areaList; + addFunction = &MainWindow::onAddNewMapToAreaClick; + actionText = "Add New Map to Area"; break; - case MapListTab::Layouts: + case MapSortOrder::SortByLayout: + model = this->layoutTreeModel; + dataRole = Qt::UserRole; + proxy = this->layoutListProxyModel; + list = this->ui->layoutList; + addFunction = &MainWindow::onAddNewMapToLayoutClick; + actionText = "Add New Map with Layout"; break; } - // QStandardItem *selectedItem = mapListModel->itemFromIndex(index); - // QVariant itemType = selectedItem->data(MapListUserRoles::TypeRole); - // if (!itemType.isValid()) { - // return; - // } + QModelIndex index = proxy->mapToSource(list->indexAt(point)); + if (!index.isValid()) { + return; + } - // // Build custom context menu depending on which type of item was selected (map group, map name, etc.) - // if (itemType == "map_group") { - // QString groupName = selectedItem->data(Qt::UserRole).toString(); - // int groupNum = selectedItem->data(MapListUserRoles::GroupRole).toInt(); - // QMenu* menu = new QMenu(this); - // QActionGroup* actions = new QActionGroup(menu); - // actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupNum); - // connect(actions, &QActionGroup::triggered, this, &MainWindow::onAddNewMapToGroupClick); - // menu->exec(QCursor::pos()); - // } else if (itemType == "map_sec") { - // QString secName = selectedItem->data(Qt::UserRole).toString(); - // QMenu* menu = new QMenu(this); - // QActionGroup* actions = new QActionGroup(menu); - // actions->addAction(menu->addAction("Add New Map to Area"))->setData(secName); - // connect(actions, &QActionGroup::triggered, this, &MainWindow::onAddNewMapToAreaClick); - // menu->exec(QCursor::pos()); - // } else if (itemType == "map_layout") { - // QString layoutId = selectedItem->data(MapListUserRoles::TypeRole2).toString(); - // QMenu* menu = new QMenu(this); - // QActionGroup* actions = new QActionGroup(menu); - // actions->addAction(menu->addAction("Add New Map with Layout"))->setData(layoutId); - // connect(actions, &QActionGroup::triggered, this, &MainWindow::onAddNewMapToLayoutClick); - // menu->exec(QCursor::pos()); - // } + QStandardItem *selectedItem = model->itemFromIndex(index); + + if (selectedItem->parent()) { + return; + } + + QVariant itemData = selectedItem->data(dataRole); + if (!itemData.isValid()) { + return; + } + + QMenu menu(this); + QActionGroup actions(&menu); + actions.addAction(menu.addAction(actionText))->setData(itemData); + (this->*addFunction)(menu.exec(QCursor::pos())); } -void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) -{ +void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) { + if (!triggeredAction) return; + openNewMapPopupWindow(); this->newMapPrompt->init(MapSortOrder::SortByGroup, triggeredAction->data()); } -void MainWindow::onAddNewMapToAreaClick(QAction* triggeredAction) -{ +void MainWindow::onAddNewMapToAreaClick(QAction* triggeredAction) { + if (!triggeredAction) return; + openNewMapPopupWindow(); this->newMapPrompt->init(MapSortOrder::SortByArea, triggeredAction->data()); } -void MainWindow::onAddNewMapToLayoutClick(QAction* triggeredAction) -{ +void MainWindow::onAddNewMapToLayoutClick(QAction* triggeredAction) { + if (!triggeredAction) return; + openNewMapPopupWindow(); this->newMapPrompt->init(MapSortOrder::SortByLayout, triggeredAction->data()); } diff --git a/src/ui/maplistmodels.cpp b/src/ui/maplistmodels.cpp index b904522c..bc9ead2f 100644 --- a/src/ui/maplistmodels.cpp +++ b/src/ui/maplistmodels.cpp @@ -29,6 +29,7 @@ QStandardItem *MapGroupModel::createMapItem(QString mapName, int groupIndex, int map->setEditable(false); map->setData(mapName, Qt::UserRole); map->setData("map_name", MapListRoles::TypeRole); + map->setData(groupIndex, MapListRoles::GroupRole); // map->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled); this->mapItems.insert(mapName, map); return map; diff --git a/src/ui/newmappopup.cpp b/src/ui/newmappopup.cpp index 597d53d1..0b13c474 100644 --- a/src/ui/newmappopup.cpp +++ b/src/ui/newmappopup.cpp @@ -26,7 +26,7 @@ NewMapPopup::~NewMapPopup() delete ui; } -void NewMapPopup::init() { +void NewMapPopup::initUi() { // Populate combo boxes ui->comboBox_NewMap_Primary_Tileset->addItems(project->primaryTilesetLabels); ui->comboBox_NewMap_Secondary_Tileset->addItems(project->secondaryTilesetLabels); @@ -35,6 +35,10 @@ void NewMapPopup::init() { ui->comboBox_NewMap_Type->addItems(project->mapTypes); ui->comboBox_NewMap_Location->addItems(project->mapSectionValueToName.values()); + const QSignalBlocker b(ui->comboBox_Layout); + ui->comboBox_Layout->addItems(project->mapLayoutsTable); + this->layoutId = project->mapLayoutsTable.first(); + // Set spin box limits ui->spinBox_NewMap_Width->setMinimum(1); ui->spinBox_NewMap_Height->setMinimum(1); @@ -66,6 +70,10 @@ void NewMapPopup::init() { ui->spinBox_NewMap_Floor_Number->setVisible(hasFloorNumber); ui->label_NewMap_Floor_Number->setVisible(hasFloorNumber); + this->updateGeometry(); +} + +void NewMapPopup::init() { // Restore previous settings ui->lineEdit_NewMap_Name->setText(project->getNewMapName()); ui->comboBox_NewMap_Group->setTextItem(settings.group); @@ -86,6 +94,7 @@ void NewMapPopup::init() { ui->spinBox_NewMap_Floor_Number->setValue(settings.floorNumber); // Connect signals + // !TODO: make sure this doesnt reconnect a million times connect(ui->spinBox_NewMap_Width, QOverload::of(&QSpinBox::valueChanged), [=](int){checkNewMapDimensions();}); connect(ui->spinBox_NewMap_Height, QOverload::of(&QSpinBox::valueChanged), [=](int){checkNewMapDimensions();}); @@ -94,6 +103,7 @@ void NewMapPopup::init() { // Creating new map by right-clicking in the map list void NewMapPopup::init(MapSortOrder type, QVariant data) { + initUi(); switch (type) { case MapSortOrder::SortByGroup: @@ -103,6 +113,7 @@ void NewMapPopup::init(MapSortOrder type, QVariant data) { settings.location = data.toString(); break; case MapSortOrder::SortByLayout: + this->ui->checkBox_UseExistingLayout->setCheckState(Qt::Checked); useLayout(data.toString()); break; } @@ -205,26 +216,58 @@ void NewMapPopup::saveSettings() { void NewMapPopup::useLayoutSettings(Layout *layout) { if (!layout) return; + settings.width = layout->width; + ui->spinBox_NewMap_Width->setValue(layout->width); + settings.height = layout->height; + ui->spinBox_NewMap_Height->setValue(layout->height); + settings.borderWidth = layout->border_width; + ui->spinBox_NewMap_BorderWidth->setValue(layout->border_width); + settings.borderHeight = layout->border_height; + ui->spinBox_NewMap_BorderWidth->setValue(layout->border_height); + settings.primaryTilesetLabel = layout->tileset_primary_label; + ui->comboBox_NewMap_Primary_Tileset->setCurrentIndex(ui->comboBox_NewMap_Primary_Tileset->findText(layout->tileset_primary_label)); + settings.secondaryTilesetLabel = layout->tileset_secondary_label; + ui->comboBox_NewMap_Secondary_Tileset->setCurrentIndex(ui->comboBox_NewMap_Secondary_Tileset->findText(layout->tileset_secondary_label)); } void NewMapPopup::useLayout(QString layoutId) { this->existingLayout = true; this->layoutId = layoutId; - useLayoutSettings(project->mapLayouts.value(this->layoutId)); - // Dimensions and tilesets can't be changed for new maps using an existing layout - ui->spinBox_NewMap_Width->setDisabled(true); - ui->spinBox_NewMap_Height->setDisabled(true); - ui->spinBox_NewMap_BorderWidth->setDisabled(true); - ui->spinBox_NewMap_BorderHeight->setDisabled(true); - ui->comboBox_NewMap_Primary_Tileset->setDisabled(true); - ui->comboBox_NewMap_Secondary_Tileset->setDisabled(true); + this->ui->comboBox_Layout->setCurrentIndex(this->ui->comboBox_Layout->findText(layoutId)); + + useLayoutSettings(project->mapLayouts.value(this->layoutId)); +} + +void NewMapPopup::on_checkBox_UseExistingLayout_stateChanged(int state) { + bool layoutEditsEnabled = (state == Qt::Unchecked); + + this->ui->comboBox_Layout->setEnabled(!layoutEditsEnabled); + + this->ui->spinBox_NewMap_Width->setEnabled(layoutEditsEnabled); + this->ui->spinBox_NewMap_Height->setEnabled(layoutEditsEnabled); + this->ui->spinBox_NewMap_BorderWidth->setEnabled(layoutEditsEnabled); + this->ui->spinBox_NewMap_BorderWidth->setEnabled(layoutEditsEnabled); + this->ui->comboBox_NewMap_Primary_Tileset->setEnabled(layoutEditsEnabled); + this->ui->comboBox_NewMap_Secondary_Tileset->setEnabled(layoutEditsEnabled); + + if (!layoutEditsEnabled) { + useLayout(this->layoutId);//this->ui->comboBox_Layout->currentText()); + } else { + this->existingLayout = false; + } +} + +void NewMapPopup::on_comboBox_Layout_currentTextChanged(const QString &text) { + if (this->project->mapLayoutsTable.contains(text)) { + useLayout(text); + } } void NewMapPopup::on_lineEdit_NewMap_Name_textChanged(const QString &text) {