Skip to content

Commit

Permalink
#5127: Move common tree view item formatting code to wxutil.
Browse files Browse the repository at this point in the history
Remove wxutil namespace specifies from EntityClassChooser, it's part of wxutil
  • Loading branch information
codereader committed Dec 27, 2020
1 parent bd2a1b4 commit 2864d59
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 69 deletions.
56 changes: 20 additions & 36 deletions libs/wxutil/EntityClassChooser.cpp
Expand Up @@ -9,11 +9,12 @@
#include "gamelib.h"

#include <wx/thread.h>

#include <wx/button.h>
#include <wx/panel.h>
#include <wx/splitter.h>

#include "TreeViewItemStyle.h"

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

Expand All @@ -32,38 +33,21 @@ namespace

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

// Get the item format for favourites / non-favourites
inline wxDataViewItemAttr getItemFormat(bool isFavourite)
{
if (isFavourite)
{
wxDataViewItemAttr blueBold;
blueBold.SetColour(wxColor(0, 0, 255));
blueBold.SetBold(true);

return blueBold;
}
else
{
return wxDataViewItemAttr();
}
}
}

/*
* EntityClassVisitor which populates a treeStore with entity classnames
* taking account of display folders and mod names.
*/
class EntityClassTreePopulator:
public wxutil::VFSTreePopulator,
public VFSTreePopulator,
public EntityClassVisitor
{
// TreeStore to populate
wxutil::TreeModel::Ptr _store;
TreeModel::Ptr _store;

// Column definition
const wxutil::EntityClassChooser::TreeColumns& _columns;
const EntityClassChooser::TreeColumns& _columns;

// Key that specifies the display folder
std::string _folderKey;
Expand All @@ -76,9 +60,9 @@ class EntityClassTreePopulator:
public:

// Constructor
EntityClassTreePopulator(wxutil::TreeModel::Ptr store,
EntityClassTreePopulator(TreeModel::Ptr store,
const EntityClassChooser::TreeColumns& columns)
: wxutil::VFSTreePopulator(store),
: VFSTreePopulator(store),
_store(store),
_columns(columns),
_folderKey(game::current::getValue<std::string>(FOLDER_KEY_PATH))
Expand All @@ -104,7 +88,7 @@ class EntityClassTreePopulator:
// of the DISPLAY_FOLDER_KEY.
addPath(
eclass->getModName() + folderPath + "/" + eclass->getName(),
[&](wxutil::TreeModel::Row& row, const std::string& path,
[&](TreeModel::Row& row, const std::string& path,
const std::string& leafName, bool isFolder)
{
bool isFavourite = !isFolder && _favourites.count(leafName) > 0;
Expand All @@ -116,7 +100,7 @@ class EntityClassTreePopulator:
);
row[_columns.isFolder] = isFolder;
row[_columns.isFavourite] = isFavourite;
row[_columns.name] = getItemFormat(isFavourite); // assign attributes
row[_columns.name] = TreeViewItemStyle::Declaration(isFavourite); // assign attributes
row.SendItemAdded();
}
);
Expand All @@ -133,7 +117,7 @@ class EntityClassChooser::ThreadedEntityClassLoader :
// The tree store to populate. We must operate on our own tree store, since
// updating the EntityClassChooser's tree store from a different thread
// wouldn't be safe
wxutil::TreeModel::Ptr _treeStore;
TreeModel::Ptr _treeStore;

// The class to be notified on finish
wxEvtHandler* _finishedHandler;
Expand Down Expand Up @@ -162,7 +146,7 @@ class EntityClassChooser::ThreadedEntityClassLoader :
ScopedDebugTimer timer("ThreadedEntityClassLoader::run()");

// Create new treestoree
_treeStore = new wxutil::TreeModel(_columns);
_treeStore = new TreeModel(_columns);

// Populate it with the list of entity classes by using a visitor class.
EntityClassTreePopulator visitor(_treeStore, _columns);
Expand All @@ -175,7 +159,7 @@ class EntityClassChooser::ThreadedEntityClassLoader :

if (!TestDestroy())
{
wxQueueEvent(_finishedHandler, new wxutil::TreeModel::PopulationFinishedEvent(_treeStore));
wxQueueEvent(_finishedHandler, new TreeModel::PopulationFinishedEvent(_treeStore));
}

return static_cast<ExitCode>(0);
Expand All @@ -184,13 +168,13 @@ class EntityClassChooser::ThreadedEntityClassLoader :

// Main constructor
EntityClassChooser::EntityClassChooser()
: wxutil::DialogBase(_(ECLASS_CHOOSER_TITLE)),
: DialogBase(_(ECLASS_CHOOSER_TITLE)),
_treeStore(nullptr),
_treeView(nullptr),
_selectedName("")
{
// Connect the finish callback to load the treestore
Bind(wxutil::EV_TREEMODEL_POPULATION_FINISHED, &EntityClassChooser::onTreeStorePopulationFinished, this);
Bind(EV_TREEMODEL_POPULATION_FINISHED, &EntityClassChooser::onTreeStorePopulationFinished, this);

loadNamedPanel(this, "EntityClassChooserMainPanel");

Expand All @@ -206,7 +190,7 @@ EntityClassChooser::EntityClassChooser()
// Add model preview to right-hand-side of main container
wxPanel* rightPanel = findNamedObject<wxPanel>(this, "EntityClassChooserRightPane");

_modelPreview.reset(new wxutil::ModelPreview(rightPanel));
_modelPreview.reset(new ModelPreview(rightPanel));

rightPanel->GetSizer()->Add(_modelPreview->getWidget(), 1, wxEXPAND);

Expand Down Expand Up @@ -371,14 +355,14 @@ void EntityClassChooser::setTreeViewModel()
void EntityClassChooser::setupTreeView()
{
// Use the TreeModel's full string search function
_treeStore = new wxutil::TreeModel(_columns);
wxutil::TreeModel::Row row = _treeStore->AddItem();
_treeStore = new TreeModel(_columns);
TreeModel::Row row = _treeStore->AddItem();

row[_columns.name] = wxVariant(wxDataViewIconText(_("Loading...")));

wxPanel* parent = findNamedObject<wxPanel>(this, "EntityClassChooserLeftPane");

_treeView = wxutil::TreeView::CreateWithModel(parent, _treeStore.get());
_treeView = TreeView::CreateWithModel(parent, _treeStore.get());
_treeView->AddSearchColumn(_columns.name);

_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &EntityClassChooser::onSelectionChanged, this);
Expand Down Expand Up @@ -408,7 +392,7 @@ void EntityClassChooser::updateSelection()

if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *_treeStore);
TreeModel::Row row(item, *_treeStore);

if (!row[_columns.isFolder].getBool())
{
Expand Down Expand Up @@ -464,7 +448,7 @@ void EntityClassChooser::onSelectionChanged(wxDataViewEvent& ev)
updateSelection();
}

void EntityClassChooser::onTreeStorePopulationFinished(wxutil::TreeModel::PopulationFinishedEvent& ev)
void EntityClassChooser::onTreeStorePopulationFinished(TreeModel::PopulationFinishedEvent& ev)
{
_treeView->UnselectAll();

Expand Down
28 changes: 14 additions & 14 deletions libs/wxutil/EntityClassChooser.h
Expand Up @@ -24,31 +24,31 @@ typedef std::shared_ptr<EntityClassChooser> EntityClassChooserPtr;
* of a class to create at the current location.
*/
class EntityClassChooser :
public wxutil::DialogBase,
private wxutil::XmlResourceBasedWidget
public DialogBase,
private XmlResourceBasedWidget
{
public:
// Treemodel definition
struct TreeColumns :
public wxutil::TreeModel::ColumnRecord
public TreeModel::ColumnRecord
{
TreeColumns() :
name(add(wxutil::TreeModel::Column::IconText)),
isFolder(add(wxutil::TreeModel::Column::Boolean)),
isFavourite(add(wxutil::TreeModel::Column::Boolean))
name(add(TreeModel::Column::IconText)),
isFolder(add(TreeModel::Column::Boolean)),
isFavourite(add(TreeModel::Column::Boolean))
{}

wxutil::TreeModel::Column name;
wxutil::TreeModel::Column isFolder;
wxutil::TreeModel::Column isFavourite;
TreeModel::Column name;
TreeModel::Column isFolder;
TreeModel::Column isFavourite;
};

private:
TreeColumns _columns;

// Tree model holding the classnames
wxutil::TreeModel::Ptr _treeStore;
wxutil::TreeView* _treeView;
TreeModel::Ptr _treeStore;
TreeView* _treeView;

// Delegated object for loading entity classes in a separate thread
class ThreadedEntityClassLoader;
Expand All @@ -61,9 +61,9 @@ class EntityClassChooser :
std::string _classToHighlight;

// Model preview widget
wxutil::ModelPreviewPtr _modelPreview;
ModelPreviewPtr _modelPreview;

wxutil::PanedPosition _panedPosition;
PanedPosition _panedPosition;

sigc::connection _defsReloaded;

Expand All @@ -88,7 +88,7 @@ class EntityClassChooser :
void onOK(wxCommandEvent& ev);
void onSelectionChanged(wxDataViewEvent& ev);
void onDeleteEvent(wxCloseEvent& ev);
void onTreeStorePopulationFinished(wxutil::TreeModel::PopulationFinishedEvent& ev);
void onTreeStorePopulationFinished(TreeModel::PopulationFinishedEvent& ev);

void onMainFrameShuttingDown();

Expand Down
27 changes: 27 additions & 0 deletions libs/wxutil/TreeViewItemStyle.h
@@ -0,0 +1,27 @@
#pragma once

#include <wx/dataview.h>

namespace wxutil
{

class TreeViewItemStyle
{
public:
// Get the item format for favourites / non-favourites item in a declaration tree
static wxDataViewItemAttr Declaration(bool isFavourite)
{
if (isFavourite)
{
wxDataViewItemAttr blueBold;
blueBold.SetColour(wxColor(0, 0, 255));
blueBold.SetBold(true);

return blueBold;
}

return wxDataViewItemAttr();
}
};

}
22 changes: 3 additions & 19 deletions radiant/ui/mediabrowser/MediaBrowser.cpp
Expand Up @@ -12,6 +12,7 @@
#include "ifavourites.h"

#include "wxutil/MultiMonitor.h"
#include "wxutil/TreeViewItemStyle.h"

#include <wx/thread.h>

Expand Down Expand Up @@ -105,23 +106,6 @@ class MediaBrowser::Favourites :
namespace
{

// Get the item format for favourites / non-favourites
inline wxDataViewItemAttr getItemFormat(bool isFavourite)
{
if (isFavourite)
{
wxDataViewItemAttr blueBold;
blueBold.SetColour(wxColor(0, 0, 255));
blueBold.SetBold(true);

return blueBold;
}
else
{
return wxDataViewItemAttr();
}
}

/* Callback functor for processing shader names */
struct ShaderNameCompareFunctor
{
Expand Down Expand Up @@ -238,7 +222,7 @@ struct ShaderNameFunctor
row[_columns.isFavourite] = isFavourite;

// Formatting
row[_columns.iconAndName] = getItemFormat(isFavourite);
row[_columns.iconAndName] = wxutil::TreeViewItemStyle::Declaration(isFavourite);
}
};

Expand Down Expand Up @@ -887,7 +871,7 @@ void MediaBrowser::setFavouriteRecursively(wxutil::TreeModel::Row& row, bool isF

// Not a folder, set the desired status on this item
row[_columns.isFavourite] = isFavourite;
row[_columns.iconAndName] = getItemFormat(isFavourite);
row[_columns.iconAndName] = wxutil::TreeViewItemStyle::Declaration(isFavourite);

// Keep track of this choice
if (isFavourite)
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/wxutillib.vcxproj
Expand Up @@ -184,6 +184,7 @@
<ClInclude Include="..\..\libs\wxutil\TreeModel.h" />
<ClInclude Include="..\..\libs\wxutil\TreeModelFilter.h" />
<ClInclude Include="..\..\libs\wxutil\TreeView.h" />
<ClInclude Include="..\..\libs\wxutil\TreeViewItemStyle.h" />
<ClInclude Include="..\..\libs\wxutil\VFSTreePopulator.h" />
<ClInclude Include="..\..\libs\wxutil\WindowPosition.h" />
<ClInclude Include="..\..\libs\wxutil\window\TransientWindow.h" />
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/wxutillib.vcxproj.filters
Expand Up @@ -114,6 +114,7 @@
<ClInclude Include="..\..\libs\wxutil\fsview\Populator.h">
<Filter>fsview</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\wxutil\TreeViewItemStyle.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\libs\wxutil\dialog\MessageBox.cpp">
Expand Down

0 comments on commit 2864d59

Please sign in to comment.