Skip to content

Commit

Permalink
#5537: Start refactoring EntityClassChooser to use a specialised Decl…
Browse files Browse the repository at this point in the history
…arationSelector
  • Loading branch information
codereader committed Sep 17, 2022
1 parent 37d94a9 commit 0ebad12
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 91 deletions.
143 changes: 68 additions & 75 deletions libs/wxutil/EntityClassChooser.cpp
Expand Up @@ -6,12 +6,12 @@
#include "dataview/ThreadedResourceTreePopulator.h"
#include "dataview/ResourceTreeViewToolbar.h"
#include "dataview/VFSTreePopulator.h"
#include "decl/DeclarationSelector.h"

#include "i18n.h"
#include "ifavourites.h"
#include "ideclmanager.h"
#include "ui/iuserinterface.h"
#include "ui/imainframe.h"
#include "gamelib.h"

#include <wx/button.h>
Expand All @@ -20,7 +20,6 @@
#include "wxutil/Bitmap.h"
#include "wxutil/Icon.h"

#include "string/string.h"
#include "eclass.h"

#include "debugging/ScopedDebugTimer.h"
Expand All @@ -30,18 +29,18 @@ namespace wxutil

namespace
{
const char* const TITLE_ADD_ENTITY = N_("Create Entity");
const char* const TITLE_CONVERT_TO_ENTITY = N_("Convert to Entity");
const char* const TITLE_SELECT_ENTITY = N_("Select Entity Class");
const char* const RKEY_SPLIT_POS = "user/ui/entityClassChooser/splitPos";
const char* const RKEY_WINDOW_STATE = "user/ui/entityClassChooser/window";
const char* const RKEY_LAST_SELECTED_ECLASS = "user/ui/entityClassChooser/lastSelectedEclass";
constexpr const char* const TITLE_ADD_ENTITY = N_("Create Entity");
constexpr const char* const TITLE_CONVERT_TO_ENTITY = N_("Convert to Entity");
constexpr const char* const TITLE_SELECT_ENTITY = N_("Select Entity Class");
constexpr const char* const RKEY_SPLIT_POS = "user/ui/entityClassChooser/splitPos";
constexpr const char* const RKEY_WINDOW_STATE = "user/ui/entityClassChooser/window";
constexpr const char* const RKEY_LAST_SELECTED_ECLASS = "user/ui/entityClassChooser/lastSelectedEclass";

const char* const FOLDER_ICON = "folder16.png";
const char* const ENTITY_ICON = "cmenu_add_entity.png";
constexpr const char* const FOLDER_ICON = "folder16.png";
constexpr const char* const ENTITY_ICON = "cmenu_add_entity.png";

// Registry XPath to lookup key that specifies the display folder
const char* const FOLDER_KEY_PATH = "/entityChooser/displayFolderKey";
constexpr const char* const FOLDER_KEY_PATH = "/entityChooser/displayFolderKey";

std::string getDialogTitle(EntityClassChooser::Purpose purpose)
{
Expand Down Expand Up @@ -166,10 +165,25 @@ class ThreadedEntityClassLoader final :
}
};

// Main constructor
class EntityClassSelector :
public DeclarationSelector
{
public:
EntityClassSelector(wxWindow* parent) :
DeclarationSelector(parent, decl::Type::EntityDef)
{
GetTreeView()->SetExpandTopLevelItemsAfterPopulation(true);
}

void LoadEntityClasses()
{
PopulateTreeView(std::make_shared<ThreadedEntityClassLoader>(GetColumns()));
}
};

