Page Contents

Topics

Classes

LAttribute Class

#include <Layers/lattribute.h>

Inherits: LObject

Public Functions

LAttribute(const LString& name, LObject* parent = nullptr)
LAttribute(const LString& name, const char* value, LObject* parent = nullptr)
LAttribute(const LString& name, LVariant value, LObject* parent = nullptr)
LAttribute(const LString& name, LJsonObject json_object, LObject* parent = nullptr)
T as(const std::vector<LString>& state_combo = std::vector<LString>())
T* as_if(const std::vector<LString>& state_combo = std::vector<LString>())
void break_link()
void clear_overrides()
void clear_theme_attribute()
void create_override(const LString& name, const char* value)
void create_override(const LString& name, LVariant value)
LAttributeList dependent_attributes(bool include_indirect_dependencies = false) const
void disconnect_change(const LConnectionID& connection)
void disconnect_link_change(const LConnectionID& connection)
bool has_overrides() const
LJsonObject& json_object()
LAttribute* link_attribute() const
LString link_path() const
LConnectionID on_change(std::function<void()> callback)
LConnectionID on_link_change(std::function<void()> callback)
LAttribute* override_attribute(const std::vector<LString>& state_combo)
LAttributeMap overrides() const
LString path() const
void set_link_attribute(LAttribute* link_attr)
void set_link_path(const LString& link_path)
void set_theme_attribute(LAttribute* theme_attr)
void set_value(const char* value)
void set_value(const LVariant& value)
LJsonObject to_json_object()
LJsonValue to_json_value()
size_t type_index() const
LVariant value()

Related Non-Members

LAttributeList
LAttributeMap
LVariant

Detailed Description

An LAttribute is a LObject that serves as a value container for Layers widgets and themes.

An attribute stores its value as an LVariant object.

Usage varies depending on the parent type. Typically, an LAttribute is owned by either a Layers widget (LThemeable subclass), an LThemeItem, or another LAttribute. These are referred to as widget attributes, theme attributes, and override attributes, respectively.

Widget attributes change the appearance of their parent widgets.

Theme attributes modify the value of widget attributes.

Override attributes overtake the value of their parent attributes when the value is requested with a state-combination that matches the override attribute.

Applying Theme Attributes to Widget Attributes

When a theme is applied to a Layers widget via LThemeable::apply_theme_item(), the theme attributes are applied to the widget's attributes using set_theme_attribute(). After this, when the value is requested from one of the widget attributes, the value of the theme attribute is returned instead.

Attribute Linking

An attribute can be linked to another attribute. In this case, it will use the value of the other attribute instead of its own. For example:

LAttribute* slider_value = new LAttribute("Slider Value", 0);
LAttribute* thickness = new LAttribute("Thickness", 10);
 
slider_value->set_link_attribute(thickness);
 
//  After set_link_attribute() is called above, slider_value is linked to
//  thickness, so the thickness value represents both attributes.
 
slider_value.set_value(15);
 
//  The above call actually sets the value of thickness to 15.

In the example above, slider_value is linked to thickness. After the relationship is established, slider_value is considered a dependency of thickness. A list of dependent attributes can be obtained through calling dependent_attributes():

//  ... continued from above
 
LAttributeList thickness_dependencies = thickness->dependent_attributes();
 
//  thickness_dependencies should contain the slider_value attribute.

Linking Theme Attributes (Soft Linking)

Themes support linking through their theme attributes. Most links are handled within themes so that they can be manipulated during theme editing. These links are referred to as soft links since they are intended to be changeable.

A theme attribute links to another by storing a link path which is a string that identifies the theme attribute being linked to. Themes do not resolve these path-based links until the end of the loading process.

Linking Widget Attributes (Hard Linking)

Links can also be established outside of themes between widget attributes directly. This is typical for control widgets where users interact with the widget to control a particular value. For example:

// Example using classes from QLayers:
 
QLWidget* widget = new QLWidget;
QLSlider* thickness_slider = new QLSlider;
 
thickness_slider->value()->set_link_attribute(widget->border_thickness());
 
//  After the above link is established, when the user interacts with
//  thickness_slider, the widget's border thickness will change.

These links are referred to as hard links since they are established by the developer and not intended to be changeable.

Override Attributes

Sometimes, you want one attribute to have multiple values. Consider a button whose fill value changes when the mouse hovers over it. This can be handled by adding an override attribute to the fill attribute:

// Initialize button_fill with green color
LAttribute* button_fill = new LAttribute("Button Fill", "#23d143");

// Create override that changes the fill to a light-green color when selected
button_fill->create_override("Selected", "#2bf250");

