diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2016-08-13 11:30:15 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2016-09-26 11:37:06 -0700 |
commit | f30765254807bedab0873a289a118906ef74b754 (patch) | |
tree | 0988f52d73931b05718642c218c8241421882eb8 /src/mbgl | |
parent | 78af55f30a37165c960c90db9a96801effc850f6 (diff) | |
download | qtlocation-mapboxgl-f30765254807bedab0873a289a118906ef74b754.tar.gz |
[core] Source-driven attribution
Implemented observer callbacks so the style knows when the source’s attribution changes and the map knows when the style’s attribution changes. Also implemented a getter for a tile source’s attribution.
Fixes #2723.
Diffstat (limited to 'src/mbgl')
-rw-r--r-- | src/mbgl/map/change.hpp | 3 | ||||
-rw-r--r-- | src/mbgl/map/map.cpp | 9 | ||||
-rw-r--r-- | src/mbgl/style/source_observer.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/style/style.cpp | 28 | ||||
-rw-r--r-- | src/mbgl/style/style.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/style/tile_source_impl.cpp | 12 | ||||
-rw-r--r-- | src/mbgl/style/tile_source_impl.hpp | 2 |
7 files changed, 54 insertions, 3 deletions
diff --git a/src/mbgl/map/change.hpp b/src/mbgl/map/change.hpp index 343d7d8e4f..50079af710 100644 --- a/src/mbgl/map/change.hpp +++ b/src/mbgl/map/change.hpp @@ -19,7 +19,8 @@ enum MapChange : uint8_t { MapChangeWillStartRenderingMap = 11, MapChangeDidFinishRenderingMap = 12, MapChangeDidFinishRenderingMapFullyRendered = 13, - MapChangeDidFinishLoadingStyle = 14 + MapChangeDidFinishLoadingStyle = 14, + MapChangeSourceAttributionDidChange = 15 }; } // namespace mbgl diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 9d8a063c44..a67de9c2c4 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -38,6 +38,7 @@ class Map::Impl : public style::Observer { public: Impl(View&, FileSource&, MapMode, GLContextMode, ConstrainMode, ViewportMode); + void onSourceAttributionChanged(style::Source&, const std::string&) override; void onUpdate(Update) override; void onStyleLoaded() override; void onStyleError() override; @@ -785,6 +786,10 @@ void Map::removeSource(const std::string& sourceID) { } } +std::vector<std::string> Map::getAttributions() const { + return impl->style ? impl->style->getAttributions() : std::vector<std::string>(); +} + style::Layer* Map::getLayer(const std::string& layerID) { if (impl->style) { impl->styleMutated = true; @@ -980,6 +985,10 @@ void Map::onLowMemory() { impl->view.invalidate(); } +void Map::Impl::onSourceAttributionChanged(style::Source&, const std::string&) { + view.notifyMapChange(MapChangeSourceAttributionDidChange); +} + void Map::Impl::onUpdate(Update flags) { if (flags & Update::Dimensions) { transform.resize(view.getSize()); diff --git a/src/mbgl/style/source_observer.hpp b/src/mbgl/style/source_observer.hpp index 26b25b78da..a6cdab6ba2 100644 --- a/src/mbgl/style/source_observer.hpp +++ b/src/mbgl/style/source_observer.hpp @@ -17,6 +17,7 @@ public: virtual ~SourceObserver() = default; virtual void onSourceLoaded(Source&) {} + virtual void onSourceAttributionChanged(Source&, const std::string&) {} virtual void onSourceError(Source&, std::exception_ptr) {} virtual void onTileChanged(Source&, const OverscaledTileID&) {} diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index a684db62d5..f0917c3b60 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -18,6 +18,7 @@ #include <mbgl/style/update_parameters.hpp> #include <mbgl/style/cascade_parameters.hpp> #include <mbgl/style/calculation_parameters.hpp> +#include <mbgl/style/tile_source_impl.hpp> #include <mbgl/sprite/sprite_atlas.hpp> #include <mbgl/text/glyph_atlas.hpp> #include <mbgl/geometry/line_atlas.hpp> @@ -25,6 +26,7 @@ #include <mbgl/renderer/render_tile.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/string.hpp> +#include <mbgl/util/tileset.hpp> #include <mbgl/platform/log.hpp> #include <mbgl/math/minmax.hpp> @@ -293,6 +295,28 @@ Source* Style::getSource(const std::string& id) const { return it != sources.end() ? it->get() : nullptr; } +std::vector<std::string> Style::getAttributions() const { + std::vector<std::string> result; + for (const auto& source : sources) { + switch (source->baseImpl->type) { + case SourceType::Vector: + case SourceType::Raster: { + style::TileSourceImpl* tileSource = static_cast<style::TileSourceImpl*>(source->baseImpl.get()); + auto attribution = tileSource->getAttribution(); + if (!attribution.empty()) { + result.push_back(std::move(attribution)); + } + } + + case SourceType::GeoJSON: + case SourceType::Video: + case SourceType::Annotations: + break; + } + } + return result; +} + bool Style::hasTransitions() const { return hasPendingTransitions; } @@ -465,6 +489,10 @@ void Style::onSourceLoaded(Source& source) { observer->onUpdate(Update::Repaint); } +void Style::onSourceAttributionChanged(Source& source, const std::string& attribution) { + observer->onSourceAttributionChanged(source, attribution); +} + void Style::onSourceError(Source& source, std::exception_ptr error) { lastError = error; Log::Error(Event::Style, "Failed to load source %s: %s", diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp index a3c51a45e7..5bd886923a 100644 --- a/src/mbgl/style/style.hpp +++ b/src/mbgl/style/style.hpp @@ -67,6 +67,7 @@ public: Source* getSource(const std::string& id) const; void addSource(std::unique_ptr<Source>); void removeSource(const std::string& sourceID); + std::vector<std::string> getAttributions() const; std::vector<const Layer*> getLayers() const; Layer* getLayer(const std::string& id) const; @@ -132,6 +133,7 @@ private: // SourceObserver implementation. void onSourceLoaded(Source&) override; + void onSourceAttributionChanged(Source&, const std::string&) override; void onSourceError(Source&, std::exception_ptr) override; void onTileChanged(Source&, const OverscaledTileID&) override; void onTileError(Source&, const OverscaledTileID&, std::exception_ptr) override; diff --git a/src/mbgl/style/tile_source_impl.cpp b/src/mbgl/style/tile_source_impl.cpp index b201045d03..a318fdd755 100644 --- a/src/mbgl/style/tile_source_impl.cpp +++ b/src/mbgl/style/tile_source_impl.cpp @@ -82,6 +82,7 @@ void TileSourceImpl::loadDescription(FileSource& fileSource) { } // Check whether previous information specifies different tile + bool attributionChanged = false; if (tileset.tiles != newTileset.tiles) { // Tile URLs changed: force tiles to be reloaded. invalidateTiles(); @@ -95,8 +96,8 @@ void TileSourceImpl::loadDescription(FileSource& fileSource) { // This is done automatically when we trigger the onSourceLoaded observer below. // Attribution changed: We need to notify the embedding application that this - // changed. See https://github.com/mapbox/mapbox-gl-native/issues/2723 - // This is not yet implemented. + // changed. + attributionChanged = true; // Center/bounds changed: We're not using these values currently } @@ -105,6 +106,9 @@ void TileSourceImpl::loadDescription(FileSource& fileSource) { loaded = true; observer->onSourceLoaded(base); + if (attributionChanged) { + observer->onSourceAttributionChanged(base, newTileset.attribution); + } } }); } @@ -114,5 +118,9 @@ Range<uint8_t> TileSourceImpl::getZoomRange() { return tileset.zoomRange; } +std::string TileSourceImpl::getAttribution() const { + return loaded ? tileset.attribution : ""; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/tile_source_impl.hpp b/src/mbgl/style/tile_source_impl.hpp index d0b4b3d8ee..4ce1eac223 100644 --- a/src/mbgl/style/tile_source_impl.hpp +++ b/src/mbgl/style/tile_source_impl.hpp @@ -33,6 +33,8 @@ public: const variant<std::string, Tileset>& getURLOrTileset() const { return urlOrTileset; } + + std::string getAttribution() const; protected: Range<uint8_t> getZoomRange() final; |