Skip to content

Commit

Permalink
#5127: Migrate ThreadedEntityClassLoader to derive from ThreadedResou…
Browse files Browse the repository at this point in the history
…rceTreePopulator, to remove all the code dealing with wxThread.
  • Loading branch information
codereader committed Jan 2, 2021
1 parent a8498d4 commit 44d2501
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 52 deletions.
61 changes: 14 additions & 47 deletions libs/wxutil/EntityClassChooser.cpp
@@ -1,6 +1,7 @@
#include "EntityClassChooser.h"
#include "dataview/TreeModel.h"
#include "dataview/TreeViewItemStyle.h"
#include "dataview/ThreadedResourceTreePopulator.h"
#include "dataview/VFSTreePopulator.h"
#include "menu/IconTextMenuItem.h"

Expand All @@ -10,7 +11,6 @@
#include "iuimanager.h"
#include "gamelib.h"

#include <wx/thread.h>
#include <wx/button.h>
#include <wx/panel.h>
#include <wx/splitter.h>
Expand Down Expand Up @@ -60,7 +60,7 @@ class EntityClassTreePopulator:
public:

// Constructor
EntityClassTreePopulator(TreeModel::Ptr store,
EntityClassTreePopulator(const TreeModel::Ptr& store,
const ResourceTreeView::Columns& columns)
: VFSTreePopulator(store),
_store(store),
Expand Down Expand Up @@ -111,61 +111,28 @@ class EntityClassTreePopulator:
};

// Local class for loading entity class definitions in a separate thread
class EntityClassChooser::ThreadedEntityClassLoader :
public wxThread
class ThreadedEntityClassLoader :
public wxutil::ThreadedResourceTreePopulator
{
// Column specification struct
const ResourceTreeView::Columns& _columns;

// 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
TreeModel::Ptr _treeStore;

// The class to be notified on finish
wxEvtHandler* _finishedHandler;

public:

// Construct and initialise variables
ThreadedEntityClassLoader(const ResourceTreeView::Columns& cols,
wxEvtHandler* finishedHandler) :
wxThread(wxTHREAD_JOINABLE),
_columns(cols),
_finishedHandler(finishedHandler)
ThreadedEntityClassLoader(const ResourceTreeView::Columns& cols, wxEvtHandler* finishedHandler) :
ThreadedResourceTreePopulator(cols, finishedHandler),
_columns(cols)
{}

~ThreadedEntityClassLoader()
void PopulateModel(const wxutil::TreeModel::Ptr& model) override
{
if (IsRunning())
{
Delete();
}
}

// The worker function that will execute in the thread
ExitCode Entry()
{
ScopedDebugTimer timer("ThreadedEntityClassLoader::run()");

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

// Populate it with the list of entity classes by using a visitor class.
EntityClassTreePopulator visitor(_treeStore, _columns);
EntityClassTreePopulator visitor(model, _columns);
GlobalEntityClassManager().forEachEntityClass(visitor);
}

if (TestDestroy()) return static_cast<ExitCode>(0);

// Ensure model is sorted before giving it to the tree view
_treeStore->SortModelFoldersFirst(_columns.leafName, _columns.isFolder);

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

return static_cast<ExitCode>(0);
void SortModel(const wxutil::TreeModel::Ptr& model) override
{
model->SortModelFoldersFirst(_columns.leafName, _columns.isFolder);
}
};

Expand Down Expand Up @@ -287,7 +254,7 @@ void EntityClassChooser::onMainFrameShuttingDown()
void EntityClassChooser::loadEntityClasses()
{
_eclassLoader.reset(new ThreadedEntityClassLoader(_columns, this));
_eclassLoader->Run();
_eclassLoader->Populate();
}

void EntityClassChooser::setSelectedEntityClass(const std::string& eclass)
Expand Down
4 changes: 2 additions & 2 deletions libs/wxutil/EntityClassChooser.h
Expand Up @@ -8,6 +8,7 @@
#include "dataview/TreeModelFilter.h"
#include "dataview/TreeModel.h"
#include "dataview/ResourceTreeView.h"
#include "dataview/IResourceTreePopulator.h"
#include "XmlResourceBasedWidget.h"
#include "PanedPosition.h"
#include "menu/PopupMenu.h"
Expand Down Expand Up @@ -38,8 +39,7 @@ class EntityClassChooser :
ResourceTreeView* _treeView;

// Delegated object for loading entity classes in a separate thread
class ThreadedEntityClassLoader;
std::unique_ptr<ThreadedEntityClassLoader> _eclassLoader; // PIMPL idiom
std::unique_ptr<wxutil::IResourceTreePopulator> _eclassLoader;

// Last selected classname
std::string _selectedName;
Expand Down
5 changes: 4 additions & 1 deletion libs/wxutil/dataview/ThreadedResourceTreePopulator.h
Expand Up @@ -12,6 +12,9 @@ namespace wxutil
* Threaded resource tree populator implementation class.
* Subclasses need to implement the abstract members to add
* the needed insertion or sorting logic.
*
* At the end of the thread execution this populator will send
* a wxutil::TreeModel::PopulationFinishedEvent to the handler.
*/
class ThreadedResourceTreePopulator :
public IResourceTreePopulator,
Expand Down Expand Up @@ -72,7 +75,7 @@ class ThreadedResourceTreePopulator :
_started(false)
{}

~ThreadedResourceTreePopulator()
virtual ~ThreadedResourceTreePopulator()
{
if (IsAlive())
{
Expand Down
2 changes: 2 additions & 0 deletions radiant/ui/mediabrowser/MediaBrowserTreeView.cpp
Expand Up @@ -8,6 +8,7 @@
#include "TextureDirectoryLoader.h"
#include "wxutil/ModalProgressDialog.h"
#include "wxutil/dataview/TreeViewItemStyle.h"
#include "wxutil/dataview/ThreadedResourceTreePopulator.h"
#include "string/string.h"
#include "shaderlib.h"

Expand Down Expand Up @@ -329,6 +330,7 @@ void MediaBrowserTreeView::clear()

void MediaBrowserTreeView::_onTreeStorePopulationFinished(wxutil::TreeModel::PopulationFinishedEvent& ev)
{
UnselectAll();
setTreeModel(ev.GetTreeModel());
}

Expand Down
4 changes: 2 additions & 2 deletions radiant/ui/mediabrowser/MediaBrowserTreeView.h
@@ -1,7 +1,7 @@
#pragma once

#include "wxutil/dataview/ResourceTreeView.h"
#include "wxutil/dataview/ThreadedResourceTreePopulator.h"
#include "wxutil/dataview/IResourceTreePopulator.h"
#include "wxutil/menu/IconTextMenuItem.h"
#include "wxutil/dataview/TreeModelFilter.h"

Expand All @@ -25,7 +25,7 @@ class MediaBrowserTreeView :

private:
// Populates the Media Browser in its own thread
std::unique_ptr<wxutil::ThreadedResourceTreePopulator> _populator;
std::unique_ptr<wxutil::IResourceTreePopulator> _populator;

// false, if the tree is not yet initialised.
bool _isPopulated;
Expand Down

0 comments on commit 44d2501

Please sign in to comment.