Class: Syskit::GUI::StateLabel

Inherits:
Qt::Label
  • Object
show all
Defined in:
lib/syskit/gui/state_label.rb

Overview

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

Direct Known Subclasses

GlobalStateLabel, JobStateLabel

Constant Summary collapse

COLORS =
Hash[
blue: "rgb(51, 181, 229)",
green: "rgb(153, 204, 0)",
red: "rgb(255, 68, 68)"]
STYLE =
"QLabel { padding: 3; background-color: %s; %s }"
TEXT_WITH_NAME =
"<b>%s</b>: %s"
TEXT_WITHOUT_NAME =
"%s"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, extra_style: '', parent: nil, rate_limited: false) ⇒ StateLabel

Returns a new instance of StateLabel



72
73
74
75
76
77
78
79
80
81
# 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

Instance Attribute Details

#current_colorString (readonly)

The current color

Returns:

  • (String)


44
45
46
# File 'lib/syskit/gui/state_label.rb', line 44

def current_color
  @current_color
end

#current_stateString (readonly)

The current state

Returns:

  • (String)


34
35
36
# File 'lib/syskit/gui/state_label.rb', line 34

def current_state
  @current_state
end

#current_textString (readonly)

The current text

Returns:

  • (String)


39
40
41
# File 'lib/syskit/gui/state_label.rb', line 39

def current_text
  @current_text
end

#default_colorObject

The default color that will be used for undeclared states

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



65
66
67
# File 'lib/syskit/gui/state_label.rb', line 65

def default_color
  @default_color
end

#extra_styleObject

Extra styling elements that should be added to the label stylesheet



53
54
55
# File 'lib/syskit/gui/state_label.rb', line 53

def extra_style
  @extra_style
end

#namenil, String

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

Returns:

  • (nil, String)


20
21
22
# File 'lib/syskit/gui/state_label.rb', line 20

def name
  @name
end

#statesObject (readonly)

Set of known states

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



49
50
51
# File 'lib/syskit/gui/state_label.rb', line 49

def states
  @states
end

Instance Method Details

#color_from_state(state) ⇒ String

Returns the color that should be used for a given state

Parameters:

  • state (String)

    the state name

Returns:

Raises:



141
142
143
144
145
146
147
148
149
150
# 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) ⇒ Object

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



126
127
128
129
# 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) ⇒ Object

Associate a state name and a color

Parameters:

  • state_name (String)

    the state name

  • color (String)

    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)



117
118
119
120
# 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

#handle_color_argument(color) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper to handle a color argument

Parameters:

  • color (String)

    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

Returns:

  • (String)

    a stylesheet color



102
103
104
105
106
107
108
# 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) ⇒ Object

Declare that the given state should be ignored

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

Parameters:

  • state_name (String)

    the name of the state that should be ignored



90
91
92
# File 'lib/syskit/gui/state_label.rb', line 90

def ignore_state(state_name)
    states[state_name.to_s] = nil
end

#rate_limited?Boolean

Returns:

  • (Boolean)


174
175
176
# 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)) ⇒ Object

Update to reflect a state change

Parameters:

  • state (String)

    the state name

  • text (String)

    the text to be displayed for this state change

  • color (String)

    the color to use for this state



158
159
160
161
162
163
# 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) ⇒ Object

Update the label's style to use the given color

Parameters:

  • color (String) (defaults to: current_color)

    a Qt stylesheet color (e.g. rgb(20,30,50))



168
169
170
171
172
# 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) ⇒ Object

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



185
186
187
188
189
190
191
192
193
194
195
# 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