EntityClassChooser::EntityClassChooser(Purpose purpose) :
DialogBase(getDialogTitle(purpose)),
_treeView(nullptr),
_selector(nullptr),
_selectedName("")
{
loadNamedPanel(this, "EntityClassChooserMainPanel");
Expand All @@ -180,13 +194,13 @@ EntityClassChooser::EntityClassChooser(Purpose purpose) :

switch (purpose)
{
case EntityClassChooser::Purpose::AddEntity:
case Purpose::AddEntity:
confirmButton->SetBitmap(wxArtProvider::GetBitmap(wxART_PLUS));
break;
case EntityClassChooser::Purpose::ConvertEntity:
case Purpose::ConvertEntity:
confirmButton->SetLabelText(_("Convert"));
break;
case EntityClassChooser::Purpose::SelectClassname:
case Purpose::SelectClassname:
confirmButton->SetLabelText(_("Select"));
break;
default:
Expand All @@ -197,7 +211,7 @@ EntityClassChooser::EntityClassChooser(Purpose purpose) :
wxEVT_BUTTON, &EntityClassChooser::onCancel, this);

// Add model preview to right-hand-side of main container
wxPanel* rightPanel = findNamedObject<wxPanel>(this, "EntityClassChooserRightPane");
auto rightPanel = findNamedObject<wxPanel>(this, "EntityClassChooserRightPane");

_modelPreview.reset(new ModelPreview(rightPanel));

Expand Down Expand Up @@ -272,12 +286,12 @@ std::string EntityClassChooser::ChooseEntityClass(Purpose purpose, const std::st

void EntityClassChooser::loadEntityClasses()
{
_treeView->Populate(std::make_shared<ThreadedEntityClassLoader>(_columns));
_selector->LoadEntityClasses();
}

void EntityClassChooser::setSelectedEntityClass(const std::string& eclass)
{
_treeView->SetSelectedFullname(eclass);
_selector->SetSelectedDeclName(eclass);
}

const std::string& EntityClassChooser::getSelectedEntityClass() const
Expand All @@ -299,13 +313,10 @@ int EntityClassChooser::ShowModal()
{
_windowPosition.applyPosition();

_treeViewToolbar->ClearFilter();

// Update the member variables
updateSelection();

// Focus on the treeview
_treeView->SetFocus();
_selector->FocusTreeView();

int returnCode = DialogBase::ShowModal();

Expand All @@ -317,35 +328,23 @@ int EntityClassChooser::ShowModal()

void EntityClassChooser::setupTreeView()
{
wxPanel* parent = findNamedObject<wxPanel>(this, "EntityClassChooserLeftPane");

_treeView = new DeclarationTreeView(parent, decl::Type::EntityDef, _columns, wxDV_NO_HEADER);
_treeView->AddSearchColumn(_columns.iconAndName);
_treeView->SetExpandTopLevelItemsAfterPopulation(true);

_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &EntityClassChooser::onSelectionChanged, this);

// Single column with icon and name
_treeView->AppendIconTextColumn(_("Classname"), _columns.iconAndName.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
auto parent = findNamedObject<wxPanel>(this, "EntityClassChooserLeftPane");

Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &EntityClassChooser::_onItemActivated, this );
_selector = new EntityClassSelector(parent);

_treeViewToolbar = new ResourceTreeViewToolbar(parent, _treeView);
_selector->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &EntityClassChooser::onSelectionChanged, this);
_selector->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &EntityClassChooser::_onItemActivated, this );

parent->GetSizer()->Prepend(_treeView, 1, wxEXPAND | wxBOTTOM | wxRIGHT, 6);
parent->GetSizer()->Prepend(_treeViewToolbar, 0, wxEXPAND | wxALIGN_LEFT | wxBOTTOM | wxLEFT | wxRIGHT, 6);
parent->GetSizer()->Prepend(_selector, 1, wxEXPAND | wxBOTTOM | wxRIGHT, 6);
}

