Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutable symbolizer properties for styles #2678

Merged
merged 6 commits into from
Sep 11, 2014

Conversation

ahocevar
Copy link
Member

@ahocevar ahocevar commented Sep 3, 2014

This change adds setters for symbolizer properties. In addition, it introduces a mutable flag on all styles. By default, this is set to true. ol.style.createStyleFunction() sets it to false for all static styles.

The new setters assert that the mutable flag is true, so whenever an application tries to set a symbolizer property on a style that was assigned to a vector layer or feature overlay, the assertion will fail.

@ahocevar ahocevar changed the title Mutable symbolizer properties when returned from a style function Mutable symbolizer properties for style functions Sep 3, 2014
This change adds setters for symbolizer properties. In addition, it
introduces a mutable flag on all styles. By default, this is set to
true. ol.style.createStyleFunction sets it to false for all static
styles.

The new setters assert that the mutable flag is true, so whenever an
application tries to set a symbolizer property on a style that was
assigned to a vector layer or feature overlay, the assertion will fail.
@tschaub
Copy link
Member

tschaub commented Sep 4, 2014

Curious to hear about the use case. If the is to be used in a style editing application, could regular old objects (like those passes to the symbolized constructors) be used instead?

I fear that people will see the setter methods and assume that is the way to modify existing styles - only to be frustrated when nothing happens (no asserts in compiled code).

@ahocevar
Copy link
Member Author

ahocevar commented Sep 4, 2014

Good point @tschaub. Yes, the use case is interactive style editing. My first solution would also have been plain object literals, but then I saw that ol.style.Image provides more than just storage for the symbolizer properties.

@elemoine
Copy link
Member

elemoine commented Sep 4, 2014

It may be obvious but I'm failing to understand the purpose of the mutable flag. When does setting it to false make sense? Why does the createStyleFunction set it to false?

And what if you do

layer.setStyle(styles);
layer.setStyle(function(feature, resolution) {
  return styles;
});

You will have your own style function but with immutable styles. Not sure that's expected.

@elemoine
Copy link
Member

elemoine commented Sep 4, 2014

And in the case where you have your own style function the layer will not be redrawn if you change a style outside the style function. You have to call layer.dispatchChangeEvent for the layer to be redrawn. So I don't see why the styles are mutable in this case while they are immutable when the style function is created by the layer.

So, if we want mutable styles, I'd just make it clear (in the docs) that the user needs to call dispatchChangeEvent on the layer when changing a style outside the style function. The user needs to understand that the layer does not know about styles and that he is the one managing them.

@tschaub tschaub modified the milestone: v3.1.0 Sep 4, 2014
@ahocevar
Copy link
Member Author

ahocevar commented Sep 4, 2014

mutable flag will be removed, and documentation improved to educate users to call setStyle for changes to take effect.

@ahocevar
Copy link
Member Author

ahocevar commented Sep 4, 2014

Removed the mutable_ flag, as discussed in the hangout.

Instead, educate users to call setStyle.
@elemoine
Copy link
Member

elemoine commented Sep 4, 2014

Code looks good to me. But I think the docs need clarifications. I can try to add a commit to your branch tomorrow if you agree.

@ahocevar
Copy link
Member Author

ahocevar commented Sep 4, 2014

Sure @elemoine, thanks.

@elemoine
Copy link
Member

elemoine commented Sep 4, 2014

For the record I know you don't like the idea of calling dispatchChangeEvent on the layer, but I continue to think that it makes more sense to call dispatchChangeEvent than to call setStyle.

The following would look ridiculous to me:

style.getFill().setColor(color);
layer.setStyle(layer.getStyle());

Hard to understand for someone reading that code I think.

This makes more sense to me:

style.getFill().setColor(color);
// notify the layer that we've changed styles (by
// dispatching a change event to it)
layer.dispatchChangeEvent();

We may be able to find better ways in the future. But calling dispatchChangeEvent is the best way currently I think.

@bartvde
Copy link
Member

bartvde commented Sep 4, 2014

I agree with @elemoine that the second way makes more sense

As an aside, I don't really like the name dispatchChangeEvent, something like update would make more sense to me.

@ahocevar
Copy link
Member Author

ahocevar commented Sep 4, 2014

Both works, and @elemoine's code does not have to look that ugly:

style.getFill().setColor(color);
layer.setStyle(style);

If the docs are changed from "Use setStyle()" to "Use setStyle() or dispatchChangeEvent()", then everyone would be happy, and we don't need to spend precious time on this rather academic discussion. Update in the works.

@bartvde
Copy link
Member

bartvde commented Sep 4, 2014

Agreed

@elemoine
Copy link
Member

elemoine commented Sep 4, 2014

@ahocevar your code won't work if what you passed to the layer initially was a style function. Calling dispatchChangeEvent is a systematic way.

then everyone would be happy, and we don't need to spend precious time on this rather academic discussion.

Too bad (and worrying) that you're taking it that way.

@ahocevar
Copy link
Member Author

ahocevar commented Sep 4, 2014

Whatever. Docs updated.

@elemoine
Copy link
Member

elemoine commented Sep 4, 2014

At the risk of being "academic" I don't like "update". Update what? This sounds too abstract to me. But I'll think more about it...

/**
* Set the color. Call `setStyle()` or `dispatchChangeEvent()` on feature, layer
* or feature overlay for changes to take effect.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling setStyle or dispatchChangeEvent on what object? What if the style you're modifying is set on a feature? This is what I mean by saying "needs clarification".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ol.Feature, ol.layer.Vector and ol.FeatureOverlay all have setStyle() and dispatchChangeEvent(). Suggestions for further clarification welcome.

And @elemoine, don't worry. You know I am a friend of pragmatic, so excuse me that I find this discussion academic.

@ahocevar
Copy link
Member Author

ahocevar commented Sep 5, 2014

Actually I was wrong. ol.FeatureOverlay does not inherit from ol.Observable, so calling dispatchChangeEvent() does not work there (but setStyle() does, and map.render() does as well).

@elemoine
Copy link
Member

I think we should update this patch to replace changed() by dispatchChangeEvent() in the docs and then merge it.

I'm not a big fan of repeating the docs for each style property as done in this patch. But I'll create a new PR if I have ideas on improving this.

@ahocevar
Copy link
Member Author

Please consider #2684. The DispatchChangeEvent method really should be renamed because it does more than just dispatch a change event.

@ahocevar ahocevar changed the title Mutable symbolizer properties for style functions Mutable symbolizer properties for styles Sep 11, 2014
@ahocevar
Copy link
Member Author

@elemoine I moved the documentation about re-rendering to the ol.style.Style classdesc, so you can change it easily to your liking. Merging now.

ahocevar added a commit that referenced this pull request Sep 11, 2014
Mutable symbolizer properties for styles
@ahocevar ahocevar merged commit 3da9a67 into openlayers:master Sep 11, 2014
@ahocevar ahocevar deleted the mutable-styles branch September 11, 2014 20:14
@elemoine
Copy link
Member

Thanks. It makes to get this change in and exercise it a bit. Looking forward to knowing if that makes less the styling framework less "awkward" for people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants