Skip to content

Commit

Permalink
#5127: Introduce signals to subscribe to when a favourite set is modi…
Browse files Browse the repository at this point in the history
…fied
  • Loading branch information
codereader committed Jan 4, 2021
1 parent 5b0f822 commit 16f8cdf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/ifavourites.h
@@ -1,6 +1,7 @@
#pragma once

#include <set>
#include <sigc++/signal.h>
#include "imodule.h"
#include "idecltypes.h"

Expand All @@ -25,6 +26,9 @@ class IFavouritesManager :

// Returns the whole set of favourites for the given declaration type
virtual std::set<std::string> getFavourites(decl::Type type) = 0;

// Returns the changed signal for the given type - will be fired when the set changes
virtual sigc::signal<void>& getSignalForType(decl::Type type) = 0;
};

}
Expand Down
8 changes: 8 additions & 0 deletions radiantcore/decl/FavouriteSet.h
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <set>
#include "iregistry.h"
#include <sigc++/signal.h>

namespace decl
{
Expand All @@ -12,6 +13,8 @@ class FavouriteSet
private:
std::set<std::string> _set;

sigc::signal<void> _sigSetChanged;

public:
std::set<std::string>& get()
{
Expand Down Expand Up @@ -45,6 +48,11 @@ class FavouriteSet
node.setAttributeValue("value", favourite);
}
}

sigc::signal<void>& signal_setChanged()
{
return _sigSetChanged;
}
};

}
25 changes: 25 additions & 0 deletions radiantcore/decl/FavouritesManager.cpp
Expand Up @@ -29,6 +29,7 @@ void FavouritesManager::addFavourite(decl::Type type, const std::string& path)
}

set->second.get().emplace(path);
set->second.signal_setChanged().emit();
}

void FavouritesManager::removeFavourite(decl::Type type, const std::string& path)
Expand All @@ -43,6 +44,7 @@ void FavouritesManager::removeFavourite(decl::Type type, const std::string& path
}

set->second.get().erase(path);
set->second.signal_setChanged().emit();
}

bool FavouritesManager::isFavourite(decl::Type type, const std::string& path)
Expand All @@ -66,6 +68,23 @@ std::set<std::string> FavouritesManager::getFavourites(decl::Type type)
return set != _favouritesByType.end() ? set->second.get() : std::set<std::string>();
}

sigc::signal<void>& FavouritesManager::getSignalForType(decl::Type type)
{
if (type == decl::Type::None)
{
throw std::logic_error("No signal for decl::Type::None");
}

auto set = _favouritesByType.find(type);

if (set == _favouritesByType.end())
{
set = _favouritesByType.emplace(type, FavouriteSet()).first;
}

return set->second.signal_setChanged();
}

const std::string& FavouritesManager::getName() const
{
static std::string _name(MODULE_FAVOURITES_MANAGER);
Expand Down Expand Up @@ -112,6 +131,12 @@ void FavouritesManager::shutdownModule()
_favouritesByType[Type::SoundShader].saveToRegistry(root + RKEY_SUBPATH_SOUNDSHADERS);
_favouritesByType[Type::Model].saveToRegistry(root + RKEY_SUBPATH_MODELS);
_favouritesByType[Type::Particle].saveToRegistry(root + RKEY_SUBPATH_PARTICLES);

// Clear observers
for (auto& pair : _favouritesByType)
{
pair.second.signal_setChanged().clear();
}
}

module::StaticModule<FavouritesManager> favouritesManagerModule;
Expand Down
1 change: 1 addition & 0 deletions radiantcore/decl/FavouritesManager.h
Expand Up @@ -18,6 +18,7 @@ class FavouritesManager :
void removeFavourite(decl::Type type, const std::string& path) override;
bool isFavourite(decl::Type type, const std::string& path) override;
std::set<std::string> getFavourites(decl::Type type) override;
sigc::signal<void>& getSignalForType(decl::Type type) override;

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

0 comments on commit 16f8cdf

Please sign in to comment.