summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2017-06-20 17:12:58 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2017-06-22 08:04:39 -0700
commit40e47556214e999c952c1e88e026d673cdd96c72 (patch)
treee8d15795cbecc07c94908a8ebed31b73def64614 /src
parent1520a56813f82bbe875774fdc2b3df26392278d6 (diff)
downloadqtlocation-mapboxgl-40e47556214e999c952c1e88e026d673cdd96c72.tar.gz
[core] Move setStyleJSON/URL to Style; add Map::setStyle
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/map/map.cpp114
-rw-r--r--src/mbgl/style/observer.hpp3
-rw-r--r--src/mbgl/style/style.cpp16
-rw-r--r--src/mbgl/style/style_impl.cpp80
-rw-r--r--src/mbgl/style/style_impl.hpp16
5 files changed, 117 insertions, 112 deletions
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index 3aed91cda4..37442770fa 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -13,9 +13,6 @@
#include <mbgl/renderer/render_source.hpp>
#include <mbgl/renderer/render_style.hpp>
#include <mbgl/renderer/render_style_observer.hpp>
-#include <mbgl/storage/file_source.hpp>
-#include <mbgl/storage/resource.hpp>
-#include <mbgl/storage/response.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/exception.hpp>
@@ -63,6 +60,7 @@ public:
void onSourceChanged(style::Source&) override;
void onUpdate(Update) override;
void onInvalidate() override;
+ void onStyleLoading() override;
void onStyleLoaded() override;
void onStyleError(std::exception_ptr) override;
void onResourceError(std::exception_ptr) override;
@@ -70,8 +68,6 @@ public:
void render(View&);
void renderStill();
- void loadStyleJSON(const std::string&);
-
Map& map;
MapObserver& observer;
Backend& backend;
@@ -95,12 +91,8 @@ public:
std::unique_ptr<Style> style;
std::unique_ptr<RenderStyle> renderStyle;
- std::string styleURL;
- std::string styleJSON;
bool cameraMutated = false;
- std::unique_ptr<AsyncRequest> styleRequest;
-
size_t sourceCacheSize;
bool loading = false;
@@ -167,8 +159,6 @@ Map::Impl::Impl(Map& map_,
Map::~Map() {
BackendScope guard(impl->backend);
- impl->styleRequest = nullptr;
-
// Explicit resets currently necessary because these abandon resources that need to be
// cleaned up by context.reset();
impl->renderStyle.reset();
@@ -323,88 +313,6 @@ void Map::Impl::render(View& view) {
#pragma mark - Style
-void Map::setStyleURL(const std::string& url) {
- if (impl->styleURL == url) {
- return;
- }
-
- impl->loading = true;
- impl->observer.onWillStartLoadingMap();
-
- impl->styleRequest = nullptr;
- impl->styleURL = url;
- impl->styleJSON.clear();
- impl->style->impl->loaded = false;
-
- impl->styleRequest = impl->fileSource.request(Resource::style(impl->styleURL), [this](Response res) {
- // Once we get a fresh style, or the style is mutated, stop revalidating.
- if (res.isFresh() || impl->style->impl->mutated) {
- impl->styleRequest.reset();
- }
-
- // Don't allow a loaded, mutated style to be overwritten with a new version.
- if (impl->style->impl->mutated && impl->style->impl->loaded) {
- return;
- }
-
- if (res.error) {
- if (res.error->reason == Response::Error::Reason::NotFound &&
- util::mapbox::isMapboxURL(impl->styleURL)) {
- const std::string message = "style " + impl->styleURL + " could not be found or is an incompatible legacy map or style";
- Log::Error(Event::Setup, message.c_str());
- impl->onStyleError(std::make_exception_ptr(util::NotFoundException(message)));
- } else {
- const std::string message = "loading style failed: " + res.error->message;
- Log::Error(Event::Setup, message.c_str());
- impl->onStyleError(std::make_exception_ptr(util::StyleLoadException(message)));
- }
- impl->onResourceError(std::make_exception_ptr(std::runtime_error(res.error->message)));
- } else if (res.notModified || res.noContent) {
- return;
- } else {
- impl->loadStyleJSON(*res.data);
- }
- });
-}
-
-void Map::setStyleJSON(const std::string& json) {
- if (impl->styleJSON == json) {
- return;
- }
-
- impl->loading = true;
- impl->observer.onWillStartLoadingMap();
-
- impl->styleURL.clear();
- impl->styleJSON.clear();
- impl->style->impl->mutated = false;
-
- impl->loadStyleJSON(json);
-}
-
-void Map::Impl::loadStyleJSON(const std::string& json) {
- style->impl->setJSON(json);
- styleJSON = json;
-
- if (!cameraMutated) {
- // Zoom first because it may constrain subsequent operations.
- map.setZoom(style->getDefaultZoom());
- map.setLatLng(style->getDefaultLatLng());
- map.setBearing(style->getDefaultBearing());
- map.setPitch(style->getDefaultPitch());
- }
-
- onUpdate(Update::AnnotationStyle);
-}
-
-std::string Map::getStyleURL() const {
- return impl->styleURL;
-}
-
-std::string Map::getStyleJSON() const {
- return impl->styleJSON;
-}
-
style::Style& Map::getStyle() {
return *impl->style;
}
@@ -413,6 +321,11 @@ const style::Style& Map::getStyle() const {
return *impl->style;
}
+void Map::setStyle(std::unique_ptr<Style> style) {
+ impl->onStyleLoading();
+ impl->style = std::move(style);
+}
+
#pragma mark - Transitions
void Map::cancelTransitions() {
@@ -943,7 +856,21 @@ void Map::Impl::onInvalidate() {
onUpdate(Update::Repaint);
}
+void Map::Impl::onStyleLoading() {
+ loading = true;
+ observer.onWillStartLoadingMap();
+}
+
void Map::Impl::onStyleLoaded() {
+ if (!cameraMutated) {
+ // Zoom first because it may constrain subsequent operations.
+ map.setZoom(style->getDefaultZoom());
+ map.setLatLng(style->getDefaultLatLng());
+ map.setBearing(style->getDefaultBearing());
+ map.setPitch(style->getDefaultPitch());
+ }
+
+ onUpdate(Update::AnnotationStyle);
observer.onDidFinishLoadingStyle();
}
@@ -960,7 +887,6 @@ void Map::Impl::onResourceError(std::exception_ptr error) {
void Map::dumpDebugLogs() const {
Log::Info(Event::General, "--------------------------------------------------------------------------------");
- Log::Info(Event::General, "MapContext::styleURL: %s", impl->styleURL.c_str());
impl->style->impl->dumpDebugLogs();
if (impl->renderStyle) {
impl->renderStyle->dumpDebugLogs();
diff --git a/src/mbgl/style/observer.hpp b/src/mbgl/style/observer.hpp
index 3b2c0db3c0..ea19c599e9 100644
--- a/src/mbgl/style/observer.hpp
+++ b/src/mbgl/style/observer.hpp
@@ -10,9 +10,10 @@ namespace style {
class Observer : public SourceObserver {
public:
+ virtual void onStyleLoading() {}
+ virtual void onStyleLoaded() {}
virtual void onUpdate(Update) {}
virtual void onStyleError(std::exception_ptr) {}
- virtual void onStyleLoaded() {}
virtual void onResourceError(std::exception_ptr) {}
};
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index f8dfd48b5c..5fe1ab4a06 100644
--- a/src/mbgl/style/style.cpp
+++ b/src/mbgl/style/style.cpp
@@ -14,6 +14,22 @@ Style::Style(Scheduler& scheduler, FileSource& fileSource, float pixelRatio)
Style::~Style() = default;
+void Style::loadJSON(const std::string& json) {
+ impl->loadJSON(json);
+}
+
+void Style::loadURL(const std::string& url) {
+ impl->loadURL(url);
+}
+
+std::string Style::getJSON() const {
+ return impl->getJSON();
+}
+
+std::string Style::getURL() const {
+ return impl->getURL();
+}
+
std::string Style::getName() const {
return impl->getName();
}
diff --git a/src/mbgl/style/style_impl.cpp b/src/mbgl/style/style_impl.cpp
index 7235226f84..2d42afd086 100644
--- a/src/mbgl/style/style_impl.cpp
+++ b/src/mbgl/style/style_impl.cpp
@@ -16,6 +16,9 @@
#include <mbgl/util/exception.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/logging.hpp>
+#include <mbgl/storage/file_source.hpp>
+#include <mbgl/storage/resource.hpp>
+#include <mbgl/storage/response.hpp>
namespace mbgl {
namespace style {
@@ -34,26 +37,47 @@ Style::Impl::Impl(Scheduler& scheduler_, FileSource& fileSource_, float pixelRat
Style::Impl::~Impl() = default;
-void Style::Impl::setTransitionOptions(const TransitionOptions& options) {
- transitionOptions = options;
-}
+void Style::Impl::loadJSON(const std::string& json_) {
+ observer->onStyleLoading();
-TransitionOptions Style::Impl::getTransitionOptions() const {
- return transitionOptions;
+ url.clear();
+ parse(json_);
}
-void Style::Impl::setJSON(const std::string& json) {
- sources.clear();
- layers.clear();
- images.clear();
+void Style::Impl::loadURL(const std::string& url_) {
+ observer->onStyleLoading();
- transitionOptions = {};
- transitionOptions.duration = util::DEFAULT_TRANSITION_DURATION;
+ loaded = false;
+ url = url_;
+ styleRequest = fileSource.request(Resource::style(url), [this](Response res) {
+ // Once we get a fresh style, or the style is mutated, stop revalidating.
+ if (res.isFresh() || mutated) {
+ styleRequest.reset();
+ }
+
+ // Don't allow a loaded, mutated style to be overwritten with a new version.
+ if (mutated && loaded) {
+ return;
+ }
+
+ if (res.error) {
+ const std::string message = "loading style failed: " + res.error->message;
+ Log::Error(Event::Setup, message.c_str());
+ observer->onStyleError(std::make_exception_ptr(util::StyleLoadException(message)));
+ observer->onResourceError(std::make_exception_ptr(std::runtime_error(res.error->message)));
+ } else if (res.notModified || res.noContent) {
+ return;
+ } else {
+ parse(*res.data);
+ }
+ });
+}
+
+void Style::Impl::parse(const std::string& json_) {
Parser parser;
- auto error = parser.parse(json);
- if (error) {
+ if (auto error = parser.parse(json_)) {
std::string message = "Failed to parse style: " + util::toString(error);
Log::Error(Event::ParseStyle, message.c_str());
observer->onStyleError(std::make_exception_ptr(util::StyleParseException(message)));
@@ -61,6 +85,17 @@ void Style::Impl::setJSON(const std::string& json) {
return;
}
+ mutated = false;
+ loaded = true;
+ json = json_;
+
+ sources.clear();
+ layers.clear();
+ images.clear();
+
+ transitionOptions = {};
+ transitionOptions.duration = util::DEFAULT_TRANSITION_DURATION;
+
for (auto& source : parser.sources) {
addSource(std::move(source));
}
@@ -79,11 +114,25 @@ void Style::Impl::setJSON(const std::string& json) {
spriteLoader->load(parser.spriteURL, scheduler, fileSource);
glyphURL = parser.glyphURL;
- loaded = true;
-
observer->onStyleLoaded();
}
+std::string Style::Impl::getJSON() const {
+ return json;
+}
+
+std::string Style::Impl::getURL() const {
+ return url;
+}
+
+void Style::Impl::setTransitionOptions(const TransitionOptions& options) {
+ transitionOptions = options;
+}
+
+TransitionOptions Style::Impl::getTransitionOptions() const {
+ return transitionOptions;
+}
+
void Style::Impl::addSource(std::unique_ptr<Source> source) {
if (sources.get(source->getID())) {
std::string msg = "Source " + source->getID() + " already exists";
@@ -296,6 +345,7 @@ void Style::Impl::onLightChanged(const Light&) {
}
void Style::Impl::dumpDebugLogs() const {
+ Log::Info(Event::General, "styleURL: %s", url.c_str());
for (const auto& source : sources) {
source->dumpDebugLogs();
}
diff --git a/src/mbgl/style/style_impl.hpp b/src/mbgl/style/style_impl.hpp
index 2bdb51a26c..76f244d5a4 100644
--- a/src/mbgl/style/style_impl.hpp
+++ b/src/mbgl/style/style_impl.hpp
@@ -25,6 +25,7 @@ namespace mbgl {
class Scheduler;
class FileSource;
+class AsyncRequest;
class SpriteLoader;
namespace style {
@@ -38,7 +39,11 @@ public:
Impl(Scheduler&, FileSource&, float pixelRatio);
~Impl() override;
- void setJSON(const std::string&);
+ void loadJSON(const std::string&);
+ void loadURL(const std::string&);
+
+ std::string getJSON() const;
+ std::string getURL() const;
void setObserver(Observer*);
@@ -92,11 +97,18 @@ public:
bool spriteLoaded = false;
private:
+ void parse(const std::string&);
+
Scheduler& scheduler;
FileSource& fileSource;
+
+ std::string url;
+ std::string json;
+
+ std::unique_ptr<AsyncRequest> styleRequest;
std::unique_ptr<SpriteLoader> spriteLoader;
- std::string glyphURL;
+ std::string glyphURL;
Collection<style::Image> images;
Collection<Source> sources;
Collection<Layer> layers;