Override attributes can not have other override attributes.

JSON Data Format

The JSON data for an attribute is formatted as follows:

{
    "value": <int,string,object>,    // The attribute's value (if not linked)
    "linked_to": <link_path>,        // The attribute's link path (if linked)
    "overrides": {                   // The attribute's overrides (if any)
        <attr_name>: <attr_object>,  // First override attribute
        ...                          // Any other override attributes follow (if any)
    }
}

If the attribute is linked, it will store a linked_to value containing the link-path.

If the attribute is not linked, it will store a value value containing the attribute's value. The attribute values can have different types. The following are some examples of attribute values in JSON format:

"value": 10           // Stores a number as an integer

"value": "#36393f"    // Stores a color-name as a string

"value": {            // Stores a gradient as an object
    "gradient": [
        "0:#3a3c42",
        "1:#42454d"
    ]
}

Member Function Documentation

LAttribute(const LString& name, LObject* parent = nullptr)

Constructs an attribute with an invalid value.

LAttribute(const LString& name, const char* value, LObject* parent = nullptr)

Constructs an attribute with a string value.

LAttribute(const LString& name, LVariant value, LObject* parent = nullptr)

Constructs an attribute with a variant value.

LAttribute(const LString& name, LJsonObject json_object, LObject* parent = nullptr)

Constructs an attribute from an JSON-object.

T as(const std::vector<LString>& state_combo = std::vector<LString>())

Returns the attribute's value converted to the template type T.

If a theme attribute has been set, its value is returned.

If the attribute contains an override attribute that matches the list of state_combo, then its value is returned.

If a link attribute has been set, its value is returned.

If none of the other conditions are true, then the attribute's stored value is returned.

T* as_if(const std::vector<LString>& state_combo = std::vector<LString>())

Returns a pointer to the attribute's value converted to the template type T, if the value is of type T.

If a theme attribute has been set, its value is returned.

If the attribute contains an override attribute that matches the list of state_combo, then its value is returned.

If a link attribute has been set, its value is returned.

If none of the other conditions are true, then the attribute's stored value is returned.

If the determined value is not of type T, then nullptr is returned.

void clear_overrides()

Deletes all override attributes.

void clear_theme_attribute()

Resets theme attribute pointer to nullptr and clears connection to it.

void create_override(const LString& name, const char* value)

Overload for create_override().

void create_override(const LString& name, LVariant value)

Creates a new override with name and value.

The name parameter should consist of the state-combination that the override pertains to. Multiple states should be separated by colons (For example: "Inactive:Hovered").

LAttributeList dependent_attributes(bool include_indirect_dependencies = false)

Returns list containing the attributes that link to this one.

If include_indirect_dependencies is true, then the function will work recursively to return the dependent attributes of dependent attributes.

void disconnect_change(const LConnectionID& connection)

Disconnects the callback associated with connection to stop it from processing when changes occur to the attribute.

bool has_overrides() const

Returns true if the attribute contains any override attributes.

LJsonObject& json_object()

Returns a reference to the attribute's JSON-object instance.

LConnectionID on_change(std::function<void()> callback)

Stores callback and processes it whenever changes occur to the attribute.

Returns a LConnectionID which can be used to disconnect the callback later through disconnect_change().

LAttribute override_attribute(const std::vector<LString>& state_combo)

Returns a pointer to the attribute identified by state_combo.

LAttributeMap overrides() const

Returns a map containing the override attributes.

LString path() const

Returns the path of the attribute.

An attribute path consists of the attribute's name appended at the end of the parent's path. If no parent exists, then just the name is returned.

void set_theme_attribute(LAttribute* theme_attr)

Sets the theme attribute to theme_attr.

This function is called by LThemeable::apply_theme_item() to apply theme attributes to widget attributes. Library users are unlikely to ever need to call this function manually.

void set_value(const char* value)

Overload for set_value().

void set_value(const LVariant& value)

Sets the attribute's value.

If a link attribute has been set, its value is set instead.

LJsonObject to_json_object()

Returns the attribute represented as a JSON-object.

LJsonValue to_json_value()

Returns the attribute's value as a JSON-value.

size_t type_index() const

Returns the index of the LVariant type that the attribute's value is stored as.

LVariant value()

Returns the attribute's value.

If a theme attribute has been set, its value is returned.

If a link attribute has been set, its value is returned.

Related Non-Members

LAttributeList

Typedef for std::vector<LAttribute*>.

LAttributeMap

Typedef for std::map<LString, LAttribute*>.

LVariant

Typedef for std::variant<LInvalidVariant, double, bool, LString, std::vector<LString>>.