Skip to content

Commit

Permalink
#5527: DialogManager is a separate module now
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Feb 7, 2021
1 parent c961681 commit ad50338
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 48 deletions.
16 changes: 10 additions & 6 deletions include/idialogmanager.h
@@ -1,6 +1,6 @@
#pragma once

#include "iuimanager.h"
#include "imodule.h"
#include <memory>

class wxWindow;
Expand Down Expand Up @@ -92,7 +92,8 @@ typedef std::shared_ptr<IDirChooser> IDirChooserPtr;
class IResourceChooser; // defined in iresourcechooser.h
class IAnimationChooser; // defined in ianimationchooser.h

class IDialogManager
class IDialogManager :
public RegisterableModule
{
public:
// Virtual destructor
Expand All @@ -111,7 +112,7 @@ class IDialogManager
* GlobalMainFrame's toplevel window if left at NULL.
*/
virtual IDialogPtr createDialog(const std::string& title,
wxWindow* parent = NULL) = 0;
wxWindow* parent = nullptr) = 0;

/**
* Create a simple message box, which can either notify the user about something,
Expand All @@ -127,7 +128,7 @@ class IDialogManager
virtual IDialogPtr createMessageBox(const std::string& title,
const std::string& text,
IDialog::MessageType type,
wxWindow* parent = NULL) = 0;
wxWindow* parent = nullptr) = 0;

/**
* Acquire a new filechooser instance with the given parameters.
Expand Down Expand Up @@ -163,8 +164,11 @@ class IDialogManager

} // namespace ui

// Shortcut method

constexpr const char* const MODULE_DIALOGMANAGER = "DialogManager";

inline ui::IDialogManager& GlobalDialogManager()
{
return GlobalUIManager().getDialogManager();
static module::InstanceReference<ui::IDialogManager> _reference(MODULE_DIALOGMANAGER);
return _reference;
}
12 changes: 2 additions & 10 deletions include/iuimanager.h
Expand Up @@ -5,13 +5,6 @@
// Forward declarations
class IGroupDialog; // see igroupdialog.h for definition

namespace ui
{

class IDialogManager; // see idialogmanager.h for definition

} // namespace ui

const char* const MODULE_UIMANAGER("UIManager");

/**
Expand All @@ -22,16 +15,15 @@ class IUIManager :
{
public:
virtual IGroupDialog& getGroupDialog() = 0;
virtual ui::IDialogManager& getDialogManager() = 0;
};

// This is the accessor for the UI manager
inline IUIManager& GlobalUIManager()
{
static module::InstanceReference<IUIManager> _reference(MODULE_UIMANAGER);
return _reference;
}

inline IGroupDialog& GlobalGroupDialog() {
inline IGroupDialog& GlobalGroupDialog()
{
return GlobalUIManager().getGroupDialog();
}
6 changes: 3 additions & 3 deletions plugins/script/interfaces/DialogInterface.cpp
@@ -1,21 +1,21 @@
#include "DialogInterface.h"

#include <pybind11/pybind11.h>
#include "iuimanager.h"
#include "idialogmanager.h"

namespace script
{

ScriptDialog DialogManagerInterface::createDialog(const std::string& title)
{
return ScriptDialog(GlobalUIManager().getDialogManager().createDialog(title));
return ScriptDialog(GlobalDialogManager().createDialog(title));
}

ScriptDialog DialogManagerInterface::createMessageBox(const std::string& title,
const std::string& text,
ui::IDialog::MessageType type)
{
return ScriptDialog(GlobalUIManager().getDialogManager().createMessageBox(title, text, type));
return ScriptDialog(GlobalDialogManager().createMessageBox(title, text, type));
}

// IScriptInterface implementation
Expand Down
40 changes: 36 additions & 4 deletions radiant/uimanager/DialogManager.cpp
Expand Up @@ -8,6 +8,7 @@
#include "wxutil/DirChooser.h"
#include "SoundChooser.h"
#include "ui/animationpreview/MD5AnimationChooser.h"
#include "module/StaticModule.h"

namespace ui
{
Expand All @@ -19,16 +20,45 @@ DialogManager::~DialogManager()
rMessage() << "DialogManager: " << _dialogs.size()
<< " dialogs still in memory at shutdown." << std::endl;

_dialogs.clear();
clear();
}
}

const std::string& DialogManager::getName() const
{
static std::string _name(MODULE_DIALOGMANAGER);
return _name;
}

const StringSet& DialogManager::getDependencies() const
{
static StringSet _dependencies
{
MODULE_MAINFRAME
};

return _dependencies;
}

void DialogManager::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;

GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(this, &DialogManager::clear));
}

void DialogManager::clear()
{
_dialogs.clear();
}

IDialogPtr DialogManager::createDialog(const std::string& title, wxWindow* parent)
{
cleanupOldDialogs();

// Allocate a new dialog
wxutil::DialogPtr dialog(new wxutil::Dialog(title, parent));
auto dialog = std::make_shared<wxutil::Dialog>(title, parent);

_dialogs.push_back(dialog);

Expand All @@ -43,7 +73,7 @@ IDialogPtr DialogManager::createMessageBox(const std::string& title,
cleanupOldDialogs();

// Allocate a new dialog, use the main window if no parent specified
wxutil::MessageboxPtr box(new wxutil::Messagebox(title, text, type, parent));
auto box = std::make_shared<wxutil::Messagebox>(title, text, type, parent);

// Store it in the local map so that references are held
_dialogs.push_back(box);
Expand All @@ -66,7 +96,7 @@ IDirChooserPtr DialogManager::createDirChooser(const std::string& title)

void DialogManager::cleanupOldDialogs()
{
for (Dialogs::iterator i = _dialogs.begin(); i != _dialogs.end(); /* in-loop increment */)
for (auto i = _dialogs.begin(); i != _dialogs.end(); /* in-loop increment */)
{
if (i->use_count() <= 1)
{
Expand All @@ -89,4 +119,6 @@ IAnimationChooser* DialogManager::createAnimationChooser(wxWindow* parent)
return new MD5AnimationChooser(parent);
}

module::StaticModule<DialogManager> dialogManagerModule;

} // namespace ui
6 changes: 6 additions & 0 deletions radiant/uimanager/DialogManager.h
Expand Up @@ -35,8 +35,14 @@ class DialogManager :
IResourceChooser* createSoundShaderChooser(wxWindow* parent) override;
IAnimationChooser* createAnimationChooser(wxWindow* parent) override;

