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

Commit

Permalink
Add generic setter for 'source' property
Browse files Browse the repository at this point in the history
- Add setter for 'source' property
- Test generic setters via setProperty method
  • Loading branch information
alexshalamov committed Apr 16, 2020
1 parent 7f0c139 commit ffea0af
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/mbgl/style/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Layer {
std::string getSourceID() const;
std::string getSourceLayer() const;
void setSourceLayer(const std::string& sourceLayer);
void setSourceID(const std::string& sourceID);

// Filter
const Filter& getFilter() const;
Expand Down
25 changes: 24 additions & 1 deletion src/mbgl/style/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,25 @@ std::string Layer::getSourceLayer() const {
}

void Layer::setSourceLayer(const std::string& sourceLayer) {
if (getSourceLayer() == sourceLayer) return;
auto impl_ = mutableBaseImpl();
impl_->sourceLayer = sourceLayer;
baseImpl = std::move(impl_);
}

void Layer::setSourceID(const std::string& sourceID) {
if (getSourceID() == sourceID) return;
auto impl_ = mutableBaseImpl();
impl_->source = sourceID;
baseImpl = std::move(impl_);
};

const Filter& Layer::getFilter() const {
return baseImpl->filter;
}

void Layer::setFilter(const Filter& filter) {
if (getFilter() == filter) return;
auto impl_ = mutableBaseImpl();
impl_->filter = filter;
baseImpl = std::move(impl_);
Expand Down Expand Up @@ -79,13 +88,15 @@ float Layer::getMaxZoom() const {
}

void Layer::setMinZoom(float minZoom) {
if (getMinZoom() == minZoom) return;
auto impl_ = mutableBaseImpl();
impl_->minZoom = minZoom;
baseImpl = std::move(impl_);
observer->onLayerChanged(*this);
}

void Layer::setMaxZoom(float maxZoom) {
if (getMaxZoom() == maxZoom) return;
auto impl_ = mutableBaseImpl();
impl_->maxZoom = maxZoom;
baseImpl = std::move(impl_);
Expand Down Expand Up @@ -168,14 +179,26 @@ optional<conversion::Error> Layer::setProperty(const std::string& name, const co
if (auto sourceLayer = convert<std::string>(value, *error)) {
if (getTypeInfo()->source != LayerTypeInfo::Source::Required) {
Log::Warning(mbgl::Event::General,
"source-layer property cannot be applied to"
"'source-layer' property cannot be set to"
"the layer %s",
baseImpl->id.c_str());
return nullopt;
}
setSourceLayer(*sourceLayer);
return nullopt;
}
} else if (name == "source") {
if (auto sourceID = convert<std::string>(value, *error)) {
if (getTypeInfo()->source != LayerTypeInfo::Source::Required) {
Log::Warning(mbgl::Event::General,
"'source' property cannot be set to"
"the layer %s",
baseImpl->id.c_str());
return nullopt;
}
setSourceID(*sourceID);
return nullopt;
}
}
return error;
}
Expand Down
16 changes: 16 additions & 0 deletions test/style/conversion/layer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,20 @@ TEST(StyleConversion, SetGenericProperties) {
EXPECT_EQ(12.0f, layer->getMinZoom());
EXPECT_EQ(18.0f, layer->getMaxZoom());
EXPECT_EQ("landmarks", layer->getSourceLayer());

const JSValue newComposite("composite_2");
layer->setProperty("source", Convertible(&newComposite));
EXPECT_EQ("composite_2", layer->getSourceID());

const JSValue newSourceLayer("poi");
layer->setProperty("source-layer", Convertible(&newSourceLayer));
EXPECT_EQ("poi", layer->getSourceLayer());

const JSValue newMinZoom(1.0f);
layer->setProperty("minzoom", Convertible(&newMinZoom));
EXPECT_EQ(1.0f, layer->getMinZoom());

const JSValue newMaxZoom(22.0f);
layer->setProperty("maxzoom", Convertible(&newMaxZoom));
EXPECT_EQ(22.0f, layer->getMaxZoom());
}
10 changes: 9 additions & 1 deletion test/style/style_layer.test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <mbgl/gl/custom_layer.hpp>
#include <mbgl/gl/custom_layer_impl.hpp>
#include <mbgl/style/conversion/filter.hpp>
#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/expression/dsl.hpp>
#include <mbgl/style/expression/format_expression.hpp>
#include <mbgl/style/expression/image.hpp>
Expand Down Expand Up @@ -28,6 +30,7 @@

using namespace mbgl;
using namespace mbgl::style;
using namespace mbgl::style::conversion;
using namespace expression;
using namespace expression::dsl;
using namespace std::literals::string_literals;
Expand Down Expand Up @@ -60,6 +63,11 @@ class MockLayoutProperties : public Properties<TextField> {};
class MockPaintProperties : public Properties<TextColor> {};
using MockOverrides = FormatSectionOverrides<MockPaintProperties::OverridableProperties>;

mbgl::style::Filter parseFilter(const std::string& expression) {
Error error;
return *convertJSON<mbgl::style::Filter>(expression, error);
}

} // namespace

TEST(Layer, BackgroundProperties) {
Expand Down Expand Up @@ -221,7 +229,7 @@ TEST(Layer, Observer) {
EXPECT_EQ(layer.get(), &layer_);
filterChanged = true;
};
layer->setFilter(Filter());
layer->setFilter(parseFilter(R"(["==", "foo", "bar"])"));
EXPECT_TRUE(filterChanged);

// Notifies observer on visibility change.
Expand Down

0 comments on commit ffea0af

Please sign in to comment.