Skip to content

Commit

Permalink
#5780: Add FileSaveConfirmationHandler to UI module. Map module is no…
Browse files Browse the repository at this point in the history
…w sending the FileSaveConfirmation to query the user's intent, remove the corresponding UI references from Map.cpp.
  • Loading branch information
codereader committed Oct 14, 2021
1 parent 2776128 commit 3d77e54
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
57 changes: 57 additions & 0 deletions radiant/ui/FileSaveConfirmationHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include "idialogmanager.h"
#include "iradiant.h"
#include "i18n.h"
#include "messages/FileSaveConfirmation.h"

namespace ui
{

class FileSaveConfirmationHandler
{
private:
std::size_t _msgSubscription;

public:
FileSaveConfirmationHandler()
{
_msgSubscription = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::FileSaveConfirmation,
radiant::TypeListener<radiant::FileSaveConfirmation>(
sigc::mem_fun(this, &FileSaveConfirmationHandler::handleRequest)));
}

~FileSaveConfirmationHandler()
{
GlobalRadiantCore().getMessageBus().removeListener(_msgSubscription);
}

private:
void handleRequest(radiant::FileSaveConfirmation& msg)
{
// Ask the user
auto msgBox = GlobalDialogManager().createMessageBox(
msg.getTitle(), msg.getMessage(),
ui::IDialog::MESSAGE_SAVECONFIRMATION
);

auto result = msgBox->run();

msg.setHandled(true);

switch (result)
{
case IDialog::RESULT_CANCELLED:
msg.setAction(radiant::FileSaveConfirmation::Action::Cancel);
break;
case IDialog::RESULT_YES:
msg.setAction(radiant::FileSaveConfirmation::Action::SaveChanges);
break;
default:
msg.setAction(radiant::FileSaveConfirmation::Action::DiscardChanges);
}
}
};

}
1 change: 1 addition & 0 deletions radiant/ui/UserInterfaceModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ void UserInterfaceModule::initialiseModule(const IApplicationContext& ctx)
_autoSaveRequestHandler.reset(new AutoSaveRequestHandler);
_fileSelectionRequestHandler.reset(new FileSelectionRequestHandler);
_fileOverwriteConfirmationHandler.reset(new FileOverwriteConfirmationHandler);
_fileSaveConfirmationHandler.reset(new FileSaveConfirmationHandler);

initialiseEntitySettings();

Expand Down
2 changes: 2 additions & 0 deletions radiant/ui/UserInterfaceModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "LongRunningOperationHandler.h"
#include "FileSelectionRequestHandler.h"
#include "FileOverwriteConfirmationHandler.h"
#include "FileSaveConfirmationHandler.h"
#include "AutoSaveRequestHandler.h"
#include "MapFileProgressHandler.h"
#include "ManipulatorToggle.h"
Expand Down Expand Up @@ -45,6 +46,7 @@ class UserInterfaceModule :
std::unique_ptr<AutoSaveRequestHandler> _autoSaveRequestHandler;
std::unique_ptr<FileSelectionRequestHandler> _fileSelectionRequestHandler;
std::unique_ptr<FileOverwriteConfirmationHandler> _fileOverwriteConfirmationHandler;
std::unique_ptr<FileSaveConfirmationHandler> _fileSaveConfirmationHandler;
std::unique_ptr<statusbar::ShaderClipboardStatus> _shaderClipboardStatus;
std::unique_ptr<statusbar::EditingStopwatchStatus> _editStopwatchStatus;
std::unique_ptr<statusbar::MapStatistics> _mapStatisticsStatus;
Expand Down
18 changes: 5 additions & 13 deletions radiantcore/map/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include "itextstream.h"
#include "iscenegraph.h"
#include "icameraview.h"
#include "imainframe.h"
#include "idialogmanager.h"
#include "ieventmanager.h"
#include "imodel.h"
#include "igrid.h"
#include "ifilesystem.h"
Expand Down Expand Up @@ -46,6 +43,7 @@
#include "map/algorithm/Skins.h"
#include "messages/ScopedLongRunningOperation.h"
#include "messages/FileOverwriteConfirmation.h"
#include "messages/FileSaveConfirmation.h"
#include "selection/algorithm/Primitives.h"
#include "selection/algorithm/Group.h"
#include "scene/Group.h"
Expand Down Expand Up @@ -709,20 +707,14 @@ bool Map::askForSave(const std::string& title)
}

// Ask the user
auto msgBox = GlobalDialogManager().createMessageBox(
title,
getSaveConfirmationText(),
ui::IDialog::MESSAGE_SAVECONFIRMATION
);

auto result = msgBox->run();
auto answer = radiant::FileSaveConfirmation::SendAndReceiveAnswer(getSaveConfirmationText(), title);

if (result == ui::IDialog::RESULT_CANCELLED)
if (answer == radiant::FileSaveConfirmation::Action::Cancel)
{
return false;
}

if (result == ui::IDialog::RESULT_YES)
if (answer == radiant::FileSaveConfirmation::Action::SaveChanges)
{
// The user wants to save the map
if (isUnnamed())
Expand All @@ -738,7 +730,7 @@ bool Map::askForSave(const std::string& title)
}
}

// Default behaviour: allow the save
// Default behaviour: allow the action
return true;
}

Expand Down
1 change: 1 addition & 0 deletions tools/msvc/DarkRadiant.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@
<ClInclude Include="..\..\radiant\ui\entitylist\GraphTreeNode.h" />
<ClInclude Include="..\..\radiant\ui\favourites\FavouritesBrowser.h" />
<ClInclude Include="..\..\radiant\ui\FileOverwriteConfirmationHandler.h" />
<ClInclude Include="..\..\radiant\ui\FileSaveConfirmationHandler.h" />
<ClInclude Include="..\..\radiant\ui\FileSelectionRequestHandler.h" />
<ClInclude Include="..\..\radiant\ui\filters\editor\Filter.h" />
<ClInclude Include="..\..\radiant\ui\filters\editor\FilterDialog.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/DarkRadiant.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,9 @@
<ClInclude Include="..\..\radiant\textool\tools\TextureToolCycleSelectionTool.h">
<Filter>src\textool\tools</Filter>
</ClInclude>
<ClInclude Include="..\..\radiant\ui\FileSaveConfirmationHandler.h">
<Filter>src\ui</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\..\radiant\darkradiant.rc" />
Expand Down

0 comments on commit 3d77e54

Please sign in to comment.