// RegisterableModule
const std::string& getName() const override;
const StringSet& getDependencies() const override;
void initialiseModule(const IApplicationContext& ctx) override;

private:
void cleanupOldDialogs();
void clear();
};
typedef std::shared_ptr<DialogManager> DialogManagerPtr;

Expand Down
16 changes: 0 additions & 16 deletions radiant/uimanager/UIManager.cpp
Expand Up @@ -8,20 +8,10 @@
namespace ui
{

IDialogManager& UIManager::getDialogManager()
{
return *_dialogManager;
}

IGroupDialog& UIManager::getGroupDialog() {
return GroupDialog::Instance();
}

void UIManager::clear()
{
_dialogManager.reset();
}

const std::string& UIManager::getName() const
{
static std::string _name(MODULE_UIMANAGER);
Expand All @@ -43,12 +33,6 @@ const StringSet& UIManager::getDependencies() const
void UIManager::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called" << std::endl;

_dialogManager = std::make_shared<DialogManager>();

GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(this, &UIManager::clear)
);
}

module::StaticModule<UIManager> uiManagerModule;
Expand Down
9 changes: 0 additions & 9 deletions radiant/uimanager/UIManager.h
Expand Up @@ -14,18 +14,9 @@ class UIManager :
public IUIManager,
public std::enable_shared_from_this<UIManager>
{
// Sub-manager classes, constructed in initialiseModule to avoid being
// called before the main window is ready.
DialogManagerPtr _dialogManager;

public:
IGroupDialog& getGroupDialog() override;

IDialogManager& getDialogManager() override;

// Called on radiant shutdown
void clear();

// RegisterableModule implementation
const std::string& getName() const override;
const StringSet& getDependencies() const override;
Expand Down

0 comments on commit ad50338

Please sign in to comment.