diff options
author | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-04-20 12:39:51 -0700 |
---|---|---|
committer | Asheem Mamoowala <asheem.mamoowala@mapbox.com> | 2017-06-01 15:38:01 -0700 |
commit | 7a010df9560e8807b9be938bb5ff7360e8e730a6 (patch) | |
tree | acb862796062e35d2ffdf77deb9514ee73f3d6ad /src/mbgl/style/sources | |
parent | 9068c90fc4b1b8b4938d89028fc3f7b46beeac29 (diff) | |
download | qtlocation-mapboxgl-7a010df9560e8807b9be938bb5ff7360e8e730a6.tar.gz |
[core] Add ImageSource support to style parsers
Diffstat (limited to 'src/mbgl/style/sources')
-rw-r--r-- | src/mbgl/style/sources/image_source.cpp | 85 | ||||
-rw-r--r-- | src/mbgl/style/sources/image_source_impl.cpp | 42 | ||||
-rw-r--r-- | src/mbgl/style/sources/image_source_impl.hpp | 31 |
3 files changed, 158 insertions, 0 deletions
diff --git a/src/mbgl/style/sources/image_source.cpp b/src/mbgl/style/sources/image_source.cpp new file mode 100644 index 0000000000..4f86befd0b --- /dev/null +++ b/src/mbgl/style/sources/image_source.cpp @@ -0,0 +1,85 @@ +#include <mbgl/style/sources/image_source.hpp> +#include <mbgl/style/sources/image_source_impl.hpp> +#include <mbgl/util/geo.hpp> +#include <mbgl/style/source_observer.hpp> +#include <mbgl/util/premultiply.hpp> +#include <mbgl/storage/file_source.hpp> + +namespace mbgl { +namespace style { + +ImageSource::ImageSource(std::string id, const std::vector<LatLng> coords_) + : Source(makeMutable<Impl>(std::move(id), coords_)) { +} + +ImageSource::~ImageSource() = default; + +const ImageSource::Impl& ImageSource::impl() const { + return static_cast<const Impl&>(*baseImpl); +} + +void ImageSource::setCoordinates(const std::vector<LatLng>& coords_) { + baseImpl = makeMutable<Impl>(impl(), coords_); + observer->onSourceChanged(*this); +} + +std::vector<LatLng> ImageSource::getCoordinates() const { + return impl().getCoordinates(); +} + +void ImageSource::setURL(const std::string& url_) { + url = std::move(url_); + // Signal that the source description needs a reload + if (loaded || req) { + loaded = false; + req.reset(); + observer->onSourceDescriptionChanged(*this); + } +} + +void ImageSource::setImage(mbgl::UnassociatedImage image_) { + url = {}; + if (req) { + req.reset(); + } + loaded = true; + baseImpl = makeMutable<Impl>(impl(), std::move(image_)); + observer->onSourceChanged(*this); +} + +const std::string& ImageSource::getURL() const { + return *url; +} + +void ImageSource::loadDescription(FileSource& fileSource) { + if (!url) { + loaded = true; + } + + if (req || loaded) { + return; + } + const Resource imageResource { Resource::Unknown, *url, {}, Resource::Necessity::Required }; + + req = fileSource.request(imageResource, [this](Response res) { + if (res.error) { + observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); + } else if (res.notModified) { + return; + } else if (res.noContent) { + observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty image url"))); + } else { + try { + UnassociatedImage image = util::unpremultiply(decodeImage(*res.data)); + baseImpl = makeMutable<Impl>(impl(), std::move(image)); + } catch (...) { + observer->onSourceError(*this, std::current_exception()); + } + loaded = true; + observer->onSourceLoaded(*this); + } + }); +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/image_source_impl.cpp b/src/mbgl/style/sources/image_source_impl.cpp new file mode 100644 index 0000000000..e16b33b4ca --- /dev/null +++ b/src/mbgl/style/sources/image_source_impl.cpp @@ -0,0 +1,42 @@ +#include <mbgl/style/sources/image_source_impl.hpp> +#include <mbgl/util/geo.hpp> + +namespace mbgl { +namespace style { + +ImageSource::Impl::Impl(std::string id_, std::vector<LatLng> coords_) + : Source::Impl(SourceType::Image, std::move(id_)), + coords(std::move(coords_)) { +} + +ImageSource::Impl::Impl(const Impl& other, std::vector<LatLng> coords_) + : Source::Impl(other), + coords(std::move(coords_)), + image(other.image.clone()) { +} + +ImageSource::Impl::Impl(const Impl& rhs, UnassociatedImage image_) + : Source::Impl(rhs), + coords(rhs.coords), + image(std::move(image_)) { +} +ImageSource::Impl::~Impl() = default; + +void ImageSource::Impl::setImage(UnassociatedImage image_) { + image = std::move(image_); +} + +const UnassociatedImage& ImageSource::Impl::getImage() const { + return image; +} + +std::vector<LatLng> ImageSource::Impl::getCoordinates() const { + return coords; +} + +optional<std::string> ImageSource::Impl::getAttribution() const { + return {}; +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/image_source_impl.hpp b/src/mbgl/style/sources/image_source_impl.hpp new file mode 100644 index 0000000000..439db4fa64 --- /dev/null +++ b/src/mbgl/style/sources/image_source_impl.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <mbgl/style/source_impl.hpp> +#include <mbgl/style/sources/image_source.hpp> +#include <mbgl/util/image.hpp> + +namespace mbgl { +class LatLng; + +namespace style { + +class ImageSource::Impl : public Source::Impl { +public: + Impl(std::string id, std::vector<LatLng> coords); + Impl(const Impl& rhs, std::vector<LatLng> coords); + Impl(const Impl& rhs, UnassociatedImage image); + + ~Impl() final; + + void setImage(UnassociatedImage ); + const mbgl::UnassociatedImage& getImage() const; + std::vector<LatLng> getCoordinates() const; + + optional<std::string> getAttribution() const final; +private: + std::vector<LatLng> coords; + mbgl::UnassociatedImage image; +}; + +} // namespace style +} // namespace mbgl |