Skip to content

Commit

Permalink
#5537: Introduce DeclarationSelectorDialog as new base type for decla…
Browse files Browse the repository at this point in the history
…ration choosers.

Start refactoring EntityClassChooser, moving common code to the base.
  • Loading branch information
codereader committed Sep 18, 2022
1 parent db00b16 commit 7001d58
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 39 deletions.
1 change: 1 addition & 0 deletions libs/wxutil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_library(wxutil
dataview/TreeModelFilter.cpp
dataview/TreeView.cpp
dataview/VFSTreePopulator.cpp
decl/DeclarationSelectorDialog.cpp
decl/DeclarationSelector.cpp
dialog/DialogBase.cpp
dialog/Dialog.cpp
Expand Down
43 changes: 15 additions & 28 deletions libs/wxutil/EntityClassChooser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,29 +233,22 @@ class EntityClassSelector :
};

EntityClassChooser::EntityClassChooser(Purpose purpose) :
DialogBase(getDialogTitle(purpose), "EntityClassChooser"),
DeclarationSelectorDialog(decl::Type::EntityDef, getDialogTitle(purpose), "EntityClassChooser"),
_selector(nullptr),
_restoreSelectionFromRegistry(true)
{
SetSizer(new wxBoxSizer(wxVERTICAL));

auto mainSizer = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(mainSizer, 1, wxEXPAND | wxALL, 12);

auto buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);

_affirmativeButton = buttonSizer->GetAffirmativeButton();
auto affirmativeButton = GetAffirmativeButton();

switch (purpose)
{
case Purpose::AddEntity:
_affirmativeButton->SetLabelText(_("Create"));
affirmativeButton->SetLabelText(_("Create"));
break;
case Purpose::ConvertEntity:
_affirmativeButton->SetLabelText(_("Convert"));
affirmativeButton->SetLabelText(_("Convert"));
break;
case Purpose::SelectClassname:
_affirmativeButton->SetLabelText(_("Select"));
affirmativeButton->SetLabelText(_("Select"));
break;
default:
throw std::logic_error("Unknown entity class chooser purpose");
Expand All @@ -269,14 +262,12 @@ EntityClassChooser::EntityClassChooser(Purpose purpose) :

Bind(wxEVT_CLOSE_WINDOW, &EntityClassChooser::onDeleteEvent, this);

mainSizer->Add(setupSelector(this), 1, wxEXPAND | wxBOTTOM, 12);
mainSizer->Add(buttonSizer, 0, wxALIGN_RIGHT, 12);
SetSelector(setupSelector(this));

loadEntityClasses();

// Save the state of the selector widget on dialog close
RegisterPersistableObject(this);
RegisterPersistableObject(_selector);
}

EntityClassChooser::~EntityClassChooser()
Expand All @@ -291,12 +282,12 @@ std::string EntityClassChooser::ChooseEntityClass(Purpose purpose, const std::st
// We'll fall back to the value saved in the registry if eclassToSelect is empty
if (!eclassToSelect.empty())
{
instance.setSelectedEntityClass(eclassToSelect);
instance.SetSelectedDeclName(eclassToSelect);
}

if (instance.ShowModal() == wxID_OK)
{
return instance.getSelectedEntityClass();
return instance.GetSelectedDeclName();
}

return ""; // Empty selection on cancel
Expand All @@ -307,17 +298,13 @@ void EntityClassChooser::loadEntityClasses()
_selector->LoadEntityClasses();
}

void EntityClassChooser::setSelectedEntityClass(const std::string& eclass)
void EntityClassChooser::SetSelectedDeclName(const std::string& declName)
{
_selector->SetSelectedDeclName(eclass);
DeclarationSelectorDialog::SetSelectedDeclName(declName);

_restoreSelectionFromRegistry = false; // prevent this selection from being overwritten
}

std::string EntityClassChooser::getSelectedEntityClass() const
{
return _selector->GetSelectedDeclName();
}