// tree double click or enter key
void EntityClassChooser::_onItemActivated( wxDataViewEvent& ev ) {
wxDataViewItem item = _treeView->GetSelection();
if ( item.IsOk() ) {
TreeModel::Row row( item, *_treeView->GetModel() );
void EntityClassChooser::_onItemActivated( wxDataViewEvent& ev )
{
auto selectedEclass = _selector->GetSelectedDeclName();

if ( !row[_columns.isFolder].getBool() ) {
onOK( ev );
}
if (!selectedEclass.empty())
{
onOK(ev);
}
}

Expand All @@ -362,45 +361,39 @@ void EntityClassChooser::updateUsageInfo(const std::string& eclass)

void EntityClassChooser::updateSelection()
{
wxDataViewItem item = _treeView->GetSelection();
auto selectedEclass = _selector->GetSelectedDeclName();

auto* defFileName = findNamedObject<wxStaticText>(this, "EntityClassChooserDefFileName");

if (item.IsOk())
if (selectedEclass.empty())
{
TreeModel::Row row(item, *_treeView->GetModel());
// Nothing selected
_modelPreview->setModel("");
_modelPreview->setSkin("");
defFileName->SetLabel("-");

if (!row[_columns.isFolder].getBool())
{
// Make the OK button active
findNamedObject<wxButton>(this, "EntityClassChooserAddButton")->Enable(true);
findNamedObject<wxButton>(this, "EntityClassChooserAddButton")->Enable(false);
return;
}

// Set the panel text with the usage information
std::string selName = row[_columns.leafName];
// Make the OK button active
findNamedObject<wxButton>(this, "EntityClassChooserAddButton")->Enable(true);

updateUsageInfo(selName);
// Set the panel text with the usage information
updateUsageInfo(selectedEclass);

// Update the _selectionName field
_selectedName = selName;
// Update the _selectionName field
_selectedName = selectedEclass;

// Lookup the IEntityClass instance
auto eclass = GlobalEntityClassManager().findClass(selName);
// Lookup the IEntityClass instance
auto eclass = GlobalEntityClassManager().findClass(selectedEclass);

if (eclass)
{
_modelPreview->setModel(eclass->getAttributeValue("model"));
_modelPreview->setSkin(eclass->getAttributeValue("skin"));
defFileName->SetLabel(eclass->getDeclFilePath());
return; // success
}
}
if (eclass)
{
_modelPreview->setModel(eclass->getAttributeValue("model"));
_modelPreview->setSkin(eclass->getAttributeValue("skin"));
defFileName->SetLabel(eclass->getDeclFilePath());
}

// Nothing selected
_modelPreview->setModel("");
_modelPreview->setSkin("");
defFileName->SetLabel("-");

findNamedObject<wxButton>(this, "EntityClassChooserAddButton")->Enable(false);
}

void EntityClassChooser::onCancel(wxCommandEvent& ev)
Expand Down
10 changes: 3 additions & 7 deletions libs/wxutil/EntityClassChooser.h
Expand Up @@ -5,20 +5,18 @@

#include "preview/ModelPreview.h"
#include "dialog/DialogBase.h"
#include "dataview/DeclarationTreeView.h"
#include "dataview/ResourceTreeViewToolbar.h"
#include "XmlResourceBasedWidget.h"
#include "PanedPosition.h"
#include "WindowPosition.h"

#include <memory>
#include <sigc++/connection.h>
#include <wx/dataview.h>

namespace wxutil
{

class EntityClassChooser;
typedef std::shared_ptr<EntityClassChooser> EntityClassChooserPtr;
class EntityClassSelector;

/**
* Dialog window displaying a tree of Entity Classes, allowing the selection
Expand All @@ -39,9 +37,7 @@ class EntityClassChooser final :
};

private:
DeclarationTreeView::Columns _columns;
DeclarationTreeView* _treeView;
ResourceTreeViewToolbar* _treeViewToolbar;
EntityClassSelector* _selector;

// Last selected classname
std::string _selectedName;
Expand Down
23 changes: 14 additions & 9 deletions libs/wxutil/decl/DeclarationSelector.cpp
Expand Up @@ -12,7 +12,7 @@ DeclarationSelector::DeclarationSelector(wxWindow* parent, decl::Type declType)
{}

DeclarationSelector::DeclarationSelector(wxWindow* parent, decl::Type declType,
const wxutil::DeclarationTreeView::Columns& columns) :
const DeclarationTreeView::Columns& columns) :
wxPanel(parent),
_declType(declType),
_columns(columns),
Expand All @@ -23,8 +23,8 @@ DeclarationSelector::DeclarationSelector(wxWindow* parent, decl::Type declType,
SetSizer(new wxBoxSizer(wxVERTICAL));
createTreeView();

auto* toolbar = new wxutil::ResourceTreeViewToolbar(this, _treeView);
_declFileInfo = new wxutil::DeclFileInfo(this, _declType);
auto* toolbar = new ResourceTreeViewToolbar(this, _treeView);
_declFileInfo = new DeclFileInfo(this, _declType);

auto treeVbox = new wxBoxSizer(wxVERTICAL);
treeVbox->Add(toolbar, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 6);
Expand Down Expand Up @@ -68,7 +68,7 @@ void DeclarationSelector::AddPreviewToBottom(ui::IDeclarationPreview* preview, i

void DeclarationSelector::createTreeView()
{
_treeView = new wxutil::DeclarationTreeView(this, _declType, _columns, wxDV_NO_HEADER | wxDV_SINGLE);
_treeView = new DeclarationTreeView(this, _declType, _columns, wxDV_NO_HEADER | wxDV_SINGLE);

// Single visible column, containing the directory/decl name and the icon
_treeView->AppendIconTextColumn(decl::getTypeName(_declType), _columns.iconAndName.getColumnIndex(),
Expand All @@ -82,12 +82,17 @@ void DeclarationSelector::createTreeView()
_treeView->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &DeclarationSelector::onTreeViewItemActivated, this);
}

wxutil::DeclarationTreeView* DeclarationSelector::GetTreeView() const
void DeclarationSelector::FocusTreeView()
{
_treeView->SetFocus();
}

DeclarationTreeView* DeclarationSelector::GetTreeView() const
{
return _treeView;
}

const wxutil::DeclarationTreeView::Columns& DeclarationSelector::GetColumns() const
const DeclarationTreeView::Columns& DeclarationSelector::GetColumns() const
{
return _columns;
}
Expand All @@ -102,14 +107,14 @@ void DeclarationSelector::SetSelectedDeclName(const std::string& declName)
_treeView->SetSelectedDeclName(declName);
}

void DeclarationSelector::PopulateTreeView(const wxutil::IResourceTreePopulator::Ptr& populator)
void DeclarationSelector::PopulateTreeView(const IResourceTreePopulator::Ptr& populator)
{
_treeView->Populate(populator);
}

const wxutil::DeclarationTreeView::Columns& DeclarationSelector::CreateDefaultColumns()
const DeclarationTreeView::Columns& DeclarationSelector::CreateDefaultColumns()
{
static wxutil::DeclarationTreeView::Columns _treeViewColumns;
static DeclarationTreeView::Columns _treeViewColumns;
return _treeViewColumns;
}

Expand Down
3 changes: 3 additions & 0 deletions libs/wxutil/decl/DeclarationSelector.h
Expand Up @@ -58,6 +58,9 @@ class DeclarationSelector :
*/
virtual void SetSelectedDeclName(const std::string& declName);

// Set the focus on the treeview widget
void FocusTreeView();

protected:
wxutil::DeclarationTreeView* GetTreeView() const;

Expand Down

0 comments on commit 0ebad12

Please sign in to comment.