mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-05 19:19:39 -06:00
Make image get function return std::optional if the image is not found
This commit is contained in:
@@ -35,9 +35,9 @@ namespace openspace {
|
||||
namespace documentation { struct Documentation; }
|
||||
|
||||
struct ImageData {
|
||||
std::string name = "";
|
||||
std::string name;
|
||||
std::string thumbnailUrl;
|
||||
std::string imageUrl = "";
|
||||
std::string imageUrl;
|
||||
std::string credits;
|
||||
std::string creditsUrl;
|
||||
std::string collection;
|
||||
@@ -52,7 +52,7 @@ class WwtDataHandler {
|
||||
public:
|
||||
void loadImages(const std::string& root, const std::filesystem::path& directory);
|
||||
int nLoadedImages() const;
|
||||
const ImageData& image(const std::string& imageUrl) const;
|
||||
std::optional<const ImageData> image(const std::string& imageUrl) const;
|
||||
const std::map<std::string, ImageData>& images() const;
|
||||
|
||||
private:
|
||||
|
||||
@@ -319,8 +319,11 @@ void SkyBrowserModule::setHoverCircle(SceneGraphNode* circle) {
|
||||
}
|
||||
|
||||
void SkyBrowserModule::moveHoverCircle(const std::string& imageUrl, bool useScript) {
|
||||
const ImageData& image = _dataHandler.image(imageUrl);
|
||||
|
||||
std::optional<const ImageData> found = _dataHandler.image(imageUrl);
|
||||
if (!found.has_value()) {
|
||||
return;
|
||||
}
|
||||
const ImageData image = found.value();
|
||||
// Only move and show circle if the image has coordinates
|
||||
if (!(_hoverCircle && image.hasCelestialCoords && _isCameraInSolarSystem)) {
|
||||
return;
|
||||
|
||||
@@ -83,14 +83,17 @@ namespace {
|
||||
if (module->isCameraInSolarSystem()) {
|
||||
TargetBrowserPair* selected = module->pair(module->selectedBrowserId());
|
||||
if (selected) {
|
||||
const ImageData& image = module->wwtDataHandler().image(imageUrl);
|
||||
if (image.name == "") {
|
||||
std::optional<const ImageData> found = module->wwtDataHandler().image(
|
||||
imageUrl
|
||||
);
|
||||
if (!found.has_value()) {
|
||||
LINFO(fmt::format(
|
||||
"No image with identifier {} was found in the collection.", imageUrl
|
||||
));
|
||||
return;
|
||||
}
|
||||
// Load image into browser
|
||||
const ImageData& image = found.value();
|
||||
std::string str = image.name;
|
||||
// Check if character is ASCII - if it isn't, remove
|
||||
str.erase(
|
||||
@@ -303,8 +306,6 @@ namespace {
|
||||
// Create Lua table to send to the GUI
|
||||
ghoul::Dictionary list;
|
||||
for (auto const& [id, img] : module->wwtDataHandler().images()) {
|
||||
const ImageData& img = module->wwtDataHandler().image(id);
|
||||
|
||||
// Push ("Key", value)
|
||||
ghoul::Dictionary image;
|
||||
image.setValue("name", img.name);
|
||||
@@ -775,7 +776,7 @@ namespace {
|
||||
std::for_each(
|
||||
images.rbegin(), images.rend(),
|
||||
[&](std::string imageUrl) {
|
||||
const ImageData& image = module->wwtDataHandler().image(imageUrl);
|
||||
const ImageData& image = module->wwtDataHandler().image(imageUrl).value();
|
||||
// Index of image is used as layer ID as it's unique in the image data set
|
||||
pair->browser()->addImageLayerToWwt(image.imageUrl);
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ ghoul::Dictionary TargetBrowserPair::dataAsDictionary() const {
|
||||
|
||||
for (const std::string& imageUrl : selectedImages()) {
|
||||
selectedImagesIndices.push_back(
|
||||
module->wwtDataHandler().image(imageUrl).identifier
|
||||
module->wwtDataHandler().image(imageUrl).value().identifier
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -338,10 +338,10 @@ int WwtDataHandler::nLoadedImages() const {
|
||||
return static_cast<int>(_images.size());
|
||||
}
|
||||
|
||||
const ImageData& WwtDataHandler::image(const std::string& imageUrl) const {
|
||||
std::optional<const ImageData> WwtDataHandler::image(const std::string& imageUrl) const {
|
||||
auto it = _images.find(imageUrl);
|
||||
if (it == _images.end()) {
|
||||
return ImageData();
|
||||
return std::nullopt;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user