void EntityClassChooser::onDeleteEvent(wxCloseEvent& ev)
{
EndModal(wxID_CANCEL); // break main loop
Expand All @@ -343,16 +330,16 @@ void EntityClassChooser::loadFromPath(const std::string& registryKey)

if (!lastSelectedDeclName.empty())
{
setSelectedEntityClass(lastSelectedDeclName);
SetSelectedDeclName(lastSelectedDeclName);
}
}

void EntityClassChooser::saveToPath(const std::string& registryKey)
{
GlobalRegistry().setAttribute(registryKey, "lastSelectedDeclName", getSelectedEntityClass());
GlobalRegistry().setAttribute(registryKey, "lastSelectedDeclName", GetSelectedDeclName());
}

wxWindow* EntityClassChooser::setupSelector(wxWindow* parent)
EntityClassSelector* EntityClassChooser::setupSelector(wxWindow* parent)
{
_selector = new EntityClassSelector(parent);

Expand All @@ -372,7 +359,7 @@ void EntityClassChooser::_onItemActivated( wxDataViewEvent& ev )

void EntityClassChooser::updateSelection()
{
_affirmativeButton->Enable(!_selector->GetSelectedDeclName().empty());
GetAffirmativeButton()->Enable(!_selector->GetSelectedDeclName().empty());
}

void EntityClassChooser::onSelectionChanged(wxDataViewEvent& ev)
Expand Down
16 changes: 5 additions & 11 deletions libs/wxutil/EntityClassChooser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "dialog/DialogBase.h"
#include "decl/DeclarationSelectorDialog.h"
#include "ui/iwindowstate.h"

#include <sigc++/connection.h>
Expand All @@ -16,7 +16,7 @@ class EntityClassSelector;
* of a class to create at the current location.
*/
class EntityClassChooser final :
public DialogBase,
public DeclarationSelectorDialog,
public ui::IPersistableObject
{
public:
Expand All @@ -34,8 +34,6 @@ class EntityClassChooser final :

sigc::connection _defsReloaded;

wxButton* _affirmativeButton;

bool _restoreSelectionFromRegistry;

private:
Expand All @@ -44,7 +42,7 @@ class EntityClassChooser final :

void loadEntityClasses();

wxWindow* setupSelector(wxWindow * parent);
EntityClassSelector* setupSelector(wxWindow * parent);

// Updates the member variables based on the current tree selection
void updateSelection();
Expand All @@ -53,12 +51,6 @@ class EntityClassChooser final :
void onSelectionChanged(wxDataViewEvent& ev);
void onDeleteEvent(wxCloseEvent& ev);

// Sets the tree selection to the given entity class
void setSelectedEntityClass(const std::string& eclass);

// Sets the tree selection to the given entity class
std::string getSelectedEntityClass() const;

void _onItemActivated( wxDataViewEvent& ev );

// Overridden from wxDialog
Expand All @@ -68,6 +60,8 @@ class EntityClassChooser final :
void loadFromPath(const std::string& registryKey) override;
void saveToPath(const std::string& registryKey) override;

void SetSelectedDeclName(const std::string& declName) override;

/**
* \brief Construct and show the dialog to choose an entity class,
* returning the result.
Expand Down
51 changes: 51 additions & 0 deletions libs/wxutil/decl/DeclarationSelectorDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "DeclarationSelectorDialog.h"

#include "DeclarationSelector.h"
#include <wx/sizer.h>

namespace wxutil
{

DeclarationSelectorDialog::DeclarationSelectorDialog(decl::Type declType,
const std::string& title, const std::string& windowName, wxWindow* parent) :
DialogBase(title, parent, windowName),
_declType(declType)
{
SetSizer(new wxBoxSizer(wxVERTICAL));

// Inner sizer with 12-pixel padding
_mainSizer = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(_mainSizer, 1, wxEXPAND | wxALL, 12);

// Button row
_buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
_mainSizer->Add(_buttonSizer, 0, wxALIGN_RIGHT, 12);
}

void DeclarationSelectorDialog::SetSelector(DeclarationSelector* selector)
{
_selector = selector;
_selector->Reparent(this);

_mainSizer->Prepend(_selector, 1, wxEXPAND | wxBOTTOM, 12);

// The selector state should be persisted on dialog close
RegisterPersistableObject(_selector);
}

std::string DeclarationSelectorDialog::GetSelectedDeclName()
{
return _selector->GetSelectedDeclName();
}

void DeclarationSelectorDialog::SetSelectedDeclName(const std::string& declName)
{
_selector->SetSelectedDeclName(declName);
}

wxButton* DeclarationSelectorDialog::GetAffirmativeButton()
{
return _buttonSizer->GetAffirmativeButton();
}

}
46 changes: 46 additions & 0 deletions libs/wxutil/decl/DeclarationSelectorDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "idecltypes.h"
#include "../dialog/DialogBase.h"

class wxSizer;
class wxStdDialogButtonSizer;

namespace wxutil
{

class DeclarationSelector;

/**
* Base implementation of a Declaration chooser dialog.
*
* Provides a tree view of available declaration items plus optional previews
* of the active selection.
*/
class DeclarationSelectorDialog :
public DialogBase
{
private:
decl::Type _declType;

DeclarationSelector* _selector;
wxSizer* _mainSizer;
wxStdDialogButtonSizer* _buttonSizer;

public:
DeclarationSelectorDialog(decl::Type declType, const std::string& title,
const std::string& windowName, wxWindow* parent = nullptr);

// Get the currently selected declaration name
virtual std::string GetSelectedDeclName();

// Set the declaration selection in the selector
virtual void SetSelectedDeclName(const std::string& declName);

protected:
virtual void SetSelector(DeclarationSelector* selector);

wxButton* GetAffirmativeButton();
};

}
2 changes: 2 additions & 0 deletions tools/msvc/wxutillib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<ClInclude Include="..\..\libs\wxutil\dataview\TreeView.h" />
<ClInclude Include="..\..\libs\wxutil\dataview\TreeViewItemStyle.h" />
<ClInclude Include="..\..\libs\wxutil\dataview\VFSTreePopulator.h" />
<ClInclude Include="..\..\libs\wxutil\decl\DeclarationSelectorDialog.h" />
<ClInclude Include="..\..\libs\wxutil\decl\DeclarationSelector.h" />
<ClInclude Include="..\..\libs\wxutil\decl\DeclFileInfo.h" />
<ClInclude Include="..\..\libs\wxutil\DeferredMotionDelta.h" />
Expand Down Expand Up @@ -217,6 +218,7 @@
<ClCompile Include="..\..\libs\wxutil\dataview\TreeView.cpp" />
<ClCompile Include="..\..\libs\wxutil\dataview\VFSTreePopulator.cpp" />
<ClCompile Include="..\..\libs\wxutil\decl\DeclarationSelector.cpp" />
<ClCompile Include="..\..\libs\wxutil\decl\DeclarationSelectorDialog.cpp" />
<ClCompile Include="..\..\libs\wxutil\dialog\Dialog.cpp" />
<ClCompile Include="..\..\libs\wxutil\dialog\DialogBase.cpp" />
<ClCompile Include="..\..\libs\wxutil\dialog\MessageBox.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions tools/msvc/wxutillib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@
<ClInclude Include="..\..\libs\wxutil\preview\SkinPreview.h">
<Filter>preview</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\wxutil\decl\DeclarationSelectorDialog.h">
<Filter>decl</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\libs\wxutil\dialog\MessageBox.cpp">
Expand Down Expand Up @@ -279,5 +282,8 @@
<Filter>decl</Filter>
</ClCompile>
<ClCompile Include="..\..\libs\wxutil\WindowState.cpp" />
<ClCompile Include="..\..\libs\wxutil\decl\DeclarationSelectorDialog.cpp">
<Filter>decl</Filter>
</ClCompile>
</ItemGroup>
</Project>

0 comments on commit 7001d58

Please sign in to comment.