class Etti::Element::Data

Constants

QR_ENCODING
QR_ERROR_LEVELS
QR_KEYS
SHOW_AS_QR
SHOW_AS_TEXT

Attributes

dataGroup[R]

Public Class Methods

new(dataGroup, text, x=nil, y=nil, size=nil) click to toggle source
Calls superclass method Etti::Element::Text::new
# File lib/etti/elements/data.rb, line 14
def initialize dataGroup, text, x=nil, y=nil, size=nil
    super text, x, y, size
    @dataGroup = dataGroup
    @qrWidth = 10.0
    @qrHeight = 10.0
    @qrVersion = 1
    @qrErrorCorrection = :low
    @qrEncoding = :ascii
    @showAs = SHOW_AS_TEXT
end

Public Instance Methods

at_pos(view, x, y) click to toggle source
Calls superclass method Etti::Element::Text#at_pos
# File lib/etti/elements/data.rb, line 122
def at_pos view, x, y
    return super if @showAs == SHOW_AS_TEXT
    ext = Rectangle.new @x, @y - @qrHeight, @qrWidth, @qrHeight
    a = -@rotation * Math::PI / 180.0
    xr = (x - @x) * Math.cos(a) - (y - @y) * Math.sin(a)
    yr = (y - @y) * Math.cos(a) + (x - @x) * Math.sin(a)
    ext.in(xr + @x, yr + @y)
end
draw_selection(view) click to toggle source
Calls superclass method Etti::Element::Text#draw_selection
# File lib/etti/elements/data.rb, line 101
def draw_selection view
    if @showAs == SHOW_AS_TEXT
        super
        return
    end
    cc = Cairo::Context.new view.backBuffer
    cc.antialias = :none
    cc.line_width = 1.0
    cc.set_dash [6, 6], 0
    cc.set_source_rgb *$colors.selection

    xd, yd = *view.pos_to_view(@x, @y)

    cc.translate xd, yd
    cc.rotate @rotation * Math::PI / 180.0 unless @rotation.near 0.0
    height = @qrHeight * view.scale
    cc.rectangle 0, -height, @qrWidth * view.scale, @qrHeight * view.scale
    cc.stroke
end
get_by_name(name) click to toggle source
Calls superclass method Etti::Element::Text#get_by_name
# File lib/etti/elements/data.rb, line 42
def get_by_name name
    if name ==      'qrWidth'                   then return @qrWidth;
    elsif name ==   'qrHeight'                  then return @qrHeight;
    elsif name ==   'qrVersion'                 then return @qrVersion;
    elsif name ==   'qrErrorCorrection'         then return QR_ERROR_LEVELS.index @qrErrorCorrection;
    elsif name ==   'qrEncoding'                then return QR_ENCODING.index @qrEncoding;
    elsif name ==   'showAs'                    then return @showAs;
    elsif name ==   'dataGroup'                 then return @dataGroup; end
    super name
end
redraw(view, cc) click to toggle source
# File lib/etti/elements/data.rb, line 132
def redraw view, cc
    return if @text.nil?
    cc.save
    cc.set_source_rgba *@color
    xd, yd = *view.pos_to_view(@x, @y)
    cc.translate xd, yd
    cc.rotate @rotation * Math::PI / 180.0
    cc.antialias = :subpixel

    if @showAs == SHOW_AS_TEXT
        cc.font_size = @size * view.scale
        cc.show_text @text
    else
        qr = QREncoder.encode @text, :version => @qrVersion,
                                     :correction => @qrErrorCorrection, 
                                     :mode => @qrEncoding
        width = qr.width
        height = @qrHeight * view.scale
        pw = @qrWidth * view.scale / width
        ph = @qrHeight * view.scale / width
        qr.pixels.each_with_index do |row_data, row|
            row_data.each_with_index do |pixel, col|
                next if pixel == 0
                cc.rectangle col * pw, row * ph - height, pw, ph
                cc.fill.stroke
            end
        end
    end
    cc.stroke
    cc.restore
end
set_by_name(name, val) click to toggle source
Calls superclass method Etti::Element::Text#set_by_name
# File lib/etti/elements/data.rb, line 27
def set_by_name name, val
    if name ==      'qrWidth'                   then @qrWidth = val.to_f;                               return;
    elsif name ==   'qrHeight'                  then @qrHeight = val.to_f;                              return;
    elsif name ==   'qrVersion'                 then @qrVersion = val.to_i;                             return;
    elsif name ==   'qrErrorCorrection'         then @qrErrorCorrection = QR_ERROR_LEVELS[val.to_i];    return;
    elsif name ==   'qrEncoding'                then @qrEncoding = QR_ENCODING[val.to_i];               return;
    elsif name ==   'dataGroup'                 then @dataGroup = val;                                  return;
    elsif name ==   'showAs'
        @showAs = (val == SHOW_AS_TEXT or val == 'text') ? SHOW_AS_TEXT : SHOW_AS_QR
        return;
    end
    super name, val
end
to_h() click to toggle source
Calls superclass method Etti::Element::Text#to_h
# File lib/etti/elements/data.rb, line 54
def to_h
    hs = super()
    hs['elementType'] = 'data'
    if @showAs == SHOW_AS_TEXT
        hs['showAs'] = 'text'
    else
        hs['showAs'] = 'qr'
        QR_KEYS.each {|name| hs[name] = get_by_name name}
    end
    hs
end
validate_from_h(hs) click to toggle source
Calls superclass method Etti::Element::Text#validate_from_h
# File lib/etti/elements/data.rb, line 67
def validate_from_h hs
    sections = Etti::PropertyEditorData::DATA_PROPS.select{|a| a.key? :revealSections}.first[:revealSections]
    
    if hs['showAs'] == 'text'
        return super(hs)
    elsif hs['showAs'] == 'qr'
        data = sections.select{|a| a[:section] == 'QR-Code'}.first[:data]
        # sort out all keys which are no in qrKeys and give them to the parent class
        keys = hs.keys - QR_KEYS
        diff = hs.select {|k, v| keys.include? k}
        ret = super diff
        return ret if ret
        
        # check the value of the remaining keys
        QR_KEYS.each do |name|
            next if hs[name].nil?
            if name == 'dataGroup'
                return _('ElementData invalid type or size for dataGroup') unless hs[name].is_a? String and hs[name].length() > 0
                next
            end
            entry = data.select {|dat| dat[:id0] == name or dat[:id1] == name}
            if entry[0][:range]
                return _("ElementData %s is out of range") % name unless entry[0][:range].cover? hs[name]
            elsif entry[0][:entries]
                return _("ElementData %s is out of range") % name if hs[name] < 0 or hs[name] >= entry[0][:entries].length
            end
        end
    else
        return _('ElementData invalid data type')
    end
    nil
end