class Syskit::GUI::StateLabel

Base class for the labels that represent an object and its states

Constants

COLORS
STYLE
TEXT_WITHOUT_NAME
TEXT_WITH_NAME

Attributes

current_color[R]

The current color

@return [String]

current_state[R]

The current state

@return [String]

current_text[R]

The current text

@return [String]

default_color[R]

The default color that will be used for undeclared states

If nil, calling {#update_state} with an unknown state will raise an exception

extra_style[R]

Extra styling elements that should be added to the label stylesheet

name[R]

The name that should be displayed in addition to the state

If left to nil (the default in {#initialize}), no name will be displayed at all

@return [nil,String]

states[R]

Set of known states

StateLabel defines 'INIT' and binds it to the blue color

Public Class Methods

new(name: nil, extra_style: '', parent: nil, rate_limited: false) click to toggle source
Calls superclass method
# File lib/syskit/gui/state_label.rb, line 72
def initialize(name: nil, extra_style: '', parent: nil, rate_limited: false)
    super(parent)
    @rate_limited = rate_limited
    @name = name
    @extra_style = extra_style
    @states = Hash.new

    declare_state :INIT, :blue
    update_state :INIT
end

Public Instance Methods

color_from_state(state) click to toggle source

Returns the color that should be used for a given state

@param [String] state the state name @return [String] the Qt stylesheet color as defined with

{#declare_state} or, if the state has not been declared, by
{#declare_default_color}

@raise [ArgumentError] if the state has not been declared with

{#declare_state} and no defeault color has been set with
{#declare_default_color}
# File lib/syskit/gui/state_label.rb, line 141
def color_from_state(state)
    state = state.to_s
    if states.has_key?(state)
        return states[state]
    elsif color = default_color
        color
    else
        raise ArgumentError, "unknown state #{state} and no default color defined"
    end
end
declare_default_color(color) click to toggle source

Declare a color for non-declared states

If unset (the default), a non-declared state will be interpreted as an error. Otherwise, this color will be chosen

# File lib/syskit/gui/state_label.rb, line 126
def declare_default_color(color)
    self.default_color = handle_color_argument(color)
    self
end
declare_state(state_name, color) click to toggle source

Associate a state name and a color

@param [String] state_name the state name @param [String] color the color. It can either be a color name in

{COLOR} or a Qt stylesheet color (e.g. 'rgb(20, 30, 50)'). Any
string that is not a color name will be interpreted as a
stylesheet color (i.e. no validation is made)
# File lib/syskit/gui/state_label.rb, line 117
def declare_state(state_name, color)
    states[state_name.to_s] = handle_color_argument(color)
    self
end
default_color=(color) click to toggle source

Sets {#default_color}

# File lib/syskit/gui/state_label.rb, line 68
def default_color=(color)
    @default_color = handle_color_argument(color)
end
extra_style=(style) click to toggle source

Sets {#extra_style}

# File lib/syskit/gui/state_label.rb, line 56
def extra_style=(style)
    @extra_style = style.to_str
    update_style
end
handle_color_argument(color) click to toggle source

@api private

Helper to handle a color argument

@param [String] color the color name (in {COLORS}) or a

stylesheet color (e.g. rgb(20, 30, 50)). Anything that is not a
key in {COLOR} is interpreted as a stylesheet color

@return [String] a stylesheet color

# File lib/syskit/gui/state_label.rb, line 102
def handle_color_argument(color)
    if c = COLORS[color]
        COLORS[color]
    else
        color.to_str
    end
end
ignore_state(state_name) click to toggle source

Declare that the given state should be ignored

The display will not be changed when the state changes to an ignored state

@param [String] state_name the name of the state that should be

ignored
# File lib/syskit/gui/state_label.rb, line 90
def ignore_state(state_name)
    states[state_name.to_s] = nil
end
name=(name) click to toggle source

Sets or resets the name that should be displayed in addition to the state

Set to nil to remove any additional text

# File lib/syskit/gui/state_label.rb, line 26
def name=(name)
    @name = name
    update_text
end
rate_limited?() click to toggle source
# File lib/syskit/gui/state_label.rb, line 174
def rate_limited?
    @rate_limited
end
update_state(state, text: state.to_s, color: color_from_state(state)) click to toggle source

Update to reflect a state change

@param [String] state the state name @param [String] text the text to be displayed for this state

change

@param [String] color the color to use for this state

# File lib/syskit/gui/state_label.rb, line 158
def update_state(state, text: state.to_s, color: color_from_state(state))
    return if !color
    update_style(color)
    update_text(text)
    @current_state = state.to_s
end
update_style(color = current_color) click to toggle source

Update the label's style to use the given color

@param [String] color a Qt stylesheet color (e.g. rgb(20,30,50))

# File lib/syskit/gui/state_label.rb, line 168
def update_style(color = current_color)
    @current_color = color
    color = handle_color_argument(color)
    self.style_sheet = STYLE % [color, extra_style]
end
update_text(text = current_text) click to toggle source

Update the displayed text

If {#name} is set, the resulting text is name: text, otherwise just text

The text is displayed using the {#current_color} and {#extra_style}

# File lib/syskit/gui/state_label.rb, line 185
def update_text(text = current_text)
    return if rate_limited? && @last_update && (Time.now - @last_update) < 1
    @last_update = Time.now

    text = text.to_str
    @current_text = text
    self.text =
        if name then TEXT_WITH_NAME % [name, text]
        else TEXT_WITHOUT_NAME % [text]
        end
end