Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Source-driven attribution
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
1ec5 committed Aug 16, 2016
1 parent 01a3374 commit 3e1d57a
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ class Map : private util::noncopyable {
void removeAnnotation(AnnotationID);

// Sources
std::vector<const style::Source*> getSources() const;
style::Source* getSource(const std::string& sourceID);
void addSource(std::unique_ptr<style::Source>);
void removeSource(const std::string& sourceID);
std::vector<std::string> getAttributions() const;

// Layers
style::Layer* getLayer(const std::string& layerID);
Expand Down
3 changes: 2 additions & 1 deletion platform/qt/include/qmapboxgl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class Q_DECL_EXPORT QMapboxGL : public QObject
MapChangeDidFinishRenderingFrameFullyRendered,
MapChangeWillStartRenderingMap,
MapChangeDidFinishRenderingMap,
MapChangeDidFinishRenderingMapFullyRendered
MapChangeDidFinishRenderingMapFullyRendered,
MapChangeSourceAttributionDidChange
};

QMapboxGL(QObject *parent = 0, const QMapboxGLSettings& = QMapboxGLSettings());
Expand Down
1 change: 1 addition & 0 deletions platform/qt/src/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static_assert(mbgl::underlying_type(QMapboxGL::MapChangeDidFinishRenderingFrameF
static_assert(mbgl::underlying_type(QMapboxGL::MapChangeWillStartRenderingMap) == mbgl::underlying_type(mbgl::MapChangeWillStartRenderingMap), "error");
static_assert(mbgl::underlying_type(QMapboxGL::MapChangeDidFinishRenderingMap) == mbgl::underlying_type(mbgl::MapChangeDidFinishRenderingMap), "error");
static_assert(mbgl::underlying_type(QMapboxGL::MapChangeDidFinishRenderingMapFullyRendered) == mbgl::underlying_type(mbgl::MapChangeDidFinishRenderingMapFullyRendered), "error");
static_assert(mbgl::underlying_type(QMapboxGL::MapChangeSourceAttributionDidChange) == mbgl::underlying_type(mbgl::MapChangeSourceAttributionDidChange), "error");

namespace {

Expand Down
1 change: 1 addition & 0 deletions src/mbgl/map/change.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum MapChange : uint8_t {
MapChangeWillStartRenderingMap = 11,
MapChangeDidFinishRenderingMap = 12,
MapChangeDidFinishRenderingMapFullyRendered = 13,
MapChangeSourceAttributionDidChange = 14,
};

} // namespace mbgl
13 changes: 13 additions & 0 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Map::Impl : public style::Observer {
public:
Impl(View&, FileSource&, MapMode, GLContextMode, ConstrainMode, ViewportMode);

void onSourceAttributionChanged(style::Source&, std::string) override;
void onNeedsRepaint() override;
void onResourceError(std::exception_ptr) override;

Expand Down Expand Up @@ -745,6 +746,10 @@ AnnotationIDs Map::queryPointAnnotations(const ScreenBox& box) {

#pragma mark - Style API

std::vector<const style::Source*> Map::getSources() const {
return impl->style ? impl->style->getSources() : std::vector<const style::Source*>();
}

style::Source* Map::getSource(const std::string& sourceID) {
return impl->style ? impl->style->getSource(sourceID) : nullptr;
}
Expand All @@ -757,6 +762,10 @@ void Map::removeSource(const std::string& sourceID) {
impl->style->removeSource(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) {
return impl->style ? impl->style->getLayer(layerID) : nullptr;
}
Expand Down Expand Up @@ -861,6 +870,10 @@ void Map::onLowMemory() {
impl->view.invalidate();
}

void Map::Impl::onSourceAttributionChanged(style::Source&, std::string) {
view.notifyMapChange(MapChangeSourceAttributionDidChange);
}

void Map::Impl::onNeedsRepaint() {
updateFlags |= Update::Repaint;
asyncUpdate.send();
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/style/source_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SourceObserver {

virtual void onSourceLoaded(Source&) {}
virtual void onSourceError(Source&, std::exception_ptr) {}
virtual void onSourceAttributionChanged(Source&, std::string) {}
virtual void onTileLoaded(Source&, const OverscaledTileID&, bool /* isNewTile */) {}
virtual void onTileError(Source&, const OverscaledTileID&, std::exception_ptr) {}
virtual void onNeedsRepaint() {}
Expand Down
34 changes: 34 additions & 0 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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_store.hpp>
#include <mbgl/sprite/sprite_atlas.hpp>
#include <mbgl/geometry/glyph_atlas.hpp>
Expand All @@ -22,6 +23,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>

Expand Down Expand Up @@ -232,6 +234,15 @@ void Style::recalculate(float z, const TimePoint& timePoint, MapMode mode) {
}
}

std::vector<const Source*> Style::getSources() const {
std::vector<const Source*> result;
result.reserve(sources.size());
for (const auto& source : sources) {
result.push_back(source.get());
}
return result;
}

Source* Style::getSource(const std::string& id) const {
const auto it = std::find_if(sources.begin(), sources.end(), [&](const auto& source) {
return source->getID() == id;
Expand All @@ -240,6 +251,25 @@ 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());
result.push_back(tileSource->getAttribution());
}

case SourceType::GeoJSON:
case SourceType::Video:
case SourceType::Annotations:
break;
}
}
return result;
}

bool Style::hasTransitions() const {
return hasPendingTransitions;
}
Expand Down Expand Up @@ -412,6 +442,10 @@ void Style::onSourceError(Source& source, std::exception_ptr error) {
observer->onResourceError(error);
}

void Style::onSourceAttributionChanged(Source& source, std::string attribution) {
observer->onSourceAttributionChanged(source, attribution);
}

void Style::onTileLoaded(Source& source, const OverscaledTileID& tileID, bool isNewTile) {
if (isNewTile) {
shouldReparsePartialTiles = true;
Expand Down
3 changes: 3 additions & 0 deletions src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ class Style : public GlyphStoreObserver,
return lastError;
}

std::vector<const Source*> getSources() const;
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;
Expand Down Expand Up @@ -111,6 +113,7 @@ class Style : public GlyphStoreObserver,
// SourceObserver implementation.
void onSourceLoaded(Source&) override;
void onSourceError(Source&, std::exception_ptr) override;
void onSourceAttributionChanged(Source&, std::string) override;
void onTileLoaded(Source&, const OverscaledTileID&, bool isNewTile) override;
void onTileError(Source&, const OverscaledTileID&, std::exception_ptr) override;
void onNeedsRepaint() override;
Expand Down
12 changes: 10 additions & 2 deletions src/mbgl/style/tile_source_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void TileSourceImpl::load(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();
Expand All @@ -95,8 +96,8 @@ void TileSourceImpl::load(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
}
Expand All @@ -105,6 +106,9 @@ void TileSourceImpl::load(FileSource& fileSource) {
loaded = true;

observer->onSourceLoaded(base);
if (attributionChanged) {
observer->onSourceAttributionChanged(base, newTileset.attribution);
}
}
});
}
Expand All @@ -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
2 changes: 2 additions & 0 deletions src/mbgl/style/tile_source_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class TileSourceImpl : public Source::Impl {
const variant<std::string, Tileset>& getURLOrTileset() const {
return urlOrTileset;
}

std::string getAttribution() const;

protected:
Range<uint8_t> getZoomRange() final;
Expand Down
7 changes: 6 additions & 1 deletion test/src/mbgl/test/stub_style_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace mbgl;
using namespace mbgl::style;

/**
* An implementation of style::Observer that forwards all methods to dynamically-settable lambas.
* An implementation of style::Observer that forwards all methods to dynamically-settable lambdas.
*/
class StubStyleObserver : public style::Observer {
public:
Expand Down Expand Up @@ -34,6 +34,10 @@ class StubStyleObserver : public style::Observer {
if (sourceError) sourceError(source, error);
}

void onSourceAttributionChanged(Source& source, std::string attribution) override {
if (sourceAttributionChanged) sourceAttributionChanged(source, attribution);
}

void onTileLoaded(Source& source, const OverscaledTileID& tileID, bool isNewTile) override {
if (tileLoaded) tileLoaded(source, tileID, isNewTile);
}
Expand All @@ -57,6 +61,7 @@ class StubStyleObserver : public style::Observer {
std::function<void (std::exception_ptr)> spriteError;
std::function<void (Source&)> sourceLoaded;
std::function<void (Source&, std::exception_ptr)> sourceError;
std::function<void (Source&, std::string)> sourceAttributionChanged;
std::function<void (Source&, const OverscaledTileID&, bool isNewTile)> tileLoaded;
std::function<void (Source&, const OverscaledTileID&, std::exception_ptr)> tileError;
std::function<void ()> needsRepaint;
Expand Down

0 comments on commit 3e1d57a

Please sign in to comment.