From 99894e5c8d4cddc17ed5f28e1545a71085bca6f3 Mon Sep 17 00:00:00 2001 From: codereader Date: Wed, 28 Sep 2022 12:09:16 +0200 Subject: [PATCH] #6107: Move rename/delete button actions to the new dialog --- radiant/ui/layers/LayerControlDialog.cpp | 112 ++++++++++++++++++++++- radiant/ui/layers/LayerControlDialog.h | 6 ++ 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/radiant/ui/layers/LayerControlDialog.cpp b/radiant/ui/layers/LayerControlDialog.cpp index b4031bebeb..9c83c30695 100644 --- a/radiant/ui/layers/LayerControlDialog.cpp +++ b/radiant/ui/layers/LayerControlDialog.cpp @@ -15,9 +15,13 @@ #include "wxutil/Button.h" #include "scene/LayerUsageBreakdown.h" +#include "wxutil/Bitmap.h" +#include "wxutil/EntryAbortedException.h" #include "wxutil/dataview/IndicatorColumn.h" #include "wxutil/dataview/TreeView.h" #include "wxutil/dataview/TreeViewItemStyle.h" +#include "wxutil/dialog/Dialog.h" +#include "wxutil/dialog/MessageBox.h" namespace ui { @@ -59,6 +63,7 @@ void LayerControlDialog::populateWindow() _layersView->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &LayerControlDialog::onItemActivated, this); _layersView->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &LayerControlDialog::onItemToggled, this); + _layersView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &LayerControlDialog::onItemSelected, this); SetSizer(new wxBoxSizer(wxVERTICAL)); @@ -83,15 +88,28 @@ void LayerControlDialog::createButtons() _hideAllLayers->Bind(wxEVT_BUTTON, [this](auto& ev) { setVisibilityOfAllLayers(false); }); // Create layer button + auto topRow = new wxBoxSizer(wxHORIZONTAL); + auto createButton = new wxButton(this, wxID_ANY, _("New")); createButton->SetBitmap(wxArtProvider::GetBitmap(wxART_PLUS)); - wxutil::button::connectToCommand(createButton, "CreateNewLayerDialog"); + _deleteButton = new wxBitmapButton(this, wxID_ANY, wxutil::GetLocalBitmap("delete.png")); + _deleteButton->Bind(wxEVT_BUTTON, &LayerControlDialog::onDeleteLayer, this); + _deleteButton->SetToolTip(_("Delete this layer")); + + _renameButton = new wxBitmapButton(this, wxID_ANY, wxutil::GetLocalBitmap("edit.png")); + _renameButton->Bind(wxEVT_BUTTON, &LayerControlDialog::onRenameLayer, this); + _renameButton->SetToolTip(_("Rename this layer")); + + topRow->Add(createButton, 1, wxEXPAND | wxRIGHT, 6); + topRow->Add(_renameButton, 0, wxEXPAND | wxRIGHT, 6); + topRow->Add(_deleteButton, 0, wxEXPAND, 6); + hideShowBox->Add(_showAllLayers, 1, wxEXPAND | wxTOP, 6); hideShowBox->Add(_hideAllLayers, 1, wxEXPAND | wxLEFT | wxTOP, 6); - GetSizer()->Add(createButton, 0, wxEXPAND | wxLEFT | wxRIGHT, 12); + GetSizer()->Add(topRow, 0, wxEXPAND | wxLEFT | wxRIGHT, 12); GetSizer()->Add(hideShowBox, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 12); } @@ -212,8 +230,20 @@ void LayerControlDialog::updateButtonSensitivity(std::size_t numVisible, std::si { _showAllLayers->Enable(numHidden > 0); _hideAllLayers->Enable(numVisible > 0); + + updateItemActionSensitivity(); } +void LayerControlDialog::updateItemActionSensitivity() +{ + auto selectedLayerId = getSelectedLayerId(); + + // Don't allow deleting or renaming the default layer (or -1) + _deleteButton->Enable(selectedLayerId > 0); + _renameButton->Enable(selectedLayerId > 0); +} + + void LayerControlDialog::updateLayerUsage() { _rescanSelectionOnIdle = false; @@ -461,4 +491,82 @@ void LayerControlDialog::onItemToggled(wxDataViewEvent& ev) } } +void LayerControlDialog::onItemSelected(wxDataViewEvent& ev) +{ + updateItemActionSensitivity(); +} + +void LayerControlDialog::onRenameLayer(wxCommandEvent& ev) +{ + if (!GlobalMapModule().getRoot()) + { + rError() << "Can't rename layer, no map root present" << std::endl; + return; + } + + auto selectedLayerId = getSelectedLayerId(); + auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager(); + + while (true) + { + // Query the name of the new layer from the user + std::string newLayerName; + + try + { + newLayerName = wxutil::Dialog::TextEntryDialog( + _("Rename Layer"), + _("Enter new Layer Name"), + layerSystem.getLayerName(selectedLayerId), + this + ); + } + catch (wxutil::EntryAbortedException&) + { + break; + } + + // Attempt to rename the layer, this will return -1 if the operation fails + bool success = layerSystem.renameLayer(selectedLayerId, newLayerName); + + if (success) + { + // Stop here, the control might already have been destroyed + GlobalMapModule().setModified(true); + return; + } + else + { + // Wrong name, let the user try again + wxutil::Messagebox::ShowError(_("Could not rename layer, please try again.")); + continue; + } + } +} + +void LayerControlDialog::onDeleteLayer(wxCommandEvent& ev) +{ + if (!GlobalMapModule().getRoot()) + { + rError() << "Can't delete layer, no map root present" << std::endl; + return; + } + + auto selectedLayerId = getSelectedLayerId(); + auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager(); + + // Ask the about the deletion + auto msg = _("Do you really want to delete this layer?"); + msg += "\n" + layerSystem.getLayerName(selectedLayerId); + + IDialogPtr box = GlobalDialogManager().createMessageBox( + _("Confirm Layer Deletion"), msg, IDialog::MESSAGE_ASK + ); + + if (box->run() == IDialog::RESULT_YES) + { + GlobalCommandSystem().executeCommand("DeleteLayer", cmd::Argument(selectedLayerId)); + } +} + } // namespace ui diff --git a/radiant/ui/layers/LayerControlDialog.h b/radiant/ui/layers/LayerControlDialog.h index 86dca2c3b2..deba32666e 100644 --- a/radiant/ui/layers/LayerControlDialog.h +++ b/radiant/ui/layers/LayerControlDialog.h @@ -50,6 +50,8 @@ class LayerControlDialog : wxButton* _showAllLayers; wxButton* _hideAllLayers; + wxButton* _renameButton; + wxButton* _deleteButton; bool _refreshTreeOnIdle; bool _updateTreeOnIdle; @@ -99,6 +101,7 @@ class LayerControlDialog : // Update the usage colours on the controls void updateLayerUsage(); void updateButtonSensitivity(std::size_t numVisible, std::size_t numHidden); + void updateItemActionSensitivity(); // Creates the option buttons void createButtons(); @@ -113,6 +116,9 @@ class LayerControlDialog : void onItemActivated(wxDataViewEvent& ev); void onItemToggled(wxDataViewEvent& ev); + void onItemSelected(wxDataViewEvent& ev); + void onRenameLayer(wxCommandEvent& ev); + void onDeleteLayer(wxCommandEvent& ev); int getSelectedLayerId(); };