class Vizkit::TypelibItem

Constants

DIRECTLY_DISPLAYED_RUBY_TYPES
MAX_NUMBER_OF_CHILDS

Attributes

typelib_val[R]

Public Class Methods

new(typelib_val=nil,bitfield_type: nil, **options) click to toggle source
Calls superclass method Vizkit::VizkitItem.new
# File lib/vizkit/vizkit_items.rb, line 208
def initialize(typelib_val=nil,bitfield_type: nil, **options)
    super()
    @options = Kernel.validate_options options,:text => nil,:item_type => :label,:editable => false,bitfield_type: nil
    @typelib_val = nil
    @truncated = false
    @bitfield_type = bitfield_type
    setEditable false
    update typelib_val if typelib_val
end

Public Instance Methods

add_child(row, field, val, **options) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 389
def add_child(row, field, val, **options)
    field_item = TypelibItem.new(val, text: field.to_s, editable: @options[:editable], **options)
    val_item   = TypelibItem.new(val, item_type: :value, editable: @options[:editable], **options)
    appendRow [field_item,val_item]
end
add_or_update_child(row, field, val, **add_options) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 377
def add_or_update_child(row, field, val, **add_options)
    if row >= @childs.size
        add_child(row, field, val, **add_options)
    else
        update_child(row, field, val)
    end
end
clear() click to toggle source
Calls superclass method Vizkit::VizkitItem#clear
# File lib/vizkit/vizkit_items.rb, line 311
def clear
    super
    @typelib_val = nil
end
data(role = Qt::UserRole+1) click to toggle source
Calls superclass method
# File lib/vizkit/vizkit_items.rb, line 249
def data(role = Qt::UserRole+1)
    # workaround memeroy leak qt-ruby
    # the returned Qt::Variant is never be deleted
    @last_data.dispose if @last_data
    @last_data = if role == Qt::DisplayRole
                     val = if @options.has_key?(:text)
                               @options[:text]
                           elsif !@typelib_val
                               "no data"
                           elsif @bitfield_type
                               "bitfield #{@bitfield_type.name}"
                           elsif !@direct_type
                               if modified?
                                   @typelib_val.class.name + " (modified)"
                               elsif truncated?
                                   @typelib_val.class.name + " (truncated,size=#{@typelib_val.size})"
                               else
                                   @typelib_val.class.name
                               end
                           else
                               item_val = @typelib_val.to_ruby
                               if item_val.is_a?(Float) || item_val.is_a?(Fixnum) ||
                                   item_val.is_a?(TrueClass) || item_val.is_a?(FalseClass)
                                   item_val
                               elsif item_val.is_a? Time
                                   "#{item_val.strftime("%-d %b %Y %H:%M:%S")}.#{"%06d" % item_val.usec}"
                               else
                                   item_val.to_s
                               end
                           end
                     begin
                         Qt::Variant.new(val)
                     rescue Exception => e
                         setEditable(false)
                         Qt::Variant.new(e.message)
                     end
                 elsif role == Qt::EditRole
                     val = if !@direct_type
                               nil
                           else
                               item_val = @typelib_val.to_ruby
                               if item_val.is_a?(Float) || item_val.is_a?(Fixnum) ||
                                   item_val.is_a?(TrueClass) || item_val.is_a?(FalseClass)
                                   item_val
                               elsif item_val.is_a? Time
                                   Qt::DateTime.new(item_val)
                               elsif item_val.is_a? Symbol
                                   #move current value to the front
                                   arr = @typelib_val.class.keys.keys
                                   arr.delete(item_val.to_s)
                                   arr.insert(0,item_val.to_s)
                                   arr
                               else
                                   item_val.to_s
                               end
                           end
                     Qt::Variant.new(val)
                 else
                     super
                 end
end
direct_type?() click to toggle source
# File lib/vizkit/vizkit_items.rb, line 241
def direct_type?
    !!@direct_type
end
has_child_for_row?(row) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 385
def has_child_for_row?(row)
    @childs[row]
end
initialize_from_first_sample(data) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 336
def initialize_from_first_sample(data)
    @typelib_val = data
    if @typelib_val.class.convertion_to_ruby
        rb_sample = @typelib_val.class.convertion_to_ruby[0]
        @direct_type = DIRECTLY_DISPLAYED_RUBY_TYPES.any? {|t| rb_sample <= t }
    elsif @typelib_val.kind_of?(Typelib::EnumType)
        @direct_type = true
    else
        @direct_type = false
    end

    updated_typelib_val

    if !@direct_type || @options[:item_type] == :label
        setEditable false
    else
        setEditable @options[:editable]
    end
