Class: Syskit::GUI::StateLabel
- Defined in:
- lib/syskit/gui/state_label.rb
Overview
Base class for the labels that represent an object and its states
Direct Known Subclasses
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
-
#current_color ⇒ String
readonly
The current color.
-
#current_state ⇒ String
readonly
The current state.
-
#current_text ⇒ String
readonly
The current text.
-
#default_color ⇒ Object
The default color that will be used for undeclared states.
-
#extra_style ⇒ Object
Extra styling elements that should be added to the label stylesheet.
-
#name ⇒ nil, String
The name that should be displayed in addition to the state.
-
#states ⇒ Object
readonly
Set of known states.
Instance Method Summary collapse
-
#color_from_state(state) ⇒ String
Returns the color that should be used for a given state.
-
#declare_default_color(color) ⇒ Object
Declare a color for non-declared states.
-
#declare_state(state_name, color) ⇒ Object
Associate a state name and a color.
-
#handle_color_argument(color) ⇒ String
private
Helper to handle a color argument.
-
#ignore_state(state_name) ⇒ Object
Declare that the given state should be ignored.
-
#initialize(name: nil, extra_style: '', parent: nil, rate_limited: false) ⇒ StateLabel
constructor
A new instance of StateLabel.
- #rate_limited? ⇒ Boolean
-
#update_state(state, text: state.to_s, color: color_from_state(state)) ⇒ Object
Update to reflect a state change.
-
#update_style(color = current_color) ⇒ Object
Update the label's style to use the given color.
-
#update_text(text = current_text) ⇒ Object
Update the displayed text.
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_color ⇒ String (readonly)
The current color
44 45 46 |
# File 'lib/syskit/gui/state_label.rb', line 44 def current_color @current_color end |
#current_state ⇒ String (readonly)
The current state
34 35 36 |
# File 'lib/syskit/gui/state_label.rb', line 34 def current_state @current_state end |
#current_text ⇒ String (readonly)
The current text
39 40 41 |
# File 'lib/syskit/gui/state_label.rb', line 39 def current_text @current_text end |
#default_color ⇒ Object
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_style ⇒ Object
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 |
#name ⇒ nil, 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
20 21 22 |
# File 'lib/syskit/gui/state_label.rb', line 20 def name @name end |
#states ⇒ Object (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
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
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
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
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
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
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
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 |