end
resolve_and_update_child(row) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 401
def resolve_and_update_child(row)
    if @bitfield_values
        if row >= @childs.size
            field_item = VizkitItem.new("")
            val_item   = VizkitItem.new(@bitfield_values[row])
            appendRow [field_item,val_item]
        else
            field_item, val_item = @childs[row]
            val_item.text = @bitfield_values[row]
        end

    elsif @typelib_val.class.respond_to? :fields
        field_name, field_type = @typelib_val.class.fields[row]
        field_value = @typelib_val.raw_get(field_name)

        if has_child_for_row?(row)
            update_child(row, field_name, field_value)
        else
            field_metadata = @typelib_val.class.field_metadata[field_name]
            bitfield_type  = resolve_bitfield_type(field_metadata)
            add_child(row, field_name, field_value, bitfield_type: bitfield_type)
        end
    else
        add_or_update_child(row, row, @typelib_val.raw_get(row))
    end
end
resolve_bitfield_type(metadata) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 218
def resolve_bitfield_type(metadata)
    if metadata.include?('bitfield')
        if bitfield_typename = metadata.get('bitfield').first
            begin
                return Orocos.registry.get(bitfield_typename)
            rescue Typelib::NotFound
                Vizkit.warn "Could not find bitfield type #{bitfield_typename}"
            end
        end
    end
    nil
end
setData(data,role = Qt::UserRole+1) click to toggle source
Calls superclass method
# File lib/vizkit/vizkit_items.rb, line 231
def setData(data,role = Qt::UserRole+1)
    return super if role != Qt::EditRole || data.isNull
    item_val = @typelib_val.to_ruby
    val = from_variant data, item_val
    return false if val.nil?
    val = Typelib.from_ruby(val, @typelib_val.class)
    update(val)
    modified!
end
truncated?() click to toggle source
# File lib/vizkit/vizkit_items.rb, line 245
def truncated?
    @truncated
end
typelib_val_children_count() click to toggle source
# File lib/vizkit/vizkit_items.rb, line 356
def typelib_val_children_count
    if @bitfield_values
        @bitfield_values.size
    elsif @direct_type
        0
    elsif @typelib_val.class.respond_to? :fields
        @typelib_val.class.fields.size
    elsif @typelib_val.respond_to? :raw_get
        r = @typelib_val.size
        if r > MAX_NUMBER_OF_CHILDS
            @truncated = true
            MAX_NUMBER_OF_CHILDS
        else
            @truncated = false
            r
        end
    else
        0
    end
end
update(data) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 428
def update(data)
    if @typelib_val
        if @typelib_val.invalidated? || (@typelib_val.class != data.class)
            clear
        end
    end

    if @typelib_val
        update_typelib_val(data)
    else
        initialize_from_first_sample(data)
    end

    # this might be called by update after the child
    # was delete therefore just return here
    return unless @typelib_val

    # we do not need to update the childs if the item is not a label
    # otherwise check all childs
    if @options[:item_type] != :label
        emitDataChanged
        return
    end

    rows = typelib_val_children_count

    # detect resizing
    if rows < @childs.size
        downsize_children(rows)
    end

    0.upto(rows-1) do |row|
        resolve_and_update_child(row)
    end
end
update_child(row, field, val) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 395
def update_child(row, field, val)
    field_item, val_item = @childs[row]
    field_item.update val
    val_item.update val
end
update_typelib_val(data) click to toggle source
# File lib/vizkit/vizkit_items.rb, line 325
def update_typelib_val(data)
    Typelib.copy(@typelib_val, Typelib.from_ruby(data,@typelib_val.class))
    updated_typelib_val

rescue ArgumentError => e
    Vizkit.error "error during copying #{@typelib_val.class.name}: #{e}"
    Vizkit.log_nest(2) do
        Vizkit.log_pp(:debug, e.backtrace)
    end
end
updated_typelib_val() click to toggle source
# File lib/vizkit/vizkit_items.rb, line 316
def updated_typelib_val
    if @bitfield_type && @typelib_val.kind_of?(Typelib::NumericType) && @typelib_val.class.integer?
        numeric = Typelib.to_ruby(typelib_val)
        @bitfield_values = @bitfield_type.keys.map do |name, value|
            name if (numeric & value) != 0
        end.compact
    end
end